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 /** 29 * Forked from: 30 * Settings/src/com/android/settings/enterprise/EnterpriseInstalledPackagesPreferenceController.java 31 */ 32 public class EnterpriseInstalledPackagesPreferenceController 33 extends AbstractPreferenceController { 34 35 private static final String KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES = 36 "number_enterprise_installed_packages"; 37 private final ApplicationFeatureProvider mFeatureProvider; 38 private final boolean mAsync; 39 EnterpriseInstalledPackagesPreferenceController(Context context, boolean async)40 public EnterpriseInstalledPackagesPreferenceController(Context context, boolean async) { 41 super(context); 42 mFeatureProvider = FlavorUtils.getFeatureFactory(context).getApplicationFeatureProvider( 43 context); 44 mAsync = async; 45 } 46 47 @Override updateState(Preference preference)48 public void updateState(Preference preference) { 49 mFeatureProvider.calculateNumberOfPolicyInstalledApps(true /* async */, 50 (num) -> { 51 final boolean available; 52 if (num == 0) { 53 available = false; 54 } else { 55 available = true; 56 preference.setSummary(mContext.getResources().getQuantityString( 57 R.plurals.enterprise_privacy_number_packages_lower_bound, num, 58 num)); 59 60 } 61 preference.setVisible(available); 62 }); 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 if (mAsync) { 68 // When called on the main UI thread, we must not block. Since calculating the number of 69 // enterprise-installed apps takes a bit of time, we always return true here and 70 // determine the pref's actual visibility asynchronously in updateState(). 71 return true; 72 } 73 74 // When called by the search indexer, we are on a background thread that we can block. Also, 75 // changes to the pref's visibility made in updateState() would not be seen by the indexer. 76 // We block and return synchronously whether there are enterprise-installed apps or not. 77 final Boolean[] haveEnterpriseInstalledPackages = {null}; 78 mFeatureProvider.calculateNumberOfPolicyInstalledApps(false /* async */, 79 (num) -> haveEnterpriseInstalledPackages[0] = num > 0); 80 return haveEnterpriseInstalledPackages[0]; 81 } 82 83 @Override getPreferenceKey()84 public String getPreferenceKey() { 85 return KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES; 86 } 87 } 88