logd: Add Developer settings for size

- Dependent on some logd/init/selinux security changes.

Bug: 14563261
Change-Id: I11e3aa97a0ba40c3f55041d98c02d7e73629e984
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index a4de070..7e094d4 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -891,6 +891,33 @@
         <item>Use ART debug build</item>
     </string-array>
 
+    <!-- Titles for logd limit size selection preference. [CHAR LIMIT=14] -->
+    <string-array name="select_logd_size_titles">
+        <item>64K</item>
+        <item>256K</item>
+        <item>1M</item>
+        <item>4M</item>
+        <item>16M</item>
+    </string-array>
+
+    <!-- Values for logd limit size selection preference. -->
+    <string-array name="select_logd_size_values" translatable="false" >
+        <item>65536</item>
+        <item>262144</item>
+        <item>1048576</item>
+        <item>4194304</item>
+        <item>16777216</item>
+    </string-array>
+
+    <!-- Summaries for logd limit size selection preference. [CHAR LIMIT=19]-->
+    <string-array name="select_logd_size_summaries" >
+        <item>64K per log buffer</item>
+        <item>256K per log buffer</item>
+        <item>1M per log buffer</item>
+        <item>4M per log buffer</item>
+        <item>16M per log buffer</item>
+    </string-array>
+
     <!-- Titles for HDCP checking preference. [CHAR LIMIT=35] -->
     <string-array name="hdcp_checking_titles">
         <item>Never check</item>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 22eb3e8..7f4293b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3166,6 +3166,10 @@
     <string name="wifi_display_certification">Wireless display certification</string>
     <!-- setting Checkbox summary whether to show options for wireless display certification  -->
     <string name="wifi_display_certification_summary">Show options for wireless display certification</string>
+    <!-- UI debug setting: limit size of Android logger buffers -->
+    <string name="select_logd_size_title">Logger buffer sizes</string>
+    <!-- UI debug setting: limit size of Android logger buffers [CHAR LIMIT=34] -->
+    <string name="select_logd_size_dialog_title">Select Logger sizes per log buffer</string>
     <!-- Setting Checkbox title whether to allow mock locations -->
     <string name="allow_mock_location">Allow mock locations</string>
     <!-- setting Checkbox summary whether to allow mock locations  -->
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index 5989c90..230af0d 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -105,6 +105,14 @@
             android:key="wifi_display_certification"
             android:title="@string/wifi_display_certification"
             android:summary="@string/wifi_display_certification_summary"/>
+
+        <ListPreference
+            android:key="select_logd_size"
+            android:title="@string/select_logd_size_title"
+            android:dialogTitle="@string/select_logd_size_dialog_title"
+            android:entries="@array/select_logd_size_titles"
+            android:entryValues="@array/select_logd_size_values" />
+
     </PreferenceCategory>
 
     <PreferenceCategory android:key="debug_input_category"
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index 229f169..a16f8b6 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -130,6 +130,9 @@
     private static final String DEBUG_DEBUGGING_CATEGORY_KEY = "debug_debugging_category";
     private static final String DEBUG_APPLICATIONS_CATEGORY_KEY = "debug_applications_category";
     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
+    private static final String SELECT_LOGD_SIZE_KEY = "select_logd_size";
+    private static final String SELECT_LOGD_SIZE_PROPERTY = "persist.logd.size";
+    private static final String SELECT_LOGD_DEFAULT_SIZE_PROPERTY = "ro.logd.size";
 
     private static final String OPENGL_TRACES_KEY = "enable_opengl_traces";
 
@@ -185,6 +188,7 @@
     private CheckBoxPreference mDebugLayout;
     private CheckBoxPreference mForceRtlLayout;
     private ListPreference mDebugHwOverdraw;
+    private ListPreference mLogdSize;
     private ListPreference mTrackFrameTime;
     private ListPreference mShowNonRectClip;
     private ListPreference mWindowAnimationScale;
@@ -291,6 +295,8 @@
         mForceRtlLayout = findAndInitCheckboxPref(FORCE_RTL_LAYOUT_KEY);
         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
         mWifiDisplayCertification = findAndInitCheckboxPref(WIFI_DISPLAY_CERTIFICATION_KEY);
+        mLogdSize = addListPreference(SELECT_LOGD_SIZE_KEY);
+
         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
         mAnimatorDurationScale = addListPreference(ANIMATOR_DURATION_SCALE_KEY);
@@ -496,6 +502,7 @@
         updateVerifyAppsOverUsbOptions();
         updateBugreportOptions();
         updateForceRtlOptions();
+        updateLogdSizeValues();
         updateWifiDisplayCertificationOptions();
     }
 
@@ -509,6 +516,7 @@
             }
         }
         resetDebuggerOptions();
+        writeLogdSizeOption(null);
         writeAnimationScaleOption(0, mWindowAnimationScale, null);
         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
         writeAnimationScaleOption(2, mAnimatorDurationScale, null);
@@ -958,6 +966,43 @@
                 mWifiDisplayCertification.isChecked() ? 1 : 0);
     }
 
+    private void updateLogdSizeValues() {
+        if (mLogdSize != null) {
+            String currentValue = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
+            if (currentValue == null) {
+                currentValue = SystemProperties.get(SELECT_LOGD_DEFAULT_SIZE_PROPERTY);
+                if (currentValue == null) {
+                    currentValue = "256K";
+                }
+            }
+            String[] values = getResources().getStringArray(R.array.select_logd_size_values);
+            String[] titles = getResources().getStringArray(R.array.select_logd_size_titles);
+            String[] summaries = getResources().getStringArray(R.array.select_logd_size_summaries);
+            int index = 1; // punt to second entry if not found
+            for (int i = 0; i < values.length; i++) {
+                if (currentValue.equals(values[i])
+                        || currentValue.equals(titles[i])) {
+                    index = i;
+                    break;
+                }
+            }
+            mLogdSize.setValue(values[index]);
+            mLogdSize.setSummary(summaries[index]);
+            mLogdSize.setOnPreferenceChangeListener(this);
+        }
+    }
+
+    private void writeLogdSizeOption(Object newValue) {
+        SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, newValue.toString());
+        pokeSystemProperties();
+        try {
+            Process p = Runtime.getRuntime().exec("logcat -b all -G " + newValue.toString());
+            int status = p.waitFor();
+        } catch (Exception e) {
+        }
+        updateLogdSizeValues();
+    }
+
     private void updateCpuUsageOptions() {
         updateCheckBox(mShowCpuUsage, Settings.Global.getInt(getActivity().getContentResolver(),
                 Settings.Global.SHOW_PROCESSES, 0) != 0);
@@ -1284,6 +1329,9 @@
             updateHdcpValues();
             pokeSystemProperties();
             return true;
+        } else if (preference == mLogdSize) {
+            writeLogdSizeOption(newValue);
+            return true;
         } else if (preference == mWindowAnimationScale) {
             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
             return true;