• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.fuelgauge.batterysaver;
17 
18 import android.content.ContentResolver;
19 import android.content.Context;
20 import android.os.PowerManager;
21 import android.provider.Settings;
22 import android.provider.Settings.Global;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.Preference.OnPreferenceChangeListener;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settings.Utils;
31 import com.android.settings.widget.SeekBarPreference;
32 
33 /**
34  * Responds to user actions in the Settings > Battery > Set a Schedule Screen for the seekbar.
35  * Note that this seekbar is only visible when the radio button selected is "Percentage".
36  *
37  * Note that this is not a preference controller since that screen does not inherit from
38  * DashboardFragment.
39  *
40  * Will call the appropriate power manager APIs and modify the correct settings to enable
41  * users to control their automatic battery saver toggling preferences.
42  * See {@link Settings.Global#AUTOMATIC_POWER_SAVE_MODE} for more details.
43  */
44 public class BatterySaverScheduleSeekBarController implements
45         OnPreferenceChangeListener {
46 
47     public static final int MAX_SEEKBAR_VALUE = 15;
48     public static final int MIN_SEEKBAR_VALUE = 1;
49     public static final String KEY_BATTERY_SAVER_SEEK_BAR = "battery_saver_seek_bar";
50 
51     @VisibleForTesting
52     public SeekBarPreference mSeekBarPreference;
53     private Context mContext;
54 
BatterySaverScheduleSeekBarController(Context context)55     public BatterySaverScheduleSeekBarController(Context context) {
56         mContext = context;
57         mSeekBarPreference = new SeekBarPreference(context);
58         mSeekBarPreference.setLayoutResource(R.layout.preference_widget_seekbar_settings);
59         mSeekBarPreference.setIconSpaceReserved(false);
60         mSeekBarPreference.setOnPreferenceChangeListener(this);
61         mSeekBarPreference.setContinuousUpdates(true);
62         mSeekBarPreference.setMax(MAX_SEEKBAR_VALUE);
63         mSeekBarPreference.setMin(MIN_SEEKBAR_VALUE);
64         mSeekBarPreference.setKey(KEY_BATTERY_SAVER_SEEK_BAR);
65         mSeekBarPreference.setHapticFeedbackMode(SeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS);
66         updateSeekBar();
67     }
68 
69     @Override
onPreferenceChange(Preference preference, Object newValue)70     public boolean onPreferenceChange(Preference preference, Object newValue) {
71         // The nits are in intervals of 5%
72         final int percentage = ((Integer) newValue) * 5;
73         Settings.Global.putInt(mContext.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL,
74                 percentage);
75         final CharSequence stateDescription = formatStateDescription(percentage);
76         preference.setTitle(stateDescription);
77         mSeekBarPreference.overrideSeekBarStateDescription(stateDescription);
78         return true;
79     }
80 
updateSeekBar()81     public void updateSeekBar() {
82         final ContentResolver resolver = mContext.getContentResolver();
83         // Note: this can also be obtained via PowerManager.getPowerSaveModeTrigger()
84         final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE,
85                 PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
86         // if mode is "dynamic" we are in routine mode, percentage with non-zero threshold is
87         // percentage mode, otherwise it is no schedule mode
88         if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
89             final int threshold =
90                     Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
91             if (threshold <= 0) {
92                 mSeekBarPreference.setVisible(false);
93             } else {
94                 final int currentSeekbarValue = Math.max(threshold / 5, MIN_SEEKBAR_VALUE);
95                 mSeekBarPreference.setVisible(true);
96                 mSeekBarPreference.setProgress(currentSeekbarValue);
97                 final CharSequence stateDescription = formatStateDescription(
98                         currentSeekbarValue * 5);
99                 mSeekBarPreference.setTitle(stateDescription);
100                 mSeekBarPreference.overrideSeekBarStateDescription(stateDescription);
101             }
102         } else {
103             mSeekBarPreference.setVisible(false);
104         }
105     }
106 
107     /**
108      * Adds the seekbar to the end of the provided preference screen
109      *
110      * @param screen The preference screen to add the seekbar to
111      */
addToScreen(PreferenceScreen screen)112     public void addToScreen(PreferenceScreen screen) {
113         // makes sure it gets added after the preferences if called due to first time battery
114         // saver message
115         mSeekBarPreference.setOrder(100);
116         screen.addPreference(mSeekBarPreference);
117     }
118 
formatStateDescription(int percentage)119     private CharSequence formatStateDescription(int percentage) {
120         return mContext.getString(R.string.battery_saver_seekbar_title,
121                 Utils.formatPercentage(percentage));
122     }
123 }
124