blob: 6df76fa2e09a7eeea59b91b21ff8ef3832bd2074 [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;
24import android.content.pm.PackageManager;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070025import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.pm.PermissionGroupInfo;
27import android.content.pm.PermissionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.drawable.Drawable;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070029import android.os.Parcel;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070030import android.os.UserHandle;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070031import android.text.SpannableStringBuilder;
32import android.text.TextUtils;
33import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.util.Log;
35import android.view.LayoutInflater;
36import android.view.View;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070037import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Aurimas Liutikas99441c52016-10-11 16:48:32 -070039import com.android.internal.R;
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.text.Collator;
42import java.util.ArrayList;
43import java.util.Collections;
44import java.util.Comparator;
45import java.util.HashMap;
46import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.util.List;
48import java.util.Map;
49import java.util.Set;
50
51/**
52 * This class contains the SecurityPermissions view implementation.
53 * Initially the package's advanced or dangerous security permissions
54 * are displayed under categorized
55 * groups. Clicking on the additional permissions presents
56 * extended information consisting of all groups and permissions.
57 * To use this view define a LinearLayout or any ViewGroup and add this
58 * view by instantiating AppSecurityPermissions and invoking getPermissionsView.
Aurimas Liutikas99441c52016-10-11 16:48:32 -070059 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 * {@hide}
61 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070062public class AppSecurityPermissions {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070064 public static final int WHICH_NEW = 1<<2;
65 public static final int WHICH_ALL = 0xffff;
Dianne Hackborn0e128bb2012-05-01 14:40:15 -070066
67 private final static String TAG = "AppSecurityPermissions";
Dianne Hackborn2ca2c872012-09-16 16:03:36 -070068 private final static boolean localLOGV = false;
Nick Kralevich10ac1d12013-03-24 14:27:00 -070069 private final Context mContext;
70 private final LayoutInflater mInflater;
71 private final PackageManager mPm;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070072 private final Map<String, MyPermissionGroupInfo> mPermGroups
73 = new HashMap<String, MyPermissionGroupInfo>();
74 private final List<MyPermissionGroupInfo> mPermGroupsList
75 = new ArrayList<MyPermissionGroupInfo>();
Julia Reynolds9a5c9112015-07-09 15:23:42 -040076 private final PermissionGroupInfoComparator mPermGroupComparator =
77 new PermissionGroupInfoComparator();
Nick Kralevich10ac1d12013-03-24 14:27:00 -070078 private final PermissionInfoComparator mPermComparator = new PermissionInfoComparator();
79 private final List<MyPermissionInfo> mPermsList = new ArrayList<MyPermissionInfo>();
80 private final CharSequence mNewPermPrefix;
Nick Kralevichddfbe002013-04-05 18:32:07 -070081 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070082
Svet Ganovae0e03a2016-02-25 18:22:10 -080083 /** @hide */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070084 static class MyPermissionGroupInfo extends PermissionGroupInfo {
85 CharSequence mLabel;
86
87 final ArrayList<MyPermissionInfo> mNewPermissions = new ArrayList<MyPermissionInfo>();
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070088 final ArrayList<MyPermissionInfo> mAllPermissions = new ArrayList<MyPermissionInfo>();
89
90 MyPermissionGroupInfo(PermissionInfo perm) {
91 name = perm.packageName;
92 packageName = perm.packageName;
93 }
94
95 MyPermissionGroupInfo(PermissionGroupInfo info) {
96 super(info);
97 }
98
Julia Reynoldsd1af4462015-07-08 11:12:28 -040099 public Drawable loadGroupIcon(Context context, PackageManager pm) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700100 if (icon != 0) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100101 return loadUnbadgedIcon(pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700102 } else {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400103 return context.getDrawable(R.drawable.ic_perm_device_info);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700104 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700105 }
106 }
107
Svet Ganovae0e03a2016-02-25 18:22:10 -0800108 /** @hide */
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700109 private static class MyPermissionInfo extends PermissionInfo {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700110 CharSequence mLabel;
111
112 /**
113 * PackageInfo.requestedPermissionsFlags for the new package being installed.
114 */
115 int mNewReqFlags;
116
117 /**
118 * PackageInfo.requestedPermissionsFlags for the currently installed
119 * package, if it is installed.
120 */
121 int mExistingReqFlags;
122
123 /**
124 * True if this should be considered a new permission.
125 */
126 boolean mNew;
127
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700128 MyPermissionInfo(PermissionInfo info) {
129 super(info);
130 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700131 }
132
Svet Ganovae0e03a2016-02-25 18:22:10 -0800133 /** @hide */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700134 public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
135 MyPermissionGroupInfo mGroup;
136 MyPermissionInfo mPerm;
137 AlertDialog mDialog;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700138 private boolean mShowRevokeUI = false;
139 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700140
141 public PermissionItemView(Context context, AttributeSet attrs) {
142 super(context, attrs);
143 setClickable(true);
144 }
145
146 public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700147 boolean first, CharSequence newPermPrefix, String packageName,
148 boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700149 mGroup = grp;
150 mPerm = perm;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700151 mShowRevokeUI = showRevokeUI;
152 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700153
Alan Viverette51efddb2017-04-05 10:00:01 -0400154 ImageView permGrpIcon = findViewById(R.id.perm_icon);
155 TextView permNameView = findViewById(R.id.perm_name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700156
157 PackageManager pm = getContext().getPackageManager();
158 Drawable icon = null;
159 if (first) {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400160 icon = grp.loadGroupIcon(getContext(), pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700161 }
162 CharSequence label = perm.mLabel;
163 if (perm.mNew && newPermPrefix != null) {
164 // If this is a new permission, format it appropriately.
165 SpannableStringBuilder builder = new SpannableStringBuilder();
166 Parcel parcel = Parcel.obtain();
167 TextUtils.writeToParcel(newPermPrefix, parcel, 0);
168 parcel.setDataPosition(0);
169 CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
170 parcel.recycle();
171 builder.append(newStr);
172 builder.append(label);
173 label = builder;
174 }
175
176 permGrpIcon.setImageDrawable(icon);
177 permNameView.setText(label);
178 setOnClickListener(this);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700179 if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
180 + ": " + label + " in group " + grp.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700181 }
182
183 @Override
184 public void onClick(View v) {
185 if (mGroup != null && mPerm != null) {
186 if (mDialog != null) {
187 mDialog.dismiss();
188 }
189 PackageManager pm = getContext().getPackageManager();
190 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
191 builder.setTitle(mGroup.mLabel);
192 if (mPerm.descriptionRes != 0) {
193 builder.setMessage(mPerm.loadDescription(pm));
194 } else {
195 CharSequence appName;
196 try {
197 ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
198 appName = app.loadLabel(pm);
199 } catch (NameNotFoundException e) {
200 appName = mPerm.packageName;
201 }
202 StringBuilder sbuilder = new StringBuilder(128);
203 sbuilder.append(getContext().getString(
204 R.string.perms_description_app, appName));
205 sbuilder.append("\n\n");
206 sbuilder.append(mPerm.name);
207 builder.setMessage(sbuilder.toString());
208 }
209 builder.setCancelable(true);
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400210 builder.setIcon(mGroup.loadGroupIcon(getContext(), pm));
Nick Kralevichddfbe002013-04-05 18:32:07 -0700211 addRevokeUIIfNecessary(builder);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700212 mDialog = builder.show();
213 mDialog.setCanceledOnTouchOutside(true);
214 }
215 }
216
217 @Override
218 protected void onDetachedFromWindow() {
219 super.onDetachedFromWindow();
220 if (mDialog != null) {
221 mDialog.dismiss();
222 }
223 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700224
225 private void addRevokeUIIfNecessary(AlertDialog.Builder builder) {
226 if (!mShowRevokeUI) {
227 return;
228 }
229
230 final boolean isRequired =
231 ((mPerm.mExistingReqFlags & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
232
233 if (isRequired) {
234 return;
235 }
236
237 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {
238 @Override
239 public void onClick(DialogInterface dialog, int which) {
240 PackageManager pm = getContext().getPackageManager();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700241 pm.revokeRuntimePermission(mPackageName, mPerm.name,
Svetoslavc6d1c342015-02-26 14:44:43 -0800242 new UserHandle(mContext.getUserId()));
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700243 PermissionItemView.this.setVisibility(View.GONE);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700244 }
245 };
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700246 builder.setNegativeButton(R.string.revoke, ocl);
247 builder.setPositiveButton(R.string.ok, null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700248 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700249 }
250
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700251 private AppSecurityPermissions(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 mContext = context;
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700253 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mPm = mContext.getPackageManager();
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700255 // Pick up from framework resources instead.
256 mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
257 }
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public AppSecurityPermissions(Context context, String packageName) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700260 this(context);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700261 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700262 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 PackageInfo pkgInfo;
264 try {
265 pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
266 } catch (NameNotFoundException e) {
Dianne Hackborn52c62342012-09-21 10:57:45 -0700267 Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 return;
269 }
270 // Extract all user permissions
271 if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
272 getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
273 }
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700274 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700275 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700277
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700278 public AppSecurityPermissions(Context context, PackageInfo info) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700279 this(context);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700280 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700281 if(info == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 return;
283 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700284 mPackageName = info.packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700285
286 // Convert to a PackageInfo
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700287 PackageInfo installedPkgInfo = null;
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700288 // Get requested permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700289 if (info.requestedPermissions != null) {
290 try {
291 installedPkgInfo = mPm.getPackageInfo(info.packageName,
292 PackageManager.GET_PERMISSIONS);
293 } catch (NameNotFoundException e) {
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700294 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700295 extractPerms(info, permSet, installedPkgInfo);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700296 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800297 // Get permissions related to shared user if any
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700298 if (info.sharedUserId != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 int sharedUid;
300 try {
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700301 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700302 getAllUsedPermissions(sharedUid, permSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 } catch (NameNotFoundException e) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700304 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700307 // Retrieve list of permissions
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700308 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700309 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700311
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800312 /**
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700313 * Utility to retrieve a view displaying a single permission. This provides
314 * the old UI layout for permissions; it is only here for the device admin
315 * settings to continue to use.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800316 */
317 public static View getPermissionItemView(Context context,
318 CharSequence grpName, CharSequence description, boolean dangerous) {
319 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
320 Context.LAYOUT_INFLATER_SERVICE);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800321 Drawable icon = context.getDrawable(dangerous
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800322 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700323 return getPermissionItemViewOld(context, inflater, grpName,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800324 description, dangerous, icon);
325 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700326
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700327 private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 String sharedPkgList[] = mPm.getPackagesForUid(sharedUid);
329 if(sharedPkgList == null || (sharedPkgList.length == 0)) {
330 return;
331 }
332 for(String sharedPkg : sharedPkgList) {
333 getPermissionsForPackage(sharedPkg, permSet);
334 }
335 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700336
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700337 private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 try {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700339 PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700340 extractPerms(pkgInfo, permSet, pkgInfo);
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700341 } catch (NameNotFoundException e) {
342 Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700345
346 private void extractPerms(PackageInfo info, Set<MyPermissionInfo> permSet,
347 PackageInfo installedPkgInfo) {
348 String[] strList = info.requestedPermissions;
349 int[] flagsList = info.requestedPermissionsFlags;
350 if ((strList == null) || (strList.length == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return;
352 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700353 for (int i=0; i<strList.length; i++) {
354 String permName = strList[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 try {
356 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700357 if (tmpPermInfo == null) {
358 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700360 int existingIndex = -1;
361 if (installedPkgInfo != null
362 && installedPkgInfo.requestedPermissions != null) {
363 for (int j=0; j<installedPkgInfo.requestedPermissions.length; j++) {
364 if (permName.equals(installedPkgInfo.requestedPermissions[j])) {
365 existingIndex = j;
366 break;
367 }
368 }
369 }
370 final int existingFlags = existingIndex >= 0 ?
371 installedPkgInfo.requestedPermissionsFlags[existingIndex] : 0;
372 if (!isDisplayablePermission(tmpPermInfo, flagsList[i], existingFlags)) {
373 // This is not a permission that is interesting for the user
374 // to see, so skip it.
375 continue;
376 }
377 final String origGroupName = tmpPermInfo.group;
378 String groupName = origGroupName;
379 if (groupName == null) {
380 groupName = tmpPermInfo.packageName;
381 tmpPermInfo.group = groupName;
382 }
383 MyPermissionGroupInfo group = mPermGroups.get(groupName);
384 if (group == null) {
385 PermissionGroupInfo grp = null;
386 if (origGroupName != null) {
387 grp = mPm.getPermissionGroupInfo(origGroupName, 0);
388 }
389 if (grp != null) {
390 group = new MyPermissionGroupInfo(grp);
391 } else {
392 // We could be here either because the permission
393 // didn't originally specify a group or the group it
394 // gave couldn't be found. In either case, we consider
395 // its group to be the permission's package name.
396 tmpPermInfo.group = tmpPermInfo.packageName;
397 group = mPermGroups.get(tmpPermInfo.group);
398 if (group == null) {
399 group = new MyPermissionGroupInfo(tmpPermInfo);
400 }
401 group = new MyPermissionGroupInfo(tmpPermInfo);
402 }
403 mPermGroups.put(tmpPermInfo.group, group);
404 }
405 final boolean newPerm = installedPkgInfo != null
406 && (existingFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0;
407 MyPermissionInfo myPerm = new MyPermissionInfo(tmpPermInfo);
408 myPerm.mNewReqFlags = flagsList[i];
409 myPerm.mExistingReqFlags = existingFlags;
410 // This is a new permission if the app is already installed and
411 // doesn't currently hold this permission.
412 myPerm.mNew = newPerm;
413 permSet.add(myPerm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 } catch (NameNotFoundException e) {
415 Log.i(TAG, "Ignoring unknown permission:"+permName);
416 }
417 }
418 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 public int getPermissionCount() {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700421 return getPermissionCount(WHICH_ALL);
422 }
423
424 private List<MyPermissionInfo> getPermissionList(MyPermissionGroupInfo grp, int which) {
425 if (which == WHICH_NEW) {
426 return grp.mNewPermissions;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700427 } else {
428 return grp.mAllPermissions;
429 }
430 }
431
432 public int getPermissionCount(int which) {
433 int N = 0;
434 for (int i=0; i<mPermGroupsList.size(); i++) {
435 N += getPermissionList(mPermGroupsList.get(i), which).size();
436 }
437 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439
440 public View getPermissionsView() {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700441 return getPermissionsView(WHICH_ALL, false);
442 }
443
444 public View getPermissionsViewWithRevokeButtons() {
445 return getPermissionsView(WHICH_ALL, true);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700446 }
447
448 public View getPermissionsView(int which) {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700449 return getPermissionsView(which, false);
450 }
451
452 private View getPermissionsView(int which, boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700453 LinearLayout permsView = (LinearLayout) mInflater.inflate(R.layout.app_perms_summary, null);
Alan Viverette8e1a7292017-02-27 10:57:58 -0500454 LinearLayout displayList = permsView.findViewById(R.id.perms_list);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700455 View noPermsView = permsView.findViewById(R.id.no_permissions);
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700456
Nick Kralevichddfbe002013-04-05 18:32:07 -0700457 displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700458 if (displayList.getChildCount() <= 0) {
459 noPermsView.setVisibility(View.VISIBLE);
460 }
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700461
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700462 return permsView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464
465 /**
466 * Utility method that displays permissions from a map containing group name and
467 * list of permission descriptions.
468 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700469 private void displayPermissions(List<MyPermissionGroupInfo> groups,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700470 LinearLayout permListView, int which, boolean showRevokeUI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 permListView.removeAllViews();
472
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700473 int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density);
474
475 for (int i=0; i<groups.size(); i++) {
476 MyPermissionGroupInfo grp = groups.get(i);
477 final List<MyPermissionInfo> perms = getPermissionList(grp, which);
478 for (int j=0; j<perms.size(); j++) {
479 MyPermissionInfo perm = perms.get(j);
480 View view = getPermissionItemView(grp, perm, j == 0,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700481 which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700482 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
483 ViewGroup.LayoutParams.MATCH_PARENT,
484 ViewGroup.LayoutParams.WRAP_CONTENT);
485 if (j == 0) {
486 lp.topMargin = spacing;
487 }
488 if (j == grp.mAllPermissions.size()-1) {
489 lp.bottomMargin = spacing;
490 }
491 if (permListView.getChildCount() == 0) {
492 lp.topMargin *= 2;
493 }
494 permListView.addView(view, lp);
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497 }
498
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700499 private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700500 MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
501 return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
502 mPackageName, showRevokeUI);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700505 private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
506 MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700507 CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
Nick Kralevicha56b78d2013-04-07 11:13:16 -0700508 PermissionItemView permView = (PermissionItemView)inflater.inflate(
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700509 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
510 ? R.layout.app_permission_item_money : R.layout.app_permission_item,
511 null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700512 permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700513 return permView;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800514 }
515
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700516 private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800517 CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700518 View permView = inflater.inflate(R.layout.app_permission_item_old, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
Alan Viverette8e1a7292017-02-27 10:57:58 -0500520 TextView permGrpView = permView.findViewById(R.id.permission_group);
521 TextView permDescView = permView.findViewById(R.id.permission_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
523 ImageView imgView = (ImageView)permView.findViewById(R.id.perm_icon);
524 imgView.setImageDrawable(icon);
525 if(grpName != null) {
526 permGrpView.setText(grpName);
527 permDescView.setText(permList);
528 } else {
529 permGrpView.setText(permList);
530 permDescView.setVisibility(View.GONE);
531 }
532 return permView;
533 }
534
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700535 private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
536 int existingReqFlags) {
537 final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700538 final boolean isNormal = (base == PermissionInfo.PROTECTION_NORMAL);
Svetoslava3f68ef2015-07-22 17:02:46 -0700539
540 // We do not show normal permissions in the UI.
541 if (isNormal) {
542 return false;
543 }
544
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700545 final boolean isDangerous = (base == PermissionInfo.PROTECTION_DANGEROUS)
546 || ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_PRE23) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700547 final boolean isRequired =
548 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
549 final boolean isDevelopment =
550 ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0);
551 final boolean wasGranted =
552 ((existingReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700553 final boolean isGranted =
554 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700555
556 // Dangerous and normal permissions are always shown to the user if the permission
557 // is required, or it was previously granted
Svetoslava3f68ef2015-07-22 17:02:46 -0700558 if (isDangerous && (isRequired || wasGranted || isGranted)) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700559 return true;
Dianne Hackborne639da72012-02-21 15:11:13 -0800560 }
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700561
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700562 // Development permissions are only shown to the user if they are already
563 // granted to the app -- if we are installing an app and they are not
564 // already granted, they will not be granted as part of the install.
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700565 if (isDevelopment && wasGranted) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700566 if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
567 + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 return true;
569 }
570 return false;
571 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700572
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700573 private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700574 private final Collator sCollator = Collator.getInstance();
Julia Reynolds9a5c9112015-07-09 15:23:42 -0400575 @Override
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700576 public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700577 return sCollator.compare(a.mLabel, b.mLabel);
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700578 }
579 }
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700580
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700581 private static class PermissionInfoComparator implements Comparator<MyPermissionInfo> {
582 private final Collator sCollator = Collator.getInstance();
583 PermissionInfoComparator() {
584 }
585 public final int compare(MyPermissionInfo a, MyPermissionInfo b) {
586 return sCollator.compare(a.mLabel, b.mLabel);
587 }
588 }
589
590 private void addPermToList(List<MyPermissionInfo> permList,
591 MyPermissionInfo pInfo) {
592 if (pInfo.mLabel == null) {
593 pInfo.mLabel = pInfo.loadLabel(mPm);
594 }
595 int idx = Collections.binarySearch(permList, pInfo, mPermComparator);
596 if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size());
597 if (idx < 0) {
598 idx = -idx-1;
599 permList.add(idx, pInfo);
600 }
601 }
602
603 private void setPermissions(List<MyPermissionInfo> permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 if (permList != null) {
605 // First pass to group permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700606 for (MyPermissionInfo pInfo : permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700608 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
610 continue;
611 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700612 MyPermissionGroupInfo group = mPermGroups.get(pInfo.group);
613 if (group != null) {
614 pInfo.mLabel = pInfo.loadLabel(mPm);
615 addPermToList(group.mAllPermissions, pInfo);
616 if (pInfo.mNew) {
617 addPermToList(group.mNewPermissions, pInfo);
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700623 for (MyPermissionGroupInfo pgrp : mPermGroups.values()) {
624 if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
625 pgrp.mLabel = pgrp.loadLabel(mPm);
626 } else {
627 ApplicationInfo app;
628 try {
629 app = mPm.getApplicationInfo(pgrp.packageName, 0);
630 pgrp.mLabel = app.loadLabel(mPm);
631 } catch (NameNotFoundException e) {
632 pgrp.mLabel = pgrp.loadLabel(mPm);
633 }
634 }
635 mPermGroupsList.add(pgrp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700637 Collections.sort(mPermGroupsList, mPermGroupComparator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639}