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 static com.android.settingslib.fuelgauge.BatterySaverLogging.SAVER_ENABLED_SETTINGS;
20 
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.net.Uri;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.PowerManager;
27 import android.provider.SettingsSlicesContract;
28 
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.TogglePreferenceController;
33 import com.android.settings.fuelgauge.BatterySaverReceiver;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnStart;
36 import com.android.settingslib.core.lifecycle.events.OnStop;
37 import com.android.settingslib.fuelgauge.BatterySaverUtils;
38 import com.android.settingslib.widget.MainSwitchPreference;
39 
40 /** Controller to update the battery saver button */
41 public class BatterySaverButtonPreferenceController extends TogglePreferenceController
42         implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener {
43     private static final long SWITCH_ANIMATION_DURATION = 350L;
44 
45     private final BatterySaverReceiver mBatterySaverReceiver;
46     private final PowerManager mPowerManager;
47 
48     private Handler mHandler;
49     private MainSwitchPreference mPreference;
50 
BatterySaverButtonPreferenceController(Context context, String key)51     public BatterySaverButtonPreferenceController(Context context, String key) {
52         super(context, key);
53         mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
54         mBatterySaverReceiver = new BatterySaverReceiver(context);
55         mBatterySaverReceiver.setBatterySaverListener(this);
56         mHandler = new Handler(Looper.getMainLooper());
57     }
58 
59     @Override
getAvailabilityStatus()60     public int getAvailabilityStatus() {
61         return AVAILABLE;
62     }
63 
64     @Override
isPublicSlice()65     public boolean isPublicSlice() {
66         return true;
67     }
68 
69     @Override
getSliceUri()70     public Uri getSliceUri() {
71         return new Uri.Builder()
72                 .scheme(ContentResolver.SCHEME_CONTENT)
73                 .authority(SettingsSlicesContract.AUTHORITY)
74                 .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
75                 .appendPath(SettingsSlicesContract.KEY_BATTERY_SAVER)
76                 .build();
77     }
78 
79     @Override
onStart()80     public void onStart() {
81         mBatterySaverReceiver.setListening(true);
82     }
83 
84     @Override
onStop()85     public void onStop() {
86         mBatterySaverReceiver.setListening(false);
87         mHandler.removeCallbacksAndMessages(null /* token */);
88     }
89 
90     @Override
displayPreference(PreferenceScreen screen)91     public void displayPreference(PreferenceScreen screen) {
92         super.displayPreference(screen);
93         mPreference = screen.findPreference(getPreferenceKey());
94         mPreference.updateStatus(isChecked());
95     }
96 
97     @Override
isChecked()98     public boolean isChecked() {
99         return mPowerManager.isPowerSaveMode();
100     }
101 
102     @Override
setChecked(boolean stateOn)103     public boolean setChecked(boolean stateOn) {
104         return BatterySaverUtils.setPowerSaveMode(
105                 mContext, stateOn, false /* needFirstTimeWarning */, SAVER_ENABLED_SETTINGS);
106     }
107 
108     @Override
getSliceHighlightMenuRes()109     public int getSliceHighlightMenuRes() {
110         return R.string.menu_key_battery;
111     }
112 
113     @Override
onPowerSaveModeChanged()114     public void onPowerSaveModeChanged() {
115         mHandler.postDelayed(() -> onPowerSaveModeChangedInternal(), SWITCH_ANIMATION_DURATION);
116     }
117 
onPowerSaveModeChangedInternal()118     private void onPowerSaveModeChangedInternal() {
119         final boolean isChecked = isChecked();
120         if (mPreference != null && mPreference.isChecked() != isChecked) {
121             mPreference.setChecked(isChecked);
122         }
123     }
124 
125     @Override
onBatteryChanged(boolean pluggedIn)126     public void onBatteryChanged(boolean pluggedIn) {
127         if (mPreference != null) {
128             mPreference.setEnabled(!pluggedIn);
129         }
130     }
131 }
132