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 /** Controller to update the battery saver entry preference. */ 39 public class BatterySaverController extends BasePreferenceController 40 implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener { 41 private static final String KEY_BATTERY_SAVER = "battery_saver_summary"; 42 private final BatterySaverReceiver mBatteryStateChangeReceiver; 43 private final PowerManager mPowerManager; 44 private Preference mBatterySaverPref; 45 private final ContentObserver mObserver = 46 new ContentObserver(new Handler(Looper.getMainLooper())) { 47 @Override 48 public void onChange(boolean selfChange) { 49 updateSummary(); 50 } 51 }; 52 BatterySaverController(Context context)53 public BatterySaverController(Context context) { 54 super(context, KEY_BATTERY_SAVER); 55 56 mPowerManager = mContext.getSystemService(PowerManager.class); 57 mBatteryStateChangeReceiver = new BatterySaverReceiver(context); 58 mBatteryStateChangeReceiver.setBatterySaverListener(this); 59 BatterySaverUtils.revertScheduleToNoneIfNeeded(context); 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return AVAILABLE; 65 } 66 67 @Override getPreferenceKey()68 public String getPreferenceKey() { 69 return KEY_BATTERY_SAVER; 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 super.displayPreference(screen); 75 mBatterySaverPref = screen.findPreference(KEY_BATTERY_SAVER); 76 } 77 78 @Override onStart()79 public void onStart() { 80 mContext.getContentResolver() 81 .registerContentObserver( 82 Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL), 83 true /* notifyForDescendants */, 84 mObserver); 85 86 mBatteryStateChangeReceiver.setListening(true); 87 updateSummary(); 88 } 89 90 @Override onStop()91 public void onStop() { 92 mContext.getContentResolver().unregisterContentObserver(mObserver); 93 mBatteryStateChangeReceiver.setListening(false); 94 } 95 96 @Override getSummary()97 public CharSequence getSummary() { 98 final boolean isPowerSaveOn = mPowerManager.isPowerSaveMode(); 99 if (isPowerSaveOn) { 100 return mContext.getString(R.string.battery_saver_on_summary); 101 } 102 103 final ContentResolver resolver = mContext.getContentResolver(); 104 final int mode = 105 Settings.Global.getInt( 106 resolver, 107 Global.AUTOMATIC_POWER_SAVE_MODE, 108 PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); 109 if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) { 110 final int percent = 111 Settings.Global.getInt( 112 resolver, Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 113 return percent != 0 114 ? mContext.getString( 115 R.string.battery_saver_off_scheduled_summary, 116 Utils.formatPercentage(percent)) 117 : mContext.getString(R.string.battery_saver_off_summary); 118 } else { 119 return mContext.getString(R.string.battery_saver_pref_auto_routine_summary); 120 } 121 } 122 updateSummary()123 private void updateSummary() { 124 if (mBatterySaverPref != null) { 125 mBatterySaverPref.setSummary(getSummary()); 126 } 127 } 128 129 @Override onPowerSaveModeChanged()130 public void onPowerSaveModeChanged() { 131 updateSummary(); 132 } 133 134 @Override onBatteryChanged(boolean pluggedIn)135 public void onBatteryChanged(boolean pluggedIn) {} 136 } 137