1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 package com.android.settings.fuelgauge.batterysaver;
17 
18 import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;
19 
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.provider.Settings;
23 import android.provider.Settings.Global;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.Utils;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.fuelgauge.BatterySaverUtils;
33 
34 /**
35  * Simple controller to navigate users to the scheduling page from "Settings > Battery > Battery
36  * Saver". Also updates the summary for preference based on the currently selected settings.
37  */
38 public class BatterySaverSchedulePreferenceController extends BasePreferenceController {
39 
40     @VisibleForTesting Preference mBatterySaverSchedulePreference;
41     public static final String KEY_BATTERY_SAVER_SCHEDULE = "battery_saver_schedule";
42 
BatterySaverSchedulePreferenceController(Context context)43     public BatterySaverSchedulePreferenceController(Context context) {
44         super(context, KEY_BATTERY_SAVER_SCHEDULE);
45         BatterySaverUtils.revertScheduleToNoneIfNeeded(context);
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return KEY_BATTERY_SAVER_SCHEDULE;
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56         mBatterySaverSchedulePreference = screen.findPreference(KEY_BATTERY_SAVER_SCHEDULE);
57     }
58 
59     @Override
getSummary()60     public CharSequence getSummary() {
61         final ContentResolver resolver = mContext.getContentResolver();
62         final String mode = BatterySaverUtils.getBatterySaverScheduleKey(mContext);
63         if (KEY_PERCENTAGE.equals(mode)) {
64             final int threshold =
65                     Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
66             return mContext.getString(
67                     R.string.battery_saver_auto_percentage_summary,
68                     Utils.formatPercentage(threshold));
69         }
70         return mContext.getText(R.string.battery_saver_auto_no_schedule);
71     }
72 
73     @Override
getAvailabilityStatus()74     public int getAvailabilityStatus() {
75         return AVAILABLE;
76     }
77 }
78