1 package com.android.settings.fuelgauge.batterysaver;
2 
3 import android.content.Context;
4 import android.provider.Settings;
5 import android.provider.Settings.Global;
6 
7 import androidx.preference.Preference;
8 import androidx.preference.TwoStatePreference;
9 
10 import com.android.settings.R;
11 import com.android.settings.Utils;
12 import com.android.settings.core.PreferenceControllerMixin;
13 import com.android.settings.core.TogglePreferenceController;
14 
15 public class BatterySaverStickyPreferenceController extends TogglePreferenceController
16         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
17     private static final int DEFAULT_STICKY_SHUTOFF_LEVEL = 90;
18 
19     private Context mContext;
20 
BatterySaverStickyPreferenceController(Context context, String preferenceKey)21     public BatterySaverStickyPreferenceController(Context context, String preferenceKey) {
22         super(context, preferenceKey);
23         mContext = context;
24     }
25 
26     @Override
isChecked()27     public boolean isChecked() {
28         return Settings.Global.getInt(
29                         mContext.getContentResolver(),
30                         Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
31                         1)
32                 == 1;
33     }
34 
35     @Override
setChecked(boolean isChecked)36     public boolean setChecked(boolean isChecked) {
37         Settings.Global.putInt(
38                 mContext.getContentResolver(),
39                 Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
40                 isChecked ? 1 : 0);
41         return true;
42     }
43 
44     @Override
refreshSummary(Preference preference)45     protected void refreshSummary(Preference preference) {
46         super.refreshSummary(preference);
47         final int stickyShutoffLevel =
48                 Settings.Global.getInt(
49                         mContext.getContentResolver(),
50                         Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL,
51                         DEFAULT_STICKY_SHUTOFF_LEVEL);
52         final String formatPercentage = Utils.formatPercentage(stickyShutoffLevel);
53         preference.setTitle(
54                 mContext.getString(
55                         R.string.battery_saver_sticky_title_percentage, formatPercentage));
56         preference.setSummary(
57                 mContext.getString(
58                         R.string.battery_saver_sticky_description_new, formatPercentage));
59     }
60 
61     @Override
updateState(Preference preference)62     public void updateState(Preference preference) {
63         int setting =
64                 Settings.Global.getInt(
65                         mContext.getContentResolver(),
66                         Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
67                         1);
68 
69         ((TwoStatePreference) preference).setChecked(setting == 1);
70         refreshSummary(preference);
71     }
72 
73     @Override
getAvailabilityStatus()74     public int getAvailabilityStatus() {
75         return AVAILABLE;
76     }
77 
78     @Override
getSliceHighlightMenuRes()79     public int getSliceHighlightMenuRes() {
80         return R.string.menu_key_battery;
81     }
82 }
83