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.List;
25 
26 /**
27  * Helper to get the runtime permissions for an app.
28  */
29 public class PermissionsSummaryHelper {
30 
getPermissionSummary(Context context, String pkg, final PermissionsResultCallback callback)31     public static void getPermissionSummary(Context context, String pkg,
32             final PermissionsResultCallback callback) {
33         final PermissionControllerManager permController =
34                 context.getSystemService(PermissionControllerManager.class);
35         permController.getAppPermissions(pkg, permissions -> {
36 
37             int grantedAdditionalCount = 0;
38             int requestedCount = 0;
39             List<CharSequence> grantedStandardLabels = new ArrayList<>();
40 
41             for (RuntimePermissionPresentationInfo permission : permissions) {
42                 requestedCount++;
43                 if (permission.isGranted()) {
44                     if (permission.isStandard()) {
45                         grantedStandardLabels.add(permission.getLabel());
46                     } else {
47                         grantedAdditionalCount++;
48                     }
49                 }
50             }
51 
52             Collator collator = Collator.getInstance();
53             collator.setStrength(Collator.PRIMARY);
54             grantedStandardLabels.sort(collator);
55 
56             callback.onPermissionSummaryResult(
57                     requestedCount, grantedAdditionalCount, grantedStandardLabels);
58         }, null);
59     }
60 
61     /**
62      * Callback for the runtime permissions result for an app.
63      */
64     public interface PermissionsResultCallback {
65 
66         /** The runtime permission summary result for an app. */
onPermissionSummaryResult( int requestedPermissionCount, int additionalGrantedPermissionCount, List<CharSequence> grantedGroupLabels)67         void onPermissionSummaryResult(
68                 int requestedPermissionCount, int additionalGrantedPermissionCount,
69                 List<CharSequence> grantedGroupLabels);
70     }
71 }
72