Merge "Add battery percentage switch to display settings"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1a2957e1..d2e3915 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4550,6 +4550,11 @@
<!-- [CHAR_LIMIT=40] Battery saver: Value for automatic entry option: pct% battery -->
<string name="battery_saver_turn_on_automatically_pct">at <xliff:g id="percent">%1$s</xliff:g> battery</string>
+ <!-- [CHAR_LIMIT=40] Battery percentage: Title -->
+ <string name="battery_percentage">Battery percentage</string>
+ <!-- [CHAR_LIMIT=NONE] Battery percentage: Description for preference -->
+ <string name="battery_percentage_description">Show battery percentage in status bar</string>
+
<!-- Process Stats strings -->
<skip />
diff --git a/res/xml/power_usage_summary.xml b/res/xml/power_usage_summary.xml
index 58cab77..aecb6f1 100644
--- a/res/xml/power_usage_summary.xml
+++ b/res/xml/power_usage_summary.xml
@@ -34,6 +34,11 @@
android:title="@string/battery_saver"
android:fragment="com.android.settings.fuelgauge.BatterySaverSettings"/>
+ <SwitchPreference
+ android:key="battery_percentage"
+ android:title="@string/battery_percentage"
+ android:summary="@string/battery_percentage_description"/>
+
<!-- Cross-listed item, if you change this, also change it in ia_display_settings.xml -->
<SwitchPreference
android:key="auto_brightness"
diff --git a/src/com/android/settings/display/BatteryPercentagePreferenceController.java b/src/com/android/settings/display/BatteryPercentagePreferenceController.java
new file mode 100644
index 0000000..4968957
--- /dev/null
+++ b/src/com/android/settings/display/BatteryPercentagePreferenceController.java
@@ -0,0 +1,65 @@
+/*
+ * 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.settings.display;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.support.v7.preference.Preference;
+import android.support.v14.preference.SwitchPreference;
+
+import com.android.settings.core.PreferenceController;
+
+import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
+
+/**
+ * A controller to manage the switch for showing battery percentage in the status bar.
+ */
+
+public class BatteryPercentagePreferenceController extends PreferenceController
+ implements Preference.OnPreferenceChangeListener {
+
+ private static final String KEY_BATTERY_PERCENTAGE = "battery_percentage";
+
+ public BatteryPercentagePreferenceController(Context context) {
+ super(context);
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return true;
+ }
+
+ @Override
+ public String getPreferenceKey() {
+ return KEY_BATTERY_PERCENTAGE;
+ }
+
+ @Override
+ public void updateState(Preference preference) {
+ int setting = Settings.System.getInt(mContext.getContentResolver(),
+ SHOW_BATTERY_PERCENT, 0);
+
+ ((SwitchPreference) preference).setChecked(setting == 1);
+ }
+
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ boolean showPercentage = (Boolean) newValue;
+ Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,
+ showPercentage ? 1 : 0);
+ return true;
+ }
+}
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 44f61c1..1ffc594 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -56,6 +56,7 @@
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.display.AutoBrightnessPreferenceController;
+import com.android.settings.display.BatteryPercentagePreferenceController;
import com.android.settings.display.TimeoutPreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -185,6 +186,7 @@
controllers.add(new AutoBrightnessPreferenceController(context));
controllers.add(new TimeoutPreferenceController(context));
controllers.add(new BatterySaverController(context, getLifecycle()));
+ controllers.add(new BatteryPercentagePreferenceController(context));
return controllers;
}
diff --git a/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java
new file mode 100644
index 0000000..792b853
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.settings.display;
+
+import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.provider.Settings;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class BatteryPercentagePreferenceControllerTest {
+ @Mock private Context mContext;
+ private BatteryPercentagePreferenceController mController;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mController = new BatteryPercentagePreferenceController(mContext);
+ }
+
+ private int getPercentageSetting() {
+ return Settings.System.getInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT, 0);
+ }
+
+ @Test
+ public void testOnPreferenceChange_TurnOnPercentage_PercentageOn() {
+ mController.onPreferenceChange(null, true);
+ final int isOn = getPercentageSetting();
+ assertThat(isOn).isEqualTo(1);
+ }
+
+ @Test
+ public void testOnPreferenceChange_TurnOffPercentage_PercentageOff() {
+ mController.onPreferenceChange(null, false);
+ final int isOn = getPercentageSetting();
+ assertThat(isOn).isEqualTo(0);
+ }
+}