Merge "Add Battery Saver Schedule screen logic"

This commit is contained in:
TreeHugger Robot
2018-12-18 00:41:43 +00:00
committed by Android (Google) Code Review
15 changed files with 386 additions and 502 deletions

View File

@@ -26,18 +26,6 @@
android:title="@string/battery_saver_schedule_settings_title" android:title="@string/battery_saver_schedule_settings_title"
settings:controller="com.android.settings.fuelgauge.batterysaver.BatterySaverSchedulePreferenceController"/> settings:controller="com.android.settings.fuelgauge.batterysaver.BatterySaverSchedulePreferenceController"/>
<!-- Turn on automatically -->
<SwitchPreference
android:key="auto_battery_saver"
android:title="@string/battery_saver_auto_title"
settings:controller="com.android.settings.fuelgauge.batterysaver.AutoBatterySaverPreferenceController" />
<com.android.settings.widget.SeekBarPreference
android:key="battery_saver_seek_bar"
android:title="@string/battery_saver_seekbar_title_placeholder"
android:max="15"
android:min="1" />
<com.android.settings.widget.TwoStateButtonPreference <com.android.settings.widget.TwoStateButtonPreference
android:key="battery_saver" android:key="battery_saver"
android:title="@string/battery_saver" android:title="@string/battery_saver"

View File

