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.BroadcastReceiver;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.res.Resources;
25 import android.database.ContentObserver;
26 import android.net.Uri;
27 import android.os.AsyncTask;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.PowerManager;
31 import android.provider.Settings.Global;
32 import android.util.Log;
33 import android.widget.Switch;
34 
35 import com.android.internal.logging.MetricsProto.MetricsEvent;
36 import com.android.settings.R;
37 import com.android.settings.SettingsActivity;
38 import com.android.settings.SettingsPreferenceFragment;
39 import com.android.settings.Utils;
40 import com.android.settings.dashboard.conditional.BatterySaverCondition;
41 import com.android.settings.dashboard.conditional.ConditionManager;
42 import com.android.settings.notification.SettingPref;
43 import com.android.settings.widget.SwitchBar;
44 
45 import static android.os.PowerManager.ACTION_POWER_SAVE_MODE_CHANGING;
46 
47 public class BatterySaverSettings extends SettingsPreferenceFragment
48         implements SwitchBar.OnSwitchChangeListener {
49     private static final String TAG = "BatterySaverSettings";
50     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
51     private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically";
52     private static final long WAIT_FOR_SWITCH_ANIM = 500;
53 
54     private final Handler mHandler = new Handler();
55     private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
56     private final Receiver mReceiver = new Receiver();
57 
58     private Context mContext;
59     private boolean mCreated;
60     private SettingPref mTriggerPref;
61     private SwitchBar mSwitchBar;
62     private Switch mSwitch;
63     private boolean mValidListener;
64     private PowerManager mPowerManager;
65 
66     @Override
getMetricsCategory()67     protected int getMetricsCategory() {
68         return MetricsEvent.FUELGAUGE_BATTERY_SAVER;
69     }
70 
71     @Override
onActivityCreated(Bundle savedInstanceState)72     public void onActivityCreated(Bundle savedInstanceState) {
73         super.onActivityCreated(savedInstanceState);
74         if (mCreated) {
75             mSwitchBar.show();
76             return;
77         }
78         mCreated = true;
79         addPreferencesFromResource(R.xml.battery_saver_settings);
80 
81         mContext = getActivity();
82         mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
83         mSwitch = mSwitchBar.getSwitch();
84         mSwitchBar.show();
85 
86         mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
87                 Global.LOW_POWER_MODE_TRIGGER_LEVEL,
88                 0, /*default*/
89                 getResources().getIntArray(R.array.battery_saver_trigger_values)) {
90             @Override
91             protected String getCaption(Resources res, int value) {
92                 if (value > 0 && value < 100) {
93                     return res.getString(R.string.battery_saver_turn_on_automatically_pct,
94                                          Utils.formatPercentage(value));
95                 }
96                 return res.getString(R.string.battery_saver_turn_on_automatically_never);
97             }
98         };
99         mTriggerPref.init(this);
100 
101         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
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     private final class Receiver extends BroadcastReceiver {
189         private boolean mRegistered;
190 
191         @Override
onReceive(Context context, Intent intent)192         public void onReceive(Context context, Intent intent) {
193             if (DEBUG) Log.d(TAG, "Received " + intent.getAction());
194             mHandler.post(mUpdateSwitch);
195         }
196 
setListening(boolean listening)197         public void setListening(boolean listening) {
198             if (listening && !mRegistered) {
199                 mContext.registerReceiver(this, new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGING));
200                 mRegistered = true;
201             } else if (!listening && mRegistered) {
202                 mContext.unregisterReceiver(this);
203                 mRegistered = false;
204             }
205         }
206     }
207 
208     private final class SettingsObserver extends ContentObserver {
209         private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
210                 = Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
211 
SettingsObserver(Handler handler)212         public SettingsObserver(Handler handler) {
213             super(handler);
214         }
215 
216         @Override
onChange(boolean selfChange, Uri uri)217         public void onChange(boolean selfChange, Uri uri) {
218             if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
219                 mTriggerPref.update(mContext);
220             }
221         }
222 
setListening(boolean listening)223         public void setListening(boolean listening) {
224             final ContentResolver cr = getContentResolver();
225             if (listening) {
226                 cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
227             } else {
228                 cr.unregisterContentObserver(this);
229             }
230         }
231     }
232 }
233