1 package com.android.settings.fuelgauge.batterysaver;
2 
3 import android.content.Context;
4 import android.icu.text.NumberFormat;
5 import android.provider.Settings;
6 import android.provider.Settings.Global;
7 import androidx.preference.Preference;
8 import androidx.preference.SwitchPreference;
9 import com.android.settings.R;
10 import com.android.settings.core.PreferenceControllerMixin;
11 import com.android.settings.core.TogglePreferenceController;
12 
13 public class BatterySaverStickyPreferenceController extends TogglePreferenceController implements
14         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
15 
16     private Context mContext;
17 
BatterySaverStickyPreferenceController(Context context, String preferenceKey)18     public BatterySaverStickyPreferenceController(Context context, String preferenceKey) {
19         super(context, preferenceKey);
20         mContext = context;
21     }
22 
23     @Override
isChecked()24     public boolean isChecked() {
25         return Settings.Global.getInt(mContext.getContentResolver(),
26             Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, 1) == 1;
27     }
28 
29     @Override
setChecked(boolean isChecked)30     public boolean setChecked(boolean isChecked) {
31         Settings.Global.putInt(mContext.getContentResolver(),
32             Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
33             isChecked ? 1 : 0);
34         return true;
35     }
36 
37     @Override
refreshSummary(Preference preference)38     protected void refreshSummary(Preference preference) {
39         super.refreshSummary(preference);
40         final double stickyShutoffLevel = Settings.Global.getInt(
41             mContext.getContentResolver(), Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL, 90);
42         final String percentage = NumberFormat
43             .getPercentInstance()
44             .format(stickyShutoffLevel / 100.0);
45         preference.setSummary(
46             mContext.getString(R.string.battery_saver_sticky_description_new, percentage));
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         int setting = Settings.Global.getInt(mContext.getContentResolver(),
52             Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, 1);
53 
54         ((SwitchPreference) preference).setChecked(setting == 1);
55         refreshSummary(preference);
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return AVAILABLE;
61     }
62 }
63