@@ -15,6 +15,7 @@
*/ */
package com.android.settings.fuelgauge; package com.android.settings.fuelgauge;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Handler; import android.os.Handler;
@@ -22,6 +23,7 @@ import android.os.Looper;
import android.os.PowerManager; import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Global;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -31,6 +33,7 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.fuelgauge.BatterySaverUtils;
public class BatterySaverController extends BasePreferenceController public class BatterySaverController extends BasePreferenceController
implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener { implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener {
@@ -45,6 +48,7 @@ public class BatterySaverController extends BasePreferenceController
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mBatteryStateChangeReceiver = new BatterySaverReceiver(context); mBatteryStateChangeReceiver = new BatterySaverReceiver(context);
mBatteryStateChangeReceiver.setBatterySaverListener(this); mBatteryStateChangeReceiver.setBatterySaverListener(this);
BatterySaverUtils.revertScheduleToNoneIfNeeded(context);
} }
@Override @Override
@@ -81,17 +85,24 @@ public class BatterySaverController extends BasePreferenceController
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
final ContentResolver resolver = mContext.getContentResolver();
final boolean isPowerSaveOn = mPowerManager.isPowerSaveMode(); final boolean isPowerSaveOn = mPowerManager.isPowerSaveMode();
final int percent = Settings.Global.getInt(mContext.getContentResolver(), final int percent = Settings.Global.getInt(resolver,
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
final int mode = Settings.Global.getInt(resolver,
Global.AUTOMATIC_POWER_SAVER_MODE, PowerManager.POWER_SAVER_MODE_PERCENTAGE);
if (isPowerSaveOn) { if (isPowerSaveOn) {
return mContext.getString(R.string.battery_saver_on_summary); return mContext.getString(R.string.battery_saver_on_summary);
} else if (percent != 0) { } else if (mode == PowerManager.POWER_SAVER_MODE_PERCENTAGE) {
if (percent != 0) {
return mContext.getString(R.string.battery_saver_off_scheduled_summary, return mContext.getString(R.string.battery_saver_off_scheduled_summary,
Utils.formatPercentage(percent)); Utils.formatPercentage(percent));
} else { } else {
return mContext.getString(R.string.battery_saver_off_summary); return mContext.getString(R.string.battery_saver_off_summary);
} }
} else {
return mContext.getString(R.string.battery_saver_auto_routine);
}
} }
private void updateSummary() { private void updateSummary() {

View File

@@ -1,70 +0,0 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterysaver;
import android.content.Context;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.fuelgauge.BatterySaverUtils;
/**
* Controller that update whether to turn on battery saver automatically
*/
public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener {
/**
* Default value for {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL}.
*/
static final int DEFAULT_TRIGGER_LEVEL = 0;
/**
* The default value to set to {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL} when the
* user enables battery saver.
*/
private final int mDefaultTriggerLevelForOn;
@VisibleForTesting
static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
public AutoBatterySaverPreferenceController(Context context) {
super(context, KEY_AUTO_BATTERY_SAVER);
mDefaultTriggerLevelForOn = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isChecked() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, DEFAULT_TRIGGER_LEVEL) != 0;
}
@Override
public boolean setChecked(boolean isChecked) {
BatterySaverUtils.setAutoBatterySaverTriggerLevel(mContext,
isChecked ? mDefaultTriggerLevelForOn : 0);
return true;
}
}

View File

@@ -1,166 +0,0 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterysaver;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* Controller that update the battery saver seekbar
*/
public class AutoBatterySeekBarPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop, SeekBarPreference.OnPreferenceChangeListener {
private static final String TAG = "AutoBatterySeekBarPreferenceController";
private static final int INTERVAL = 5;
@VisibleForTesting
static final String KEY_AUTO_BATTERY_SEEK_BAR = "battery_saver_seek_bar";
private SeekBarPreference mPreference;
private AutoBatterySaverSettingObserver mContentObserver;
public AutoBatterySeekBarPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY_AUTO_BATTERY_SEEK_BAR);
mContentObserver = new AutoBatterySaverSettingObserver(new Handler(Looper.getMainLooper()));
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = (SeekBarPreference) screen.findPreference(
KEY_AUTO_BATTERY_SEEK_BAR);
mPreference.setContinuousUpdates(true);
mPreference.setAccessibilityRangeInfoType(
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_PERCENT);
updatePreference(mPreference);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
updatePreference(preference);
}
@Override
public void onStart() {
mContentObserver.registerContentObserver();
}
@Override
public void onStop() {
mContentObserver.unRegisterContentObserver();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final int progress = (int) newValue;
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, progress * INTERVAL);
return true;
}
@VisibleForTesting
void updatePreference(Preference preference) {
final ContentResolver contentResolver = mContext.getContentResolver();
// Override the max value with LOW_POWER_MODE_TRIGGER_LEVEL_MAX, if set.
final int maxLevel = Settings.Global.getInt(contentResolver,
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, 0) / INTERVAL;
if (maxLevel > 0) {
if (!(preference instanceof SeekBarPreference)) {
Log.e(TAG, "Unexpected preference class: " + preference.getClass());
} else {
final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
if (maxLevel < seekBarPreference.getMin()) {
Log.e(TAG, "LOW_POWER_MODE_TRIGGER_LEVEL_MAX too low; ignored.");
} else {
seekBarPreference.setMax(maxLevel);
}
}
}
// Set the current value.
final int level = Settings.Global.getInt(contentResolver,
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL,
AutoBatterySaverPreferenceController.DEFAULT_TRIGGER_LEVEL);
if (level == 0) {
preference.setVisible(false);
} else {
preference.setVisible(true);
preference.setTitle(mContext.getString(R.string.battery_saver_seekbar_title,
Utils.formatPercentage(level)));
SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
seekBarPreference.setProgress(level / INTERVAL);
seekBarPreference.setSeekBarContentDescription(
mContext.getString(R.string.battery_saver_turn_on_automatically_title));
}
}
/**
* Observer that listens to change from {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL}
*/
private final class AutoBatterySaverSettingObserver extends ContentObserver {
private final Uri mUri = Settings.Global.getUriFor(
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL);
private final ContentResolver mContentResolver;
public AutoBatterySaverSettingObserver(Handler handler) {
super(handler);
mContentResolver = mContext.getContentResolver();
}
public void registerContentObserver() {
mContentResolver.registerContentObserver(mUri, false, this);
}
public void unRegisterContentObserver() {
mContentResolver.unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange, Uri uri, int userId) {
if (mUri.equals(uri)) {
updatePreference(mPreference);
}
}
}
}

View File

