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