Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a6b01f19 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add battery percentage switch to display settings"

parents 6c2f9fe6 3822f122
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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 />
+5 −0
Original line number Diff line number Diff line
@@ -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"
+65 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import com.android.settings.core.PreferenceController;
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 @@ public class PowerUsageSummary extends PowerUsageBase {
        controllers.add(new AutoBrightnessPreferenceController(context));
        controllers.add(new TimeoutPreferenceController(context));
        controllers.add(new BatterySaverController(context, getLifecycle()));
        controllers.add(new BatteryPercentagePreferenceController(context));
        return controllers;
    }

+62 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}