@@ -26,7 +26,13 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.fuelgauge.BatterySaverUtils;
/**
* Simple controller to navigate users to the scheduling page from
* "Settings > Battery > Battery Saver". Also updates the summary for preference based on
* the currently selected settings.
*/
public class BatterySaverSchedulePreferenceController extends BasePreferenceController { public class BatterySaverSchedulePreferenceController extends BasePreferenceController {
@VisibleForTesting @VisibleForTesting
@@ -36,6 +42,7 @@ public class BatterySaverSchedulePreferenceController extends BasePreferenceCont
public BatterySaverSchedulePreferenceController(Context context) { public BatterySaverSchedulePreferenceController(Context context) {
super(context, KEY_BATTERY_SAVER_SCHEDULE); super(context, KEY_BATTERY_SAVER_SCHEDULE);
BatterySaverUtils.revertScheduleToNoneIfNeeded(context);
} }
@Override @Override

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterysaver;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
/**
* Responds to user actions in the Settings > Battery > Set a Schedule Screen
*
* Note that this is not a preference controller since that screen does not inherit from
* DashboardFragment.
*
* Will call the appropriate power manager APIs and modify the correct settings to enable
* users to control their automatic battery saver toggling preferences.
* See {@link Settings.Global#AUTOMATIC_POWER_SAVER_MODE} for more details.
*/
public class BatterySaverScheduleRadioButtonsController {
public static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule";
public static final String KEY_ROUTINE = "key_battery_saver_routine";
public static final String KEY_PERCENTAGE = "key_battery_saver_percentage";
private Context mContext;
private BatterySaverScheduleSeekBarController mSeekBarController;
public BatterySaverScheduleRadioButtonsController(Context context,
BatterySaverScheduleSeekBarController seekbar) {
mContext = context;
mSeekBarController = seekbar;
}
public String getDefaultKey() {
final ContentResolver resolver = mContext.getContentResolver();
// Note: this can also be obtained via PowerManager.getPowerSaveMode()
final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_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_SAVER_MODE_PERCENTAGE) {
final int threshold =
Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
if (threshold <= 0) {
return KEY_NO_SCHEDULE;
}
return KEY_PERCENTAGE;
}
return KEY_ROUTINE;
}
public boolean setDefaultKey(String key) {
final ContentResolver resolver = mContext.getContentResolver();
switch(key) {
case KEY_NO_SCHEDULE:
Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_PERCENTAGE);
Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
break;
case KEY_PERCENTAGE:
Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_PERCENTAGE);
Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
break;
case KEY_ROUTINE:
Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_DYNAMIC);
break;
default:
throw new IllegalStateException(
"Not a valid key for " + this.getClass().getSimpleName());
}
mSeekBarController.updateSeekBar();
return true;
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterysaver;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.widget.SeekBarPreference;
/**
* Responds to user actions in the Settings > Battery > Set a Schedule Screen for the seekbar.
* Note that this seekbar is only visible when the radio button selected is "Percentage".
*
* Note that this is not a preference controller since that screen does not inherit from
* DashboardFragment.
*
* Will call the appropriate power manager APIs and modify the correct settings to enable
* users to control their automatic battery saver toggling preferences.
* See {@link Settings.Global#AUTOMATIC_POWER_SAVER_MODE} for more details.
*/
public class BatterySaverScheduleSeekBarController implements
OnPreferenceChangeListener {
public static final int MAX_SEEKBAR_VALUE = 15;
public static final int MIN_SEEKBAR_VALUE = 1;
public static final String KEY_BATTERY_SAVER_SEEK_BAR = "battery_saver_seek_bar";
@VisibleForTesting
public SeekBarPreference mSeekBarPreference;
private Context mContext;
public BatterySaverScheduleSeekBarController(Context context) {
mContext = context;
mSeekBarPreference = new SeekBarPreference(context);
mSeekBarPreference.setOnPreferenceChangeListener(this);
mSeekBarPreference.setMax(BatterySaverScheduleSeekBarController.MAX_SEEKBAR_VALUE);
mSeekBarPreference.setMin(BatterySaverScheduleSeekBarController.MIN_SEEKBAR_VALUE);
mSeekBarPreference.setKey(KEY_BATTERY_SAVER_SEEK_BAR);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// The nits are in intervals of 5%
final int percentage = ((Integer) newValue) * 5;
Settings.Global.putInt(mContext.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL,
percentage);
preference.setTitle(mContext.getString(
R.string.battery_saver_seekbar_title, Utils.formatPercentage(percentage)));
return true;
}
public void updateSeekBar() {
final ContentResolver resolver = mContext.getContentResolver();
// Note: this can also be obtained via PowerManager.getPowerSaveMode()
final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_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_SAVER_MODE_PERCENTAGE) {
final int threshold =
Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
if (threshold <= 0) {
mSeekBarPreference.setVisible(false);
} else {
mSeekBarPreference.setVisible(true);
mSeekBarPreference.setProgress(MIN_SEEKBAR_VALUE);
mSeekBarPreference.setTitle(mContext.getString(
R.string.battery_saver_seekbar_title,
Utils.formatPercentage(MIN_SEEKBAR_VALUE * 5)));
}
} else {
mSeekBarPreference.setVisible(false);
}
}
public void addToScreen(PreferenceScreen screen) {
screen.addPreference(mSeekBarPreference);
}
}

