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