blob: a12b15ef012981ebd41a5e8c45814320177959b0 [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
Svetoslavc6d1c342015-02-26 14:44:43 -080019import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.R;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070021
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070022import android.app.AlertDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Nick Kralevichddfbe002013-04-05 18:32:07 -070024import android.content.DialogInterface;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070025import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.pm.PackageInfo;
27import android.content.pm.PackageManager;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070028import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.pm.PermissionGroupInfo;
30import android.content.pm.PermissionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.graphics.drawable.Drawable;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070032import android.os.Parcel;
33import android.text.SpannableStringBuilder;
34import android.text.TextUtils;
35import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Log;
37import android.view.LayoutInflater;
38import android.view.View;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070039import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
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.
59 *
60 * {@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
83 static class MyPermissionGroupInfo extends PermissionGroupInfo {
84 CharSequence mLabel;
85
86 final ArrayList<MyPermissionInfo> mNewPermissions = new ArrayList<MyPermissionInfo>();
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070087 final ArrayList<MyPermissionInfo> mAllPermissions = new ArrayList<MyPermissionInfo>();
88
89 MyPermissionGroupInfo(PermissionInfo perm) {
90 name = perm.packageName;
91 packageName = perm.packageName;
92 }
93
94 MyPermissionGroupInfo(PermissionGroupInfo info) {
95 super(info);
96 }
97
Julia Reynoldsd1af4462015-07-08 11:12:28 -040098 public Drawable loadGroupIcon(Context context, PackageManager pm) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070099 if (icon != 0) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100100 return loadUnbadgedIcon(pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700101 } else {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400102 return context.getDrawable(R.drawable.ic_perm_device_info);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700103 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700104 }
105 }
106
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700107 private static class MyPermissionInfo extends PermissionInfo {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700108 CharSequence mLabel;
109
110 /**
111 * PackageInfo.requestedPermissionsFlags for the new package being installed.
112 */
113 int mNewReqFlags;
114
115 /**
116 * PackageInfo.requestedPermissionsFlags for the currently installed
117 * package, if it is installed.
118 */
119 int mExistingReqFlags;
120
121 /**
122 * True if this should be considered a new permission.
123 */
124 boolean mNew;
125
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700126 MyPermissionInfo(PermissionInfo info) {
127 super(info);
128 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700129 }
130
131 public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
132 MyPermissionGroupInfo mGroup;
133 MyPermissionInfo mPerm;
134 AlertDialog mDialog;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700135 private boolean mShowRevokeUI = false;
136 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700137
138 public PermissionItemView(Context context, AttributeSet attrs) {
139 super(context, attrs);
140 setClickable(true);
141 }
142
143 public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700144 boolean first, CharSequence newPermPrefix, String packageName,
145 boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700146 mGroup = grp;
147 mPerm = perm;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700148 mShowRevokeUI = showRevokeUI;
149 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700150
151 ImageView permGrpIcon = (ImageView) findViewById(R.id.perm_icon);
152 TextView permNameView = (TextView) findViewById(R.id.perm_name);
153
154 PackageManager pm = getContext().getPackageManager();
155 Drawable icon = null;
156 if (first) {
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400157 icon = grp.loadGroupIcon(getContext(), pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700158 }
159 CharSequence label = perm.mLabel;
160 if (perm.mNew && newPermPrefix != null) {
161 // If this is a new permission, format it appropriately.
162 SpannableStringBuilder builder = new SpannableStringBuilder();
163 Parcel parcel = Parcel.obtain();
164 TextUtils.writeToParcel(newPermPrefix, parcel, 0);
165 parcel.setDataPosition(0);
166 CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
167 parcel.recycle();
168 builder.append(newStr);
169 builder.append(label);
170 label = builder;
171 }
172
173 permGrpIcon.setImageDrawable(icon);
174 permNameView.setText(label);
175 setOnClickListener(this);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700176 if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
177 + ": " + label + " in group " + grp.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700178 }
179
180 @Override
181 public void onClick(View v) {
182 if (mGroup != null && mPerm != null) {
183 if (mDialog != null) {
184 mDialog.dismiss();
185 }
186 PackageManager pm = getContext().getPackageManager();
187 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
188 builder.setTitle(mGroup.mLabel);
189 if (mPerm.descriptionRes != 0) {
190 builder.setMessage(mPerm.loadDescription(pm));
191 } else {
192 CharSequence appName;
193 try {
194 ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
195 appName = app.loadLabel(pm);
196 } catch (NameNotFoundException e) {
197 appName = mPerm.packageName;
198 }
199 StringBuilder sbuilder = new StringBuilder(128);
200 sbuilder.append(getContext().getString(
201 R.string.perms_description_app, appName));
202 sbuilder.append("\n\n");
203 sbuilder.append(mPerm.name);
204 builder.setMessage(sbuilder.toString());
205 }
206 builder.setCancelable(true);
Julia Reynoldsd1af4462015-07-08 11:12:28 -0400207 builder.setIcon(mGroup.loadGroupIcon(getContext(), pm));
Nick Kralevichddfbe002013-04-05 18:32:07 -0700208 addRevokeUIIfNecessary(builder);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700209 mDialog = builder.show();
210 mDialog.setCanceledOnTouchOutside(true);
211 }
212 }
213
214 @Override
215 protected void onDetachedFromWindow() {
216 super.onDetachedFromWindow();
217 if (mDialog != null) {
218 mDialog.dismiss();
219 }
220 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700221
222 private void addRevokeUIIfNecessary(AlertDialog.Builder builder) {
223 if (!mShowRevokeUI) {
224 return;
225 }
226
227 final boolean isRequired =
228 ((mPerm.mExistingReqFlags & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
229
230 if (isRequired) {
231 return;
232 }
233
234 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {
235 @Override
236 public void onClick(DialogInterface dialog, int which) {
237 PackageManager pm = getContext().getPackageManager();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700238 pm.revokeRuntimePermission(mPackageName, mPerm.name,
Svetoslavc6d1c342015-02-26 14:44:43 -0800239 new UserHandle(mContext.getUserId()));
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700240 PermissionItemView.this.setVisibility(View.GONE);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700241 }
242 };
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700243 builder.setNegativeButton(R.string.revoke, ocl);
244 builder.setPositiveButton(R.string.ok, null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700245 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700246 }
247
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700248 private AppSecurityPermissions(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 mContext = context;
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700250 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 mPm = mContext.getPackageManager();
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700252 // Pick up from framework resources instead.
253 mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
254 }
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 public AppSecurityPermissions(Context context, String packageName) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700257 this(context);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700258 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700259 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 PackageInfo pkgInfo;
261 try {
262 pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
263 } catch (NameNotFoundException e) {
Dianne Hackborn52c62342012-09-21 10:57:45 -0700264 Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 return;
266 }
267 // Extract all user permissions
268 if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
269 getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
270 }
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700271 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700272 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700274
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700275 public AppSecurityPermissions(Context context, PackageInfo info) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700276 this(context);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700277 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700278 if(info == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 return;
280 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700281 mPackageName = info.packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700282
283 // Convert to a PackageInfo
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700284 PackageInfo installedPkgInfo = null;
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700285 // Get requested permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700286 if (info.requestedPermissions != null) {
287 try {
288 installedPkgInfo = mPm.getPackageInfo(info.packageName,
289 PackageManager.GET_PERMISSIONS);
290 } catch (NameNotFoundException e) {
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700291 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700292 extractPerms(info, permSet, installedPkgInfo);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700293 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800294 // Get permissions related to shared user if any
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700295 if (info.sharedUserId != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 int sharedUid;
297 try {
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700298 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700299 getAllUsedPermissions(sharedUid, permSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 } catch (NameNotFoundException e) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700301 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700304 // Retrieve list of permissions
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700305 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700306 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700308
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800309 /**
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700310 * Utility to retrieve a view displaying a single permission. This provides
311 * the old UI layout for permissions; it is only here for the device admin
312 * settings to continue to use.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800313 */
314 public static View getPermissionItemView(Context context,
315 CharSequence grpName, CharSequence description, boolean dangerous) {
316 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
317 Context.LAYOUT_INFLATER_SERVICE);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800318 Drawable icon = context.getDrawable(dangerous
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800319 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700320 return getPermissionItemViewOld(context, inflater, grpName,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800321 description, dangerous, icon);
322 }
323
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700324 private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 String sharedPkgList[] = mPm.getPackagesForUid(sharedUid);
326 if(sharedPkgList == null || (sharedPkgList.length == 0)) {
327 return;
328 }
329 for(String sharedPkg : sharedPkgList) {
330 getPermissionsForPackage(sharedPkg, permSet);
331 }
332 }
333
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700334 private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 try {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700336 PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700337 extractPerms(pkgInfo, permSet, pkgInfo);
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700338 } catch (NameNotFoundException e) {
339 Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700342
343 private void extractPerms(PackageInfo info, Set<MyPermissionInfo> permSet,
344 PackageInfo installedPkgInfo) {
345 String[] strList = info.requestedPermissions;
346 int[] flagsList = info.requestedPermissionsFlags;
347 if ((strList == null) || (strList.length == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return;
349 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700350 for (int i=0; i<strList.length; i++) {
351 String permName = strList[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 try {
353 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700354 if (tmpPermInfo == null) {
355 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700357 int existingIndex = -1;
358 if (installedPkgInfo != null
359 && installedPkgInfo.requestedPermissions != null) {
360 for (int j=0; j<installedPkgInfo.requestedPermissions.length; j++) {
361 if (permName.equals(installedPkgInfo.requestedPermissions[j])) {
362 existingIndex = j;
363 break;
364 }
365 }
366 }
367 final int existingFlags = existingIndex >= 0 ?
368 installedPkgInfo.requestedPermissionsFlags[existingIndex] : 0;
369 if (!isDisplayablePermission(tmpPermInfo, flagsList[i], existingFlags)) {
370 // This is not a permission that is interesting for the user
371 // to see, so skip it.
372 continue;
373 }
374 final String origGroupName = tmpPermInfo.group;
375 String groupName = origGroupName;
376 if (groupName == null) {
377 groupName = tmpPermInfo.packageName;
378 tmpPermInfo.group = groupName;
379 }
380 MyPermissionGroupInfo group = mPermGroups.get(groupName);
381 if (group == null) {
382 PermissionGroupInfo grp = null;
383 if (origGroupName != null) {
384 grp = mPm.getPermissionGroupInfo(origGroupName, 0);
385 }
386 if (grp != null) {
387 group = new MyPermissionGroupInfo(grp);
388 } else {
389 // We could be here either because the permission
390 // didn't originally specify a group or the group it
391 // gave couldn't be found. In either case, we consider
392 // its group to be the permission's package name.
393 tmpPermInfo.group = tmpPermInfo.packageName;
394 group = mPermGroups.get(tmpPermInfo.group);
395 if (group == null) {
396 group = new MyPermissionGroupInfo(tmpPermInfo);
397 }
398 group = new MyPermissionGroupInfo(tmpPermInfo);
399 }
400 mPermGroups.put(tmpPermInfo.group, group);
401 }
402 final boolean newPerm = installedPkgInfo != null
403 && (existingFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0;
404 MyPermissionInfo myPerm = new MyPermissionInfo(tmpPermInfo);
405 myPerm.mNewReqFlags = flagsList[i];
406 myPerm.mExistingReqFlags = existingFlags;
407 // This is a new permission if the app is already installed and
408 // doesn't currently hold this permission.
409 myPerm.mNew = newPerm;
410 permSet.add(myPerm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 } catch (NameNotFoundException e) {
412 Log.i(TAG, "Ignoring unknown permission:"+permName);
413 }
414 }
415 }
416
417 public int getPermissionCount() {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700418 return getPermissionCount(WHICH_ALL);
419 }
420
421 private List<MyPermissionInfo> getPermissionList(MyPermissionGroupInfo grp, int which) {
422 if (which == WHICH_NEW) {
423 return grp.mNewPermissions;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700424 } else {
425 return grp.mAllPermissions;
426 }
427 }
428
429 public int getPermissionCount(int which) {
430 int N = 0;
431 for (int i=0; i<mPermGroupsList.size(); i++) {
432 N += getPermissionList(mPermGroupsList.get(i), which).size();
433 }
434 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
437 public View getPermissionsView() {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700438 return getPermissionsView(WHICH_ALL, false);
439 }
440
441 public View getPermissionsViewWithRevokeButtons() {
442 return getPermissionsView(WHICH_ALL, true);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700443 }
444
445 public View getPermissionsView(int which) {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700446 return getPermissionsView(which, false);
447 }
448
449 private View getPermissionsView(int which, boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700450 LinearLayout permsView = (LinearLayout) mInflater.inflate(R.layout.app_perms_summary, null);
451 LinearLayout displayList = (LinearLayout) permsView.findViewById(R.id.perms_list);
452 View noPermsView = permsView.findViewById(R.id.no_permissions);
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700453
Nick Kralevichddfbe002013-04-05 18:32:07 -0700454 displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700455 if (displayList.getChildCount() <= 0) {
456 noPermsView.setVisibility(View.VISIBLE);
457 }
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700458
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700459 return permsView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
462 /**
463 * Utility method that displays permissions from a map containing group name and
464 * list of permission descriptions.
465 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700466 private void displayPermissions(List<MyPermissionGroupInfo> groups,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700467 LinearLayout permListView, int which, boolean showRevokeUI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 permListView.removeAllViews();
469
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700470 int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density);
471
472 for (int i=0; i<groups.size(); i++) {
473 MyPermissionGroupInfo grp = groups.get(i);
474 final List<MyPermissionInfo> perms = getPermissionList(grp, which);
475 for (int j=0; j<perms.size(); j++) {
476 MyPermissionInfo perm = perms.get(j);
477 View view = getPermissionItemView(grp, perm, j == 0,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700478 which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700479 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
480 ViewGroup.LayoutParams.MATCH_PARENT,
481 ViewGroup.LayoutParams.WRAP_CONTENT);
482 if (j == 0) {
483 lp.topMargin = spacing;
484 }
485 if (j == grp.mAllPermissions.size()-1) {
486 lp.bottomMargin = spacing;
487 }
488 if (permListView.getChildCount() == 0) {
489 lp.topMargin *= 2;
490 }
491 permListView.addView(view, lp);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494 }
495
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700496 private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700497 MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
498 return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
499 mPackageName, showRevokeUI);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700502 private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
503 MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700504 CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
Nick Kralevicha56b78d2013-04-07 11:13:16 -0700505 PermissionItemView permView = (PermissionItemView)inflater.inflate(
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700506 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
507 ? R.layout.app_permission_item_money : R.layout.app_permission_item,
508 null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700509 permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700510 return permView;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800511 }
512
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700513 private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800514 CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700515 View permView = inflater.inflate(R.layout.app_permission_item_old, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
517 TextView permGrpView = (TextView) permView.findViewById(R.id.permission_group);
518 TextView permDescView = (TextView) permView.findViewById(R.id.permission_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 ImageView imgView = (ImageView)permView.findViewById(R.id.perm_icon);
521 imgView.setImageDrawable(icon);
522 if(grpName != null) {
523 permGrpView.setText(grpName);
524 permDescView.setText(permList);
525 } else {
526 permGrpView.setText(permList);
527 permDescView.setVisibility(View.GONE);
528 }
529 return permView;
530 }
531
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700532 private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
533 int existingReqFlags) {
534 final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700535 final boolean isNormal = (base == PermissionInfo.PROTECTION_NORMAL);
Svetoslava3f68ef2015-07-22 17:02:46 -0700536
537 // We do not show normal permissions in the UI.
538 if (isNormal) {
539 return false;
540 }
541
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700542 final boolean isDangerous = (base == PermissionInfo.PROTECTION_DANGEROUS)
543 || ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_PRE23) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700544 final boolean isRequired =
545 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
546 final boolean isDevelopment =
547 ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0);
548 final boolean wasGranted =
549 ((existingReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700550 final boolean isGranted =
551 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700552
553 // Dangerous and normal permissions are always shown to the user if the permission
554 // is required, or it was previously granted
Svetoslava3f68ef2015-07-22 17:02:46 -0700555 if (isDangerous && (isRequired || wasGranted || isGranted)) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700556 return true;
Dianne Hackborne639da72012-02-21 15:11:13 -0800557 }
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700558
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700559 // Development permissions are only shown to the user if they are already
560 // granted to the app -- if we are installing an app and they are not
561 // already granted, they will not be granted as part of the install.
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700562 if (isDevelopment && wasGranted) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700563 if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
564 + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 return true;
566 }
567 return false;
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700570 private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700571 private final Collator sCollator = Collator.getInstance();
Julia Reynolds9a5c9112015-07-09 15:23:42 -0400572 @Override
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700573 public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700574 return sCollator.compare(a.mLabel, b.mLabel);
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700575 }
576 }
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700577
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700578 private static class PermissionInfoComparator implements Comparator<MyPermissionInfo> {
579 private final Collator sCollator = Collator.getInstance();
580 PermissionInfoComparator() {
581 }
582 public final int compare(MyPermissionInfo a, MyPermissionInfo b) {
583 return sCollator.compare(a.mLabel, b.mLabel);
584 }
585 }
586
587 private void addPermToList(List<MyPermissionInfo> permList,
588 MyPermissionInfo pInfo) {
589 if (pInfo.mLabel == null) {
590 pInfo.mLabel = pInfo.loadLabel(mPm);
591 }
592 int idx = Collections.binarySearch(permList, pInfo, mPermComparator);
593 if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size());
594 if (idx < 0) {
595 idx = -idx-1;
596 permList.add(idx, pInfo);
597 }
598 }
599
600 private void setPermissions(List<MyPermissionInfo> permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 if (permList != null) {
602 // First pass to group permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700603 for (MyPermissionInfo pInfo : permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700605 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
607 continue;
608 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700609 MyPermissionGroupInfo group = mPermGroups.get(pInfo.group);
610 if (group != null) {
611 pInfo.mLabel = pInfo.loadLabel(mPm);
612 addPermToList(group.mAllPermissions, pInfo);
613 if (pInfo.mNew) {
614 addPermToList(group.mNewPermissions, pInfo);
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700620 for (MyPermissionGroupInfo pgrp : mPermGroups.values()) {
621 if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
622 pgrp.mLabel = pgrp.loadLabel(mPm);
623 } else {
624 ApplicationInfo app;
625 try {
626 app = mPm.getApplicationInfo(pgrp.packageName, 0);
627 pgrp.mLabel = app.loadLabel(mPm);
628 } catch (NameNotFoundException e) {
629 pgrp.mLabel = pgrp.loadLabel(mPm);
630 }
631 }
632 mPermGroupsList.add(pgrp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700634 Collections.sort(mPermGroupsList, mPermGroupComparator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636}