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.content.Context; 22 import android.os.UserManager; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.core.InstrumentedPreferenceFragment; 30 import com.android.settings.fuelgauge.batterytip.AppInfo; 31 import com.android.settings.fuelgauge.batterytip.BatteryTipUtils; 32 33 import java.util.List; 34 35 /** 36 * Controller to change and update the smart battery toggle 37 */ 38 public class RestrictAppPreferenceController extends BasePreferenceController { 39 @VisibleForTesting 40 static final String KEY_RESTRICT_APP = "restricted_app"; 41 42 @VisibleForTesting 43 List<AppInfo> mAppInfos; 44 private AppOpsManager mAppOpsManager; 45 private InstrumentedPreferenceFragment mPreferenceFragment; 46 private UserManager mUserManager; 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 } 53 RestrictAppPreferenceController(InstrumentedPreferenceFragment preferenceFragment)54 public RestrictAppPreferenceController(InstrumentedPreferenceFragment preferenceFragment) { 55 this(preferenceFragment.getContext()); 56 mPreferenceFragment = preferenceFragment; 57 } 58 59 @Override getAvailabilityStatus()60 public int getAvailabilityStatus() { 61 return AVAILABLE; 62 } 63 64 @Override updateState(Preference preference)65 public void updateState(Preference preference) { 66 super.updateState(preference); 67 68 mAppInfos = BatteryTipUtils.getRestrictedAppsList(mAppOpsManager, mUserManager); 69 70 final int num = mAppInfos.size(); 71 // Don't show it if no app been restricted 72 preference.setVisible(num > 0); 73 preference.setSummary( 74 mContext.getResources().getQuantityString(R.plurals.restricted_app_summary, num, 75 num)); 76 } 77 78 @Override handlePreferenceTreeClick(Preference preference)79 public boolean handlePreferenceTreeClick(Preference preference) { 80 if (getPreferenceKey().equals(preference.getKey())) { 81 // start fragment 82 RestrictedAppDetails.startRestrictedAppDetails(mPreferenceFragment, 83 mAppInfos); 84 return true; 85 } 86 87 return super.handlePreferenceTreeClick(preference); 88 } 89 } 90