Bug 1239376 - Fix StrictMode thread read access in SysInfo.getMemSize. r=mfinkle draft
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 27 Jan 2016 12:06:33 -0800
changeset 326271 faa74a9297c8476f965a5f9268d5b9095458dd2b
parent 325499 0fa26d9b2b8f947356bd746cf5dc4c42785a06a2
child 326272 2000032303be40a20eecbec2e1e3336f2cfc0d35
push id10128
push usermichael.l.comella@gmail.com
push dateWed, 27 Jan 2016 20:23:48 +0000
reviewersmfinkle
bugs1239376
milestone46.0a1
Bug 1239376 - Fix StrictMode thread read access in SysInfo.getMemSize. r=mfinkle This caused a StrictMode violation on startup.
mobile/android/base/java/org/mozilla/gecko/SysInfo.java
--- a/mobile/android/base/java/org/mozilla/gecko/SysInfo.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SysInfo.java
@@ -138,16 +138,19 @@ public final class SysInfo {
      */
     public static int getMemSize() {
         if (totalRAM >= 0) {
             return totalRAM;
         }
 
         // This is the string "MemTotal" that we're searching for in the buffer.
         final byte[] MEMTOTAL = {'M', 'e', 'm', 'T', 'o', 't', 'a', 'l'};
+
+        // `/proc/meminfo` is not a real file and thus safe to read on the main thread.
+        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
         try {
             final byte[] buffer = new byte[MEMINFO_BUFFER_SIZE_BYTES];
             final FileInputStream is = new FileInputStream("/proc/meminfo");
             try {
                 final int length = is.read(buffer);
 
                 for (int i = 0; i < length; i++) {
                     if (matchMemText(buffer, i, length, MEMTOTAL)) {
@@ -162,16 +165,18 @@ public final class SysInfo {
             }
 
             Log.w(LOG_TAG, "Did not find MemTotal line in /proc/meminfo.");
             return totalRAM = 0;
         } catch (FileNotFoundException f) {
             return totalRAM = 0;
         } catch (IOException e) {
             return totalRAM = 0;
+        } finally {
+            StrictMode.setThreadPolicy(savedPolicy);
         }
     }
 
     /**
      * @return the SDK version supported by this device, such as '16'.
      */
     public static int getVersion() {
         return android.os.Build.VERSION.SDK_INT;