Home | History | Annotate | Download | only in fuelgauge
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.settings.fuelgauge;
     16 
     17 import android.content.Context;
     18 import android.database.ContentObserver;
     19 import android.os.Handler;
     20 import android.os.PowerManager;
     21 import android.provider.Settings;
     22 import android.provider.Settings.Global;
     23 import android.support.v7.preference.Preference;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import com.android.settings.R;
     27 import com.android.settings.Utils;
     28 
     29 public class BatterySaverPreference extends Preference {
     30 
     31     private PowerManager mPowerManager;
     32 
     33     public BatterySaverPreference(Context context, AttributeSet attrs) {
     34         super(context, attrs);
     35     }
     36 
     37     @Override
     38     protected void performClick(View view) {
     39         Utils.startWithFragment(getContext(), getFragment(), null, null, 0, 0, getTitle());
     40     }
     41 
     42     @Override
     43     public void onAttached() {
     44         super.onAttached();
     45         mPowerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
     46         mObserver.onChange(true);
     47         getContext().getContentResolver().registerContentObserver(
     48                 Settings.Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL), true, mObserver);
     49         getContext().getContentResolver().registerContentObserver(
     50                 Settings.Global.getUriFor(Global.LOW_POWER_MODE), true, mObserver);
     51     }
     52 
     53     @Override
     54     public void onDetached() {
     55         super.onDetached();
     56         getContext().getContentResolver().unregisterContentObserver(mObserver);
     57     }
     58 
     59     private void updateSwitch() {
     60         final Context context = getContext();
     61         final boolean mode = mPowerManager.isPowerSaveMode();
     62         int format = mode ? R.string.battery_saver_on_summary
     63                 : R.string.battery_saver_off_summary;
     64         int percent = Settings.Global.getInt(context.getContentResolver(),
     65                 Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
     66         int percentFormat = percent > 0 ? R.string.battery_saver_desc_turn_on_auto_pct
     67                 : R.string.battery_saver_desc_turn_on_auto_never;
     68         setSummary(context.getString(format, context.getString(percentFormat,
     69                 Utils.formatPercentage(percent))));
     70     }
     71 
     72     private final ContentObserver mObserver = new ContentObserver(new Handler()) {
     73         @Override
     74         public void onChange(boolean selfChange) {
     75             updateSwitch();
     76         }
     77     };
     78 
     79 }
     80