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

Commit cf5ef773 authored by Wesley Wang's avatar Wesley Wang Committed by Android (Google) Code Review
Browse files

Merge "Refactor battery saver schedule method"

parents 88972a7f 934700b5
Loading
Loading
Loading
Loading
+7 −12
Original line number Diff line number Diff line
@@ -15,9 +15,10 @@
 */
package com.android.settings.fuelgauge.batterysaver;

import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;

import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;

@@ -61,20 +62,14 @@ public class BatterySaverSchedulePreferenceController extends BasePreferenceCont
    @Override
    public CharSequence getSummary() {
        final ContentResolver resolver = mContext.getContentResolver();
        final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
        final String mode = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
        if (KEY_PERCENTAGE.equals(mode)) {
            final int threshold =
                    Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
            if (threshold <= 0) {
                return mContext.getText(R.string.battery_saver_auto_no_schedule);
            } else {
            return mContext.getString(R.string.battery_saver_auto_percentage_summary,
                    Utils.formatPercentage(threshold));
        }
        } else {
            return mContext.getText(R.string.battery_saver_auto_routine);
        }
        return mContext.getText(R.string.battery_saver_auto_no_schedule);
    }

    @Override
+3 −18
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.settings.fuelgauge.batterysaver;

import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_NO_SCHEDULE;
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;

import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
@@ -22,7 +25,6 @@ import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.text.TextUtils;
import android.util.Log;

import com.android.settingslib.fuelgauge.BatterySaverUtils;

@@ -39,8 +41,6 @@ import com.android.settingslib.fuelgauge.BatterySaverUtils;
public class BatterySaverScheduleRadioButtonsController {
    private static final String TAG = "BatterySaverScheduleRadioButtonsController";

    public static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule";
    public static final String KEY_PERCENTAGE = "key_battery_saver_percentage";
    public static final int TRIGGER_LEVEL_MIN = 10;

    private Context mContext;
@@ -52,21 +52,6 @@ public class BatterySaverScheduleRadioButtonsController {
        mSeekBarController = seekbar;
    }

    public String getDefaultKey() {
        final ContentResolver resolver = mContext.getContentResolver();
        final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
            final int threshold =
                    Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
            return threshold <= 0 ? KEY_NO_SCHEDULE : KEY_PERCENTAGE;
        }
        // Convert the legacy routine mode into none.
        BatterySaverUtils.revertScheduleToNoneIfNeeded(mContext);
        Log.w(TAG, "Found the legacy routine mode and set into none");
        return KEY_NO_SCHEDULE;
    }

    public boolean setDefaultKey(String key) {
        if (key == null) {
            return false;
+12 −18
Original line number Diff line number Diff line
@@ -15,9 +15,10 @@
 */
package com.android.settings.fuelgauge.batterysaver;

import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;

import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.widget.SeekBar;
@@ -31,6 +32,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.fuelgauge.BatterySaverUtils;

/**
 * Responds to user actions in the Settings > Battery > Set a Schedule Screen for the seekbar.
@@ -99,17 +101,10 @@ public class BatterySaverScheduleSeekBarController implements

    public void updateSeekBar() {
        final ContentResolver resolver = mContext.getContentResolver();
        // Note: this can also be obtained via PowerManager.getPowerSaveModeTrigger()
        final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        // if mode is "dynamic" we are in routine mode, percentage with non-zero threshold is
        // percentage mode, otherwise it is no schedule mode
        if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
        final String mode = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
        if (KEY_PERCENTAGE.equals(mode)) {
            final int threshold =
                    Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
            if (threshold <= 0) {
                mSeekBarPreference.setVisible(false);
            } else {
            final int currentSeekbarValue = Math.max(threshold / 5, MIN_SEEKBAR_VALUE);
            mSeekBarPreference.setVisible(true);
            mSeekBarPreference.setProgress(currentSeekbarValue);
@@ -117,7 +112,6 @@ public class BatterySaverScheduleSeekBarController implements
                    currentSeekbarValue * 5);
            mSeekBarPreference.setTitle(stateDescription);
            mSeekBarPreference.overrideSeekBarStateDescription(stateDescription);
            }
        } else {
            mSeekBarPreference.setVisible(false);
        }
+9 −6
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.settings.fuelgauge.batterysaver;

import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_NO_SCHEDULE;
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.database.ContentObserver;
@@ -27,7 +30,6 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
@@ -95,7 +97,7 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
                Settings.Secure.getUriFor(Settings.Secure.LOW_POWER_WARNING_ACKNOWLEDGED),
                false,
                mSettingsObserver);
        mSaverScheduleKey = mRadioButtonController.getDefaultKey();
        mSaverScheduleKey = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
        mSaverPercentage = getSaverPercentage();
    }

@@ -125,13 +127,13 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
        candidates.add(new BatterySaverScheduleCandidateInfo(
                context.getText(R.string.battery_saver_auto_no_schedule),
                /* summary */ null,
                BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE,
                KEY_NO_SCHEDULE,
                /* enabled */ true));
        BatterySaverUtils.revertScheduleToNoneIfNeeded(context);
        candidates.add(new BatterySaverScheduleCandidateInfo(
                context.getText(R.string.battery_saver_auto_percentage),
                /* summary */ null,
                BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE,
                KEY_PERCENTAGE,
                /* enabled */ true));

        return candidates;
@@ -157,7 +159,7 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {

    @Override
    protected String getDefaultKey() {
        return mRadioButtonController.getDefaultKey();
        return BatterySaverUtils.getBatterySaverScheduleKey(mContext);
    }

    @Override
@@ -172,7 +174,8 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {

    private void logPowerSaver() {
        final int currentSaverPercentage = getSaverPercentage();
        final String currentSaverScheduleKey = mRadioButtonController.getDefaultKey();
        final String currentSaverScheduleKey = BatterySaverUtils.getBatterySaverScheduleKey(
                mContext);
        if (mSaverScheduleKey.equals(currentSaverScheduleKey)
                && mSaverPercentage == currentSaverPercentage) {
            return;
+3 −28
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.settings.fuelgauge.batterysaver;

import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;

import static com.google.common.truth.Truth.assertThat;

import android.content.ContentResolver;
@@ -47,33 +49,6 @@ public class BatterySaverScheduleRadioButtonsControllerTest {
        mResolver = mContext.getContentResolver();
    }

    @Test
    public void getDefaultKey_routine_returnsCorrectValue() {
        Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
        assertThat(mController.getDefaultKey())
                .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE);
    }

    @Test
    public void getDefaultKey_automatic_returnsCorrectValue() {
        Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
        assertThat(mController.getDefaultKey())
                .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE);
    }

    @Test
    public void getDefaultKey_none_returnsCorrectValue() {
        Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
        assertThat(mController.getDefaultKey())
                .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE);
    }


    @Test
    public void setDefaultKey_percentage_shouldSuppressNotification() {
        Secure.putInt(
@@ -81,7 +56,7 @@ public class BatterySaverScheduleRadioButtonsControllerTest {
        Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
                PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
        Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
        mController.setDefaultKey(BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE);
        mController.setDefaultKey(KEY_PERCENTAGE);

        final int result = Settings.Secure.getInt(mResolver,
                Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0);