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 static android.os.PowerManager.ACTION_POWER_SAVE_MODE_CHANGING; 20 21 import android.content.BroadcastReceiver; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.content.res.Resources; 27 import android.database.ContentObserver; 28 import android.net.Uri; 29 import android.os.AsyncTask; 30 import android.os.Bundle; 31 import android.os.Handler; 32 import android.os.PowerManager; 33 import android.provider.Settings.Global; 34 import android.util.Log; 35 import android.widget.Switch; 36 37 import com.android.internal.logging.MetricsLogger; 38 import com.android.settings.R; 39 import com.android.settings.SettingsActivity; 40 import com.android.settings.SettingsPreferenceFragment; 41 import com.android.settings.Utils; 42 import com.android.settings.notification.SettingPref; 43 import com.android.settings.widget.SwitchBar; 44 45 public class BatterySaverSettings extends SettingsPreferenceFragment 46 implements SwitchBar.OnSwitchChangeListener { 47 private static final String TAG = "BatterySaverSettings"; 48 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 49 private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically"; 50 private static final long WAIT_FOR_SWITCH_ANIM = 500; 51 52 private final Handler mHandler = new Handler(); 53 private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler); 54 private final Receiver mReceiver = new Receiver(); 55 56 private Context mContext; 57 private boolean mCreated; 58 private SettingPref mTriggerPref; 59 private SwitchBar mSwitchBar; 60 private Switch mSwitch; 61 private boolean mValidListener; 62 private PowerManager mPowerManager; 63 64 @Override getMetricsCategory()65 protected int getMetricsCategory() { 66 return MetricsLogger.FUELGAUGE_BATTERY_SAVER; 67 } 68 69 @Override onActivityCreated(Bundle savedInstanceState)70 public void onActivityCreated(Bundle savedInstanceState) { 71 super.onActivityCreated(savedInstanceState); 72 if (mCreated) { 73 mSwitchBar.show(); 74 return; 75 } 76 mCreated = true; 77 addPreferencesFromResource(R.xml.battery_saver_settings); 78 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 } 101 102 @Override onDestroyView()103 public void onDestroyView() { 104 super.onDestroyView(); 105 mSwitchBar.hide(); 106 } 107 108 @Override onResume()109 public void onResume() { 110 super.onResume(); 111 mSettingsObserver.setListening(true); 112 mReceiver.setListening(true); 113 if (!mValidListener) { 114 mSwitchBar.addOnSwitchChangeListener(this); 115 mValidListener = true; 116 } 117 updateSwitch(); 118 } 119 120 @Override onPause()121 public void onPause() { 122 super.onPause(); 123 mSettingsObserver.setListening(false); 124 mReceiver.setListening(false); 125 if (mValidListener) { 126 mSwitchBar.removeOnSwitchChangeListener(this); 127 mValidListener = false; 128 } 129 } 130 131 @Override onSwitchChanged(Switch switchView, boolean isChecked)132 public void onSwitchChanged(Switch switchView, boolean isChecked) { 133 mHandler.removeCallbacks(mStartMode); 134 if (isChecked) { 135 mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM); 136 } else { 137 if (DEBUG) Log.d(TAG, "Stopping low power mode from settings"); 138 trySetPowerSaveMode(false); 139 } 140 } 141 trySetPowerSaveMode(boolean mode)142 private void trySetPowerSaveMode(boolean mode) { 143 if (!mPowerManager.setPowerSaveMode(mode)) { 144 if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value"); 145 mHandler.post(mUpdateSwitch); 146 } 147 } 148 updateSwitch()149 private void updateSwitch() { 150 final boolean mode = mPowerManager.isPowerSaveMode(); 151 if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode); 152 if (mode == mSwitch.isChecked()) return; 153 154 // set listener to null so that that code below doesn't trigger onCheckedChanged() 155 if (mValidListener) { 156 mSwitchBar.removeOnSwitchChangeListener(this); 157 } 158 mSwitch.setChecked(mode); 159 if (mValidListener) { 160 mSwitchBar.addOnSwitchChangeListener(this); 161 } 162 } 163 164 private final Runnable mUpdateSwitch = new Runnable() { 165 @Override 166 public void run() { 167 updateSwitch(); 168 } 169 }; 170 171 private final Runnable mStartMode = new Runnable() { 172 @Override 173 public void run() { 174 AsyncTask.execute(new Runnable() { 175 @Override 176 public void run() { 177 if (DEBUG) Log.d(TAG, "Starting low power mode from settings"); 178 trySetPowerSaveMode(true); 179 } 180 }); 181 } 182 }; 183 184 private final class Receiver extends BroadcastReceiver { 185 private boolean mRegistered; 186 187 @Override onReceive(Context context, Intent intent)188 public void onReceive(Context context, Intent intent) { 189 if (DEBUG) Log.d(TAG, "Received " + intent.getAction()); 190 mHandler.post(mUpdateSwitch); 191 } 192 setListening(boolean listening)193 public void setListening(boolean listening) { 194 if (listening && !mRegistered) { 195 mContext.registerReceiver(this, new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGING)); 196 mRegistered = true; 197 } else if (!listening && mRegistered) { 198 mContext.unregisterReceiver(this); 199 mRegistered = false; 200 } 201 } 202 } 203 204 private final class SettingsObserver extends ContentObserver { 205 private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI 206 = Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL); 207 SettingsObserver(Handler handler)208 public SettingsObserver(Handler handler) { 209 super(handler); 210 } 211 212 @Override onChange(boolean selfChange, Uri uri)213 public void onChange(boolean selfChange, Uri uri) { 214 if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) { 215 mTriggerPref.update(mContext); 216 } 217 } 218 setListening(boolean listening)219 public void setListening(boolean listening) { 220 final ContentResolver cr = getContentResolver(); 221 if (listening) { 222 cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this); 223 } else { 224 cr.unregisterContentObserver(this); 225 } 226 } 227 } 228 } 229