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 import com.android.settingslib.utils.StringUtil;
26 
27 public class EnterpriseInstalledPackagesPreferenceController
28         extends AbstractPreferenceController implements PreferenceControllerMixin {
29 
30     private static final String KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES
31             = "number_enterprise_installed_packages";
32     private final ApplicationFeatureProvider mFeatureProvider;
33     private final boolean mAsync;
34 
EnterpriseInstalledPackagesPreferenceController(Context context, boolean async)35     public EnterpriseInstalledPackagesPreferenceController(Context context, boolean async) {
36         super(context);
37         mFeatureProvider = FeatureFactory.getFeatureFactory()
38                 .getApplicationFeatureProvider();
39         mAsync = async;
40     }
41 
42     @Override
updateState(Preference preference)43     public void updateState(Preference preference) {
44         mFeatureProvider.calculateNumberOfPolicyInstalledApps(true /* async */,
45                 (num) -> {
46                     final boolean available;
47                     if (num == 0) {
48                         available = false;
49                     } else {
50                         available = true;
51                         preference.setSummary(StringUtil.getIcuPluralsString(mContext, num,
52                                 R.string.enterprise_privacy_number_packages_lower_bound));
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