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