View File

@@ -18,48 +18,77 @@ package com.android.settings.fuelgauge.batterysaver;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.widget.RadioButtonPickerFragment; import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.widget.RadioButtonPreference; import com.android.settings.widget.RadioButtonPreference;
import com.android.settings.widget.SeekBarPreference; import com.android.settingslib.fuelgauge.BatterySaverUtils;
import com.android.settingslib.widget.CandidateInfo; import com.android.settingslib.widget.CandidateInfo;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.List; import java.util.List;
/**
* Fragment that allows users to customize their automatic battery saver mode settings.
*
* Location: Settings > Battery > Battery Saver > Set a Schedule
* See {@link BatterySaverSchedulePreferenceController} for the controller that manages navigation
* to this screen from "Settings > Battery > Battery Saver" and the summary.
* See {@link BatterySaverScheduleRadioButtonsController} &
* {@link BatterySaverScheduleSeekBarController} for the controller that manages user
* interactions in this screen.
*/
public class BatterySaverScheduleSettings extends RadioButtonPickerFragment { public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
private static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule"; public BatterySaverScheduleRadioButtonsController mRadioButtonController;
private static final String KEY_ROUTINE = "key_battery_saver_routine"; private BatterySaverScheduleSeekBarController mSeekBarController;
private static final String KEY_PERCENTAGE = "key_battery_saver_percentage";
public static final int MAX_SEEKBAR_VALUE = 15;
public static final int MIN_SEEKBAR_VALUE = 1;
public static final String KEY_BATTERY_SAVER_SEEK_BAR = "battery_saver_seek_bar";
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
return R.xml.battery_saver_schedule_settings; return R.xml.battery_saver_schedule_settings;
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
mSeekBarController = new BatterySaverScheduleSeekBarController(context);
mRadioButtonController = new BatterySaverScheduleRadioButtonsController(
context, mSeekBarController);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override @Override
protected List<? extends CandidateInfo> getCandidates() { protected List<? extends CandidateInfo> getCandidates() {
Context context = getContext(); Context context = getContext();
List<CandidateInfo> candidates = Lists.newArrayList(); List<CandidateInfo> candidates = Lists.newArrayList();
String routineProviderApp = getContext().getResources()
.getString(com.android.internal.R.string.config_batterySaverScheduleProvider);
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,
KEY_NO_SCHEDULE, BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE,
/* enabled */ true)); /* enabled */ true));
// only add routine option if an app has been specified
if (!TextUtils.isEmpty(routineProviderApp)) {
candidates.add(new BatterySaverScheduleCandidateInfo( candidates.add(new BatterySaverScheduleCandidateInfo(
context.getText(R.string.battery_saver_auto_routine), context.getText(R.string.battery_saver_auto_routine),
context.getText(R.string.battery_saver_auto_routine_summary), context.getText(R.string.battery_saver_auto_routine_summary),
KEY_ROUTINE, BatterySaverScheduleRadioButtonsController.KEY_ROUTINE,
/* enabled */ true)); /* enabled */ true));
} else {
// Make sure routine is not selected if no provider app is configured
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,
KEY_PERCENTAGE, BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE,
/* enabled */ true)); /* enabled */ true));
return candidates; return candidates;
@@ -79,22 +108,18 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment {
@Override @Override
protected void addStaticPreferences(PreferenceScreen screen) { protected void addStaticPreferences(PreferenceScreen screen) {
SeekBarPreference seekbar = new SeekBarPreference(getContext()); mSeekBarController.updateSeekBar();
seekbar.setMax(MAX_SEEKBAR_VALUE); mSeekBarController.addToScreen(screen);
seekbar.setMin(MIN_SEEKBAR_VALUE);
seekbar.setTitle(R.string.battery_saver_seekbar_title_placeholder);
seekbar.setKey(KEY_BATTERY_SAVER_SEEK_BAR);
screen.addPreference(seekbar);
} }
@Override @Override
protected String getDefaultKey() { protected String getDefaultKey() {
return null; return mRadioButtonController.getDefaultKey();
} }
@Override @Override
protected boolean setDefaultKey(String key) { protected boolean setDefaultKey(String key) {
return false; return mRadioButtonController.setDefaultKey(key);
} }
@Override @Override

View File

@@ -60,24 +60,11 @@ public class BatterySaverSettings extends DashboardFragment {
return TAG; return TAG;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle());
}
@Override @Override
public int getHelpResource() { public int getHelpResource() {
return R.string.help_url_battery_saver_settings; return R.string.help_url_battery_saver_settings;
} }
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context, Lifecycle lifecycle) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new AutoBatterySaverPreferenceController(context));
controllers.add(new AutoBatterySeekBarPreferenceController(context, lifecycle));
return controllers;
}
/** /**
* For Search. * For Search.
*/ */
@@ -90,11 +77,5 @@ public class BatterySaverSettings extends DashboardFragment {
sir.xmlResId = R.xml.battery_saver_settings; sir.xmlResId = R.xml.battery_saver_settings;
return Arrays.asList(sir); return Arrays.asList(sir);
} }
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context, null);
}
}; };
} }

