• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
6  * except in compliance with the License. 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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package com.android.settings.enterprise;
17 
18 import android.content.Context;
19 import android.support.v7.preference.Preference;
20 
21 import com.android.settings.R;
22 import com.android.settings.applications.ApplicationFeatureProvider;
23 import com.android.settings.core.PreferenceControllerMixin;
24 import com.android.settings.overlay.FeatureFactory;
25 import com.android.settingslib.core.AbstractPreferenceController;
26 
27 public abstract class AdminGrantedPermissionsPreferenceControllerBase
28         extends AbstractPreferenceController implements PreferenceControllerMixin {
29 
30     private final String[] mPermissions;
31     private final ApplicationFeatureProvider mFeatureProvider;
32     private final boolean mAsync;
33     private boolean mHasApps;
34 
AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, String[] permissions)35     public AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async,
36             String[] permissions) {
37         super(context);
38         mPermissions = permissions;
39         mFeatureProvider = FeatureFactory.getFactory(context)
40                 .getApplicationFeatureProvider(context);
41         mAsync = async;
42         mHasApps = false;
43     }
44 
45     @Override
updateState(Preference preference)46     public void updateState(Preference preference) {
47         mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions,
48                 true /* async */,
49                 (num) -> {
50                     if (num == 0) {
51                         mHasApps = false;
52                     } else {
53                         preference.setSummary(mContext.getResources().getQuantityString(
54                                 R.plurals.enterprise_privacy_number_packages_lower_bound,
55                                 num, num));
56                         mHasApps = true;
57                     }
58                     preference.setVisible(mHasApps);
59                 });
60     }
61 
62     @Override
isAvailable()63     public boolean isAvailable() {
64         if (mAsync) {
65             // When called on the main UI thread, we must not block. Since calculating the number of
66             // apps that the admin has granted a given permissions takes a bit of time, we always
67             // return true here and determine the pref's actual visibility asynchronously in
68             // updateState().
69             return true;
70         }
71 
72         // When called by the search indexer, we are on a background thread that we can block. Also,
73         // changes to the pref's visibility made in updateState() would not be seen by the indexer.
74         // We block and return synchronously whether the admin has granted the given permissions to
75         // any apps or not.
76         final Boolean[] haveAppsWithAdminGrantedPermissions = { null };
77         mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions,
78                 false /* async */, (num) -> haveAppsWithAdminGrantedPermissions[0] = num > 0);
79         mHasApps = haveAppsWithAdminGrantedPermissions[0];
80         return mHasApps;
81     }
82 
83     @Override
handlePreferenceTreeClick(Preference preference)84     public boolean handlePreferenceTreeClick(Preference preference) {
85         if (!getPreferenceKey().equals(preference.getKey())) {
86             return false;
87         }
88         if (!mHasApps) {
89             return false;
90         }
91         return super.handlePreferenceTreeClick(preference);
92     }
93 }
94