1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.database.ContentObserver; 23 import android.net.Uri; 24 import android.os.AsyncTask; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.os.PowerManager; 28 import android.provider.Settings.Global; 29 import android.support.annotation.VisibleForTesting; 30 import android.util.Log; 31 import android.widget.Switch; 32 33 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 34 import com.android.settings.R; 35 import com.android.settings.SettingsActivity; 36 import com.android.settings.SettingsPreferenceFragment; 37 import com.android.settings.Utils; 38 import com.android.settings.dashboard.conditional.BatterySaverCondition; 39 import com.android.settings.dashboard.conditional.ConditionManager; 40 import com.android.settings.notification.SettingPref; 41 import com.android.settings.widget.SwitchBar; 42 43 public class BatterySaverSettings extends SettingsPreferenceFragment 44 implements SwitchBar.OnSwitchChangeListener, BatterySaverReceiver.BatterySaverListener { 45 private static final String TAG = "BatterySaverSettings"; 46 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 47 private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically"; 48 private static final long WAIT_FOR_SWITCH_ANIM = 500; 49 50 private final Handler mHandler = new Handler(); 51 private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler); 52 53 @VisibleForTesting 54 SwitchBar mSwitchBar; 55 private Context mContext; 56 private boolean mCreated; 57 private SettingPref mTriggerPref; 58 private Switch mSwitch; 59 private boolean mValidListener; 60 private PowerManager mPowerManager; 61 private BatterySaverReceiver mReceiver; 62 63 @Override getMetricsCategory()64 public int getMetricsCategory() { 65 return MetricsEvent.FUELGAUGE_BATTERY_SAVER; 66 } 67 68 @Override onActivityCreated(Bundle savedInstanceState)69 public void onActivityCreated(Bundle savedInstanceState) { 70 super.onActivityCreated(savedInstanceState); 71 if (mCreated) { 72 mSwitchBar.show(); 73 return; 74 } 75 mCreated = true; 76 addPreferencesFromResource(R.xml.battery_saver_settings); 77 mFooterPreferenceMixin.createFooterPreference() 78 .setTitle(com.android.internal.R.string.battery_saver_description); 79 mContext = getActivity(); 80 mSwitchBar = ((SettingsActivity) mContext).getSwitchBar(); 81 mSwitch = mSwitchBar.getSwitch(); 82 mSwitchBar.show(); 83 84 mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY, 85 Global.LOW_POWER_MODE_TRIGGER_LEVEL, 86 0, /*default*/ 87 getResources().getIntArray(R.array.battery_saver_trigger_values)) { 88 @Override 89 protected String getCaption(Resources res, int value) { 90 if (value > 0 && value < 100) { 91 return res.getString(R.string.battery_saver_turn_on_automatically_pct, 92 Utils.formatPercentage(value)); 93 } 94 return res.getString(R.string.battery_saver_turn_on_automatically_never); 95 } 96 }; 97 mTriggerPref.init(this); 98 99 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); 100 mReceiver = new BatterySaverReceiver(mContext); 101 mReceiver.setBatterySaverListener(this); 102 } 103 104 @Override onDestroyView()105 public void onDestroyView() { 106 super.onDestroyView(); 107 mSwitchBar.hide(); 108 } 109 110 @Override onResume()111 public void onResume() { 112 super.onResume(); 113 mSettingsObserver.setListening(true); 114 mReceiver.setListening(true); 115 if (!mValidListener) { 116 mSwitchBar.addOnSwitchChangeListener(this); 117 mValidListener = true; 118 } 119 updateSwitch(); 120 } 121 122 @Override onPause()123 public void onPause() { 124 super.onPause(); 125 mSettingsObserver.setListening(false); 126 mReceiver.setListening(false); 127 if (mValidListener) { 128 mSwitchBar.removeOnSwitchChangeListener(this); 129 mValidListener = false; 130 } 131 } 132 133 @Override onSwitchChanged(Switch switchView, boolean isChecked)134 public void onSwitchChanged(Switch switchView, boolean isChecked) { 135 mHandler.removeCallbacks(mStartMode); 136 if (isChecked) { 137 mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM); 138 } else { 139 if (DEBUG) Log.d(TAG, "Stopping low power mode from settings"); 140 trySetPowerSaveMode(false); 141 } 142 } 143 trySetPowerSaveMode(boolean mode)144 private void trySetPowerSaveMode(boolean mode) { 145 if (!mPowerManager.setPowerSaveMode(mode)) { 146 if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value"); 147 mHandler.post(mUpdateSwitch); 148 } 149 // TODO: Remove once broadcast is in place. 150 ConditionManager.get(getContext()).getCondition(BatterySaverCondition.class).refreshState(); 151 } 152 updateSwitch()153 private void updateSwitch() { 154 final boolean mode = mPowerManager.isPowerSaveMode(); 155 if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode); 156 if (mode == mSwitch.isChecked()) return; 157 158 // set listener to null so that that code below doesn't trigger onCheckedChanged() 159 if (mValidListener) { 160 mSwitchBar.removeOnSwitchChangeListener(this); 161 } 162 mSwitch.setChecked(mode); 163 if (mValidListener) { 164 mSwitchBar.addOnSwitchChangeListener(this); 165 } 166 } 167 168 private final Runnable mUpdateSwitch = new Runnable() { 169 @Override 170 public void run() { 171 updateSwitch(); 172 } 173 }; 174 175 private final Runnable mStartMode = new Runnable() { 176 @Override 177 public void run() { 178 AsyncTask.execute(new Runnable() { 179 @Override 180 public void run() { 181 if (DEBUG) Log.d(TAG, "Starting low power mode from settings"); 182 trySetPowerSaveMode(true); 183 } 184 }); 185 } 186 }; 187 188 @Override onPowerSaveModeChanged()189 public void onPowerSaveModeChanged() { 190 mHandler.post(mUpdateSwitch); 191 } 192 193 @Override onBatteryChanged(boolean pluggedIn)194 public void onBatteryChanged(boolean pluggedIn) { 195 mSwitchBar.setEnabled(!pluggedIn); 196 } 197 198 private final class SettingsObserver extends ContentObserver { 199 private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI 200 = Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL); 201 SettingsObserver(Handler handler)202 public SettingsObserver(Handler handler) { 203 super(handler); 204 } 205 206 @Override onChange(boolean selfChange, Uri uri)207 public void onChange(boolean selfChange, Uri uri) { 208 if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) { 209 mTriggerPref.update(mContext); 210 } 211 } 212 setListening(boolean listening)213 public void setListening(boolean listening) { 214 final ContentResolver cr = getContentResolver(); 215 if (listening) { 216 cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this); 217 } else { 218 cr.unregisterContentObserver(this); 219 } 220 } 221 } 222 } 223