View File

@@ -7,7 +7,6 @@ com.android.settings.bluetooth.BluetoothDeviceNamePreferenceController
com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController
com.android.settings.datausage.DataUsageSummaryPreferenceController com.android.settings.datausage.DataUsageSummaryPreferenceController
com.android.settings.fuelgauge.RestrictAppPreferenceController com.android.settings.fuelgauge.RestrictAppPreferenceController
com.android.settings.fuelgauge.batterysaver.AutoBatterySeekBarPreferenceController
com.android.settings.fuelgauge.batterysaver.BatterySaverButtonPreferenceController com.android.settings.fuelgauge.batterysaver.BatterySaverButtonPreferenceController
com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController
com.android.settings.security.VisiblePatternProfilePreferenceController com.android.settings.security.VisiblePatternProfilePreferenceController

View File

@@ -71,7 +71,9 @@ public class BasePreferenceControllerSignatureInspector extends CodeInspector {
.that(badClasses.toString()) .that(badClasses.toString())
.isEmpty(); .isEmpty();
assertWithMessage("Something in the grandfather list is no longer relevant. Please remove") assertWithMessage("Something in the grandfather list is no longer relevant. Please remove"
+ "it from packages/apps/Settings/tests/robotests/assets/"
+ "grandfather_invalid_base_preference_controller_constructor")
.that(grandfather) .that(grandfather)
.isEmpty(); .isEmpty();
} }

View File

