blob: e6f948f2d96926d8b67569702c0ce460210f44af [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17package android.widget;
18
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070019import android.app.AlertDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Nick Kralevichddfbe002013-04-05 18:32:07 -070021import android.content.DialogInterface;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070022import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.pm.PackageInfo;
Philip P. Moltmann004e4892018-06-14 11:52:14 -070024import android.content.pm.PackageItemInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.pm.PackageManager;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070026import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.pm.PermissionGroupInfo;
28import android.content.pm.PermissionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.drawable.Drawable;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070030import android.os.Parcel;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070031import android.os.UserHandle;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070032import android.text.SpannableStringBuilder;
33import android.text.TextUtils;
34import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.Log;
36import android.view.LayoutInflater;
37import android.view.View;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070038import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Aurimas Liutikas99441c52016-10-11 16:48:32 -070040import com.android.internal.R;
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.text.Collator;
43import java.util.ArrayList;
44import java.util.Collections;
45import java.util.Comparator;
46import java.util.HashMap;
47import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.util.List;
49import java.util.Map;
50import java.util.Set;
51
52/**
53 * This class contains the SecurityPermissions view implementation.
54 * Initially the package's advanced or dangerous security permissions
55 * are displayed under categorized
56 * groups. Clicking on the additional permissions presents
57 * extended information consisting of all groups and permissions.
58 * To use this view define a LinearLayout or any ViewGroup and add this
59 * view by instantiating AppSecurityPermissions and invoking getPermissionsView.
Aurimas Liutikas99441c52016-10-11 16:48:32 -070060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * {@hide}
62 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070063public class AppSecurityPermissions {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070065 public static final int WHICH_NEW = 1<<2;
66 public static final int WHICH_ALL = 0xffff;
Dianne Hackborn0e128bb2012-05-01 14:40:15 -070067
68 private final static String TAG = "AppSecurityPermissions";
Dianne Hackborn2ca2c872012-09-16 16:03:36 -070069 private final static boolean localLOGV = false;
Nick Kralevich10ac1d12013-03-24 14:27:00 -070070 private final Context mContext;
71 private final LayoutInflater mInflater;
72 private final PackageManager mPm;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070073 private final Map<String, MyPermissionGroupInfo> mPermGroups
74 = new HashMap<String, MyPermissionGroupInfo>();
75 private final List<MyPermissionGroupInfo> mPermGroupsList
76 = new ArrayList<MyPermissionGroupInfo>();
Julia Reynolds9a5c9112015-07-09 15:23:42 -040077 private final PermissionGroupInfoComparator mPermGroupComparator =
78 new PermissionGroupInfoComparator();
Nick Kralevich10ac1d12013-03-24 14:27:00 -070079 private final PermissionInfoComparator mPermComparator = new PermissionInfoComparator();
80 private final List<MyPermissionInfo> mPermsList = new ArrayList<MyPermissionInfo>();
81 private final CharSequence mNewPermPrefix;
Nick Kralevichddfbe002013-04-05 18:32:07 -070082 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070083
Svet Ganovae0e03a2016-02-25 18:22:10 -080084 /** @hide */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070085 static class MyPermissionGroupInfo extends PermissionGroupInfo {
86 CharSequence mLabel;
87
88 final ArrayList<MyPermissionInfo> mNewPermissions = new ArrayList<MyPermissionInfo>();
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070089 final ArrayList<MyPermissionInfo> mAllPermissions = new ArrayList<MyPermissionInfo>();
90
91 MyPermissionGroupInfo(PermissionInfo perm) {
92 name = perm.packageName;
93 packageName = perm.packageName;
94 }
95
96 MyPermissionGroupInfo(PermissionGroupInfo info) {
97 super(info);
98 }
99
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400100 public Drawable loadGroupIcon(Context context, PackageManager pm) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700101 if (icon != 0) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100102 return loadUnbadgedIcon(pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700103 } else {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400104 return context.getDrawable(R.drawable.ic_perm_device_info);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700105 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700106 }
107 }
108
Svet Ganovae0e03a2016-02-25 18:22:10 -0800109 /** @hide */
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700110 private static class MyPermissionInfo extends PermissionInfo {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700111 CharSequence mLabel;
112
113 /**
114 * PackageInfo.requestedPermissionsFlags for the new package being installed.
115 */
116 int mNewReqFlags;
117
118 /**
119 * PackageInfo.requestedPermissionsFlags for the currently installed
120 * package, if it is installed.
121 */
122 int mExistingReqFlags;
123
124 /**
125 * True if this should be considered a new permission.
126 */
127 boolean mNew;
128
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700129 MyPermissionInfo(PermissionInfo info) {
130 super(info);
131 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700132 }
133
Svet Ganovae0e03a2016-02-25 18:22:10 -0800134 /** @hide */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700135 public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
136 MyPermissionGroupInfo mGroup;
137 MyPermissionInfo mPerm;
138 AlertDialog mDialog;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700139 private boolean mShowRevokeUI = false;
140 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700141
142 public PermissionItemView(Context context, AttributeSet attrs) {
143 super(context, attrs);
144 setClickable(true);
145 }
146
147 public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700148 boolean first, CharSequence newPermPrefix, String packageName,
149 boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700150 mGroup = grp;
151 mPerm = perm;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700152 mShowRevokeUI = showRevokeUI;
153 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700154
Alan Viverette51efddb2017-04-05 10:00:01 -0400155 ImageView permGrpIcon = findViewById(R.id.perm_icon);
156 TextView permNameView = findViewById(R.id.perm_name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700157
158 PackageManager pm = getContext().getPackageManager();
159 Drawable icon = null;
160 if (first) {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400161 icon = grp.loadGroupIcon(getContext(), pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700162 }
163 CharSequence label = perm.mLabel;
164 if (perm.mNew && newPermPrefix != null) {
165 // If this is a new permission, format it appropriately.
166 SpannableStringBuilder builder = new SpannableStringBuilder();
167 Parcel parcel = Parcel.obtain();
168 TextUtils.writeToParcel(newPermPrefix, parcel, 0);
169 parcel.setDataPosition(0);
170 CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
171 parcel.recycle();
172 builder.append(newStr);
173 builder.append(label);
174 label = builder;
175 }
176
177 permGrpIcon.setImageDrawable(icon);
178 permNameView.setText(label);
179 setOnClickListener(this);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700180 if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
181 + ": " + label + " in group " + grp.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700182 }
183
184 @Override
185 public void onClick(View v) {
186 if (mGroup != null && mPerm != null) {
187 if (mDialog != null) {
188 mDialog.dismiss();
189 }
190 PackageManager pm = getContext().getPackageManager();
191 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
192 builder.setTitle(mGroup.mLabel);
193 if (mPerm.descriptionRes != 0) {
194 builder.setMessage(mPerm.loadDescription(pm));
195 } else {
196 CharSequence appName;
197 try {
198 ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
199 appName = app.loadLabel(pm);
200 } catch (NameNotFoundException e) {
201 appName = mPerm.packageName;
202 }
203 StringBuilder sbuilder = new StringBuilder(128);
204 sbuilder.append(getContext().getString(
205 R.string.perms_description_app, appName));
206 sbuilder.append("\n\n");
207 sbuilder.append(mPerm.name);
208 builder.setMessage(sbuilder.toString());
209 }
210 builder.setCancelable(true);
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400211 builder.setIcon(mGroup.loadGroupIcon(getContext(), pm));
Nick Kralevichddfbe002013-04-05 18:32:07 -0700212 addRevokeUIIfNecessary(builder);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700213 mDialog = builder.show();
214 mDialog.setCanceledOnTouchOutside(true);
215 }
216 }
217
218 @Override
219 protected void onDetachedFromWindow() {
220 super.onDetachedFromWindow();
221 if (mDialog != null) {
222 mDialog.dismiss();
223 }
224 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700225
226 private void addRevokeUIIfNecessary(AlertDialog.Builder builder) {
227 if (!mShowRevokeUI) {
228 return;
229 }
230
231 final boolean isRequired =
232 ((mPerm.mExistingReqFlags & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
233
234 if (isRequired) {
235 return;
236 }
237
238 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {
239 @Override
240 public void onClick(DialogInterface dialog, int which) {
241 PackageManager pm = getContext().getPackageManager();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700242 pm.revokeRuntimePermission(mPackageName, mPerm.name,
Svetoslavc6d1c342015-02-26 14:44:43 -0800243 new UserHandle(mContext.getUserId()));
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700244 PermissionItemView.this.setVisibility(View.GONE);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700245 }
246 };
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700247 builder.setNegativeButton(R.string.revoke, ocl);
248 builder.setPositiveButton(R.string.ok, null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700249 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700250 }
251
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700252 private AppSecurityPermissions(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mContext = context;
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700254 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 mPm = mContext.getPackageManager();
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700256 // Pick up from framework resources instead.
257 mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 public AppSecurityPermissions(Context context, String packageName) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700261 this(context);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700262 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700263 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 PackageInfo pkgInfo;
265 try {
266 pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
267 } catch (NameNotFoundException e) {
Dianne Hackborn52c62342012-09-21 10:57:45 -0700268 Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 return;
270 }
271 // Extract all user permissions
272 if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
273 getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
274 }
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700275 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700276 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700278
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700279 public AppSecurityPermissions(Context context, PackageInfo info) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700280 this(context);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700281 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700282 if(info == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 return;
284 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700285 mPackageName = info.packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700286
287 // Convert to a PackageInfo
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700288 PackageInfo installedPkgInfo = null;
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700289 // Get requested permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700290 if (info.requestedPermissions != null) {
291 try {
292 installedPkgInfo = mPm.getPackageInfo(info.packageName,
293 PackageManager.GET_PERMISSIONS);
294 } catch (NameNotFoundException e) {
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700295 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700296 extractPerms(info, permSet, installedPkgInfo);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700297 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800298 // Get permissions related to shared user if any
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700299 if (info.sharedUserId != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 int sharedUid;
301 try {
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700302 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700303 getAllUsedPermissions(sharedUid, permSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 } catch (NameNotFoundException e) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700305 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700308 // Retrieve list of permissions
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700309 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700310 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700312
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800313 /**
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700314 * Utility to retrieve a view displaying a single permission. This provides
315 * the old UI layout for permissions; it is only here for the device admin
316 * settings to continue to use.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800317 */
318 public static View getPermissionItemView(Context context,
319 CharSequence grpName, CharSequence description, boolean dangerous) {
320 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
321 Context.LAYOUT_INFLATER_SERVICE);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800322 Drawable icon = context.getDrawable(dangerous
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800323 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700324 return getPermissionItemViewOld(context, inflater, grpName,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800325 description, dangerous, icon);
326 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700327
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700328 private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 String sharedPkgList[] = mPm.getPackagesForUid(sharedUid);
330 if(sharedPkgList == null || (sharedPkgList.length == 0)) {
331 return;
332 }
333 for(String sharedPkg : sharedPkgList) {
334 getPermissionsForPackage(sharedPkg, permSet);
335 }
336 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700337
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700338 private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 try {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700340 PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700341 extractPerms(pkgInfo, permSet, pkgInfo);
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700342 } catch (NameNotFoundException e) {
343 Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700346
347 private void extractPerms(PackageInfo info, Set<MyPermissionInfo> permSet,
348 PackageInfo installedPkgInfo) {
349 String[] strList = info.requestedPermissions;
350 int[] flagsList = info.requestedPermissionsFlags;
351 if ((strList == null) || (strList.length == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return;
353 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700354 for (int i=0; i<strList.length; i++) {
355 String permName = strList[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 try {
357 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700358 if (tmpPermInfo == null) {
359 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700361 int existingIndex = -1;
362 if (installedPkgInfo != null
363 && installedPkgInfo.requestedPermissions != null) {
364 for (int j=0; j<installedPkgInfo.requestedPermissions.length; j++) {
365 if (permName.equals(installedPkgInfo.requestedPermissions[j])) {
366 existingIndex = j;
367 break;
368 }
369 }
370 }
371 final int existingFlags = existingIndex >= 0 ?
372 installedPkgInfo.requestedPermissionsFlags[existingIndex] : 0;
373 if (!isDisplayablePermission(tmpPermInfo, flagsList[i], existingFlags)) {
374 // This is not a permission that is interesting for the user
375 // to see, so skip it.
376 continue;
377 }
378 final String origGroupName = tmpPermInfo.group;
379 String groupName = origGroupName;
380 if (groupName == null) {
381 groupName = tmpPermInfo.packageName;
382 tmpPermInfo.group = groupName;
383 }
384 MyPermissionGroupInfo group = mPermGroups.get(groupName);
385 if (group == null) {
386 PermissionGroupInfo grp = null;
387 if (origGroupName != null) {
388 grp = mPm.getPermissionGroupInfo(origGroupName, 0);
389 }
390 if (grp != null) {
391 group = new MyPermissionGroupInfo(grp);
392 } else {
393 // We could be here either because the permission
394 // didn't originally specify a group or the group it
395 // gave couldn't be found. In either case, we consider
396 // its group to be the permission's package name.
397 tmpPermInfo.group = tmpPermInfo.packageName;
398 group = mPermGroups.get(tmpPermInfo.group);
399 if (group == null) {
400 group = new MyPermissionGroupInfo(tmpPermInfo);
401 }
402 group = new MyPermissionGroupInfo(tmpPermInfo);
403 }
404 mPermGroups.put(tmpPermInfo.group, group);
405 }
406 final boolean newPerm = installedPkgInfo != null
407 && (existingFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0;
408 MyPermissionInfo myPerm = new MyPermissionInfo(tmpPermInfo);
409 myPerm.mNewReqFlags = flagsList[i];
410 myPerm.mExistingReqFlags = existingFlags;
411 // This is a new permission if the app is already installed and
412 // doesn't currently hold this permission.
413 myPerm.mNew = newPerm;
414 permSet.add(myPerm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 } catch (NameNotFoundException e) {
416 Log.i(TAG, "Ignoring unknown permission:"+permName);
417 }
418 }
419 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public int getPermissionCount() {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700422 return getPermissionCount(WHICH_ALL);
423 }
424
425 private List<MyPermissionInfo> getPermissionList(MyPermissionGroupInfo grp, int which) {
426 if (which == WHICH_NEW) {
427 return grp.mNewPermissions;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700428 } else {
429 return grp.mAllPermissions;
430 }
431 }
432
433 public int getPermissionCount(int which) {
434 int N = 0;
435 for (int i=0; i<mPermGroupsList.size(); i++) {
436 N += getPermissionList(mPermGroupsList.get(i), which).size();
437 }
438 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
441 public View getPermissionsView() {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700442 return getPermissionsView(WHICH_ALL, false);
443 }
444
445 public View getPermissionsViewWithRevokeButtons() {
446 return getPermissionsView(WHICH_ALL, true);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700447 }
448
449 public View getPermissionsView(int which) {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700450 return getPermissionsView(which, false);
451 }
452
453 private View getPermissionsView(int which, boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700454 LinearLayout permsView = (LinearLayout) mInflater.inflate(R.layout.app_perms_summary, null);
Alan Viverette8e1a7292017-02-27 10:57:58 -0500455 LinearLayout displayList = permsView.findViewById(R.id.perms_list);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700456 View noPermsView = permsView.findViewById(R.id.no_permissions);
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700457
Nick Kralevichddfbe002013-04-05 18:32:07 -0700458 displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700459 if (displayList.getChildCount() <= 0) {
460 noPermsView.setVisibility(View.VISIBLE);
461 }
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700462
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700463 return permsView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
466 /**
467 * Utility method that displays permissions from a map containing group name and
468 * list of permission descriptions.
469 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700470 private void displayPermissions(List<MyPermissionGroupInfo> groups,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700471 LinearLayout permListView, int which, boolean showRevokeUI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 permListView.removeAllViews();
473
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700474 int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density);
475
476 for (int i=0; i<groups.size(); i++) {
477 MyPermissionGroupInfo grp = groups.get(i);
478 final List<MyPermissionInfo> perms = getPermissionList(grp, which);
479 for (int j=0; j<perms.size(); j++) {
480 MyPermissionInfo perm = perms.get(j);
481 View view = getPermissionItemView(grp, perm, j == 0,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700482 which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700483 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
484 ViewGroup.LayoutParams.MATCH_PARENT,
485 ViewGroup.LayoutParams.WRAP_CONTENT);
486 if (j == 0) {
487 lp.topMargin = spacing;
488 }
489 if (j == grp.mAllPermissions.size()-1) {
490 lp.bottomMargin = spacing;
491 }
492 if (permListView.getChildCount() == 0) {
493 lp.topMargin *= 2;
494 }
495 permListView.addView(view, lp);
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498 }
499
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700500 private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700501 MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
502 return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
503 mPackageName, showRevokeUI);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700506 private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
507 MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700508 CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
Nick Kralevicha56b78d2013-04-07 11:13:16 -0700509 PermissionItemView permView = (PermissionItemView)inflater.inflate(
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700510 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
511 ? R.layout.app_permission_item_money : R.layout.app_permission_item,
512 null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700513 permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700514 return permView;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800515 }
516
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700517 private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800518 CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700519 View permView = inflater.inflate(R.layout.app_permission_item_old, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
Alan Viverette8e1a7292017-02-27 10:57:58 -0500521 TextView permGrpView = permView.findViewById(R.id.permission_group);
522 TextView permDescView = permView.findViewById(R.id.permission_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523
524 ImageView imgView = (ImageView)permView.findViewById(R.id.perm_icon);
525 imgView.setImageDrawable(icon);
526 if(grpName != null) {
527 permGrpView.setText(grpName);
528 permDescView.setText(permList);
529 } else {
530 permGrpView.setText(permList);
531 permDescView.setVisibility(View.GONE);
532 }
533 return permView;
534 }
535
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700536 private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
537 int existingReqFlags) {
538 final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700539 final boolean isNormal = (base == PermissionInfo.PROTECTION_NORMAL);
Svetoslava3f68ef2015-07-22 17:02:46 -0700540
541 // We do not show normal permissions in the UI.
542 if (isNormal) {
543 return false;
544 }
545
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700546 final boolean isDangerous = (base == PermissionInfo.PROTECTION_DANGEROUS)
547 || ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_PRE23) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700548 final boolean isRequired =
549 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
550 final boolean isDevelopment =
551 ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0);
552 final boolean wasGranted =
553 ((existingReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700554 final boolean isGranted =
555 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700556
557 // Dangerous and normal permissions are always shown to the user if the permission
558 // is required, or it was previously granted
Svetoslava3f68ef2015-07-22 17:02:46 -0700559 if (isDangerous && (isRequired || wasGranted || isGranted)) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700560 return true;
Dianne Hackborne639da72012-02-21 15:11:13 -0800561 }
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700562
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700563 // Development permissions are only shown to the user if they are already
564 // granted to the app -- if we are installing an app and they are not
565 // already granted, they will not be granted as part of the install.
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700566 if (isDevelopment && wasGranted) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700567 if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
568 + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 return true;
570 }
571 return false;
572 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700573
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700574 private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700575 private final Collator sCollator = Collator.getInstance();
Julia Reynolds9a5c9112015-07-09 15:23:42 -0400576 @Override
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700577 public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700578 return sCollator.compare(a.mLabel, b.mLabel);
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700579 }
580 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700581
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700582 private static class PermissionInfoComparator implements Comparator<MyPermissionInfo> {
583 private final Collator sCollator = Collator.getInstance();
584 PermissionInfoComparator() {
585 }
586 public final int compare(MyPermissionInfo a, MyPermissionInfo b) {
587 return sCollator.compare(a.mLabel, b.mLabel);
588 }
589 }
590
591 private void addPermToList(List<MyPermissionInfo> permList,
592 MyPermissionInfo pInfo) {
593 if (pInfo.mLabel == null) {
Philip P. Moltmann004e4892018-06-14 11:52:14 -0700594 pInfo.mLabel = pInfo.loadSafeLabel(mPm, 20000, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
595 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700596 }
597 int idx = Collections.binarySearch(permList, pInfo, mPermComparator);
598 if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size());
599 if (idx < 0) {
600 idx = -idx-1;
601 permList.add(idx, pInfo);
602 }
603 }
604
605 private void setPermissions(List<MyPermissionInfo> permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if (permList != null) {
607 // First pass to group permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700608 for (MyPermissionInfo pInfo : permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700610 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
612 continue;
613 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700614 MyPermissionGroupInfo group = mPermGroups.get(pInfo.group);
615 if (group != null) {
Philip P. Moltmann004e4892018-06-14 11:52:14 -0700616 pInfo.mLabel = pInfo.loadSafeLabel(mPm, 20000,
617 PackageItemInfo.SAFE_LABEL_FLAG_TRIM
618 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700619 addPermToList(group.mAllPermissions, pInfo);
620 if (pInfo.mNew) {
621 addPermToList(group.mNewPermissions, pInfo);
622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 }
626
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700627 for (MyPermissionGroupInfo pgrp : mPermGroups.values()) {
628 if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
Philip P. Moltmann004e4892018-06-14 11:52:14 -0700629 pgrp.mLabel = pgrp.loadSafeLabel(mPm, 20000, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
630 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700631 } else {
632 ApplicationInfo app;
633 try {
634 app = mPm.getApplicationInfo(pgrp.packageName, 0);
Philip P. Moltmann004e4892018-06-14 11:52:14 -0700635 pgrp.mLabel = app.loadSafeLabel(mPm, 20000, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
636 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700637 } catch (NameNotFoundException e) {
Philip P. Moltmann004e4892018-06-14 11:52:14 -0700638 pgrp.mLabel = pgrp.loadSafeLabel(mPm, 20000,
639 PackageItemInfo.SAFE_LABEL_FLAG_TRIM
640 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700641 }
642 }
643 mPermGroupsList.add(pgrp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700645 Collections.sort(mPermGroupsList, mPermGroupComparator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647}