Bug 906230 - Add HardwareUtils.isLowMemoryPlatform() (r=kats)

This commit is contained in:
Lucas Rocha 2013-10-01 14:22:15 +01:00
parent 25e60eaf8c
commit b655fa84ef
2 changed files with 63 additions and 0 deletions

View File

@ -12,15 +12,34 @@ import android.os.Build;
import android.util.Log;
import android.view.ViewConfiguration;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class HardwareUtils {
private static final String LOGTAG = "GeckoHardwareUtils";
// Minimum memory threshold for a device to be considered
// a low memory platform (see sIsLowMemoryPlatform). This value
// has be in sync with Gecko's equivalent threshold (defined in
// xpcom/base/nsMemoryImpl.cpp) and should only be used in cases
// where we can't depend on Gecko to be up and running e.g. show/hide
// reading list capabilities in HomePager.
private static final int LOW_MEMORY_THRESHOLD_KB = 384 * 1024;
private static final String PROC_MEMINFO_FILE = "/proc/meminfo";
private static final Pattern PROC_MEMTOTAL_FORMAT =
Pattern.compile("^MemTotal:[ \t]*([0-9]*)[ \t]kB");
private static Context sContext;
private static Boolean sIsLargeTablet;
private static Boolean sIsSmallTablet;
private static Boolean sIsTelevision;
private static Boolean sHasMenuButton;
private static Boolean sIsLowMemoryPlatform;
private HardwareUtils() {
}
@ -73,4 +92,43 @@ public final class HardwareUtils {
}
return sHasMenuButton;
}
public static boolean isLowMemoryPlatform() {
if (sIsLowMemoryPlatform == null) {
RandomAccessFile fileReader = null;
try {
fileReader = new RandomAccessFile(PROC_MEMINFO_FILE, "r");
// Defaults to false
long totalMem = LOW_MEMORY_THRESHOLD_KB;
String line = null;
while ((line = fileReader.readLine()) != null) {
final Matcher matcher = PROC_MEMTOTAL_FORMAT.matcher(line);
if (matcher.find()) {
totalMem = Long.parseLong(matcher.group(1));
break;
}
}
sIsLowMemoryPlatform = (totalMem < LOW_MEMORY_THRESHOLD_KB);
} catch (IOException e) {
// Fallback to false if we fail to read meminfo
// for some reason.
Log.w(LOGTAG, "Could not read " + PROC_MEMINFO_FILE + "." +
"Falling back to isLowMemoryPlatform = false", e);
sIsLowMemoryPlatform = false;
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
// Do nothing
}
}
}
}
return sIsLowMemoryPlatform;
}
}

View File

@ -15,6 +15,11 @@
#ifdef ANDROID
#include <stdio.h>
// Minimum memory threshold for a device to be considered
// a low memory platform. This value has be in sync with
// Java's equivalent threshold, defined in
// mobile/android/base/util/HardwareUtils.java
#define LOW_MEMORY_THRESHOLD_KB (384 * 1024)
#endif