所有存储设备约定成俗,实际容量会比显示容量小。比如,1TB厂商会以1000GB计算,而不是1024GB,所以显示的是1000/1024=976GB。
android设备也是如此,对外规格有1+8和2+16,故客户希望显示实际的值。
RAM
android RAM是直接读取/proc/meminfo里的数值,修改的接口是在framework层,仅需在此修改即可解决设置中显示的数值。
这里需要注意的是,无法针对第三方检测软件,因为第三方无需root也能直接读取/proc/meminfo的值。
P2000L:/ $ cat /proc/meminfo
MemTotal: 1930740 kB
MemFree: 212328 kB
Buffers: 31960 kB
Cached: 808864 kB
SwapCached: 0 kB
Active: 786688 kB
Inactive: 500180 kB
Active(anon): 446392 kB
Inactive(anon): 1124 kB
Active(file): 340296 kB
Inactive(file): 499056 kB
Unevictable: 256 kB
Mlocked: 256 kB
SwapTotal: 524284 kB
SwapFree: 524284 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 446340 kB
Mapped: 279252 kB
Shmem: 1488 kB
Slab: 255676 kB
SReclaimable: 40900 kB
SUnreclaim: 214776 kB
KernelStack: 21264 kB
PageTables: 29404 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1489652 kB
Committed_AS: 40764468 kB
VmallocTotal: 251658176 kB
VmallocUsed: 103424 kB
VmallocChunk: 251454644 kB
通过指令可以获取MemTotal、MemFree等数值,上层在读取之后构建数组分别存储这些数值。
在存储时做特殊的修改即可。
这里需要注意,只要是系统直接读取/proc/meminfo的地方都需要修改,目前作者在测试的协助下找到2处。
Index: frameworks/base/core/jni/android_os_Debug.cpp
===================================================================
--- frameworks/base/core/jni/android_os_Debug.cpp (版本 491)
+++ frameworks/base/core/jni/android_os_Debug.cpp (工作副本)
@@ -737,6 +737,11 @@
};
long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+ // @ + for BUG # {
+ static const long RAM_1G = 1048576L;
+ static const long RAM_2G = 2097152L;
+ // @ + for BUG # }
+
char* p = buffer;
while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
int i = 0;
@@ -751,6 +756,15 @@
p++;
}
mem[i] = atoll(num);
+ // @ + for BUG # {
+ if (i == 0) {
+ if (mem[i] < RAM_1G) {
+ mem[i] = RAM_1G;
+ } else if (mem[i] > RAM_1G && mem[i] < RAM_2G) {
+ mem[i] = RAM_2G;
+ }
+ }
+ // @ + for BUG # }
numFound++;
break;
}
Index: frameworks/base/core/jni/android_util_Process.cpp
===================================================================
--- frameworks/base/core/jni/android_util_Process.cpp (版本 491)
+++ frameworks/base/core/jni/android_util_Process.cpp (工作副本)
@@ -609,6 +609,11 @@
size_t numFound = 0;
jlong mem = 0;
+
+ // @ + for BUG # {
+ static const long RAM_1G = 1048576L * 1024;
+ static const long RAM_2G = 2097152L * 1024;
+ // @ + for BUG # }
char* p = buffer;
while (*p && numFound < num) {
@@ -625,6 +630,15 @@
if (*p == 0) p--;
}
mem += atoll(num) * 1024;
+ // @ + for BUG # {
+ if (i == 0) {
+ if (mem < RAM_1G) {
+ mem = RAM_1G;
+ } else if (mem > RAM_1G && mem < RAM_2G) {
+ mem = RAM_2G;
+ }
+ }
+ // @ + for BUG # }
numFound++;
break;
}
ROM
android rom实现的机制没有深究,在android N之前的该法:
Index: frameworks/base/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java
===================================================================
--- frameworks/base/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java (版本 495)
+++ frameworks/base/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java (工作副本)
@@ -222,9 +222,15 @@
try {
final StructStatVfs stat = Os.statvfs(path);
- final long totalSize = stat.f_blocks * stat.f_bsize;
- final long availSize = stat.f_bavail * stat.f_bsize;
- return new long[] { totalSize, availSize };
+ if ("/data".equals(path)) { // @ +
+ final long totalSize = 8589934592L; // set totalSize (ROM) to 8Gb.
+ final long availSize = stat.f_bavail * stat.f_bsize;
+ return new long[] { totalSize, availSize };
+ } else {
+ final long totalSize = stat.f_blocks * stat.f_bsize;
+ final long availSize = stat.f_bavail * stat.f_bsize;
+ return new long[] { totalSize, availSize };
+ }
} catch (ErrnoException e) {
throw new IllegalStateException(e);
}
android N之后的该法:
Index: frameworks/base/core/java/android/os/storage/StorageManager.java
===================================================================
--- frameworks/base/core/java/android/os/storage/StorageManager.java (版本 495)
+++ frameworks/base/core/java/android/os/storage/StorageManager.java (工作副本)
@@ -142,11 +142,8 @@
};
private static final int INTERNAL_STORAGE_SECTOR_SIZE = 512;
- // @ + for BUG # {
- private static final long TOTAL_8G = 8589934592L;
- private static final long TOTAL_16G = 17179869184L;
- // @ + for BUG # }
+
private final Context mContext;
private final ContentResolver mResolver;
@@ -939,14 +936,6 @@
for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
final long numberBlocks = readLong(path);
if (numberBlocks > 0) {
- // @ + for BUG # {
- if (numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE < TOTAL_8G) {
- return TOTAL_8G;
- } else if (numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE > TOTAL_8G &&
- numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE < TOTAL_16G) {
- return TOTAL_16G;
- }
- // @ +- for BUG # }
return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
}
}