Home | History | Annotate | Download | only in fuelgauge
      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.settings.R;
     38 import com.android.settings.SettingsActivity;
     39 import com.android.settings.SettingsPreferenceFragment;
     40 import com.android.settings.Utils;
     41 import com.android.settings.notification.SettingPref;
     42 import com.android.settings.widget.SwitchBar;
     43 
     44 public class BatterySaverSettings extends SettingsPreferenceFragment
     45         implements SwitchBar.OnSwitchChangeListener {
     46     private static final String TAG = "BatterySaverSettings";
     47     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
     48     private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically";
     49     private static final long WAIT_FOR_SWITCH_ANIM = 500;
     50 
     51     private final Handler mHandler = new Handler();
     52     private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
     53     private final Receiver mReceiver = new Receiver();
     54 
     55     private Context mContext;
     56     private boolean mCreated;
     57     private SettingPref mTriggerPref;
     58     private SwitchBar mSwitchBar;
     59     private Switch mSwitch;
     60     private boolean mValidListener;
     61     private PowerManager mPowerManager;
     62 
     63     @Override
     64     public void onActivityCreated(Bundle savedInstanceState) {
     65         super.onActivityCreated(savedInstanceState);
     66         if (mCreated) {
     67             mSwitchBar.show();
     68             return;
     69         }
     70         mCreated = true;
     71         addPreferencesFromResource(R.xml.battery_saver_settings);
     72 
     73         mContext = getActivity();
     74         mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
     75         mSwitch = mSwitchBar.getSwitch();
     76         mSwitchBar.show();
     77 
     78         mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
     79                 Global.LOW_POWER_MODE_TRIGGER_LEVEL,
     80                 0, /*default*/
     81                 getResources().getIntArray(R.array.battery_saver_trigger_values)) {
     82             @Override
     83             protected String getCaption(Resources res, int value) {
     84                 if (value > 0 && value < 100) {
     85                     return res.getString(R.string.battery_saver_turn_on_automatically_pct,
     86                                          Utils.formatPercentage(value));
     87                 }
     88                 return res.getString(R.string.battery_saver_turn_on_automatically_never);
     89             }
     90         };
     91         mTriggerPref.init(this);
     92 
     93         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
     94     }
     95 
     96     @Override
     97     public void onDestroyView() {
     98         super.onDestroyView();
     99         mSwitchBar.hide();
    100     }
    101 
    102     @Override
    103     public void onResume() {
    104         super.onResume();
    105         mSettingsObserver.setListening(true);
    106         mReceiver.setListening(true);
    107         if (!mValidListener) {
    108             mSwitchBar.addOnSwitchChangeListener(this);
    109             mValidListener = true;
    110         }
    111         updateSwitch();
    112     }
    113 
    114     @Override
    115     public void onPause() {
    116         super.onPause();
    117         mSettingsObserver.setListening(false);
    118         mReceiver.setListening(false);
    119         if (mValidListener) {
    120             mSwitchBar.removeOnSwitchChangeListener(this);
    121             mValidListener = false;
    122         }
    123     }
    124 
    125     @Override
    126     public void onSwitchChanged(Switch switchView, boolean isChecked) {
    127         mHandler.removeCallbacks(mStartMode);
    128         if (isChecked) {
    129             mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
    130         } else {
    131             if (DEBUG) Log.d(TAG, "Stopping low power mode from settings");
    132             trySetPowerSaveMode(false);
    133         }
    134     }
    135 
    136     private void trySetPowerSaveMode(boolean mode) {
    137         if (!mPowerManager.setPowerSaveMode(mode)) {
    138             if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value");
    139             mHandler.post(mUpdateSwitch);
    140         }
    141     }
    142 
    143     private void updateSwitch() {
    144         final boolean mode = mPowerManager.isPowerSaveMode();
    145         if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode);
    146         if (mode == mSwitch.isChecked()) return;
    147 
    148         // set listener to null so that that code below doesn't trigger onCheckedChanged()
    149         if (mValidListener) {
    150             mSwitchBar.removeOnSwitchChangeListener(this);
    151         }
    152         mSwitch.setChecked(mode);
    153         if (mValidListener) {
    154             mSwitchBar.addOnSwitchChangeListener(this);
    155         }
    156     }
    157 
    158     private final Runnable mUpdateSwitch = new Runnable() {
    159         @Override
    160         public void run() {
    161             updateSwitch();
    162         }
    163     };
    164 
    165     private final Runnable mStartMode = new Runnable() {
    166         @Override
    167         public void run() {
    168             AsyncTask.execute(new Runnable() {
    169                 @Override
    170                 public void run() {
    171                     if (DEBUG) Log.d(TAG, "Starting low power mode from settings");
    172                     trySetPowerSaveMode(true);
    173                 }
    174             });
    175         }
    176     };
    177 
    178     private final class Receiver extends BroadcastReceiver {
    179         private boolean mRegistered;
    180 
    181         @Override
    182         public void onReceive(Context context, Intent intent) {
    183             if (DEBUG) Log.d(TAG, "Received " + intent.getAction());
    184             mHandler.post(mUpdateSwitch);
    185         }
    186 
    187         public void setListening(boolean listening) {
    188             if (listening && !mRegistered) {
    189                 mContext.registerReceiver(this, new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGING));
    190                 mRegistered = true;
    191             } else if (!listening && mRegistered) {
    192                 mContext.unregisterReceiver(this);
    193                 mRegistered = false;
    194             }
    195         }
    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 
    202         public SettingsObserver(Handler handler) {
    203             super(handler);
    204         }
    205 
    206         @Override
    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 
    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