1 /*
2  * Copyright (C) 2021 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 
17 package com.android.settings.fuelgauge;
18 
19 import android.content.Context;
20 import android.util.Log;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.preference.Preference;
24 
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.core.AbstractPreferenceController;
27 import com.android.settingslib.widget.RadioButtonPreference;
28 
29 public class OptimizedPreferenceController extends AbstractPreferenceController
30         implements PreferenceControllerMixin {
31 
32     private static final String TAG = "OPTIMIZED_PREF";
33 
34     @VisibleForTesting String KEY_OPTIMIZED_PREF = "optimized_pref";
35     @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
36 
OptimizedPreferenceController(Context context, int uid, String packageName)37     public OptimizedPreferenceController(Context context, int uid, String packageName) {
38         super(context);
39         mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
40     }
41 
42     @Override
isAvailable()43     public boolean isAvailable() {
44         return true;
45     }
46 
47     @Override
updateState(Preference preference)48     public void updateState(Preference preference) {
49         if (!mBatteryOptimizeUtils.isValidPackageName()) {
50             Log.d(TAG, "invalid package name, optimized states only");
51             preference.setEnabled(true);
52             ((RadioButtonPreference) preference).setChecked(true);
53             return;
54         }
55 
56         if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) {
57             Log.d(TAG, "is system or default app, disable pref");
58             ((RadioButtonPreference) preference).setChecked(false);
59             preference.setEnabled(false);
60         } else if (mBatteryOptimizeUtils.getAppOptimizationMode()
61                 == BatteryOptimizeUtils.MODE_OPTIMIZED) {
62             Log.d(TAG, "is optimized states");
63             ((RadioButtonPreference) preference).setChecked(true);
64         } else {
65             ((RadioButtonPreference) preference).setChecked(false);
66         }
67     }
68 
69     @Override
getPreferenceKey()70     public String getPreferenceKey() {
71         return KEY_OPTIMIZED_PREF;
72     }
73 
74     @Override
handlePreferenceTreeClick(Preference preference)75     public boolean handlePreferenceTreeClick(Preference preference) {
76         return getPreferenceKey().equals(preference.getKey());
77     }
78 }
79