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