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 import android.support.v7.preference.Preference; 18 19 import com.android.settings.R; 20 import com.android.settings.applications.ApplicationFeatureProvider; 21 import com.android.settings.core.DynamicAvailabilityPreferenceController; 22 import com.android.settings.overlay.FeatureFactory; 23 import com.android.settingslib.core.lifecycle.Lifecycle; 24 25 public class EnterpriseInstalledPackagesPreferenceController 26 extends DynamicAvailabilityPreferenceController { 27 28 private static final String KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES 29 = "number_enterprise_installed_packages"; 30 private final ApplicationFeatureProvider mFeatureProvider; 31 private final boolean mAsync; 32 33 public EnterpriseInstalledPackagesPreferenceController(Context context, Lifecycle lifecycle, 34 boolean async) { 35 super(context, lifecycle); 36 mFeatureProvider = FeatureFactory.getFactory(context) 37 .getApplicationFeatureProvider(context); 38 mAsync = async; 39 } 40 41 @Override 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 notifyOnAvailabilityUpdate(available); 57 }); 58 } 59 60 @Override 61 public boolean isAvailable() { 62 if (mAsync) { 63 // When called on the main UI thread, we must not block. Since calculating the number of 64 // enterprise-installed apps takes a bit of time, we always return true here and 65 // determine the pref's actual visibility asynchronously in updateState(). 66 return true; 67 } 68 69 // When called by the search indexer, we are on a background thread that we can block. Also, 70 // changes to the pref's visibility made in updateState() would not be seen by the indexer. 71 // We block and return synchronously whether there are enterprise-installed apps or not. 72 final Boolean[] haveEnterpriseInstalledPackages = { null }; 73 mFeatureProvider.calculateNumberOfPolicyInstalledApps(false /* async */, 74 (num) -> haveEnterpriseInstalledPackages[0] = num > 0); 75 final boolean available = haveEnterpriseInstalledPackages[0]; 76 notifyOnAvailabilityUpdate(available); 77 return available; 78 } 79 80 @Override 81 public String getPreferenceKey() { 82 return KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES; 83 } 84 } 85