SysUI Tuner: UI cleanup and grouping

Clean up strings and locations of various features.

Change-Id: Ie427a2d8c51afa6f2a68b4cde1d6b15cda897e1a
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index fe3391c..03ccb3b 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -1315,15 +1315,14 @@
     <string name="keyboard_shortcut_group_system_back">Back</string>
 
     <!-- SysUI Tuner: Option to show full do not disturb panel in volume [CHAR LIMIT=60] -->
-    <string name="tuner_full_zen_title">Show do not disturb in volume</string>
-    <!-- SysUI Tuner: Summary of option to show full do not disturb panel in volume [CHAR LIMIT=NONE] -->
-    <string name="tuner_full_zen_summary">Allow full control of do not disturb in the volume dialog.</string>
+    <string name="tuner_full_zen_title">Show with volume controls</string>
 
-    <!-- SysUI Tuner: Label for screen about volume and do not disturb settings [CHAR LIMIT=60] -->
-    <string name="volume_and_do_not_disturb">Volume and Do not disturb</string>
+    <!-- SysUI Tuner: Label for screen about do not disturb settings [CHAR LIMIT=60] -->
+    <string name="volume_and_do_not_disturb">Do not disturb</string>
 
-    <!-- SysUI Tuner: Switch to control volume down behavior [CHAR LIMIT=60] -->
-    <string name="volume_down_silent">Enter do not disturb on volume down</string>
+    <!-- SysUI Tuner: Switch to control whether volume buttons enter/exit do
+         not disturb [CHAR LIMIT=60] -->
+    <string name="volume_dnd_silent">Volume buttons shortcut</string>
 
     <!-- SysUI Tuner: Switch to control volume up behavior [CHAR LIMIT=60] -->
     <string name="volume_up_silent">Exit do not disturb on volume up</string>
@@ -1343,9 +1342,6 @@
     <!-- Accessibility description of headset icon [CHAR LIMIT=NONE] -->
     <string name="accessibility_status_bar_headset">Headset connected</string>
 
-    <!-- Explanation of the status bar section of the tuner [CHAR LIMIT=NONE] -->
-    <string name="tuner_status_bar_explanation">Enable or disable icons from being shown in the status bar.</string>
-
     <!-- Label for quick settings tile for data saver [CHAR LIMIT=30] -->
     <string name="data_saver">Data Saver</string>
 
@@ -1421,4 +1417,21 @@
     <!-- Button to edit the tile ordering of quick settings [CHAR LIMIT=60] -->
     <string name="qs_edit">Edit</string>
 
+    <!-- SysUI Tuner: Options for how clock is displayed [CHAR LIMIT=NONE] -->
+    <string name="tuner_time">Time</string>
+
+    <!-- SysUI Tuner: Options for how clock is displayed [CHAR LIMIT=NONE] -->
+    <string-array name="clock_options">
+        <item>Show hours, minutes, and seconds</item>
+        <item>Show hours and minutes (default)</item>
+        <item>Don\'t show this icon</item>
+    </string-array>
+
+    <!-- SysUI Tuner: Options for how battery is displayed [CHAR LIMIT=NONE] -->
+    <string-array name="battery_options">
+        <item>Always show percentage</item>
+        <item>Show percentage when charging (default)</item>
+        <item>Don\'t show this icon</item>
+    </string-array>
+
 </resources>
diff --git a/packages/SystemUI/res/xml/tuner_prefs.xml b/packages/SystemUI/res/xml/tuner_prefs.xml
index 800762e..023a3f0 100644
--- a/packages/SystemUI/res/xml/tuner_prefs.xml
+++ b/packages/SystemUI/res/xml/tuner_prefs.xml
@@ -18,19 +18,10 @@
     xmlns:sysui="http://schemas.android.com/apk/res-auto"
     android:title="@string/system_ui_tuner">
 
-    <com.android.systemui.tuner.TunerSwitch
-        android:key="qs_show_brightness"
-        android:title="@string/show_brightness"
-        sysui:defValue="true" />
-
     <PreferenceScreen
         android:key="status_bar"
         android:title="@string/status_bar" >
 
-        <Preference
-            android:selectable="false"
-            android:summary="@string/tuner_status_bar_explanation" />
-
         <com.android.systemui.tuner.StatusBarSwitch
             android:key="rotate"
             android:title="@string/status_bar_settings_auto_rotation" />
