blob: bb4a9482664b4f5fd6230cd38a3131044961e24b [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_PERSONAL = 1<<0;
65 public static final int WHICH_DEVICE = 1<<1;
66 public static final int WHICH_NEW = 1<<2;
67 public static final int WHICH_ALL = 0xffff;
Dianne Hackborn0e128bb2012-05-01 14:40:15 -070068
69 private final static String TAG = "AppSecurityPermissions";
Dianne Hackborn2ca2c872012-09-16 16:03:36 -070070 private final static boolean localLOGV = false;
Nick Kralevich10ac1d12013-03-24 14:27:00 -070071 private final Context mContext;
72 private final LayoutInflater mInflater;
73 private final PackageManager mPm;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070074 private final Map<String, MyPermissionGroupInfo> mPermGroups
75 = new HashMap<String, MyPermissionGroupInfo>();
76 private final List<MyPermissionGroupInfo> mPermGroupsList
77 = new ArrayList<MyPermissionGroupInfo>();
Nick Kralevich10ac1d12013-03-24 14:27:00 -070078 private final PermissionGroupInfoComparator mPermGroupComparator = new PermissionGroupInfoComparator();
79 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
84 static class MyPermissionGroupInfo extends PermissionGroupInfo {
85 CharSequence mLabel;
86
87 final ArrayList<MyPermissionInfo> mNewPermissions = new ArrayList<MyPermissionInfo>();
88 final ArrayList<MyPermissionInfo> mPersonalPermissions = new ArrayList<MyPermissionInfo>();
89 final ArrayList<MyPermissionInfo> mDevicePermissions = new ArrayList<MyPermissionInfo>();
90 final ArrayList<MyPermissionInfo> mAllPermissions = new ArrayList<MyPermissionInfo>();
91
92 MyPermissionGroupInfo(PermissionInfo perm) {
93 name = perm.packageName;
94 packageName = perm.packageName;
95 }
96
97 MyPermissionGroupInfo(PermissionGroupInfo info) {
98 super(info);
99 }
100
101 public Drawable loadGroupIcon(PackageManager pm) {
102 if (icon != 0) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100103 return loadUnbadgedIcon(pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700104 } else {
105 ApplicationInfo appInfo;
106 try {
107 appInfo = pm.getApplicationInfo(packageName, 0);
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100108 return appInfo.loadUnbadgedIcon(pm);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700109 } catch (NameNotFoundException e) {
110 }
111 }
112 return null;
113 }
114 }
115
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700116 private static class MyPermissionInfo extends PermissionInfo {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700117 CharSequence mLabel;
118
119 /**
120 * PackageInfo.requestedPermissionsFlags for the new package being installed.
121 */
122 int mNewReqFlags;
123
124 /**
125 * PackageInfo.requestedPermissionsFlags for the currently installed
126 * package, if it is installed.
127 */
128 int mExistingReqFlags;
129
130 /**
131 * True if this should be considered a new permission.
132 */
133 boolean mNew;
134
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700135 MyPermissionInfo(PermissionInfo info) {
136 super(info);
137 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700138 }
139
140 public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
141 MyPermissionGroupInfo mGroup;
142 MyPermissionInfo mPerm;
143 AlertDialog mDialog;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700144 private boolean mShowRevokeUI = false;
145 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700146
147 public PermissionItemView(Context context, AttributeSet attrs) {
148 super(context, attrs);
149 setClickable(true);
150 }
151
152 public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700153 boolean first, CharSequence newPermPrefix, String packageName,
154 boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700155 mGroup = grp;
156 mPerm = perm;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700157 mShowRevokeUI = showRevokeUI;
158 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700159
160 ImageView permGrpIcon = (ImageView) findViewById(R.id.perm_icon);
161 TextView permNameView = (TextView) findViewById(R.id.perm_name);
162
163 PackageManager pm = getContext().getPackageManager();
164 Drawable icon = null;
165 if (first) {
166 icon = grp.loadGroupIcon(pm);
167 }
168 CharSequence label = perm.mLabel;
169 if (perm.mNew && newPermPrefix != null) {
170 // If this is a new permission, format it appropriately.
171 SpannableStringBuilder builder = new SpannableStringBuilder();
172 Parcel parcel = Parcel.obtain();
173 TextUtils.writeToParcel(newPermPrefix, parcel, 0);
174 parcel.setDataPosition(0);
175 CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
176 parcel.recycle();
177 builder.append(newStr);
178 builder.append(label);
179 label = builder;
180 }
181
182 permGrpIcon.setImageDrawable(icon);
183 permNameView.setText(label);
184 setOnClickListener(this);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700185 if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
186 + ": " + label + " in group " + grp.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700187 }
188
189 @Override
190 public void onClick(View v) {
191 if (mGroup != null && mPerm != null) {
192 if (mDialog != null) {
193 mDialog.dismiss();
194 }
195 PackageManager pm = getContext().getPackageManager();
196 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
197 builder.setTitle(mGroup.mLabel);
198 if (mPerm.descriptionRes != 0) {
199 builder.setMessage(mPerm.loadDescription(pm));
200 } else {
201 CharSequence appName;
202 try {
203 ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
204 appName = app.loadLabel(pm);
205 } catch (NameNotFoundException e) {
206 appName = mPerm.packageName;
207 }
208 StringBuilder sbuilder = new StringBuilder(128);
209 sbuilder.append(getContext().getString(
210 R.string.perms_description_app, appName));
211 sbuilder.append("\n\n");
212 sbuilder.append(mPerm.name);
213 builder.setMessage(sbuilder.toString());
214 }
215 builder.setCancelable(true);
216 builder.setIcon(mGroup.loadGroupIcon(pm));
Nick Kralevichddfbe002013-04-05 18:32:07 -0700217 addRevokeUIIfNecessary(builder);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700218 mDialog = builder.show();
219 mDialog.setCanceledOnTouchOutside(true);
220 }
221 }
222
223 @Override
224 protected void onDetachedFromWindow() {
225 super.onDetachedFromWindow();
226 if (mDialog != null) {
227 mDialog.dismiss();
228 }
229 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700230
231 private void addRevokeUIIfNecessary(AlertDialog.Builder builder) {
232 if (!mShowRevokeUI) {
233 return;
234 }
235
236 final boolean isRequired =
237 ((mPerm.mExistingReqFlags & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
238
239 if (isRequired) {
240 return;
241 }
242
243 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {
244 @Override
245 public void onClick(DialogInterface dialog, int which) {
246 PackageManager pm = getContext().getPackageManager();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700247 pm.revokeRuntimePermission(mPackageName, mPerm.name,
Svetoslavc6d1c342015-02-26 14:44:43 -0800248 new UserHandle(mContext.getUserId()));
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700249 PermissionItemView.this.setVisibility(View.GONE);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700250 }
251 };
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700252 builder.setNegativeButton(R.string.revoke, ocl);
253 builder.setPositiveButton(R.string.ok, null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700254 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700255 }
256
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700257 private AppSecurityPermissions(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 mContext = context;
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700259 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mPm = mContext.getPackageManager();
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700261 // Pick up from framework resources instead.
262 mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
263 }
264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public AppSecurityPermissions(Context context, String packageName) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700266 this(context);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700267 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700268 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 PackageInfo pkgInfo;
270 try {
271 pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
272 } catch (NameNotFoundException e) {
Dianne Hackborn52c62342012-09-21 10:57:45 -0700273 Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 return;
275 }
276 // Extract all user permissions
277 if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
278 getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
279 }
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700280 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700281 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700283
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700284 public AppSecurityPermissions(Context context, PackageInfo info) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700285 this(context);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700286 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700287 if(info == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return;
289 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700290 mPackageName = info.packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700291
292 // Convert to a PackageInfo
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700293 PackageInfo installedPkgInfo = null;
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700294 // Get requested permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700295 if (info.requestedPermissions != null) {
296 try {
297 installedPkgInfo = mPm.getPackageInfo(info.packageName,
298 PackageManager.GET_PERMISSIONS);
299 } catch (NameNotFoundException e) {
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700300 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700301 extractPerms(info, permSet, installedPkgInfo);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700302 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800303 // Get permissions related to shared user if any
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700304 if (info.sharedUserId != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 int sharedUid;
306 try {
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700307 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700308 getAllUsedPermissions(sharedUid, permSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 } catch (NameNotFoundException e) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700310 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700313 // Retrieve list of permissions
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700314 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700315 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700317
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800318 /**
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700319 * Utility to retrieve a view displaying a single permission. This provides
320 * the old UI layout for permissions; it is only here for the device admin
321 * settings to continue to use.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800322 */
323 public static View getPermissionItemView(Context context,
324 CharSequence grpName, CharSequence description, boolean dangerous) {
325 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
326 Context.LAYOUT_INFLATER_SERVICE);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800327 Drawable icon = context.getDrawable(dangerous
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800328 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700329 return getPermissionItemViewOld(context, inflater, grpName,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800330 description, dangerous, icon);
331 }
332
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700333 private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 String sharedPkgList[] = mPm.getPackagesForUid(sharedUid);
335 if(sharedPkgList == null || (sharedPkgList.length == 0)) {
336 return;
337 }
338 for(String sharedPkg : sharedPkgList) {
339 getPermissionsForPackage(sharedPkg, permSet);
340 }
341 }
342
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700343 private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 try {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700345 PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700346 extractPerms(pkgInfo, permSet, pkgInfo);
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700347 } catch (NameNotFoundException e) {
348 Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700351
352 private void extractPerms(PackageInfo info, Set<MyPermissionInfo> permSet,
353 PackageInfo installedPkgInfo) {
354 String[] strList = info.requestedPermissions;
355 int[] flagsList = info.requestedPermissionsFlags;
356 if ((strList == null) || (strList.length == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 return;
358 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700359 for (int i=0; i<strList.length; i++) {
360 String permName = strList[i];
361 // If we are only looking at an existing app, then we only
362 // care about permissions that have actually been granted to it.
Svetoslavc6d1c342015-02-26 14:44:43 -0800363 if (installedPkgInfo != null && info != installedPkgInfo) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700364 if ((flagsList[i]&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
365 continue;
366 }
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 try {
369 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700370 if (tmpPermInfo == null) {
371 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700373 int existingIndex = -1;
374 if (installedPkgInfo != null
375 && installedPkgInfo.requestedPermissions != null) {
376 for (int j=0; j<installedPkgInfo.requestedPermissions.length; j++) {
377 if (permName.equals(installedPkgInfo.requestedPermissions[j])) {
378 existingIndex = j;
379 break;
380 }
381 }
382 }
383 final int existingFlags = existingIndex >= 0 ?
384 installedPkgInfo.requestedPermissionsFlags[existingIndex] : 0;
385 if (!isDisplayablePermission(tmpPermInfo, flagsList[i], existingFlags)) {
386 // This is not a permission that is interesting for the user
387 // to see, so skip it.
388 continue;
389 }
390 final String origGroupName = tmpPermInfo.group;
391 String groupName = origGroupName;
392 if (groupName == null) {
393 groupName = tmpPermInfo.packageName;
394 tmpPermInfo.group = groupName;
395 }
396 MyPermissionGroupInfo group = mPermGroups.get(groupName);
397 if (group == null) {
398 PermissionGroupInfo grp = null;
399 if (origGroupName != null) {
400 grp = mPm.getPermissionGroupInfo(origGroupName, 0);
401 }
402 if (grp != null) {
403 group = new MyPermissionGroupInfo(grp);
404 } else {
405 // We could be here either because the permission
406 // didn't originally specify a group or the group it
407 // gave couldn't be found. In either case, we consider
408 // its group to be the permission's package name.
409 tmpPermInfo.group = tmpPermInfo.packageName;
410 group = mPermGroups.get(tmpPermInfo.group);
411 if (group == null) {
412 group = new MyPermissionGroupInfo(tmpPermInfo);
413 }
414 group = new MyPermissionGroupInfo(tmpPermInfo);
415 }
416 mPermGroups.put(tmpPermInfo.group, group);
417 }
418 final boolean newPerm = installedPkgInfo != null
419 && (existingFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0;
420 MyPermissionInfo myPerm = new MyPermissionInfo(tmpPermInfo);
421 myPerm.mNewReqFlags = flagsList[i];
422 myPerm.mExistingReqFlags = existingFlags;
423 // This is a new permission if the app is already installed and
424 // doesn't currently hold this permission.
425 myPerm.mNew = newPerm;
426 permSet.add(myPerm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 } catch (NameNotFoundException e) {
428 Log.i(TAG, "Ignoring unknown permission:"+permName);
429 }
430 }
431 }
432
433 public int getPermissionCount() {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700434 return getPermissionCount(WHICH_ALL);
435 }
436
437 private List<MyPermissionInfo> getPermissionList(MyPermissionGroupInfo grp, int which) {
438 if (which == WHICH_NEW) {
439 return grp.mNewPermissions;
440 } else if (which == WHICH_PERSONAL) {
441 return grp.mPersonalPermissions;
442 } else if (which == WHICH_DEVICE) {
443 return grp.mDevicePermissions;
444 } else {
445 return grp.mAllPermissions;
446 }
447 }
448
449 public int getPermissionCount(int which) {
450 int N = 0;
451 for (int i=0; i<mPermGroupsList.size(); i++) {
452 N += getPermissionList(mPermGroupsList.get(i), which).size();
453 }
454 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
457 public View getPermissionsView() {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700458 return getPermissionsView(WHICH_ALL, false);
459 }
460
461 public View getPermissionsViewWithRevokeButtons() {
462 return getPermissionsView(WHICH_ALL, true);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700463 }
464
465 public View getPermissionsView(int which) {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700466 return getPermissionsView(which, false);
467 }
468
469 private View getPermissionsView(int which, boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700470 LinearLayout permsView = (LinearLayout) mInflater.inflate(R.layout.app_perms_summary, null);
471 LinearLayout displayList = (LinearLayout) permsView.findViewById(R.id.perms_list);
472 View noPermsView = permsView.findViewById(R.id.no_permissions);
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700473
Nick Kralevichddfbe002013-04-05 18:32:07 -0700474 displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700475 if (displayList.getChildCount() <= 0) {
476 noPermsView.setVisibility(View.VISIBLE);
477 }
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700478
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700479 return permsView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481
482 /**
483 * Utility method that displays permissions from a map containing group name and
484 * list of permission descriptions.
485 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700486 private void displayPermissions(List<MyPermissionGroupInfo> groups,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700487 LinearLayout permListView, int which, boolean showRevokeUI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 permListView.removeAllViews();
489
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700490 int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density);
491
492 for (int i=0; i<groups.size(); i++) {
493 MyPermissionGroupInfo grp = groups.get(i);
494 final List<MyPermissionInfo> perms = getPermissionList(grp, which);
495 for (int j=0; j<perms.size(); j++) {
496 MyPermissionInfo perm = perms.get(j);
497 View view = getPermissionItemView(grp, perm, j == 0,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700498 which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700499 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
500 ViewGroup.LayoutParams.MATCH_PARENT,
501 ViewGroup.LayoutParams.WRAP_CONTENT);
502 if (j == 0) {
503 lp.topMargin = spacing;
504 }
505 if (j == grp.mAllPermissions.size()-1) {
506 lp.bottomMargin = spacing;
507 }
508 if (permListView.getChildCount() == 0) {
509 lp.topMargin *= 2;
510 }
511 permListView.addView(view, lp);
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514 }
515
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700516 private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700517 MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
518 return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
519 mPackageName, showRevokeUI);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700522 private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
523 MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700524 CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
Nick Kralevicha56b78d2013-04-07 11:13:16 -0700525 PermissionItemView permView = (PermissionItemView)inflater.inflate(
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700526 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
527 ? R.layout.app_permission_item_money : R.layout.app_permission_item,
528 null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700529 permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700530 return permView;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800531 }
532
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700533 private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800534 CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700535 View permView = inflater.inflate(R.layout.app_permission_item_old, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 TextView permGrpView = (TextView) permView.findViewById(R.id.permission_group);
538 TextView permDescView = (TextView) permView.findViewById(R.id.permission_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539
540 ImageView imgView = (ImageView)permView.findViewById(R.id.perm_icon);
541 imgView.setImageDrawable(icon);
542 if(grpName != null) {
543 permGrpView.setText(grpName);
544 permDescView.setText(permList);
545 } else {
546 permGrpView.setText(permList);
547 permDescView.setVisibility(View.GONE);
548 }
549 return permView;
550 }
551
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700552 private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
553 int existingReqFlags) {
554 final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700555 final boolean isNormal = (base == PermissionInfo.PROTECTION_NORMAL);
556 final boolean isDangerous = (base == PermissionInfo.PROTECTION_DANGEROUS);
557 final boolean isRequired =
558 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
559 final boolean isDevelopment =
560 ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0);
561 final boolean wasGranted =
562 ((existingReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700563 final boolean isGranted =
564 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700565
566 // Dangerous and normal permissions are always shown to the user if the permission
567 // is required, or it was previously granted
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700568 if ((isNormal || isDangerous) && (isRequired || wasGranted || isGranted)) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700569 return true;
Dianne Hackborne639da72012-02-21 15:11:13 -0800570 }
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700571
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700572 // Development permissions are only shown to the user if they are already
573 // granted to the app -- if we are installing an app and they are not
574 // already granted, they will not be granted as part of the install.
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700575 if (isDevelopment && wasGranted) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700576 if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
577 + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 return true;
579 }
580 return false;
581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700583 private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700584 private final Collator sCollator = Collator.getInstance();
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700585 PermissionGroupInfoComparator() {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700586 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700587 public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) {
588 if (((a.flags^b.flags)&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) {
589 return ((a.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) ? -1 : 1;
590 }
591 if (a.priority != b.priority) {
592 return a.priority > b.priority ? -1 : 1;
593 }
594 return sCollator.compare(a.mLabel, b.mLabel);
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700595 }
596 }
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700597
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700598 private static class PermissionInfoComparator implements Comparator<MyPermissionInfo> {
599 private final Collator sCollator = Collator.getInstance();
600 PermissionInfoComparator() {
601 }
602 public final int compare(MyPermissionInfo a, MyPermissionInfo b) {
603 return sCollator.compare(a.mLabel, b.mLabel);
604 }
605 }
606
607 private void addPermToList(List<MyPermissionInfo> permList,
608 MyPermissionInfo pInfo) {
609 if (pInfo.mLabel == null) {
610 pInfo.mLabel = pInfo.loadLabel(mPm);
611 }
612 int idx = Collections.binarySearch(permList, pInfo, mPermComparator);
613 if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size());
614 if (idx < 0) {
615 idx = -idx-1;
616 permList.add(idx, pInfo);
617 }
618 }
619
620 private void setPermissions(List<MyPermissionInfo> permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 if (permList != null) {
622 // First pass to group permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700623 for (MyPermissionInfo pInfo : permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700625 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
627 continue;
628 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700629 MyPermissionGroupInfo group = mPermGroups.get(pInfo.group);
630 if (group != null) {
631 pInfo.mLabel = pInfo.loadLabel(mPm);
632 addPermToList(group.mAllPermissions, pInfo);
633 if (pInfo.mNew) {
634 addPermToList(group.mNewPermissions, pInfo);
635 }
636 if ((group.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) {
637 addPermToList(group.mPersonalPermissions, pInfo);
638 } else {
639 addPermToList(group.mDevicePermissions, pInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 }
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700645 for (MyPermissionGroupInfo pgrp : mPermGroups.values()) {
646 if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
647 pgrp.mLabel = pgrp.loadLabel(mPm);
648 } else {
649 ApplicationInfo app;
650 try {
651 app = mPm.getApplicationInfo(pgrp.packageName, 0);
652 pgrp.mLabel = app.loadLabel(mPm);
653 } catch (NameNotFoundException e) {
654 pgrp.mLabel = pgrp.loadLabel(mPm);
655 }
656 }
657 mPermGroupsList.add(pgrp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700659 Collections.sort(mPermGroupsList, mPermGroupComparator);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700660 if (localLOGV) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700661 for (MyPermissionGroupInfo grp : mPermGroupsList) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700662 Log.i(TAG, "Group " + grp.name + " personal="
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700663 + ((grp.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0)
664 + " priority=" + grp.priority);
665 }
666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668}