• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
31 /**
32  * Preference controller to control the battery manager
33  */
34 public class BatteryManagerPreferenceController extends BasePreferenceController {
35     private static final String KEY_BATTERY_MANAGER = "smart_battery_manager";
36     private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
37     private AppOpsManager mAppOpsManager;
38     private UserManager mUserManager;
39 
BatteryManagerPreferenceController(Context context)40     public BatteryManagerPreferenceController(Context context) {
41         super(context, KEY_BATTERY_MANAGER);
42         mPowerUsageFeatureProvider = FeatureFactory.getFactory(
43                 context).getPowerUsageFeatureProvider(context);
44         mAppOpsManager = context.getSystemService(AppOpsManager.class);
45         mUserManager = context.getSystemService(UserManager.class);
46     }
47 
48     @Override
getAvailabilityStatus()49     public int getAvailabilityStatus() {
50         return AVAILABLE_UNSEARCHABLE;
51     }
52 
53     @Override
updateState(Preference preference)54     public void updateState(Preference preference) {
55         super.updateState(preference);
56         final int num = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager).size();
57 
58         updateSummary(preference, num);
59     }
60 
61     @VisibleForTesting
updateSummary(Preference preference, int num)62     void updateSummary(Preference preference, int num) {
63         if (num > 0) {
64             preference.setSummary(mContext.getResources().getQuantityString(
65                     R.plurals.battery_manager_app_restricted, num, num));
66         } else {
67             preference.setSummary(R.string.battery_manager_summary);
68         }
69     }
70 }
71