Settings: add logpersist selection (actions)

- Only add logpersist menu if debuggable.
- Only enable logpersist menu if Logging is On.
- Add selection for Off, All and All minus radio.
- Pop up warning box that persistent storage is to be cleared when
  selecting Off (but not if turned off by Logging Size Off or
  Disabling Debugger Options).

Bug: 28813587
Bug: 28936216
Change-Id: Ic0a58abdcd6ca41069efb818d1dcef4e3927d2de
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index 2288520..cd3c5ee 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -148,6 +148,13 @@
             android:entries="@array/select_logd_size_titles"
             android:entryValues="@array/select_logd_size_values" />
 
+        <ListPreference
+            android:key="select_logpersist"
+            android:title="@string/select_logpersist_title"
+            android:dialogTitle="@string/select_logpersist_dialog_title"
+            android:entries="@array/select_logpersist_titles"
+            android:entryValues="@array/select_logpersist_values" />
+
     </PreferenceCategory>
 
     <PreferenceCategory android:key="debug_networking_category"
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index 4697f43..4335817 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -172,6 +172,12 @@
     // 32768 is merely a menu marker, 64K is our lowest log buffer size we replace it with.
     private static final String SELECT_LOGD_MINIMUM_SIZE_VALUE = "65536";
     private static final String SELECT_LOGD_OFF_SIZE_MARKER_VALUE = "32768";
+    private static final String SELECT_LOGPERSIST_KEY = "select_logpersist";
+    private static final String SELECT_LOGPERSIST_PROPERTY = "persist.logd.logpersistd";
+    private static final String SELECT_LOGPERSIST_PROPERTY_SERVICE = "logcatd";
+    private static final String SELECT_LOGPERSIST_PROPERTY_CLEAR = "clear";
+    private static final String SELECT_LOGPERSIST_PROPERTY_STOP = "stop";
+    private static final String SELECT_LOGPERSIST_PROPERTY_BUFFER = "persist.logd.logpersistd.buffer";
 
     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
     private static final String WIFI_VERBOSE_LOGGING_KEY = "wifi_verbose_logging";
@@ -274,6 +280,7 @@
     private SwitchPreference mForceRtlLayout;
     private ListPreference mDebugHwOverdraw;
     private ListPreference mLogdSize;
+    private ListPreference mLogpersist;
     private ListPreference mUsbConfiguration;
     private ListPreference mTrackFrameTime;
     private ListPreference mShowNonRectClip;
@@ -314,6 +321,9 @@
     private Dialog mAdbKeysDialog;
     private boolean mUnavailable;
 
+    private boolean mLogpersistCleared;
+    private Dialog mLogpersistClearDialog;
+
     public DevelopmentSettings() {
         super(UserManager.DISALLOW_DEBUGGING_FEATURES);
     }
@@ -426,6 +436,18 @@
         mWifiAllowScansWithTraffic = findAndInitSwitchPref(WIFI_ALLOW_SCAN_WITH_TRAFFIC_KEY);
         mMobileDataAlwaysOn = findAndInitSwitchPref(MOBILE_DATA_ALWAYS_ON);
         mLogdSize = addListPreference(SELECT_LOGD_SIZE_KEY);
+        if ("1".equals(SystemProperties.get("ro.debuggable", "0"))) {
+            mLogpersist = addListPreference(SELECT_LOGPERSIST_KEY);
+        } else {
+            mLogpersist = (ListPreference) findPreference(SELECT_LOGPERSIST_KEY);
+            if (mLogpersist != null) {
+                mLogpersist.setEnabled(false);
+                if (debugDebuggingCategory != null) {
+                    debugDebuggingCategory.removePreference(mLogpersist);
+                }
+            }
+            mLogpersist = null;
+        }
         mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
         mWebViewProvider = addListPreference(WEBVIEW_PROVIDER_KEY);
         mWebViewMultiprocess = findAndInitSwitchPref(WEBVIEW_MULTIPROCESS_KEY);
@@ -694,6 +716,7 @@
         updateBugreportOptions();
         updateForceRtlOptions();
         updateLogdSizeValues();
+        updateLogpersistValues();
         updateWifiDisplayCertificationOptions();
         updateWifiVerboseLoggingOptions();
         updateWifiAggressiveHandoverOptions();
@@ -721,6 +744,7 @@
             }
         }
         resetDebuggerOptions();
+        writeLogpersistOption(null, true);
         writeLogdSizeOption(null);
         writeAnimationScaleOption(0, mWindowAnimationScale, null);
         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
@@ -1459,6 +1483,14 @@
             String currentValue = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
             if ((currentTag != null) && currentTag.startsWith(SELECT_LOGD_TAG_SILENCE)) {
                 currentValue = SELECT_LOGD_OFF_SIZE_MARKER_VALUE;
+                if (mLogpersist != null) {
+                   writeLogpersistOption(null, true);
+                   mLogpersist.setEnabled(false);
+                }
+            } else {
+                if ((mLogpersist != null) && mLastEnabledState) {
+                   mLogpersist.setEnabled(true);
+                }
             }
             if ((currentValue == null) || (currentValue.length() == 0)) {
                 currentValue = defaultLogdSizeValue();
@@ -1527,6 +1559,103 @@
         updateLogdSizeValues();
     }
 
