1 /*
2  * Copyright (C) 2018 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.batterytip;
18 
19 import android.app.AppOpsManager;
20 import android.content.Context;
21 import android.os.UserManager;
22 import android.provider.Settings;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
30 import com.android.settings.overlay.FeatureFactory;
31 
32 /**
33  * Preference controller to control the battery manager
34  */
35 public class BatteryManagerPreferenceController extends BasePreferenceController {
36     private static final String KEY_BATTERY_MANAGER = "smart_battery_manager";
37     private static final int ON = 1;
38     private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
39     private AppOpsManager mAppOpsManager;
40     private UserManager mUserManager;
41 
BatteryManagerPreferenceController(Context context)42     public BatteryManagerPreferenceController(Context context) {
43         super(context, KEY_BATTERY_MANAGER);
44         mPowerUsageFeatureProvider = FeatureFactory.getFactory(
45                 context).getPowerUsageFeatureProvider(context);
46         mAppOpsManager = context.getSystemService(AppOpsManager.class);
47         mUserManager = context.getSystemService(UserManager.class);
48     }
49 
50     @Override
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return AVAILABLE_UNSEARCHABLE;
53     }
54 
55     @Override
updateState(Preference preference)56     public void updateState(Preference preference) {
57         super.updateState(preference);
58         final int num = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager).size();
59         final String setting = mPowerUsageFeatureProvider.isSmartBatterySupported()
60                 ? Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED
61                 : Settings.Global.APP_AUTO_RESTRICTION_ENABLED;
62         final boolean featureOn =
63                 Settings.Global.getInt(mContext.getContentResolver(), setting, ON) == ON;
64 
65         updateSummary(preference, featureOn, num);
66     }
67 
68     @VisibleForTesting
updateSummary(Preference preference, boolean featureOn, int num)69     void updateSummary(Preference preference, boolean featureOn, int num) {
70         if (num > 0) {
71             preference.setSummary(mContext.getResources().getQuantityString(
72                     R.plurals.battery_manager_app_restricted, num, num));
73         } else if (featureOn) {
74             preference.setSummary(R.string.battery_manager_on);
75         } else {
76             preference.setSummary(R.string.battery_manager_off);
77         }
78     }
79 }
80