• 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 
20 import androidx.preference.Preference;
21 
22 import com.android.settings.R;
23 import com.android.settings.applications.ApplicationFeatureProvider;
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settings.overlay.FeatureFactory;
26 import com.android.settingslib.core.AbstractPreferenceController;
27 
28 public abstract class AdminGrantedPermissionsPreferenceControllerBase
29         extends AbstractPreferenceController implements PreferenceControllerMixin {
30 
31     private final String[] mPermissions;
32     private final ApplicationFeatureProvider mFeatureProvider;
33     private final boolean mAsync;
34     private boolean mHasApps;
35 
AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async, String[] permissions)36     public AdminGrantedPermissionsPreferenceControllerBase(Context context, boolean async,
37             String[] permissions) {
38         super(context);
39         mPermissions = permissions;
40         mFeatureProvider = FeatureFactory.getFactory(context)
41                 .getApplicationFeatureProvider(context);
42         mAsync = async;
43         mHasApps = false;
44     }
45 
46     @Override
updateState(Preference preference)47     public void updateState(Preference preference) {
48         mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions,
49                 true /* async */,
50                 (num) -> {
51                     if (num == 0) {
52                         mHasApps = false;
53                     } else {
54                         preference.setSummary(mContext.getResources().getQuantityString(
55                                 R.plurals.enterprise_privacy_number_packages_lower_bound,
56                                 num, num));
57                         mHasApps = true;
58                     }
59                     preference.setVisible(mHasApps);
60                 });
61     }
62 
63     @Override
isAvailable()64     public boolean isAvailable() {
65         if (mAsync) {
66             // When called on the main UI thread, we must not block. Since calculating the number of
67             // apps that the admin has granted a given permissions takes a bit of time, we always
68             // return true here and determine the pref's actual visibility asynchronously in
69             // updateState().
70             return true;
71         }
72 
73         // When called by the search indexer, we are on a background thread that we can block. Also,
74         // changes to the pref's visibility made in updateState() would not be seen by the indexer.
75         // We block and return synchronously whether the admin has granted the given permissions to
76         // any apps or not.
77         final Boolean[] haveAppsWithAdminGrantedPermissions = { null };
78         mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions,
79                 false /* async */, (num) -> haveAppsWithAdminGrantedPermissions[0] = num > 0);
80         mHasApps = haveAppsWithAdminGrantedPermissions[0];
81         return mHasApps;
82     }
83 
84     @Override
handlePreferenceTreeClick(Preference preference)85     public boolean handlePreferenceTreeClick(Preference preference) {
86         if (!getPreferenceKey().equals(preference.getKey())) {
87             return false;
88         }
89         if (!mHasApps) {
90             return false;
91         }
92         return super.handlePreferenceTreeClick(preference);
93     }
94 }
95