• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SelectorWithWidgetPreference;
28 
29 public class RestrictedPreferenceController extends AbstractPreferenceController
30         implements PreferenceControllerMixin {
31 
32     private static final String TAG = "RESTRICTED_PREF";
33 
34     @VisibleForTesting String KEY_RESTRICTED_PREF = "restricted_pref";
35     @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
36 
RestrictedPreferenceController(Context context, int uid, String packageName)37     public RestrictedPreferenceController(Context context, int uid, String packageName) {
38         super(context);
39         mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
40     }
41 
42     @Override
updateState(Preference preference)43     public void updateState(Preference preference) {
44 
45         if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) {
46             Log.d(TAG, "disable preference for " + mBatteryOptimizeUtils.getPackageName());
47             preference.setEnabled(false);
48             return;
49         } else {
50             preference.setEnabled(true);
51         }
52 
53         if (mBatteryOptimizeUtils.getAppOptimizationMode()
54                 == BatteryOptimizeUtils.MODE_RESTRICTED) {
55             Log.d(TAG, "is restricted states");
56             ((SelectorWithWidgetPreference) preference).setChecked(true);
57         } else {
58             ((SelectorWithWidgetPreference) preference).setChecked(false);
59             if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) {
60                 Log.d(TAG, "is system or default app, disable pref");
61                 preference.setEnabled(false);
62             }
63         }
64     }
65 
66     @Override
isAvailable()67     public boolean isAvailable() {
68         return true;
69     }
70 
71     @Override
getPreferenceKey()72     public String getPreferenceKey() {
73         return KEY_RESTRICTED_PREF;
74     }
75 
76     @Override
handlePreferenceTreeClick(Preference preference)77     public boolean handlePreferenceTreeClick(Preference preference) {
78         return getPreferenceKey().equals(preference.getKey());
79     }
80 }
81