1 /* 2 * Copyright (C) 2017 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 package com.android.settings.fuelgauge; 17 18 import android.content.Context; 19 import android.database.ContentObserver; 20 import android.os.Handler; 21 import android.os.Looper; 22 import android.os.PowerManager; 23 import android.provider.Settings; 24 import android.support.annotation.VisibleForTesting; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.Utils; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.dashboard.conditional.BatterySaverCondition; 32 import com.android.settings.dashboard.conditional.ConditionManager; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 36 37 public class BatterySaverController extends BasePreferenceController 38 implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener { 39 private static final String KEY_BATTERY_SAVER = "battery_saver_summary"; 40 private final BatterySaverReceiver mBatteryStateChangeReceiver; 41 private final PowerManager mPowerManager; 42 private Preference mBatterySaverPref; 43 BatterySaverController(Context context)44 public BatterySaverController(Context context) { 45 super(context, KEY_BATTERY_SAVER); 46 47 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); 48 mBatteryStateChangeReceiver = new BatterySaverReceiver(context); 49 mBatteryStateChangeReceiver.setBatterySaverListener(this); 50 } 51 52 @Override getAvailabilityStatus()53 public int getAvailabilityStatus() { 54 return AVAILABLE; 55 } 56 57 @Override getPreferenceKey()58 public String getPreferenceKey() { 59 return KEY_BATTERY_SAVER; 60 } 61 62 @Override displayPreference(PreferenceScreen screen)63 public void displayPreference(PreferenceScreen screen) { 64 super.displayPreference(screen); 65 mBatterySaverPref = screen.findPreference(KEY_BATTERY_SAVER); 66 } 67 68 @Override onStart()69 public void onStart() { 70 mContext.getContentResolver().registerContentObserver( 71 Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL) 72 , true, mObserver); 73 74 mBatteryStateChangeReceiver.setListening(true); 75 updateSummary(); 76 } 77 78 @Override onStop()79 public void onStop() { 80 mContext.getContentResolver().unregisterContentObserver(mObserver); 81 mBatteryStateChangeReceiver.setListening(false); 82 } 83 84 @VisibleForTesting refreshConditionManager()85 void refreshConditionManager() { 86 ConditionManager.get(mContext).getCondition(BatterySaverCondition.class).refreshState(); 87 } 88 89 @Override getSummary()90 public CharSequence getSummary() { 91 final boolean isPowerSaveOn = mPowerManager.isPowerSaveMode(); 92 final int percent = Settings.Global.getInt(mContext.getContentResolver(), 93 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 94 if (isPowerSaveOn) { 95 return mContext.getString(R.string.battery_saver_on_summary); 96 } else if (percent != 0) { 97 return mContext.getString(R.string.battery_saver_off_scheduled_summary, 98 Utils.formatPercentage(percent)); 99 } else { 100 return mContext.getString(R.string.battery_saver_off_summary); 101 } 102 } 103 updateSummary()104 private void updateSummary() { 105 mBatterySaverPref.setSummary(getSummary()); 106 } 107 108 private final ContentObserver mObserver = new ContentObserver( 109 new Handler(Looper.getMainLooper())) { 110 @Override 111 public void onChange(boolean selfChange) { 112 updateSummary(); 113 } 114 }; 115 116 @Override onPowerSaveModeChanged()117 public void onPowerSaveModeChanged() { 118 updateSummary(); 119 } 120 121 @Override onBatteryChanged(boolean pluggedIn)122 public void onBatteryChanged(boolean pluggedIn) { 123 } 124 } 125