Merge "Refactor battery saver schedule method"

This commit is contained in:
Wesley Wang
2022-12-23 06:16:17 +00:00
committed by Android (Google) Code Review
5 changed files with 34 additions and 82 deletions

View File

@@ -15,9 +15,10 @@
*/ */
package com.android.settings.fuelgauge.batterysaver; package com.android.settings.fuelgauge.batterysaver;
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Global; import android.provider.Settings.Global;
@@ -61,20 +62,14 @@ public class BatterySaverSchedulePreferenceController extends BasePreferenceCont
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
final ContentResolver resolver = mContext.getContentResolver(); final ContentResolver resolver = mContext.getContentResolver();
final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE, final String mode = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); if (KEY_PERCENTAGE.equals(mode)) {
if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
final int threshold = final int threshold =
Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 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, return mContext.getString(R.string.battery_saver_auto_percentage_summary,
Utils.formatPercentage(threshold)); Utils.formatPercentage(threshold));
} }
} else { return mContext.getText(R.string.battery_saver_auto_no_schedule);
return mContext.getText(R.string.battery_saver_auto_routine);
}
} }
@Override @Override

View File

@@ -15,6 +15,9 @@
*/ */
package com.android.settings.fuelgauge.batterysaver; 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.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
@@ -22,7 +25,6 @@ import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Global; import android.provider.Settings.Global;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.android.settingslib.fuelgauge.BatterySaverUtils; import com.android.settingslib.fuelgauge.BatterySaverUtils;
@@ -39,8 +41,6 @@ import com.android.settingslib.fuelgauge.BatterySaverUtils;
public class BatterySaverScheduleRadioButtonsController { public class BatterySaverScheduleRadioButtonsController {
private static final String TAG = "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; public static final int TRIGGER_LEVEL_MIN = 10;
private Context mContext; private Context mContext;
@@ -52,21 +52,6 @@ public class BatterySaverScheduleRadioButtonsController {
mSeekBarController = seekbar; 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) { public boolean setDefaultKey(String key) {
if (key == null) { if (key == null) {
return false; return false;

View File

@@ -15,9 +15,10 @@
*/ */
package com.android.settings.fuelgauge.batterysaver; package com.android.settings.fuelgauge.batterysaver;
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Global; import android.provider.Settings.Global;
import android.widget.SeekBar; import android.widget.SeekBar;
@@ -31,6 +32,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.widget.SeekBarPreference; 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. * 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() { public void updateSeekBar() {
final ContentResolver resolver = mContext.getContentResolver(); final ContentResolver resolver = mContext.getContentResolver();
// Note: this can also be obtained via PowerManager.getPowerSaveModeTrigger() final String mode = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE, if (KEY_PERCENTAGE.equals(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 int threshold = final int threshold =
Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 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); final int currentSeekbarValue = Math.max(threshold / 5, MIN_SEEKBAR_VALUE);
mSeekBarPreference.setVisible(true); mSeekBarPreference.setVisible(true);
mSeekBarPreference.setProgress(currentSeekbarValue); mSeekBarPreference.setProgress(currentSeekbarValue);
@@ -117,7 +112,6 @@ public class BatterySaverScheduleSeekBarController implements
currentSeekbarValue * 5); currentSeekbarValue * 5);
mSeekBarPreference.setTitle(stateDescription); mSeekBarPreference.setTitle(stateDescription);
mSeekBarPreference.overrideSeekBarStateDescription(stateDescription); mSeekBarPreference.overrideSeekBarStateDescription(stateDescription);
}
} else { } else {
mSeekBarPreference.setVisible(false); mSeekBarPreference.setVisible(false);
} }

View File

@@ -16,6 +16,9 @@
package com.android.settings.fuelgauge.batterysaver; 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.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
@@ -27,7 +30,6 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@@ -95,7 +97,7 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
Settings.Secure.getUriFor(Settings.Secure.LOW_POWER_WARNING_ACKNOWLEDGED), Settings.Secure.getUriFor(Settings.Secure.LOW_POWER_WARNING_ACKNOWLEDGED),
false, false,
mSettingsObserver); mSettingsObserver);
mSaverScheduleKey = mRadioButtonController.getDefaultKey(); mSaverScheduleKey = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
mSaverPercentage = getSaverPercentage(); mSaverPercentage = getSaverPercentage();
} }
@@ -125,13 +127,13 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
candidates.add(new BatterySaverScheduleCandidateInfo( candidates.add(new BatterySaverScheduleCandidateInfo(
context.getText(R.string.battery_saver_auto_no_schedule), context.getText(R.string.battery_saver_auto_no_schedule),
/* summary */ null, /* summary */ null,
BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE, KEY_NO_SCHEDULE,
/* enabled */ true)); /* enabled */ true));
BatterySaverUtils.revertScheduleToNoneIfNeeded(context); BatterySaverUtils.revertScheduleToNoneIfNeeded(context);
candidates.add(new BatterySaverScheduleCandidateInfo( candidates.add(new BatterySaverScheduleCandidateInfo(
context.getText(R.string.battery_saver_auto_percentage), context.getText(R.string.battery_saver_auto_percentage),
/* summary */ null, /* summary */ null,
BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE, KEY_PERCENTAGE,
/* enabled */ true)); /* enabled */ true));
return candidates; return candidates;
@@ -157,7 +159,7 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
@Override @Override
protected String getDefaultKey() { protected String getDefaultKey() {
return mRadioButtonController.getDefaultKey(); return BatterySaverUtils.getBatterySaverScheduleKey(mContext);
} }
@Override @Override
@@ -172,7 +174,8 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
private void logPowerSaver() { private void logPowerSaver() {
final int currentSaverPercentage = getSaverPercentage(); final int currentSaverPercentage = getSaverPercentage();
final String currentSaverScheduleKey = mRadioButtonController.getDefaultKey(); final String currentSaverScheduleKey = BatterySaverUtils.getBatterySaverScheduleKey(
mContext);
if (mSaverScheduleKey.equals(currentSaverScheduleKey) if (mSaverScheduleKey.equals(currentSaverScheduleKey)
&& mSaverPercentage == currentSaverPercentage) { && mSaverPercentage == currentSaverPercentage) {
return; return;

View File

@@ -15,6 +15,8 @@
*/ */
package com.android.settings.fuelgauge.batterysaver; package com.android.settings.fuelgauge.batterysaver;
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -47,33 +49,6 @@ public class BatterySaverScheduleRadioButtonsControllerTest {
mResolver = mContext.getContentResolver(); 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 @Test
public void setDefaultKey_percentage_shouldSuppressNotification() { public void setDefaultKey_percentage_shouldSuppressNotification() {
Secure.putInt( Secure.putInt(
@@ -81,7 +56,7 @@ public class BatterySaverScheduleRadioButtonsControllerTest {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE, Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVE_MODE,
PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5); 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, final int result = Settings.Secure.getInt(mResolver,
Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0); Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0);