1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.enterprise; 18 19 import android.content.Context; 20 21 import androidx.preference.Preference; 22 23 import com.android.settingslib.core.AbstractPreferenceController; 24 import com.android.tv.settings.R; 25 import com.android.tv.settings.enterprise.apps.ApplicationFeatureProvider; 26 import com.android.tv.settings.overlay.FlavorUtils; 27 28 public abstract class AdminGrantedPermissionsPreferenceControllerBase extends 29 AbstractPreferenceController { 30 31 private final String[] mPermissions; 32 private final ApplicationFeatureProvider mFeatureProvider; 33 private final boolean mAsync; 34 private boolean mHasApps; 35 AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, String[] permissions)36 public AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, 37 String[] permissions) { 38 super(context); 39 mPermissions = permissions; 40 mFeatureProvider = FlavorUtils.getFeatureFactory(context).getApplicationFeatureProvider( 41 context); 42 mAsync = async; 43 mHasApps = false; 44 } 45 46 @Override updateState(Preference preference)47 public void updateState(Preference preference) { 48 mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions, 49 true /* async */, 50 (num) -> { 51 if (num == 0) { 52 mHasApps = false; 53 } else { 54 preference.setSummary(mContext.getResources().getQuantityString( 55 R.plurals.enterprise_privacy_number_packages_lower_bound, 56 num, num)); 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