1 /*
2  * Copyright (C) 2024 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 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
29 import com.android.settings.overlay.FeatureFactory;
30 import com.android.settingslib.utils.StringUtil;
31 
32 /** Preference controller to control the battery manager */
33 public class BatteryManagerPreferenceController extends BasePreferenceController {
34     private static final String KEY_BATTERY_MANAGER = "smart_battery_manager";
35 
36     private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
37     private AppOpsManager mAppOpsManager;
38     private UserManager mUserManager;
39     private boolean mEnableAppBatteryUsagePage;
40 
BatteryManagerPreferenceController(Context context)41     public BatteryManagerPreferenceController(Context context) {
42         super(context, KEY_BATTERY_MANAGER);
43         mPowerUsageFeatureProvider = FeatureFactory.getFeatureFactory()
44                 .getPowerUsageFeatureProvider();
45         mAppOpsManager = context.getSystemService(AppOpsManager.class);
46         mUserManager = context.getSystemService(UserManager.class);
47         mEnableAppBatteryUsagePage =
48                 mContext.getResources().getBoolean(R.bool.config_app_battery_usage_list_enabled);
49     }
50 
51     @Override
getAvailabilityStatus()52     public int getAvailabilityStatus() {
53         if (!mPowerUsageFeatureProvider.isBatteryManagerSupported()) {
54             return UNSUPPORTED_ON_DEVICE;
55         }
56         if (!mContext.getResources().getBoolean(R.bool.config_battery_manager_consider_ac)) {
57             return AVAILABLE_UNSEARCHABLE;
58         }
59         return mPowerUsageFeatureProvider.isAdaptiveChargingSupported()
60                 ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE;
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         super.updateState(preference);
66         if (!mEnableAppBatteryUsagePage) {
67             final int num = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager,
68                     mUserManager).size();
69             updateSummary(preference, num);
70         }
71     }
72 
73     @VisibleForTesting
updateSummary(Preference preference, int num)74     void updateSummary(Preference preference, int num) {
75         if (num > 0) {
76             preference.setSummary(StringUtil.getIcuPluralsString(mContext, num,
77                     R.string.battery_manager_app_restricted));
78         } else {
79             preference.setSummary(
80                     mPowerUsageFeatureProvider.isAdaptiveChargingSupported()
81                             ? R.string.battery_manager_summary
82                             : R.string.battery_manager_summary_unsupported);
83         }
84     }
85 }
86