@@ -91,9 +82,10 @@
 
         <!-- other weird signal stuff -->
 
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="battery"
-            android:title="@string/battery" />
+        <com.android.systemui.tuner.BatteryPreference
+            android:title="@string/battery"
+            android:summary="%s"
+            android:entries="@array/battery_options" />
 
         <com.android.systemui.tuner.StatusBarSwitch
             android:key="alarm_clock"
@@ -101,12 +93,37 @@
 
         <!-- secure -->
 
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="clock"
-            android:title="@string/clock" />
+        <com.android.systemui.tuner.ClockPreference
+            android:title="@string/tuner_time"
+            android:summary="%s"
+            android:entries="@array/clock_options" />
 
     </PreferenceScreen>
 
+    <Preference
+        android:key="color_transform"
+        android:title="@string/color_and_appearance"
+        android:fragment="com.android.systemui.tuner.ColorAndAppearanceFragment" />
+
+    <PreferenceScreen
+        android:key="volume_and_do_not_disturb"
+        android:title="@string/volume_and_do_not_disturb">
+
+        <com.android.systemui.tuner.TunerSwitch
+            android:key="sysui_show_full_zen"
+            android:title="@string/tuner_full_zen_title" />
+
+        <com.android.systemui.tuner.TunerSwitch
+            android:key="sysui_volume_down_silent,sysui_volume_up_silent"
+            android:title="@string/volume_dnd_silent"
+            sysui:defValue="true" />
+
+    </PreferenceScreen>
+
+    <Preference
+        android:key="nav_bar"
+        android:title="@string/nav_bar"
+        android:fragment="com.android.systemui.tuner.NavBarTuner" />
 
     <PreferenceScreen
         android:key="overview"
@@ -124,53 +141,6 @@
 
     </PreferenceScreen>
 
-    <SwitchPreference
-        android:key="battery_pct"
-        android:title="@string/show_battery_percentage"
-        android:summary="@string/show_battery_percentage_summary"
-        android:persistent="false" />
-
-    <com.android.systemui.tuner.TunerSwitch
-        android:key="clock_seconds"
-        android:title="@string/clock_seconds"
-        android:summary="@string/clock_seconds_desc" />
-
-    <Preference
-        android:key="demo_mode"
-        android:title="@string/demo_mode"
-        android:fragment="com.android.systemui.tuner.DemoModeFragment" />
-
-    <Preference
-        android:key="color_transform"
-        android:title="@string/color_and_appearance"
-        android:fragment="com.android.systemui.tuner.ColorAndAppearanceFragment" />
-
-    <PreferenceScreen
-        android:key="volume_and_do_not_disturb"
-        android:title="@string/volume_and_do_not_disturb">
-
-        <com.android.systemui.tuner.TunerSwitch
-            android:key="sysui_show_full_zen"
-            android:title="@string/tuner_full_zen_title"
-            android:summary="@string/tuner_full_zen_summary" />
-
-        <com.android.systemui.tuner.TunerSwitch
-            android:key="sysui_volume_down_silent"
-            android:title="@string/volume_down_silent"
-            sysui:defValue="true" />
-
-        <com.android.systemui.tuner.TunerSwitch
-            android:key="sysui_volume_up_silent"
-            android:title="@string/volume_up_silent"
-            sysui:defValue="true" />
-
-    </PreferenceScreen>
-
-    <Preference
-        android:key="nav_bar"
-        android:title="@string/nav_bar"
-        android:fragment="com.android.systemui.tuner.NavBarTuner" />
-
     <!-- Warning, this goes last. -->
     <Preference
         android:summary="@string/tuner_persistent_warning"
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
index df5a622..6550c87 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
@@ -456,11 +456,9 @@
                 mIconController.setIcon(mSlotRotate, R.drawable.stat_sys_rotate_landscape,
                         mContext.getString(R.string.accessibility_rotation_lock_on_landscape));
             }
