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