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