2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge;
19 import static android.os.PowerManager.ACTION_POWER_SAVE_MODE_CHANGING;
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;
37 import com.android.settings.R;
38 import com.android.settings.SettingsActivity;
39 import com.android.settings.SettingsPreferenceFragment;
40 import com.android.settings.notification.SettingPref;
41 import com.android.settings.widget.SwitchBar;
43 public class BatterySaverSettings extends SettingsPreferenceFragment
44 implements SwitchBar.OnSwitchChangeListener {
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;
50 private final Handler mHandler = new Handler();
51 private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
52 private final Receiver mReceiver = new Receiver();
54 private Context mContext;
55 private boolean mCreated;
56 private SettingPref mTriggerPref;
57 private SwitchBar mSwitchBar;
58 private Switch mSwitch;
59 private boolean mValidListener;
60 private PowerManager mPowerManager;
63 public void onActivityCreated(Bundle savedInstanceState) {
64 super.onActivityCreated(savedInstanceState);
67 addPreferencesFromResource(R.xml.battery_saver_settings);
69 mContext = getActivity();
70 mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
71 mSwitch = mSwitchBar.getSwitch();
74 mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
75 Global.LOW_POWER_MODE_TRIGGER_LEVEL,
77 getResources().getIntArray(R.array.battery_saver_trigger_values)) {
79 protected String getCaption(Resources res, int value) {
80 if (value > 0 && value < 100) {
81 return res.getString(R.string.battery_saver_turn_on_automatically_pct, value);
83 return res.getString(R.string.battery_saver_turn_on_automatically_never);
86 mTriggerPref.init(this);
88 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
92 public void onDestroyView() {
93 super.onDestroyView();
98 public void onResume() {
100 mSettingsObserver.setListening(true);
101 mReceiver.setListening(true);
102 if (!mValidListener) {
103 mSwitchBar.addOnSwitchChangeListener(this);
104 mValidListener = true;
110 public void onPause() {
112 mSettingsObserver.setListening(false);
113 mReceiver.setListening(false);
114 if (mValidListener) {
115 mSwitchBar.removeOnSwitchChangeListener(this);
116 mValidListener = false;
121 public void onSwitchChanged(Switch switchView, boolean isChecked) {
122 mHandler.removeCallbacks(mStartMode);
124 mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
126 if (DEBUG) Log.d(TAG, "Stopping low power mode from settings");
127 trySetPowerSaveMode(false);
131 private void trySetPowerSaveMode(boolean mode) {
132 if (!mPowerManager.setPowerSaveMode(mode)) {
133 if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value");
134 mHandler.post(mUpdateSwitch);
138 private void updateSwitch() {
139 final boolean mode = mPowerManager.isPowerSaveMode();
140 if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode);
141 if (mode == mSwitch.isChecked()) return;
143 // set listener to null so that that code below doesn't trigger onCheckedChanged()
144 if (mValidListener) {
145 mSwitchBar.removeOnSwitchChangeListener(this);
147 mSwitch.setChecked(mode);
148 if (mValidListener) {
149 mSwitchBar.addOnSwitchChangeListener(this);
153 private final Runnable mUpdateSwitch = new Runnable() {
160 private final Runnable mStartMode = new Runnable() {
163 AsyncTask.execute(new Runnable() {
166 if (DEBUG) Log.d(TAG, "Starting low power mode from settings");
167 trySetPowerSaveMode(true);
173 private final class Receiver extends BroadcastReceiver {
174 private boolean mRegistered;
177 public void onReceive(Context context, Intent intent) {
178 if (DEBUG) Log.d(TAG, "Received " + intent.getAction());
179 mHandler.post(mUpdateSwitch);
182 public void setListening(boolean listening) {
183 if (listening && !mRegistered) {
184 mContext.registerReceiver(this, new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGING));
186 } else if (!listening && mRegistered) {
187 mContext.unregisterReceiver(this);
193 private final class SettingsObserver extends ContentObserver {
194 private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
195 = Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
197 public SettingsObserver(Handler handler) {
202 public void onChange(boolean selfChange, Uri uri) {
203 if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
204 mTriggerPref.update(mContext);
208 public void setListening(boolean listening) {
209 final ContentResolver cr = getContentResolver();
211 cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
213 cr.unregisterContentObserver(this);