1 /*
2  * Copyright (C) 2023 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 
21 import androidx.annotation.VisibleForTesting;
22 import androidx.preference.Preference;
23 
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settingslib.PrimarySwitchPreference;
26 import com.android.settingslib.core.AbstractPreferenceController;
27 import com.android.settingslib.widget.MainSwitchPreference;
28 
29 /** Controller to update the app background usage state */
30 public class AllowBackgroundPreferenceController extends AbstractPreferenceController
31         implements PreferenceControllerMixin {
32 
33     private static final String TAG = "AllowBackgroundPreferenceController";
34 
35     @VisibleForTesting static final String KEY_ALLOW_BACKGROUND_USAGE = "allow_background_usage";
36 
37     @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
38 
AllowBackgroundPreferenceController(Context context, int uid, String packageName)39     public AllowBackgroundPreferenceController(Context context, int uid, String packageName) {
40         super(context);
41         mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
42     }
43 
setChecked(Preference preference, boolean checked)44     private void setChecked(Preference preference, boolean checked) {
45         if (preference instanceof PrimarySwitchPreference) {
46             ((PrimarySwitchPreference) preference).setChecked(checked);
47         } else if (preference instanceof MainSwitchPreference) {
48             ((MainSwitchPreference) preference).setChecked(checked);
49         }
50     }
51 
setEnabled(Preference preference, boolean enabled)52     private void setEnabled(Preference preference, boolean enabled) {
53         if (preference instanceof PrimarySwitchPreference) {
54             ((PrimarySwitchPreference) preference).setEnabled(enabled);
55             ((PrimarySwitchPreference) preference).setSwitchEnabled(enabled);
56         } else if (preference instanceof MainSwitchPreference) {
57             ((MainSwitchPreference) preference).setEnabled(enabled);
58         }
59     }
60 
61     @Override
updateState(Preference preference)62     public void updateState(Preference preference) {
63         setEnabled(preference, mBatteryOptimizeUtils.isOptimizeModeMutable());
64 
65         final boolean isAllowBackground =
66                 mBatteryOptimizeUtils.getAppOptimizationMode()
67                         != BatteryOptimizeUtils.MODE_RESTRICTED;
68         setChecked(preference, isAllowBackground);
69     }
70 
71     @Override
isAvailable()72     public boolean isAvailable() {
73         return true;
74     }
75 
76     @Override
getPreferenceKey()77     public String getPreferenceKey() {
78         return KEY_ALLOW_BACKGROUND_USAGE;
79     }
80 
81     @Override
handlePreferenceTreeClick(Preference preference)82     public boolean handlePreferenceTreeClick(Preference preference) {
83         return getPreferenceKey().equals(preference.getKey());
84     }
85 }
86