Revamp the battery saver page
1. Make BatterySaverSettings extend from DashboardFragment 2. Add new layout 3. Add battery saver controller Future cl will add controllers for seekbar and button Bug: 72228477 Test: RunSettingsRoboTests Change-Id: I21fb62aef874c04eca2988271f2fd9d7aacb0c6c
This commit is contained in:
@@ -74,7 +74,7 @@ import com.android.settings.display.NightDisplaySettings;
|
||||
import com.android.settings.dream.DreamSettings;
|
||||
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
||||
import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummaryLegacy;
|
||||
import com.android.settings.gestures.AssistGestureSettings;
|
||||
|
@@ -20,7 +20,7 @@ import android.os.PowerManager;
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||
|
||||
public class BatterySaverCondition extends Condition {
|
||||
public BatterySaverCondition(ConditionManager manager) {
|
||||
|
@@ -1,253 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.provider.Settings;
|
||||
import android.provider.Settings.Global;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.util.Log;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.dashboard.conditional.BatterySaverCondition;
|
||||
import com.android.settings.dashboard.conditional.ConditionManager;
|
||||
import com.android.settings.notification.SettingPref;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BatterySaverSettings extends SettingsPreferenceFragment
|
||||
implements SwitchBar.OnSwitchChangeListener, BatterySaverReceiver.BatterySaverListener,
|
||||
Indexable {
|
||||
private static final String TAG = "BatterySaverSettings";
|
||||
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
|
||||
private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically";
|
||||
private static final long WAIT_FOR_SWITCH_ANIM = 500;
|
||||
|
||||
private final Handler mHandler = new Handler();
|
||||
private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
|
||||
|
||||
@VisibleForTesting
|
||||
SwitchBar mSwitchBar;
|
||||
private Context mContext;
|
||||
private boolean mCreated;
|
||||
private SettingPref mTriggerPref;
|
||||
private Switch mSwitch;
|
||||
private boolean mValidListener;
|
||||
private PowerManager mPowerManager;
|
||||
private BatterySaverReceiver mReceiver;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.FUELGAUGE_BATTERY_SAVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
if (mCreated) {
|
||||
mSwitchBar.show();
|
||||
return;
|
||||
}
|
||||
mCreated = true;
|
||||
addPreferencesFromResource(R.xml.battery_saver_settings);
|
||||
mFooterPreferenceMixin.createFooterPreference()
|
||||
.setTitle(com.android.internal.R.string.battery_saver_description);
|
||||
mContext = getActivity();
|
||||
mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
|
||||
mSwitchBar.setSwitchBarText(R.string.battery_saver_master_switch_title,
|
||||
R.string.battery_saver_master_switch_title);
|
||||
mSwitch = mSwitchBar.getSwitch();
|
||||
mSwitchBar.show();
|
||||
|
||||
int[] levelChoices = getResources().getIntArray(R.array.battery_saver_trigger_values);
|
||||
final int currentThreshold = Global.getInt(mContext.getContentResolver(),
|
||||
Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
|
||||
levelChoices = ArrayUtils.appendInt(levelChoices, currentThreshold);
|
||||
Arrays.sort(levelChoices);
|
||||
|
||||
mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
|
||||
Global.LOW_POWER_MODE_TRIGGER_LEVEL,
|
||||
0, /*default*/
|
||||
levelChoices) {
|
||||
@Override
|
||||
protected String getCaption(Resources res, int value) {
|
||||
if (value > 0 && value <= 100) {
|
||||
return res.getString(R.string.battery_saver_turn_on_automatically_pct,
|
||||
Utils.formatPercentage(value));
|
||||
}
|
||||
return res.getString(R.string.battery_saver_turn_on_automatically_never);
|
||||
}
|
||||
};
|
||||
mTriggerPref.init(this);
|
||||
|
||||
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
|
||||
mReceiver = new BatterySaverReceiver(mContext);
|
||||
mReceiver.setBatterySaverListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
mSwitchBar.hide();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mSettingsObserver.setListening(true);
|
||||
mReceiver.setListening(true);
|
||||
if (!mValidListener) {
|
||||
mSwitchBar.addOnSwitchChangeListener(this);
|
||||
mValidListener = true;
|
||||
}
|
||||
updateSwitch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mSettingsObserver.setListening(false);
|
||||
mReceiver.setListening(false);
|
||||
if (mValidListener) {
|
||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||
mValidListener = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||
mHandler.removeCallbacks(mStartMode);
|
||||
if (isChecked) {
|
||||
mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
|
||||
} else {
|
||||
if (DEBUG) Log.d(TAG, "Stopping low power mode from settings");
|
||||
trySetPowerSaveMode(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void trySetPowerSaveMode(boolean mode) {
|
||||
if (!mPowerManager.setPowerSaveMode(mode)) {
|
||||
if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value");
|
||||
mHandler.post(mUpdateSwitch);
|
||||
}
|
||||
// TODO: Remove once broadcast is in place.
|
||||
ConditionManager.get(getContext()).getCondition(BatterySaverCondition.class).refreshState();
|
||||
}
|
||||
|
||||
private void updateSwitch() {
|
||||
final boolean mode = mPowerManager.isPowerSaveMode();
|
||||
if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode);
|
||||
if (mode == mSwitch.isChecked()) return;
|
||||
|
||||
// set listener to null so that that code below doesn't trigger onCheckedChanged()
|
||||
if (mValidListener) {
|
||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||
}
|
||||
mSwitch.setChecked(mode);
|
||||
if (mValidListener) {
|
||||
mSwitchBar.addOnSwitchChangeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
private final Runnable mUpdateSwitch = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateSwitch();
|
||||
}
|
||||
};
|
||||
|
||||
private final Runnable mStartMode = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
AsyncTask.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (DEBUG) Log.d(TAG, "Starting low power mode from settings");
|
||||
trySetPowerSaveMode(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onPowerSaveModeChanged() {
|
||||
mHandler.post(mUpdateSwitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryChanged(boolean pluggedIn) {
|
||||
mSwitchBar.setEnabled(!pluggedIn);
|
||||
}
|
||||
|
||||
private final class SettingsObserver extends ContentObserver {
|
||||
private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
|
||||
= Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
|
||||
|
||||
public SettingsObserver(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
|
||||
mTriggerPref.update(mContext);
|
||||
}
|
||||
}
|
||||
|
||||
public void setListening(boolean listening) {
|
||||
final ContentResolver cr = getContentResolver();
|
||||
if (listening) {
|
||||
cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
|
||||
} else {
|
||||
cr.unregisterContentObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
*/
|
||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(
|
||||
Context context, boolean enabled) {
|
||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.battery_saver_settings;
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
};
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 android.support.annotation.VisibleForTesting;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
|
||||
/**
|
||||
* Controller that update whether to turn on battery saver automatically
|
||||
*/
|
||||
public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
|
||||
Preference.OnPreferenceChangeListener {
|
||||
private static final int LOW_POWER_MODE_TRIGGER_THRESHOLD = 15;
|
||||
|
||||
@VisibleForTesting
|
||||
static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
|
||||
|
||||
public AutoBatterySaverPreferenceController(Context context) {
|
||||
super(context, KEY_AUTO_BATTERY_SAVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL,
|
||||
isChecked
|
||||
? LOW_POWER_MODE_TRIGGER_THRESHOLD
|
||||
: 0);
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.provider.Settings.Global;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Battery saver settings page
|
||||
*/
|
||||
public class BatterySaverSettings extends DashboardFragment {
|
||||
private static final String TAG = "BatterySaverSettings";
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.FUELGAUGE_BATTERY_SAVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.battery_saver_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
return buildPreferenceControllers(context, getLifecycle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHelpResource() {
|
||||
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));
|
||||
return controllers;
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
*/
|
||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(
|
||||
Context context, boolean enabled) {
|
||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.battery_saver_settings;
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
return buildPreferenceControllers(context, null);
|
||||
}
|
||||
};
|
||||
}
|
@@ -49,7 +49,7 @@ import com.android.settings.display.NightDisplaySettings;
|
||||
import com.android.settings.display.ScreenZoomSettings;
|
||||
import com.android.settings.dream.DreamSettings;
|
||||
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.PowerUsageAdvanced;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||
import com.android.settings.fuelgauge.SmartBatterySettings;
|
||||
|
Reference in New Issue
Block a user