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 35 import java.util.List; 36 37 /** 38 * Controller to change and update the smart battery toggle 39 */ 40 public class RestrictAppPreferenceController extends BasePreferenceController { 41 @VisibleForTesting 42 static final String KEY_RESTRICT_APP = "restricted_app"; 43 44 @VisibleForTesting 45 List<AppInfo> mAppInfos; 46 private AppOpsManager mAppOpsManager; 47 private InstrumentedPreferenceFragment mPreferenceFragment; 48 private UserManager mUserManager; 49 RestrictAppPreferenceController(Context context)50 public RestrictAppPreferenceController(Context context) { 51 super(context, KEY_RESTRICT_APP); 52 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 53 mUserManager = context.getSystemService(UserManager.class); 54 mAppInfos = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager); 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 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 super.updateState(preference); 70 mAppInfos = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager); 71 final int num = mAppInfos.size(); 72 // Fragment change RestrictedAppsList after onPause(), UI needs to be updated in onResume() 73 preference.setVisible(num > 0); 74 preference.setSummary( 75 mContext.getResources().getQuantityString(R.plurals.restricted_app_summary, num, 76 num)); 77 } 78 79 @Override handlePreferenceTreeClick(Preference preference)80 public boolean handlePreferenceTreeClick(Preference preference) { 81 if (getPreferenceKey().equals(preference.getKey())) { 82 // start fragment 83 RestrictedAppDetails.startRestrictedAppDetails(mPreferenceFragment, 84 mAppInfos); 85 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider() 86 .action(mContext, SettingsEnums.OPEN_APP_RESTRICTED_LIST); 87 return true; 88 } 89 90 return super.handlePreferenceTreeClick(preference); 91 } 92 } 93