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.annotation.NonNull;
19 import android.content.Context;
20 import android.content.pm.permission.RuntimePermissionPresentationInfo;
21 import android.content.pm.permission.RuntimePermissionPresenter;
22 
23 import java.text.Collator;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 
28 public class PermissionsSummaryHelper  {
29 
getPermissionSummary(Context context, String pkg, final PermissionsResultCallback callback)30     public static void getPermissionSummary(Context context, String pkg,
31             final PermissionsResultCallback callback) {
32         final RuntimePermissionPresenter presenter =
33                 RuntimePermissionPresenter.getInstance(context);
34         presenter.getAppPermissions(pkg, new RuntimePermissionPresenter.OnResultCallback() {
35             @Override
36             public void onGetAppPermissions(
37                     @NonNull List<RuntimePermissionPresentationInfo> permissions) {
38                 final int permissionCount = permissions.size();
39 
40                 int grantedStandardCount = 0;
41                 int grantedAdditionalCount = 0;
42                 int requestedCount = 0;
43                 List<CharSequence> grantedStandardLabels = new ArrayList<>();
44 
45                 for (int i = 0; i < permissionCount; i++) {
46                     RuntimePermissionPresentationInfo permission = permissions.get(i);
47                     requestedCount++;
48                     if (permission.isGranted()) {
49                         if (permission.isStandard()) {
50                             grantedStandardLabels.add(permission.getLabel());
51                             grantedStandardCount++;
52                         } else {
53                             grantedAdditionalCount++;
54                         }
55                     }
56                 }
57 
58                 Collator collator = Collator.getInstance();
59                 collator.setStrength(Collator.PRIMARY);
60                 Collections.sort(grantedStandardLabels, collator);
61 
62                 callback.onPermissionSummaryResult(grantedStandardCount, requestedCount,
63                         grantedAdditionalCount, grantedStandardLabels);
64             }
65         }, null);
66     }
67 
68     public static abstract class PermissionsResultCallback {
onAppWithPermissionsCountsResult(int standardGrantedPermissionAppCount, int standardUsedPermissionAppCount)69         public void onAppWithPermissionsCountsResult(int standardGrantedPermissionAppCount,
70                 int standardUsedPermissionAppCount) {
71             /* do nothing - stub */
72         }
73 
onPermissionSummaryResult(int standardGrantedPermissionCount, int requestedPermissionCount, int additionalGrantedPermissionCount, List<CharSequence> grantedGroupLabels)74         public void onPermissionSummaryResult(int standardGrantedPermissionCount,
75                 int requestedPermissionCount, int additionalGrantedPermissionCount,
76                 List<CharSequence> grantedGroupLabels) {
77             /* do nothing - stub */
78         }
79     }
80 }
81