+            mIconController.setIconVisibility(mSlotRotate, true);
         } else {
-            mIconController.setIcon(mSlotRotate, portrait
-                    ? R.drawable.stat_sys_auto_rotate_portrait
-                    : R.drawable.stat_sys_auto_rotate_landscape,
-                    mContext.getString(R.string.accessibility_rotation_lock_off));
+            mIconController.setIconVisibility(mSlotRotate, false);
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java b/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java
new file mode 100644
index 0000000..8881c79
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.systemui.tuner;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.support.v7.preference.DropDownPreference;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import android.util.AttributeSet;
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.MetricsProto.MetricsEvent;
+import com.android.systemui.statusbar.phone.StatusBarIconController;
+
+import static com.android.systemui.BatteryMeterDrawable.SHOW_PERCENT_SETTING;
+
+public class BatteryPreference extends DropDownPreference implements TunerService.Tunable {
+
+    private static final String PERCENT = "percent";
+    private static final String DEFAULT = "default";
+    private static final String DISABLED = "disabled";
+
+    private final String mBattery;
+    private boolean mBatteryEnabled;
+    private boolean mHasPercentage;
+    private ArraySet<String> mBlacklist;
+    private boolean mHasSetValue;
+
+    public BatteryPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        mBattery = context.getString(com.android.internal.R.string.status_bar_battery);
+        setEntryValues(new CharSequence[] {PERCENT, DEFAULT, DISABLED });
+    }
+
+    @Override
+    public void onAttached() {
+        super.onAttached();
+        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
+        mHasPercentage = Settings.System.getInt(getContext().getContentResolver(),
+                SHOW_PERCENT_SETTING, 0) != 0;
+    }
+
+    @Override
+    public void onDetached() {
+        TunerService.get(getContext()).removeTunable(this);
+        super.onDetached();
+    }
+
+    @Override
+    public void onTuningChanged(String key, String newValue) {
+        if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
+            mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
+            mBatteryEnabled = !mBlacklist.contains(mBattery);
+        }
+        if (!mHasSetValue) {
+            // Because of the complicated tri-state it can end up looping and setting state back to
+            // what the user didn't choose.  To avoid this, just set the state once and rely on the
+            // preference to handle updates.
+            mHasSetValue = true;
+            if (mBatteryEnabled && mHasPercentage) {
+                setValue(PERCENT);
+            } else if (mBatteryEnabled) {
+                setValue(DEFAULT);
+            } else {
+                setValue(DISABLED);
+            }
+        }
+    }
+
+    @Override
+    protected boolean persistString(String value) {
+        final boolean v = PERCENT.equals(value);
+        MetricsLogger.action(getContext(), MetricsEvent.TUNER_BATTERY_PERCENTAGE, v);
+        Settings.System.putInt(getContext().getContentResolver(), SHOW_PERCENT_SETTING, v ? 1 : 0);
+        if (DISABLED.equals(value)) {
+            mBlacklist.add(mBattery);
+        } else {
+            mBlacklist.remove(mBattery);
+        }
+        TunerService.get(getContext()).setValue(StatusBarIconController.ICON_BLACKLIST,
+                TextUtils.join(",", mBlacklist));
+        return true;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java b/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java
new file mode 100644
index 0000000..ea92443
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.systemui.tuner;
+
+import android.content.Context;
+import android.support.v7.preference.DropDownPreference;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import android.util.AttributeSet;
+import com.android.systemui.statusbar.phone.StatusBarIconController;
+import com.android.systemui.statusbar.policy.Clock;
+
+public class ClockPreference extends DropDownPreference implements TunerService.Tunable {
+
+    private static final String SECONDS = "seconds";
+    private static final String DEFAULT = "default";
+    private static final String DISABLED = "disabled";
+
+    private final String mClock;
+    private boolean mClockEnabled;
+    private boolean mHasSeconds;
+    private ArraySet<String> mBlacklist;
+    private boolean mHasSetValue;
+
+    public ClockPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        mClock = context.getString(com.android.internal.R.string.status_bar_clock);
+        setEntryValues(new CharSequence[] { SECONDS, DEFAULT, DISABLED });
+    }
+
+    @Override
+    public void onAttached() {
+        super.onAttached();
+        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST,
+                Clock.CLOCK_SECONDS);
+    }
+
+    @Override
+    public void onDetached() {
+        TunerService.get(getContext()).removeTunable(this);
+        super.onDetached();
+    }
+
+    @Override
+    public void onTuningChanged(String key, String newValue) {
+        if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
+            mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
+            mClockEnabled = !mBlacklist.contains(mClock);
+        } else if (Clock.CLOCK_SECONDS.equals(key)) {
+            mHasSeconds = newValue != null && Integer.parseInt(newValue) != 0;
+        }
+        if (!mHasSetValue) {
+            // Because of the complicated tri-state it can end up looping and setting state back to
+            // what the user didn't choose.  To avoid this, just set the state once and rely on the
+            // preference to handle updates.
+            mHasSetValue = true;
+            if (mClockEnabled && mHasSeconds) {
+                setValue(SECONDS);
+            } else if (mClockEnabled) {
+                setValue(DEFAULT);
+            } else {
+                setValue(DISABLED);
+            }
+        }
+    }
+
+    @Override
+    protected boolean persistString(String value) {
+        TunerService.get(getContext()).setValue(Clock.CLOCK_SECONDS, SECONDS.equals(value) ? 1 : 0);
+        if (DISABLED.equals(value)) {
+            mBlacklist.add(mClock);
+        } else {
+            mBlacklist.remove(mClock);
+        }
+        TunerService.get(getContext()).setValue(StatusBarIconController.ICON_BLACKLIST,
+                TextUtils.join(",", mBlacklist));
+        return true;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
index 9df5368..70f2fdc 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerFragment.java
@@ -37,8 +37,6 @@
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.systemui.R;
 
-import static com.android.systemui.BatteryMeterDrawable.SHOW_PERCENT_SETTING;
-
 public class TunerFragment extends PreferenceFragment {
 
     private static final String TAG = "TunerFragment";
@@ -51,10 +49,6 @@
 
     private static final int MENU_REMOVE = Menu.FIRST + 1;
 
-    private final SettingObserver mSettingObserver = new SettingObserver();
-
-    private SwitchPreference mBatteryPct;
-
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -72,7 +66,6 @@
     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
         addPreferencesFromResource(R.xml.tuner_prefs);
 
-        mBatteryPct = (SwitchPreference) findPreference(KEY_BATTERY_PCT);
         if (Settings.Secure.getInt(getContext().getContentResolver(), SETTING_SEEN_TUNER_WARNING,
                 0) == 0) {
             if (getFragmentManager().findFragmentByTag(WARNING_TAG) == null) {
@@ -85,9 +78,6 @@
     public void onResume() {
         super.onResume();
         getActivity().setTitle(R.string.system_ui_tuner);
-        updateBatteryPct();
-        getContext().getContentResolver().registerContentObserver(
-                System.getUriFor(SHOW_PERCENT_SETTING), false, mSettingObserver);
 
         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, true);
     }
@@ -95,7 +85,6 @@
     @Override
     public void onPause() {
         super.onPause();
-        getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
 
         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, false);
     }
@@ -123,35 +112,6 @@
         return super.onOptionsItemSelected(item);
     }
 
-    private void updateBatteryPct() {
-        mBatteryPct.setOnPreferenceChangeListener(null);
-        mBatteryPct.setChecked(System.getInt(getContext().getContentResolver(),
-                SHOW_PERCENT_SETTING, 0) != 0);
-        mBatteryPct.setOnPreferenceChangeListener(mBatteryPctChange);
-    }
-
-    private final class SettingObserver extends ContentObserver {
-        public SettingObserver() {
-            super(new Handler());
-        }
-
-        @Override
-        public void onChange(boolean selfChange, Uri uri, int userId) {
-            super.onChange(selfChange, uri, userId);
-            updateBatteryPct();
-        }
-    }
-
-    private final OnPreferenceChangeListener mBatteryPctChange = new OnPreferenceChangeListener() {
-        @Override
-        public boolean onPreferenceChange(Preference preference, Object newValue) {
-            final boolean v = (Boolean) newValue;
-            MetricsLogger.action(getContext(), MetricsEvent.TUNER_BATTERY_PERCENTAGE, v);
-            System.putInt(getContext().getContentResolver(), SHOW_PERCENT_SETTING, v ? 1 : 0);
-            return true;
-        }
-    };
-
     public static class TunerWarningFragment extends DialogFragment {
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
index 7ad752e..b738136 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerSwitch.java
@@ -23,7 +23,7 @@
     @Override
     public void onAttached() {
         super.onAttached();
-        TunerService.get(getContext()).addTunable(this, getKey());
+        TunerService.get(getContext()).addTunable(this, getKey().split(","));
     }
 
     @Override
@@ -39,7 +39,9 @@
 
     @Override
     protected boolean persistBoolean(boolean value) {
-        Settings.Secure.putString(getContext().getContentResolver(), getKey(), value ? "1" : "0");
+        for (String key : getKey().split(",")) {
+            Settings.Secure.putString(getContext().getContentResolver(), key, value ? "1" : "0");
+        }
         return true;
     }