1 /* 2 * Copyright (C) 2018 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.batterysaver; 18 19 import android.content.Context; 20 import android.os.PowerManager; 21 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.core.TogglePreferenceController; 26 import com.android.settings.fuelgauge.BatterySaverReceiver; 27 import com.android.settings.widget.TwoStateButtonPreference; 28 import com.android.settingslib.core.lifecycle.LifecycleObserver; 29 import com.android.settingslib.core.lifecycle.events.OnStart; 30 import com.android.settingslib.core.lifecycle.events.OnStop; 31 import com.android.settingslib.fuelgauge.BatterySaverUtils; 32 33 /** 34 * Controller to update the battery saver button 35 */ 36 public class BatterySaverButtonPreferenceController extends 37 TogglePreferenceController implements 38 LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener { 39 40 private final BatterySaverReceiver mBatterySaverReceiver; 41 private final PowerManager mPowerManager; 42 43 private TwoStateButtonPreference mPreference; 44 BatterySaverButtonPreferenceController(Context context, String key)45 public BatterySaverButtonPreferenceController(Context context, String key) { 46 super(context, key); 47 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 48 mBatterySaverReceiver = new BatterySaverReceiver(context); 49 mBatterySaverReceiver.setBatterySaverListener(this); 50 } 51 52 @Override getAvailabilityStatus()53 public int getAvailabilityStatus() { 54 return AVAILABLE; 55 } 56 57 @Override isSliceable()58 public boolean isSliceable() { 59 return true; 60 } 61 62 @Override onStart()63 public void onStart() { 64 mBatterySaverReceiver.setListening(true); 65 } 66 67 @Override onStop()68 public void onStop() { 69 mBatterySaverReceiver.setListening(false); 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 super.displayPreference(screen); 75 mPreference = screen.findPreference(getPreferenceKey()); 76 } 77 78 @Override isChecked()79 public boolean isChecked() { 80 return mPowerManager.isPowerSaveMode(); 81 } 82 83 @Override setChecked(boolean stateOn)84 public boolean setChecked(boolean stateOn) { 85 // This screen already shows a warning, so we don't need another warning. 86 return BatterySaverUtils.setPowerSaveMode(mContext, stateOn, 87 false /* needFirstTimeWarning */); 88 } 89 90 @Override updateState(Preference preference)91 public void updateState(Preference preference) { 92 super.updateState(preference); 93 if (mPreference != null) { 94 mPreference.setChecked(isChecked()); 95 } 96 } 97 98 @Override onPowerSaveModeChanged()99 public void onPowerSaveModeChanged() { 100 final boolean isChecked = isChecked(); 101 if (mPreference != null && mPreference.isChecked() != isChecked) { 102 mPreference.setChecked(isChecked); 103 } 104 } 105 106 @Override onBatteryChanged(boolean pluggedIn)107 public void onBatteryChanged(boolean pluggedIn) { 108 if (mPreference != null) { 109 mPreference.setButtonEnabled(!pluggedIn); 110 } 111 } 112 } 113