1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18 package com.android.settings.fuelgauge; 19 20 import android.app.AppOpsManager; 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.os.UserManager; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 28 import com.android.settings.R; 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.core.InstrumentedPreferenceFragment; 31 import com.android.settings.fuelgauge.batterytip.AppInfo; 32 import com.android.settings.fuelgauge.batterytip.BatteryTipUtils; 33 import com.android.settings.overlay.FeatureFactory; 34 import com.android.settingslib.utils.StringUtil; 35 36 import java.util.List; 37 38 /** Controller to change and update the smart battery toggle */ 39 public class RestrictAppPreferenceController extends BasePreferenceController { 40 @VisibleForTesting static final String KEY_RESTRICT_APP = "restricted_app"; 41 42 @VisibleForTesting List<AppInfo> mAppInfos; 43 private AppOpsManager mAppOpsManager; 44 private InstrumentedPreferenceFragment mPreferenceFragment; 45 private UserManager mUserManager; 46 private boolean mEnableAppBatteryUsagePage; 47 RestrictAppPreferenceController(Context context)48 public RestrictAppPreferenceController(Context context) { 49 super(context, KEY_RESTRICT_APP); 50 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 51 mUserManager = context.getSystemService(UserManager.class); 52 mAppInfos = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager); 53 mEnableAppBatteryUsagePage = 54 mContext.getResources().getBoolean(R.bool.config_app_battery_usage_list_enabled); 55 } 56 RestrictAppPreferenceController(InstrumentedPreferenceFragment preferenceFragment)57 public RestrictAppPreferenceController(InstrumentedPreferenceFragment preferenceFragment) { 58 this(preferenceFragment.getContext()); 59 mPreferenceFragment = preferenceFragment; 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return mAppInfos.size() > 0 && !mEnableAppBatteryUsagePage 65 ? AVAILABLE 66 : CONDITIONALLY_UNAVAILABLE; 67 } 68 69 @Override updateState(Preference preference)70 public void updateState(Preference preference) { 71 super.updateState(preference); 72 mAppInfos = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager); 73 final int num = mAppInfos.size(); 74 // Fragment change RestrictedAppsList after onPause(), UI needs to be updated in onResume() 75 preference.setVisible(num > 0); 76 preference.setSummary( 77 StringUtil.getIcuPluralsString(mContext, num, R.string.restricted_app_summary)); 78 } 79 80 @Override handlePreferenceTreeClick(Preference preference)81 public boolean handlePreferenceTreeClick(Preference preference) { 82 if (getPreferenceKey().equals(preference.getKey())) { 83 // start fragment 84 RestrictedAppDetails.startRestrictedAppDetails(mPreferenceFragment, mAppInfos); 85 FeatureFactory.getFeatureFactory() 86 .getMetricsFeatureProvider() 87 .action(mContext, SettingsEnums.ACTION_APP_RESTRICTED_LIST_MANAGED); 88 return true; 89 } 90 91 return super.handlePreferenceTreeClick(preference); 92 } 93 } 94