1 2 /* 3 * Copyright (C) 2017 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 6 * except in compliance with the License. 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 distributed under the 11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 * KIND, either express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package com.android.settings.enterprise; 17 18 import android.content.Context; 19 20 import androidx.preference.Preference; 21 22 import com.android.settings.R; 23 import com.android.settings.applications.ApplicationFeatureProvider; 24 import com.android.settings.core.PreferenceControllerMixin; 25 import com.android.settings.overlay.FeatureFactory; 26 import com.android.settingslib.core.AbstractPreferenceController; 27 import com.android.settingslib.utils.StringUtil; 28 29 public abstract class AdminGrantedPermissionsPreferenceControllerBase 30 extends AbstractPreferenceController implements PreferenceControllerMixin { 31 32 private final String[] mPermissions; 33 private final ApplicationFeatureProvider mFeatureProvider; 34 private final boolean mAsync; 35 private boolean mHasApps; 36 AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, String[] permissions)37 public AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, 38 String[] permissions) { 39 super(context); 40 mPermissions = permissions; 41 mFeatureProvider = FeatureFactory.getFeatureFactory() 42 .getApplicationFeatureProvider(); 43 mAsync = async; 44 mHasApps = false; 45 } 46 47 @Override updateState(Preference preference)48 public void updateState(Preference preference) { 49 mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions, 50 true /* async */, 51 (num) -> { 52 if (num == 0) { 53 mHasApps = false; 54 } else { 55 preference.setSummary(StringUtil.getIcuPluralsString(mContext, num, 56 R.string.enterprise_privacy_number_packages_lower_bound)); 57 mHasApps = true; 58 } 59 preference.setVisible(mHasApps); 60 }); 61 } 62 63 @Override isAvailable()64 public boolean isAvailable() { 65 if (mAsync) { 66 // When called on the main UI thread, we must not block. Since calculating the number of 67 // apps that the admin has granted a given permissions takes a bit of time, we always 68 // return true here and determine the pref's actual visibility asynchronously in 69 // updateState(). 70 return true; 71 } 72 73 // When called by the search indexer, we are on a background thread that we can block. Also, 74 // changes to the pref's visibility made in updateState() would not be seen by the indexer. 75 // We block and return synchronously whether the admin has granted the given permissions to 76 // any apps or not. 77 final Boolean[] haveAppsWithAdminGrantedPermissions = { null }; 78 mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions, 79 false /* async */, (num) -> haveAppsWithAdminGrantedPermissions[0] = num > 0); 80 mHasApps = haveAppsWithAdminGrantedPermissions[0]; 81 return mHasApps; 82 } 83 84 @Override handlePreferenceTreeClick(Preference preference)85 public boolean handlePreferenceTreeClick(Preference preference) { 86 if (!getPreferenceKey().equals(preference.getKey())) { 87 return false; 88 } 89 if (!mHasApps) { 90 return false; 91 } 92 return super.handlePreferenceTreeClick(preference); 93 } 94 } 95