• 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.Bundle;
21 import android.os.PowerManager;
22 import android.provider.Settings;
23 import android.provider.Settings.Global;
24 import android.text.TextUtils;
25 
26 import com.android.settingslib.fuelgauge.BatterySaverUtils;
27 
28 /**
29  * Responds to user actions in the Settings > Battery > Set a Schedule Screen
30  *
31  * Note that this is not a preference controller since that screen does not inherit from
32  * DashboardFragment.
33  *
34  * Will call the appropriate power manager APIs and modify the correct settings to enable
35  * users to control their automatic battery saver toggling preferences.
36  * See {@link Settings.Global#AUTOMATIC_POWER_SAVE_MODE} for more details.
37  */
38 public class BatterySaverScheduleRadioButtonsController {
39 
40     public static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule";
41     public static final String KEY_ROUTINE = "key_battery_saver_routine";
42     public static final String KEY_PERCENTAGE = "key_battery_saver_percentage";
43     public static final int TRIGGER_LEVEL_MIN = 5;
44 
45     private Context mContext;
46     private BatterySaverScheduleSeekBarController mSeekBarController;
47 
BatterySaverScheduleRadioButtonsController(Context context, BatterySaverScheduleSeekBarController seekbar)48     public BatterySaverScheduleRadioButtonsController(Context context,
49             BatterySaverScheduleSeekBarController seekbar) {
50         mContext = context;
51         mSeekBarController = seekbar;
52     }
53 
getDefaultKey()54     public String getDefaultKey() {
55         final ContentResolver resolver = mContext.getContentResolver();
56         // Note: this can also be obtained via PowerManager.getPowerSaveModeTrigger()
57         final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE,
58                 PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
59         // if mode is "dynamic" we are in routine mode, percentage with non-zero threshold is
60         // percentage mode, otherwise it is no schedule mode
61         if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) {
62             final int threshold =
63                     Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
64             if (threshold <= 0) {
65                 return KEY_NO_SCHEDULE;
66             }
67             return KEY_PERCENTAGE;
68         }
69         return KEY_ROUTINE;
70     }
71 
setDefaultKey(String key)72     public boolean setDefaultKey(String key) {
73         if (key == null) {
74             return false;
75         }
76 
77         final ContentResolver resolver = mContext.getContentResolver();
78         int mode = PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE;
79         int triggerLevel = 0;
80         final Bundle confirmationExtras = new Bundle(3);
81         switch (key) {
82             case KEY_NO_SCHEDULE:
83                 break;
84             case KEY_PERCENTAGE:
85                 triggerLevel = TRIGGER_LEVEL_MIN;
86                 confirmationExtras.putBoolean(BatterySaverUtils.EXTRA_CONFIRM_TEXT_ONLY, true);
87                 confirmationExtras.putInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER,
88                         PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
89                 confirmationExtras.putInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER_LEVEL,
90                         triggerLevel);
91                 break;
92             case KEY_ROUTINE:
93                 mode = PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC;
94                 confirmationExtras.putBoolean(BatterySaverUtils.EXTRA_CONFIRM_TEXT_ONLY, true);
95                 confirmationExtras.putInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER,
96                         PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
97                 break;
98             default:
99                 throw new IllegalStateException(
100                         "Not a valid key for " + this.getClass().getSimpleName());
101         }
102 
103         if (!TextUtils.equals(key, KEY_NO_SCHEDULE)
104                 && BatterySaverUtils.maybeShowBatterySaverConfirmation(
105                 mContext, confirmationExtras)) {
106             // reset this if we need to show the confirmation message
107             mode = PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE;
108             triggerLevel = 0;
109         }
110         // Trigger level is intentionally left alone when going between dynamic and percentage modes
111         // so that a users percentage based schedule is preserved when they toggle between the two.
112         Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE, mode);
113         if (mode != PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC) {
114             Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, triggerLevel);
115         }
116         // Suppress battery saver suggestion notification if enabling scheduling battery saver.
117         if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC || triggerLevel != 0) {
118             BatterySaverUtils.suppressAutoBatterySaver(mContext);
119         }
120         mSeekBarController.updateSeekBar();
121         return true;
122     }
123 }
124