1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.applications; 15 16 import android.content.Context; 17 import android.content.pm.PackageInfo; 18 import android.content.pm.PackageManager; 19 import android.content.pm.PackageManager.NameNotFoundException; 20 import android.content.pm.PermissionGroupInfo; 21 import android.content.pm.PermissionInfo; 22 import android.support.v7.preference.Preference; 23 import android.text.TextUtils; 24 import android.util.ArraySet; 25 import android.util.Log; 26 import com.android.settings.R; 27 import com.android.settings.core.PreferenceControllerMixin; 28 import com.android.settingslib.core.AbstractPreferenceController; 29 30 import java.util.List; 31 import java.util.Set; 32 33 public class AppPermissionsPreferenceController extends AbstractPreferenceController 34 implements PreferenceControllerMixin { 35 36 private static final String TAG = "AppPermissionPrefCtrl"; 37 private static final String KEY_APP_PERMISSION_GROUPS = "manage_perms"; 38 private static final String[] PERMISSION_GROUPS = new String[] { 39 "android.permission-group.LOCATION", 40 "android.permission-group.MICROPHONE", 41 "android.permission-group.CAMERA", 42 "android.permission-group.SMS", 43 "android.permission-group.CONTACTS", 44 "android.permission-group.PHONE"}; 45 46 private static final int NUM_PERMISSION_TO_USE = 3; 47 48 private final PackageManager mPackageManager; 49 AppPermissionsPreferenceController(Context context)50 public AppPermissionsPreferenceController(Context context) { 51 super(context); 52 mPackageManager = context.getPackageManager(); 53 } 54 55 @Override isAvailable()56 public boolean isAvailable() { 57 return true; 58 } 59 60 @Override getPreferenceKey()61 public String getPreferenceKey() { 62 return KEY_APP_PERMISSION_GROUPS; 63 } 64 65 @Override updateState(Preference preference)66 public void updateState(Preference preference) { 67 final String summary = getSummary(); 68 if (summary != null) { 69 preference.setSummary(summary); 70 } 71 } 72 73 /* 74 Summary text looks like: Apps using Permission1, Permission2, Permission3 75 The 3 permissions are the first three from the list which any app has granted: 76 Location, Microphone, Camera, Sms, Contacts, and Phone 77 */ getSummary()78 private String getSummary() { 79 final Set<String> permissions = getAllPermissionsInGroups(); 80 Set<String> grantedPermissionGroups = getGrantedPermissionGroups(permissions); 81 CharSequence summary = null; 82 int count = 0; 83 for (String group : PERMISSION_GROUPS) { 84 if (!grantedPermissionGroups.contains(group)) { 85 continue; 86 } 87 summary = concatSummaryText(summary, group); 88 if (++count >= NUM_PERMISSION_TO_USE) { 89 break; 90 } 91 } 92 return count > 0 ? mContext.getString(R.string.app_permissions_summary, summary) : null; 93 } 94 getGrantedPermissionGroups(Set<String> permissions)95 private Set<String> getGrantedPermissionGroups(Set<String> permissions) { 96 ArraySet<String> grantedPermissionGroups = new ArraySet<>(); 97 List<PackageInfo> installedPackages = 98 mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS); 99 for (PackageInfo installedPackage : installedPackages) { 100 if (installedPackage.permissions == null) { 101 continue; 102 } 103 for (PermissionInfo permissionInfo : installedPackage.permissions) { 104 if (permissions.contains(permissionInfo.name) 105 && !grantedPermissionGroups.contains(permissionInfo.group)) { 106 grantedPermissionGroups.add(permissionInfo.group); 107 } 108 } 109 } 110 return grantedPermissionGroups; 111 } 112 concatSummaryText(CharSequence currentSummary, String permission)113 private CharSequence concatSummaryText(CharSequence currentSummary, String permission) { 114 final CharSequence label = getPermissionGroupLabel(permission); 115 if (TextUtils.isEmpty(currentSummary)) { 116 return label; 117 } 118 return mContext.getString(R.string.join_many_items_middle, currentSummary, label); 119 } 120 getPermissionGroupLabel(String group)121 private CharSequence getPermissionGroupLabel(String group) { 122 try { 123 final PermissionGroupInfo groupInfo = mPackageManager.getPermissionGroupInfo(group, 0); 124 return groupInfo.loadLabel(mPackageManager); 125 } catch (NameNotFoundException e) { 126 Log.e(TAG, "Error getting permissions label.", e); 127 } 128 return group; 129 } 130 getAllPermissionsInGroups()131 private Set<String> getAllPermissionsInGroups() { 132 ArraySet<String> result = new ArraySet<>(); 133 for (String group : PERMISSION_GROUPS) { 134 try { 135 final List<PermissionInfo> permissions = 136 mPackageManager.queryPermissionsByGroup(group, 0); 137 for (PermissionInfo permissionInfo : permissions) { 138 result.add(permissionInfo.name); 139 } 140 } catch (NameNotFoundException e) { 141 Log.e(TAG, "Error getting permissions in group "+group, e); 142 } 143 } 144 return result; 145 } 146 } 147