@@ -1,89 +0,0 @@
/*
* Copyright (C) 2017 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.fuelgauge.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class AutoBatterySaverPreferenceControllerTest {
private AutoBatterySaverPreferenceController mController;
private Context mContext;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new SwitchPreference(mContext);
mController = new AutoBatterySaverPreferenceController(mContext);
}
@Test
public void testUpdateState_lowPowerLevelZero_preferenceNotChecked() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void testUpdateState_lowPowerLevelZero_preferenceChecked() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void testOnPreferenceChange_turnOn_setValueNotZero() {
mController.onPreferenceChange(mPreference, true);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isNotEqualTo(0);
}
@Test
public void testOnPreferenceChange_turnOff_setValueZero() {
mController.onPreferenceChange(mPreference, false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0);
}
@Test
public void testIsChecked_useDefaultValue_returnFalse() {
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -1,117 +0,0 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class AutoBatterySeekBarPreferenceControllerTest {
private static final int TRIGGER_LEVEL = 20;
private static final int INTERVAL = 5;
private AutoBatterySeekBarPreferenceController mController;
private Context mContext;
private SeekBarPreference mPreference;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = RuntimeEnvironment.application;
mPreference = new SeekBarPreference(mContext);
mPreference.setMax(100);
mController = new AutoBatterySeekBarPreferenceController(mContext, mLifecycle);
}
@Test
public void testPreference_lowPowerLevelZero_preferenceInvisible() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void testPreference_defaultValue_preferenceNotVisible() {
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void testPreference_lowPowerLevelNotZero_updatePreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, TRIGGER_LEVEL);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getTitle()).isEqualTo("20%");
assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL / INTERVAL);
}
@Test
public void testOnPreferenceChange_updateValue() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
mController.onPreferenceChange(mPreference, TRIGGER_LEVEL / INTERVAL);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(TRIGGER_LEVEL);
}
@Test
public void testOnPreferenceChange_changeMax() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, 50);
mController.updateState(mPreference);
assertThat(mPreference.getMax()).isEqualTo(50 / INTERVAL);
}
@Test
public void testOnPreferenceChange_noChangeMax() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, 0);
mController.updateState(mPreference);
assertThat(mPreference.getMax()).isEqualTo(100);
}
}

View File

@@ -0,0 +1,57 @@
package com.android.settings.fuelgauge.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class BatterySaverScheduleRadioButtonsControllerTest {
private Context mContext;
private ContentResolver mResolver;
private BatterySaverScheduleRadioButtonsController mController;
private BatterySaverScheduleSeekBarController mSeekBarController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mSeekBarController = new BatterySaverScheduleSeekBarController(mContext);
mController = new BatterySaverScheduleRadioButtonsController(
mContext, mSeekBarController);
mResolver = mContext.getContentResolver();
}
@Test
public void getDefaultKey_routine_returnsCorrectValue() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_DYNAMIC);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_ROUTINE);
}
@Test
public void getDefaultKey_automatic_returnsCorrectValue() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_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_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
assertThat(mController.getDefaultKey())
.isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE);
}
}

View File

@@ -0,0 +1,65 @@
package com.android.settings.fuelgauge.batterysaver;
import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class BatterySaverScheduleSeekBarControllerTest {
private Context mContext;
private ContentResolver mResolver;
private BatterySaverScheduleSeekBarController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new BatterySaverScheduleSeekBarController(mContext);
mResolver = mContext.getContentResolver();
}
@Test
public void onPreferenceChange_updatesSettingsGlobal() {
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
mController.onPreferenceChange(mController.mSeekBarPreference, 10);
assertThat(Settings.Global.getInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1))
.isEqualTo(50);
assertThat(mController.mSeekBarPreference.getTitle()).isEqualTo("50%");
}
@Test
public void updateSeekBar_routineMode_hasCorrectProperties() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_DYNAMIC);
mController.updateSeekBar();
assertThat(mController.mSeekBarPreference.isVisible()).isFalse();
}
@Test
public void updateSeekBar_percentageMode_hasCorrectProperties() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5);
mController.updateSeekBar();
assertThat(mController.mSeekBarPreference.isVisible()).isTrue();
}
@Test
public void updateSeekBar_noneMode_hasCorrectProperties() {
Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE,
PowerManager.POWER_SAVER_MODE_PERCENTAGE);
Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
mController.updateSeekBar();
assertThat(mController.mSeekBarPreference.isVisible()).isFalse();
}
}