• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PreferenceControllerMixin;
22 import com.android.settings.overlay.FeatureFactory;
23 import com.android.settingslib.core.AbstractPreferenceController;
24 
25 public class EnterpriseInstalledPackagesPreferenceController
26         extends AbstractPreferenceController implements PreferenceControllerMixin {
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 
EnterpriseInstalledPackagesPreferenceController(Context context, boolean async)33     public EnterpriseInstalledPackagesPreferenceController(Context context, boolean async) {
34         super(context);
35         mFeatureProvider = FeatureFactory.getFactory(context)
36                 .getApplicationFeatureProvider(context);
37         mAsync = async;
38     }
39 
40     @Override
updateState(Preference preference)41     public void updateState(Preference preference) {
42         mFeatureProvider.calculateNumberOfPolicyInstalledApps(true /* async */,
43                 (num) -> {
44                     final boolean available;
45                     if (num == 0) {
46                         available = false;
47                     } else {
48                         available = true;
49                         preference.setSummary(mContext.getResources().getQuantityString(
50                                 R.plurals.enterprise_privacy_number_packages_lower_bound, num,
51                                 num));
52 
53                     }
54                     preference.setVisible(available);
55                 });
56     }
57 
58     @Override
isAvailable()59     public boolean isAvailable() {
60         if (mAsync) {
61             // When called on the main UI thread, we must not block. Since calculating the number of
62             // enterprise-installed apps takes a bit of time, we always return true here and
63             // determine the pref's actual visibility asynchronously in updateState().
64             return true;
65         }
66 
67         // When called by the search indexer, we are on a background thread that we can block. Also,
68         // changes to the pref's visibility made in updateState() would not be seen by the indexer.
69         // We block and return synchronously whether there are enterprise-installed apps or not.
70         final Boolean[] haveEnterpriseInstalledPackages = { null };
71         mFeatureProvider.calculateNumberOfPolicyInstalledApps(false /* async */,
72                 (num) -> haveEnterpriseInstalledPackages[0] = num > 0);
73         return haveEnterpriseInstalledPackages[0];
74 
75     }
76 
77     @Override
getPreferenceKey()78     public String getPreferenceKey() {
79         return KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES;
80     }
81 }
82