+    private void updateLogpersistValues() {
+        if (mLogpersist == null) {
+            return;
+        }
+        String currentValue = SystemProperties.get(SELECT_LOGPERSIST_PROPERTY);
+        if (currentValue == null) {
+            currentValue = "";
+        }
+        String currentBuffers = SystemProperties.get(SELECT_LOGPERSIST_PROPERTY_BUFFER);
+        if ((currentBuffers == null) || (currentBuffers.length() == 0)) {
+            currentBuffers = "all";
+        }
+        int index = 0;
+        if (currentValue.equals(SELECT_LOGPERSIST_PROPERTY_SERVICE)) {
+            index = 1;
+            if (!currentBuffers.equals("all") &&
+                    !currentBuffers.contains("radio") &&
+                    currentBuffers.contains("security") &&
+                    currentBuffers.contains("kernel")) {
+                index = 2;
+                if (!currentBuffers.contains("default")) {
+                    String[] contains = { "main", "events", "system", "crash" };
+                    for (int i = 0; i < contains.length; i++) {
+                        if (!currentBuffers.contains(contains[i])) {
+                            index = 1;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        mLogpersist.setValue(getResources().getStringArray(R.array.select_logpersist_values)[index]);
+        mLogpersist.setSummary(getResources().getStringArray(R.array.select_logpersist_summaries)[index]);
+        mLogpersist.setOnPreferenceChangeListener(this);
+        if (index != 0) {
+            mLogpersistCleared = false;
+        } else if (!mLogpersistCleared) {
+            // would File.delete() directly but need to switch uid/gid to access
+            SystemProperties.set(SELECT_LOGPERSIST_PROPERTY, SELECT_LOGPERSIST_PROPERTY_CLEAR);
+            pokeSystemProperties();
+            mLogpersistCleared = true;
+        }
+    }
+
+    private void setLogpersistOff(boolean update) {
+        SystemProperties.set(SELECT_LOGPERSIST_PROPERTY_BUFFER, "");
+        SystemProperties.set(SELECT_LOGPERSIST_PROPERTY,
+            update ? "" : SELECT_LOGPERSIST_PROPERTY_STOP);
+        pokeSystemProperties();
+        if (update) {
+            updateLogpersistValues();
+        }
+    }
+
+    private void writeLogpersistOption(Object newValue, boolean skipWarning) {
+        if (mLogpersist == null) {
+            return;
+        }
+        String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
+        if ((currentTag != null) && currentTag.startsWith(SELECT_LOGD_TAG_SILENCE)) {
+            newValue = null;
+            skipWarning = true;
+        }
+
+        if ((newValue == null) || newValue.toString().equals("")) {
+            if (skipWarning) {
+                mLogpersistCleared = false;
+            } else if (!mLogpersistCleared) {
+                // if transitioning from on to off, pop up an are you sure?
+                String currentValue = SystemProperties.get(SELECT_LOGPERSIST_PROPERTY);
+                if ((currentValue != null) && (currentValue.length() != 0)) {
+                    if (mLogpersistClearDialog != null) dismissDialogs();
+                    mLogpersistClearDialog = new AlertDialog.Builder(getActivity()).setMessage(
+                            getActivity().getResources().getString(
+                                    R.string.dev_logpersist_clear_warning_message))
+                            .setTitle(R.string.dev_logpersist_clear_warning_title)
+                            .setPositiveButton(android.R.string.yes, this)
+                            .setNegativeButton(android.R.string.no, this)
+                            .show();
+                    mLogpersistClearDialog.setOnDismissListener(this);
+                    return;
+                }
+            }
+            setLogpersistOff(true);
+            return;
+        }
+
+        String currentBuffer = SystemProperties.get(SELECT_LOGPERSIST_PROPERTY_BUFFER);
+        if ((currentBuffer != null) && !currentBuffer.equals(newValue.toString())) {
+            setLogpersistOff(false);
+        }
+        SystemProperties.set(SELECT_LOGPERSIST_PROPERTY_BUFFER, newValue.toString());
+        SystemProperties.set(SELECT_LOGPERSIST_PROPERTY, SELECT_LOGPERSIST_PROPERTY_SERVICE);
+        pokeSystemProperties();
+        updateLogpersistValues();
+    }
+
     private void updateUsbConfigurationValues() {
         if (mUsbConfiguration != null) {
             UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
@@ -1957,6 +2086,9 @@
         } else if (preference == mLogdSize) {
             writeLogdSizeOption(newValue);
             return true;
+        } else if (preference == mLogpersist) {
+            writeLogpersistOption(newValue, false);
+            return true;
         } else if (preference == mUsbConfiguration) {
             writeUsbConfigurationOption(newValue);
             return true;
@@ -2004,6 +2136,10 @@
             mEnableDialog.dismiss();
             mEnableDialog = null;
         }
+        if (mLogpersistClearDialog != null) {
+            mLogpersistClearDialog.dismiss();
+            mLogpersistClearDialog = null;
+        }
     }
 
     public void onClick(DialogInterface dialog, int which) {
@@ -2040,6 +2176,12 @@
                 // Reset the toggle
                 mSwitchBar.setChecked(false);
             }
+        } else if (dialog == mLogpersistClearDialog) {
+            if (which == DialogInterface.BUTTON_POSITIVE) {
+                setLogpersistOff(true);
+            } else {
+                updateLogpersistValues();
+            }
         }
     }
 
@@ -2055,6 +2197,8 @@
                 mSwitchBar.setChecked(false);
             }
             mEnableDialog = null;
+        } else if (dialog == mLogpersistClearDialog) {
+            mLogpersistClearDialog = null;
         }
     }