1.动态获取DDR
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SWLogger;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.DeviceInfoUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class StoragePreferenceController extends BasePreferenceController {
private static final String TAG = "DeviceModelPrefCtrl";
private static SWLogger log = SWLogger.getLogger(StoragePreferenceController.class);
public StoragePreferenceController(Context context, String key) {
super(context, key);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
return getStorage();
}
public String getStorage() {
int flashSize = (int) getFlashSize();
String value = String.valueOf(flashSize) + "GB";
log.d("getStorage==size==" + value);
return value;
}
private float getFlashSize() {
Process process = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
float flashSize = 0L;
try {
process = Runtime.getRuntime().exec("cat /sys/block/mmcblk2/size");
inputStream = process.getInputStream();
byte[] bytes = new byte[1024];
inputStreamReader = new InputStreamReader(inputStream); //读取
//创建字符流缓冲区
bufferedReader = new BufferedReader(inputStreamReader); //从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的
String line = bufferedReader.readLine();
if (line != null) {
long size = Long.parseLong(line);
log.e("getFlashSize==Size==" + size);
if (size > 0) {
flashSize = (float) size / 1024L / 1024L / 2L;
log.e("getFlashSize==flashSize==" + flashSize);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (process != null) {
process.destroy();
}
if (inputStream != null) {
inputStream.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return flashSize = Math.round((float) flashSize) + 1;
}
}
2.获取EMCC大小
方法1:简化实现
long size = roundStorageSize(Environment.getDataDirectory().getTotalSpace()
+ Environment.getRootDirectory().getTotalSpace());
public static long roundStorageSize(long size) {
long val = 1;
long pow = 1;
while ((val * pow) < size) {
val <<= 1;
if (val > 512) {
val = 1;
pow *= 1000;
}
}
return val * pow;
}
方法2:
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SWLogger;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.DeviceInfoUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class StoragePreferenceController extends BasePreferenceController {
private static final String TAG = "DeviceModelPrefCtrl";
private static SWLogger log = SWLogger.getLogger(StoragePreferenceController.class);
public StoragePreferenceController(Context context, String key) {
super(context, key);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
return getStorage();
}
public String getStorage() {
int flashSize = (int) getFlashSize();
String value = String.valueOf(flashSize) + "GB";
log.d("getStorage==size==" + value);
return value;
}
private float getFlashSize() {
Process process = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
float flashSize = 0L;
try {
process = Runtime.getRuntime().exec("cat /sys/block/mmcblk2/size");
inputStream = process.getInputStream();
byte[] bytes = new byte[1024];
inputStreamReader = new InputStreamReader(inputStream); //读取
//创建字符流缓冲区
bufferedReader = new BufferedReader(inputStreamReader); //从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的
String line = bufferedReader.readLine();
if (line != null) {
long size = Long.parseLong(line);
log.e("getFlashSize==Size==" + size);
if (size > 0) {
flashSize = (float) size / 1024L / 1024L / 2L;
log.e("getFlashSize==flashSize==" + flashSize);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (process != null) {
process.destroy();
}
if (inputStream != null) {
inputStream.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return flashSize = Math.round((float) flashSize) + 1;
}
}