1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settingslib.applications;
17 
18 import android.content.Context;
19 import android.permission.PermissionControllerManager;
20 import android.permission.RuntimePermissionPresentationInfo;
21 
22 import java.text.Collator;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 
27 public class PermissionsSummaryHelper  {
28 
getPermissionSummary(Context context, String pkg, final PermissionsResultCallback callback)29     public static void getPermissionSummary(Context context, String pkg,
30             final PermissionsResultCallback callback) {
31         final PermissionControllerManager permController =
32                 context.getSystemService(PermissionControllerManager.class);
33         permController.getAppPermissions(pkg, permissions -> {
34             final int permissionCount = permissions.size();
35 
36             int grantedStandardCount = 0;
37             int grantedAdditionalCount = 0;
38             int requestedCount = 0;
39             List<CharSequence> grantedStandardLabels = new ArrayList<>();
40 
41             for (int i = 0; i < permissionCount; i++) {
42                 RuntimePermissionPresentationInfo permission = permissions.get(i);
43                 requestedCount++;
44                 if (permission.isGranted()) {
45                     if (permission.isStandard()) {
46                         grantedStandardLabels.add(permission.getLabel());
47                         grantedStandardCount++;
48                     } else {
49                         grantedAdditionalCount++;
50                     }
51                 }
52             }
53 
54             Collator collator = Collator.getInstance();
55             collator.setStrength(Collator.PRIMARY);
56             Collections.sort(grantedStandardLabels, collator);
57 
58             callback.onPermissionSummaryResult(grantedStandardCount, requestedCount,
59                     grantedAdditionalCount, grantedStandardLabels);
60         }, null);
61     }
62 
63     public static abstract class PermissionsResultCallback {
onAppWithPermissionsCountsResult(int standardGrantedPermissionAppCount, int standardUsedPermissionAppCount)64         public void onAppWithPermissionsCountsResult(int standardGrantedPermissionAppCount,
65                 int standardUsedPermissionAppCount) {
66             /* do nothing - stub */
67         }
68 
onPermissionSummaryResult(int standardGrantedPermissionCount, int requestedPermissionCount, int additionalGrantedPermissionCount, List<CharSequence> grantedGroupLabels)69         public void onPermissionSummaryResult(int standardGrantedPermissionCount,
70                 int requestedPermissionCount, int additionalGrantedPermissionCount,
71                 List<CharSequence> grantedGroupLabels) {
72             /* do nothing - stub */
73         }
74     }
75 }
76