WifiLogger: use R for ring buffer sizing

Use Resources to determine ring-buffer sizing.
This is to support board-specific ring-buffer
sizes.

BUG=29225988
TEST=unit tests
TEST=manual

Manual test (bullhead): Added some Log.e() statements,
and verified that RING_BUFFER_BYTE_LIMIT_SMALL and
RING_BUFFER_BYTE_LIMIT_LARGE were 32KB and 1024KB,
respectively.

Change-Id: I25ee7d871609c4ebe186424bfdd324a8283f43e3
(cherry picked from commit eacd212af097fada70bdb49da3ed06e8d172237b)
diff --git a/service/java/com/android/server/wifi/FrameworkFacade.java b/service/java/com/android/server/wifi/FrameworkFacade.java
index 7805d70..cb03f7a 100644
--- a/service/java/com/android/server/wifi/FrameworkFacade.java
+++ b/service/java/com/android/server/wifi/FrameworkFacade.java
@@ -49,8 +49,9 @@
     }
 
     public BaseWifiLogger makeRealLogger(
-            WifiStateMachine stateMachine, WifiNative wifiNative, BuildProperties buildProperties) {
-        return new WifiLogger(stateMachine, wifiNative, buildProperties);
+            Context context, WifiStateMachine stateMachine, WifiNative wifiNative,
+            BuildProperties buildProperties) {
+        return new WifiLogger(context, stateMachine, wifiNative, buildProperties);
     }
 
     public boolean setIntegerSetting(Context context, String name, int def) {
diff --git a/service/java/com/android/server/wifi/WifiLogger.java b/service/java/com/android/server/wifi/WifiLogger.java
index 4251df4..12a42c8 100644
--- a/service/java/com/android/server/wifi/WifiLogger.java
+++ b/service/java/com/android/server/wifi/WifiLogger.java
@@ -16,10 +16,12 @@
 
 package com.android.server.wifi;
 
+import android.content.Context;
 import android.util.Base64;
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.R;
 import com.android.server.wifi.util.ByteArrayRingBuffer;
 import com.android.server.wifi.util.StringUtil;
 
@@ -89,13 +91,13 @@
     /** minimum buffer size for each of the log levels */
     private static final int MinBufferSizes[] = new int[] { 0, 16384, 16384, 65536 };
 
-    @VisibleForTesting public static final int RING_BUFFER_BYTE_LIMIT_SMALL = 32 * 1024;
-    @VisibleForTesting public static final int RING_BUFFER_BYTE_LIMIT_LARGE = 1024 * 1024;
     @VisibleForTesting public static final String FIRMWARE_DUMP_SECTION_HEADER =
             "FW Memory dump";
     @VisibleForTesting public static final String DRIVER_DUMP_SECTION_HEADER =
             "Driver state dump";
 
+    private final int RING_BUFFER_BYTE_LIMIT_SMALL;
+    private final int RING_BUFFER_BYTE_LIMIT_LARGE;
     private int mLogLevel = VERBOSE_NO_LOG;
     private boolean mIsLoggingEventHandlerRegistered;
     private WifiNative.RingBufferStatus[] mRingBuffers;
@@ -103,15 +105,20 @@
     private WifiStateMachine mWifiStateMachine;
     private final WifiNative mWifiNative;
     private final BuildProperties mBuildProperties;
-    private int mMaxRingBufferSizeBytes = RING_BUFFER_BYTE_LIMIT_SMALL;
+    private int mMaxRingBufferSizeBytes;
 
-    public WifiLogger(
-            WifiStateMachine wifiStateMachine, WifiNative wifiNative,
-            BuildProperties buildProperties) {
+    public WifiLogger(Context context, WifiStateMachine wifiStateMachine, WifiNative wifiNative,
+                      BuildProperties buildProperties) {
+        RING_BUFFER_BYTE_LIMIT_SMALL = context.getResources().getInteger(
+                R.integer.config_wifi_logger_ring_buffer_default_size_limit_kb) * 1024;
+        RING_BUFFER_BYTE_LIMIT_LARGE = context.getResources().getInteger(
+                R.integer.config_wifi_logger_ring_buffer_verbose_size_limit_kb) * 1024;
+
         mWifiStateMachine = wifiStateMachine;
         mWifiNative = wifiNative;
         mBuildProperties = buildProperties;
         mIsLoggingEventHandlerRegistered = false;
+        mMaxRingBufferSizeBytes = RING_BUFFER_BYTE_LIMIT_SMALL;
     }
 
     @Override
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index fb3b293..13c29f3 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -1031,7 +1031,7 @@
                 R.bool.config_wifi_enable_wifi_firmware_debugging);
 
         if (enableFirmwareLogs) {
-            mWifiLogger = facade.makeRealLogger(this, mWifiNative, mBuildProperties);
+            mWifiLogger = facade.makeRealLogger(mContext, this, mWifiNative, mBuildProperties);
         } else {
             mWifiLogger = facade.makeBaseLogger();
         }
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiLoggerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiLoggerTest.java
index 55dc683..81c6274 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiLoggerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiLoggerTest.java
@@ -16,7 +16,9 @@
 
 package com.android.server.wifi;
 
+import android.content.Context;
 import android.test.suitebuilder.annotation.SmallTest;
+import com.android.internal.R;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
@@ -52,11 +54,15 @@
     @Mock WifiStateMachine mWsm;
     @Mock WifiNative mWifiNative;
     @Mock BuildProperties mBuildProperties;
+    @Mock Context mContext;
     WifiLogger mWifiLogger;
 
     private static final String FAKE_RING_BUFFER_NAME = "fake-ring-buffer";
-    private WifiNative.RingBufferStatus mFakeRbs;
+    private static final int SMALL_RING_BUFFER_SIZE_KB = 32;
+    private static final int LARGE_RING_BUFFER_SIZE_KB = 1024;
+    private static final int BYTES_PER_KBYTE = 1024;
 
+    private WifiNative.RingBufferStatus mFakeRbs;
     /**
      * Returns the data that we would dump in a bug report, for our ring buffer.
      * @return a 2-D byte array, where the first dimension is the record number, and the second
@@ -85,7 +91,14 @@
         when(mBuildProperties.isUserdebugBuild()).thenReturn(false);
         when(mBuildProperties.isUserBuild()).thenReturn(true);
 
-        mWifiLogger = new WifiLogger(mWsm, mWifiNative, mBuildProperties);
+        MockResources resources = new MockResources();
+        resources.setInteger(R.integer.config_wifi_logger_ring_buffer_default_size_limit_kb,
+                SMALL_RING_BUFFER_SIZE_KB);
+        resources.setInteger(R.integer.config_wifi_logger_ring_buffer_verbose_size_limit_kb,
+                LARGE_RING_BUFFER_SIZE_KB);
+        when(mContext.getResources()).thenReturn(resources);
+
+        mWifiLogger = new WifiLogger(mContext, mWsm, mWifiNative, mBuildProperties);
         mWifiNative.enableVerboseLogging(0);
     }
 
@@ -197,7 +210,7 @@
         final boolean verbosityToggle = false;
         mWifiLogger.startLogging(verbosityToggle);
 
-        final byte[] data = new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL];
+        final byte[] data = new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE];
         mWifiLogger.onRingBufferData(mFakeRbs, data);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
 
@@ -214,7 +227,7 @@
         final boolean verbosityToggle = false;
         mWifiLogger.startLogging(verbosityToggle);
 
-        final byte[] data1 = new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL];
+        final byte[] data1 = new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE];
         final byte[] data2 = {1, 2, 3};
         mWifiLogger.onRingBufferData(mFakeRbs, data1);
         mWifiLogger.onRingBufferData(mFakeRbs, data2);
@@ -526,7 +539,7 @@
         final boolean verbosityToggle = false;
         mWifiLogger.startLogging(verbosityToggle);
         mWifiLogger.onRingBufferData(
-                mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL + 1]);
+                mFakeRbs, new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE + 1]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(0, getLoggerRingBufferData().length);
     }
@@ -540,7 +553,7 @@
         when(mBuildProperties.isUserBuild()).thenReturn(false);
         mWifiLogger.startLogging(verbosityToggle);
         mWifiLogger.onRingBufferData(
-                mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL + 1]);
+                mFakeRbs, new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE + 1]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(0, getLoggerRingBufferData().length);
     }
@@ -554,7 +567,7 @@
         when(mBuildProperties.isUserBuild()).thenReturn(false);
         mWifiLogger.startLogging(verbosityToggle);
         mWifiLogger.onRingBufferData(
-                mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL + 1]);
+                mFakeRbs, new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE + 1]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(0, getLoggerRingBufferData().length);
     }
@@ -564,7 +577,8 @@
     public void ringBufferSizeIsLargeInVerboseMode() throws Exception {
         final boolean verbosityToggle = true;
         mWifiLogger.startLogging(verbosityToggle);
-        mWifiLogger.onRingBufferData(mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_LARGE]);
+        mWifiLogger.onRingBufferData(
+                mFakeRbs, new byte[LARGE_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(1, getLoggerRingBufferData().length);
     }
@@ -574,7 +588,8 @@
     public void startLoggingGrowsRingBuffersIfNeeded() throws Exception {
         mWifiLogger.startLogging(false  /* verbose disabled */);
         mWifiLogger.startLogging(true  /* verbose enabled */);
-        mWifiLogger.onRingBufferData(mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_LARGE]);
+        mWifiLogger.onRingBufferData(
+                mFakeRbs, new byte[LARGE_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(1, getLoggerRingBufferData().length);
     }
@@ -584,7 +599,7 @@
     public void startLoggingShrinksRingBuffersIfNeeded() throws Exception {
         mWifiLogger.startLogging(true  /* verbose enabled */);
         mWifiLogger.onRingBufferData(
-                mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL + 1]);
+                mFakeRbs, new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE + 1]);
 
         // Existing data is nuked (too large).
         mWifiLogger.startLogging(false  /* verbose disabled */);
@@ -593,7 +608,7 @@
 
         // New data must obey limit as well.
         mWifiLogger.onRingBufferData(
-                mFakeRbs, new byte[WifiLogger.RING_BUFFER_BYTE_LIMIT_SMALL + 1]);
+                mFakeRbs, new byte[SMALL_RING_BUFFER_SIZE_KB * BYTES_PER_KBYTE + 1]);
         mWifiLogger.captureBugReportData(WifiLogger.REPORT_REASON_NONE);
         assertEquals(0, getLoggerRingBufferData().length);
     }