blob: 10e56c74f979a95219187026773ebb9af86f5da2 [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
19import com.android.internal.R;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070020
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070021import android.app.AlertDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Nick Kralevichddfbe002013-04-05 18:32:07 -070023import android.content.DialogInterface;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070024import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
Gilles Debunne6b8bdaa2010-10-10 13:24:39 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PermissionGroupInfo;
29import android.content.pm.PermissionInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.drawable.Drawable;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070031import android.os.Parcel;
32import android.text.SpannableStringBuilder;
33import android.text.TextUtils;
34import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.Log;
36import android.view.LayoutInflater;
37import android.view.View;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070038import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.text.Collator;
41import java.util.ArrayList;
42import java.util.Collections;
43import java.util.Comparator;
44import java.util.HashMap;
45import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.util.List;
47import java.util.Map;
48import java.util.Set;
49
50/**
51 * This class contains the SecurityPermissions view implementation.
52 * Initially the package's advanced or dangerous security permissions
53 * are displayed under categorized
54 * groups. Clicking on the additional permissions presents
55 * extended information consisting of all groups and permissions.
56 * To use this view define a LinearLayout or any ViewGroup and add this
57 * view by instantiating AppSecurityPermissions and invoking getPermissionsView.
58 *
59 * {@hide}
60 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070061public class AppSecurityPermissions {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070063 public static final int WHICH_PERSONAL = 1<<0;
64 public static final int WHICH_DEVICE = 1<<1;
65 public static final int WHICH_NEW = 1<<2;
66 public static final int WHICH_ALL = 0xffff;
Dianne Hackborn0e128bb2012-05-01 14:40:15 -070067
68 private final static String TAG = "AppSecurityPermissions";
Dianne Hackborn2ca2c872012-09-16 16:03:36 -070069 private final static boolean localLOGV = false;
Nick Kralevich10ac1d12013-03-24 14:27:00 -070070 private final Context mContext;
71 private final LayoutInflater mInflater;
72 private final PackageManager mPm;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -070073 private final Map<String, MyPermissionGroupInfo> mPermGroups
74 = new HashMap<String, MyPermissionGroupInfo>();
75 private final List<MyPermissionGroupInfo> mPermGroupsList
76 = new ArrayList<MyPermissionGroupInfo>();
Nick Kralevich10ac1d12013-03-24 14:27:00 -070077 private final PermissionGroupInfoComparator mPermGroupComparator = new PermissionGroupInfoComparator();
78 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>();
87 final ArrayList<MyPermissionInfo> mPersonalPermissions = new ArrayList<MyPermissionInfo>();
88 final ArrayList<MyPermissionInfo> mDevicePermissions = new ArrayList<MyPermissionInfo>();
89 final ArrayList<MyPermissionInfo> mAllPermissions = new ArrayList<MyPermissionInfo>();
90
91 MyPermissionGroupInfo(PermissionInfo perm) {
92 name = perm.packageName;
93 packageName = perm.packageName;
94 }
95
96 MyPermissionGroupInfo(PermissionGroupInfo info) {
97 super(info);
98 }
99
100 public Drawable loadGroupIcon(PackageManager pm) {
101 if (icon != 0) {
102 return loadIcon(pm);
103 } else {
104 ApplicationInfo appInfo;
105 try {
106 appInfo = pm.getApplicationInfo(packageName, 0);
107 return appInfo.loadIcon(pm);
108 } catch (NameNotFoundException e) {
109 }
110 }
111 return null;
112 }
113 }
114
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700115 private static class MyPermissionInfo extends PermissionInfo {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700116 CharSequence mLabel;
117
118 /**
119 * PackageInfo.requestedPermissionsFlags for the new package being installed.
120 */
121 int mNewReqFlags;
122
123 /**
124 * PackageInfo.requestedPermissionsFlags for the currently installed
125 * package, if it is installed.
126 */
127 int mExistingReqFlags;
128
129 /**
130 * True if this should be considered a new permission.
131 */
132 boolean mNew;
133
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700134 MyPermissionInfo(PermissionInfo info) {
135 super(info);
136 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700137 }
138
139 public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
140 MyPermissionGroupInfo mGroup;
141 MyPermissionInfo mPerm;
142 AlertDialog mDialog;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700143 private boolean mShowRevokeUI = false;
144 private String mPackageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700145
146 public PermissionItemView(Context context, AttributeSet attrs) {
147 super(context, attrs);
148 setClickable(true);
149 }
150
151 public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700152 boolean first, CharSequence newPermPrefix, String packageName,
153 boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700154 mGroup = grp;
155 mPerm = perm;
Nick Kralevichddfbe002013-04-05 18:32:07 -0700156 mShowRevokeUI = showRevokeUI;
157 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700158
159 ImageView permGrpIcon = (ImageView) findViewById(R.id.perm_icon);
160 TextView permNameView = (TextView) findViewById(R.id.perm_name);
161
162 PackageManager pm = getContext().getPackageManager();
163 Drawable icon = null;
164 if (first) {
165 icon = grp.loadGroupIcon(pm);
166 }
167 CharSequence label = perm.mLabel;
168 if (perm.mNew && newPermPrefix != null) {
169 // If this is a new permission, format it appropriately.
170 SpannableStringBuilder builder = new SpannableStringBuilder();
171 Parcel parcel = Parcel.obtain();
172 TextUtils.writeToParcel(newPermPrefix, parcel, 0);
173 parcel.setDataPosition(0);
174 CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
175 parcel.recycle();
176 builder.append(newStr);
177 builder.append(label);
178 label = builder;
179 }
180
181 permGrpIcon.setImageDrawable(icon);
182 permNameView.setText(label);
183 setOnClickListener(this);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700184 if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
185 + ": " + label + " in group " + grp.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700186 }
187
188 @Override
189 public void onClick(View v) {
190 if (mGroup != null && mPerm != null) {
191 if (mDialog != null) {
192 mDialog.dismiss();
193 }
194 PackageManager pm = getContext().getPackageManager();
195 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
196 builder.setTitle(mGroup.mLabel);
197 if (mPerm.descriptionRes != 0) {
198 builder.setMessage(mPerm.loadDescription(pm));
199 } else {
200 CharSequence appName;
201 try {
202 ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
203 appName = app.loadLabel(pm);
204 } catch (NameNotFoundException e) {
205 appName = mPerm.packageName;
206 }
207 StringBuilder sbuilder = new StringBuilder(128);
208 sbuilder.append(getContext().getString(
209 R.string.perms_description_app, appName));
210 sbuilder.append("\n\n");
211 sbuilder.append(mPerm.name);
212 builder.setMessage(sbuilder.toString());
213 }
214 builder.setCancelable(true);
215 builder.setIcon(mGroup.loadGroupIcon(pm));
Nick Kralevichddfbe002013-04-05 18:32:07 -0700216 addRevokeUIIfNecessary(builder);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700217 mDialog = builder.show();
218 mDialog.setCanceledOnTouchOutside(true);
219 }
220 }
221
222 @Override
223 protected void onDetachedFromWindow() {
224 super.onDetachedFromWindow();
225 if (mDialog != null) {
226 mDialog.dismiss();
227 }
228 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700229
230 private void addRevokeUIIfNecessary(AlertDialog.Builder builder) {
231 if (!mShowRevokeUI) {
232 return;
233 }
234
235 final boolean isRequired =
236 ((mPerm.mExistingReqFlags & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
237
238 if (isRequired) {
239 return;
240 }
241
242 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() {
243 @Override
244 public void onClick(DialogInterface dialog, int which) {
245 PackageManager pm = getContext().getPackageManager();
246 pm.revokePermission(mPackageName, mPerm.name);
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700247 PermissionItemView.this.setVisibility(View.GONE);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700248 }
249 };
Nick Kralevich98cbcf12013-04-08 10:02:08 -0700250 builder.setNegativeButton(R.string.revoke, ocl);
251 builder.setPositiveButton(R.string.ok, null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700252 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700253 }
254
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700255 private AppSecurityPermissions(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 mContext = context;
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700257 mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 mPm = mContext.getPackageManager();
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700259 // Pick up from framework resources instead.
260 mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
261 }
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public AppSecurityPermissions(Context context, String packageName) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700264 this(context);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700265 mPackageName = packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700266 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 PackageInfo pkgInfo;
268 try {
269 pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
270 } catch (NameNotFoundException e) {
Dianne Hackborn52c62342012-09-21 10:57:45 -0700271 Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 return;
273 }
274 // Extract all user permissions
275 if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
276 getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
277 }
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700278 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700279 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700281
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700282 public AppSecurityPermissions(Context context, PackageInfo info) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700283 this(context);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700284 Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700285 if(info == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 return;
287 }
Nick Kralevichddfbe002013-04-05 18:32:07 -0700288 mPackageName = info.packageName;
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700289
290 // Convert to a PackageInfo
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700291 PackageInfo installedPkgInfo = null;
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700292 // Get requested permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700293 if (info.requestedPermissions != null) {
294 try {
295 installedPkgInfo = mPm.getPackageInfo(info.packageName,
296 PackageManager.GET_PERMISSIONS);
297 } catch (NameNotFoundException e) {
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700298 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700299 extractPerms(info, permSet, installedPkgInfo);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700300 }
301 // Get permissions related to shared user if any
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700302 if (info.sharedUserId != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 int sharedUid;
304 try {
Dianne Hackborneba784ff2012-09-19 12:42:37 -0700305 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700306 getAllUsedPermissions(sharedUid, permSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 } catch (NameNotFoundException e) {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700308 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
Suchi Amalapurapuc7b14e92009-05-27 14:11:50 -0700311 // Retrieve list of permissions
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700312 mPermsList.addAll(permSet);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700313 setPermissions(mPermsList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700315
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800316 /**
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700317 * Utility to retrieve a view displaying a single permission. This provides
318 * the old UI layout for permissions; it is only here for the device admin
319 * settings to continue to use.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800320 */
321 public static View getPermissionItemView(Context context,
322 CharSequence grpName, CharSequence description, boolean dangerous) {
323 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
324 Context.LAYOUT_INFLATER_SERVICE);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800325 Drawable icon = context.getDrawable(dangerous
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800326 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700327 return getPermissionItemViewOld(context, inflater, grpName,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800328 description, dangerous, icon);
329 }
330
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700331 private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 String sharedPkgList[] = mPm.getPackagesForUid(sharedUid);
333 if(sharedPkgList == null || (sharedPkgList.length == 0)) {
334 return;
335 }
336 for(String sharedPkg : sharedPkgList) {
337 getPermissionsForPackage(sharedPkg, permSet);
338 }
339 }
340
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700341 private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 try {
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700343 PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700344 extractPerms(pkgInfo, permSet, pkgInfo);
Nick Kralevich10ac1d12013-03-24 14:27:00 -0700345 } catch (NameNotFoundException e) {
346 Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700349
350 private void extractPerms(PackageInfo info, Set<MyPermissionInfo> permSet,
351 PackageInfo installedPkgInfo) {
352 String[] strList = info.requestedPermissions;
353 int[] flagsList = info.requestedPermissionsFlags;
354 if ((strList == null) || (strList.length == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 return;
356 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700357 for (int i=0; i<strList.length; i++) {
358 String permName = strList[i];
359 // If we are only looking at an existing app, then we only
360 // care about permissions that have actually been granted to it.
361 if (installedPkgInfo != null && info == installedPkgInfo) {
362 if ((flagsList[i]&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
363 continue;
364 }
365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 try {
367 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700368 if (tmpPermInfo == null) {
369 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700371 int existingIndex = -1;
372 if (installedPkgInfo != null
373 && installedPkgInfo.requestedPermissions != null) {
374 for (int j=0; j<installedPkgInfo.requestedPermissions.length; j++) {
375 if (permName.equals(installedPkgInfo.requestedPermissions[j])) {
376 existingIndex = j;
377 break;
378 }
379 }
380 }
381 final int existingFlags = existingIndex >= 0 ?
382 installedPkgInfo.requestedPermissionsFlags[existingIndex] : 0;
383 if (!isDisplayablePermission(tmpPermInfo, flagsList[i], existingFlags)) {
384 // This is not a permission that is interesting for the user
385 // to see, so skip it.
386 continue;
387 }
388 final String origGroupName = tmpPermInfo.group;
389 String groupName = origGroupName;
390 if (groupName == null) {
391 groupName = tmpPermInfo.packageName;
392 tmpPermInfo.group = groupName;
393 }
394 MyPermissionGroupInfo group = mPermGroups.get(groupName);
395 if (group == null) {
396 PermissionGroupInfo grp = null;
397 if (origGroupName != null) {
398 grp = mPm.getPermissionGroupInfo(origGroupName, 0);
399 }
400 if (grp != null) {
401 group = new MyPermissionGroupInfo(grp);
402 } else {
403 // We could be here either because the permission
404 // didn't originally specify a group or the group it
405 // gave couldn't be found. In either case, we consider
406 // its group to be the permission's package name.
407 tmpPermInfo.group = tmpPermInfo.packageName;
408 group = mPermGroups.get(tmpPermInfo.group);
409 if (group == null) {
410 group = new MyPermissionGroupInfo(tmpPermInfo);
411 }
412 group = new MyPermissionGroupInfo(tmpPermInfo);
413 }
414 mPermGroups.put(tmpPermInfo.group, group);
415 }
416 final boolean newPerm = installedPkgInfo != null
417 && (existingFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0;
418 MyPermissionInfo myPerm = new MyPermissionInfo(tmpPermInfo);
419 myPerm.mNewReqFlags = flagsList[i];
420 myPerm.mExistingReqFlags = existingFlags;
421 // This is a new permission if the app is already installed and
422 // doesn't currently hold this permission.
423 myPerm.mNew = newPerm;
424 permSet.add(myPerm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 } catch (NameNotFoundException e) {
426 Log.i(TAG, "Ignoring unknown permission:"+permName);
427 }
428 }
429 }
430
431 public int getPermissionCount() {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700432 return getPermissionCount(WHICH_ALL);
433 }
434
435 private List<MyPermissionInfo> getPermissionList(MyPermissionGroupInfo grp, int which) {
436 if (which == WHICH_NEW) {
437 return grp.mNewPermissions;
438 } else if (which == WHICH_PERSONAL) {
439 return grp.mPersonalPermissions;
440 } else if (which == WHICH_DEVICE) {
441 return grp.mDevicePermissions;
442 } else {
443 return grp.mAllPermissions;
444 }
445 }
446
447 public int getPermissionCount(int which) {
448 int N = 0;
449 for (int i=0; i<mPermGroupsList.size(); i++) {
450 N += getPermissionList(mPermGroupsList.get(i), which).size();
451 }
452 return N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454
455 public View getPermissionsView() {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700456 return getPermissionsView(WHICH_ALL, false);
457 }
458
459 public View getPermissionsViewWithRevokeButtons() {
460 return getPermissionsView(WHICH_ALL, true);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700461 }
462
463 public View getPermissionsView(int which) {
Nick Kralevichddfbe002013-04-05 18:32:07 -0700464 return getPermissionsView(which, false);
465 }
466
467 private View getPermissionsView(int which, boolean showRevokeUI) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700468 LinearLayout permsView = (LinearLayout) mInflater.inflate(R.layout.app_perms_summary, null);
469 LinearLayout displayList = (LinearLayout) permsView.findViewById(R.id.perms_list);
470 View noPermsView = permsView.findViewById(R.id.no_permissions);
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700471
Nick Kralevichddfbe002013-04-05 18:32:07 -0700472 displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700473 if (displayList.getChildCount() <= 0) {
474 noPermsView.setVisibility(View.VISIBLE);
475 }
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700476
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700477 return permsView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479
480 /**
481 * Utility method that displays permissions from a map containing group name and
482 * list of permission descriptions.
483 */
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700484 private void displayPermissions(List<MyPermissionGroupInfo> groups,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700485 LinearLayout permListView, int which, boolean showRevokeUI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 permListView.removeAllViews();
487
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700488 int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density);
489
490 for (int i=0; i<groups.size(); i++) {
491 MyPermissionGroupInfo grp = groups.get(i);
492 final List<MyPermissionInfo> perms = getPermissionList(grp, which);
493 for (int j=0; j<perms.size(); j++) {
494 MyPermissionInfo perm = perms.get(j);
495 View view = getPermissionItemView(grp, perm, j == 0,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700496 which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700497 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
498 ViewGroup.LayoutParams.MATCH_PARENT,
499 ViewGroup.LayoutParams.WRAP_CONTENT);
500 if (j == 0) {
501 lp.topMargin = spacing;
502 }
503 if (j == grp.mAllPermissions.size()-1) {
504 lp.bottomMargin = spacing;
505 }
506 if (permListView.getChildCount() == 0) {
507 lp.topMargin *= 2;
508 }
509 permListView.addView(view, lp);
510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
513
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700514 private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700515 MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
516 return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
517 mPackageName, showRevokeUI);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700520 private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
521 MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
Nick Kralevichddfbe002013-04-05 18:32:07 -0700522 CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
Nick Kralevicha56b78d2013-04-07 11:13:16 -0700523 PermissionItemView permView = (PermissionItemView)inflater.inflate(
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700524 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
525 ? R.layout.app_permission_item_money : R.layout.app_permission_item,
526 null);
Nick Kralevichddfbe002013-04-05 18:32:07 -0700527 permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700528 return permView;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800529 }
530
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700531 private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800532 CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700533 View permView = inflater.inflate(R.layout.app_permission_item_old, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534
535 TextView permGrpView = (TextView) permView.findViewById(R.id.permission_group);
536 TextView permDescView = (TextView) permView.findViewById(R.id.permission_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537
538 ImageView imgView = (ImageView)permView.findViewById(R.id.perm_icon);
539 imgView.setImageDrawable(icon);
540 if(grpName != null) {
541 permGrpView.setText(grpName);
542 permDescView.setText(permList);
543 } else {
544 permGrpView.setText(permList);
545 permDescView.setVisibility(View.GONE);
546 }
547 return permView;
548 }
549
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700550 private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
551 int existingReqFlags) {
552 final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700553 final boolean isNormal = (base == PermissionInfo.PROTECTION_NORMAL);
554 final boolean isDangerous = (base == PermissionInfo.PROTECTION_DANGEROUS);
555 final boolean isRequired =
556 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
557 final boolean isDevelopment =
558 ((pInfo.protectionLevel&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0);
559 final boolean wasGranted =
560 ((existingReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700561 final boolean isGranted =
562 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700563
564 // Dangerous and normal permissions are always shown to the user if the permission
565 // is required, or it was previously granted
Nick Kralevich7a8c1352013-03-26 16:59:26 -0700566 if ((isNormal || isDangerous) && (isRequired || wasGranted || isGranted)) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700567 return true;
Dianne Hackborne639da72012-02-21 15:11:13 -0800568 }
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700569
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700570 // Development permissions are only shown to the user if they are already
571 // granted to the app -- if we are installing an app and they are not
572 // already granted, they will not be granted as part of the install.
Nick Kralevicha5043ed2013-03-25 12:11:58 -0700573 if (isDevelopment && wasGranted) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700574 if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
575 + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 return true;
577 }
578 return false;
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700581 private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700582 private final Collator sCollator = Collator.getInstance();
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700583 PermissionGroupInfoComparator() {
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700584 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700585 public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) {
586 if (((a.flags^b.flags)&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) {
587 return ((a.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) ? -1 : 1;
588 }
589 if (a.priority != b.priority) {
590 return a.priority > b.priority ? -1 : 1;
591 }
592 return sCollator.compare(a.mLabel, b.mLabel);
Dianne Hackborn0e128bb2012-05-01 14:40:15 -0700593 }
594 }
Dianne Hackborn4034bc42012-06-01 12:45:49 -0700595
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700596 private static class PermissionInfoComparator implements Comparator<MyPermissionInfo> {
597 private final Collator sCollator = Collator.getInstance();
598 PermissionInfoComparator() {
599 }
600 public final int compare(MyPermissionInfo a, MyPermissionInfo b) {
601 return sCollator.compare(a.mLabel, b.mLabel);
602 }
603 }
604
605 private void addPermToList(List<MyPermissionInfo> permList,
606 MyPermissionInfo pInfo) {
607 if (pInfo.mLabel == null) {
608 pInfo.mLabel = pInfo.loadLabel(mPm);
609 }
610 int idx = Collections.binarySearch(permList, pInfo, mPermComparator);
611 if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size());
612 if (idx < 0) {
613 idx = -idx-1;
614 permList.add(idx, pInfo);
615 }
616 }
617
618 private void setPermissions(List<MyPermissionInfo> permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 if (permList != null) {
620 // First pass to group permissions
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700621 for (MyPermissionInfo pInfo : permList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700623 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
625 continue;
626 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700627 MyPermissionGroupInfo group = mPermGroups.get(pInfo.group);
628 if (group != null) {
629 pInfo.mLabel = pInfo.loadLabel(mPm);
630 addPermToList(group.mAllPermissions, pInfo);
631 if (pInfo.mNew) {
632 addPermToList(group.mNewPermissions, pInfo);
633 }
634 if ((group.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) {
635 addPermToList(group.mPersonalPermissions, pInfo);
636 } else {
637 addPermToList(group.mDevicePermissions, pInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639 }
640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700643 for (MyPermissionGroupInfo pgrp : mPermGroups.values()) {
644 if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
645 pgrp.mLabel = pgrp.loadLabel(mPm);
646 } else {
647 ApplicationInfo app;
648 try {
649 app = mPm.getApplicationInfo(pgrp.packageName, 0);
650 pgrp.mLabel = app.loadLabel(mPm);
651 } catch (NameNotFoundException e) {
652 pgrp.mLabel = pgrp.loadLabel(mPm);
653 }
654 }
655 mPermGroupsList.add(pgrp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700657 Collections.sort(mPermGroupsList, mPermGroupComparator);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700658 if (localLOGV) {
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700659 for (MyPermissionGroupInfo grp : mPermGroupsList) {
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700660 Log.i(TAG, "Group " + grp.name + " personal="
Dianne Hackborn7454d3b2012-09-12 17:22:00 -0700661 + ((grp.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0)
662 + " priority=" + grp.priority);
663 }
664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666}