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