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 import android.support.annotation.VisibleForTesting; 24 import android.support.v7.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 static final int ON = 1; 37 private PowerUsageFeatureProvider mPowerUsageFeatureProvider; 38 private AppOpsManager mAppOpsManager; 39 private UserManager mUserManager; 40 41 public BatteryManagerPreferenceController(Context context) { 42 super(context, KEY_BATTERY_MANAGER); 43 mPowerUsageFeatureProvider = FeatureFactory.getFactory( 44 context).getPowerUsageFeatureProvider(context); 45 mAppOpsManager = context.getSystemService(AppOpsManager.class); 46 mUserManager = context.getSystemService(UserManager.class); 47 } 48 49 @Override 50 public int getAvailabilityStatus() { 51 return AVAILABLE; 52 } 53 54 @Override 55 public void updateState(Preference preference) { 56 super.updateState(preference); 57 final int num = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager).size(); 58 final String setting = mPowerUsageFeatureProvider.isSmartBatterySupported() 59 ? Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED 60 : Settings.Global.APP_AUTO_RESTRICTION_ENABLED; 61 final boolean featureOn = 62 Settings.Global.getInt(mContext.getContentResolver(), setting, ON) == ON; 63 64 updateSummary(preference, featureOn, num); 65 } 66 67 @VisibleForTesting 68 void updateSummary(Preference preference, boolean featureOn, int num) { 69 if (num > 0) { 70 preference.setSummary(mContext.getResources().getQuantityString( 71 R.plurals.battery_manager_app_restricted, num, num)); 72 } else if (featureOn) { 73 preference.setSummary(R.string.battery_manager_on); 74 } else { 75 preference.setSummary(R.string.battery_manager_off); 76 } 77 } 78 } 79