From 0e128bb2e03dafdabc06710a6b0ea93f0e62a188 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 1 May 2012 14:40:15 -0700 Subject: New permissions UI. Had to keep around a little of the old API for the device admin settings UI to continue to use. Change-Id: I84c5666a7f3fec0495eae196a0ffc2223c9e6f82 --- core/java/android/content/Intent.java | 4 + .../android/widget/AppSecurityPermissions.java | 592 ++++++++++----------- core/res/res/layout/app_permission_item.xml | 37 +- core/res/res/layout/app_permission_item_old.xml | 55 ++ core/res/res/layout/app_perms_summary.xml | 79 +-- core/res/res/values/public.xml | 9 +- core/res/res/values/strings.xml | 2 +- 7 files changed, 375 insertions(+), 403 deletions(-) create mode 100644 core/res/res/layout/app_permission_item_old.xml (limited to 'core') diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java index b4669a7..cb8fea2 100644 --- a/core/java/android/content/Intent.java +++ b/core/java/android/content/Intent.java @@ -1240,7 +1240,11 @@ public class Intent implements Parcelable, Cloneable { * Used as a boolean extra field with {@link #ACTION_INSTALL_PACKAGE} to install a * package. Tells the installer UI to skip the confirmation with the user * if the .apk is replacing an existing one. + * @deprecated As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, Android + * will no longer show an interstitial message about updating existing + * applications so this is no longer needed. */ + @Deprecated public static final String EXTRA_ALLOW_REPLACE = "android.intent.extra.ALLOW_REPLACE"; diff --git a/core/java/android/widget/AppSecurityPermissions.java b/core/java/android/widget/AppSecurityPermissions.java index b409e26..5d8bdbb 100755 --- a/core/java/android/widget/AppSecurityPermissions.java +++ b/core/java/android/widget/AppSecurityPermissions.java @@ -18,7 +18,9 @@ package android.widget; import com.android.internal.R; +import android.app.AlertDialog; import android.content.Context; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -27,14 +29,13 @@ import android.content.pm.PermissionGroupInfo; import android.content.pm.PermissionInfo; import android.graphics.drawable.Drawable; import android.os.Parcel; -import android.text.Spannable; -import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.TextUtils; -import android.text.style.ForegroundColorSpan; +import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import java.text.Collator; import java.util.ArrayList; @@ -42,7 +43,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -58,16 +58,51 @@ import java.util.Set; * * {@hide} */ -public class AppSecurityPermissions implements View.OnClickListener { +public class AppSecurityPermissions { - private enum State { - NO_PERMS, - DANGEROUS_ONLY, - NORMAL_ONLY, - BOTH + public static final int WHICH_PERSONAL = 1<<0; + public static final int WHICH_DEVICE = 1<<1; + public static final int WHICH_NEW = 1<<2; + public static final int WHICH_ALL = 0xffff; + + private final static String TAG = "AppSecurityPermissions"; + private boolean localLOGV = false; + private Context mContext; + private LayoutInflater mInflater; + private PackageManager mPm; + private PackageInfo mInstalledPackageInfo; + private final Map mPermGroups + = new HashMap(); + private final List mPermGroupsList + = new ArrayList(); + private final PermissionGroupInfoComparator mPermGroupComparator; + private final PermissionInfoComparator mPermComparator; + private List mPermsList; + private CharSequence mNewPermPrefix; + private Drawable mNormalIcon; + private Drawable mDangerousIcon; + + static class MyPermissionGroupInfo extends PermissionGroupInfo { + CharSequence mLabel; + + final ArrayList mNewPermissions = new ArrayList(); + final ArrayList mPersonalPermissions = new ArrayList(); + final ArrayList mDevicePermissions = new ArrayList(); + final ArrayList mAllPermissions = new ArrayList(); + + MyPermissionGroupInfo(PermissionInfo perm) { + name = perm.packageName; + packageName = perm.packageName; + } + + MyPermissionGroupInfo(PermissionGroupInfo info) { + super(info); + } } static class MyPermissionInfo extends PermissionInfo { + CharSequence mLabel; + /** * PackageInfo.requestedPermissionsFlags for the new package being installed. */ @@ -99,46 +134,99 @@ public class AppSecurityPermissions implements View.OnClickListener { } } - private final static String TAG = "AppSecurityPermissions"; - private boolean localLOGV = false; - private Context mContext; - private LayoutInflater mInflater; - private PackageManager mPm; - private LinearLayout mPermsView; - private Map mNewMap; - private Map mDangerousMap; - private Map mNormalMap; - private List mPermsList; - private String mDefaultGrpLabel; - private String mDefaultGrpName="DefaultGrp"; - private String mPermFormat; - private CharSequence mNewPermPrefix; - private Drawable mNormalIcon; - private Drawable mDangerousIcon; - private boolean mExpanded; - private Drawable mShowMaxIcon; - private Drawable mShowMinIcon; - private View mShowMore; - private TextView mShowMoreText; - private ImageView mShowMoreIcon; - private State mCurrentState; - private LinearLayout mNonDangerousList; - private LinearLayout mDangerousList; - private LinearLayout mNewList; - private HashMap mGroupLabelCache; - private View mNoPermsView; + public static class PermissionItemView extends LinearLayout implements View.OnClickListener { + MyPermissionGroupInfo mGroup; + MyPermissionInfo mPerm; + AlertDialog mDialog; + + public PermissionItemView(Context context, AttributeSet attrs) { + super(context, attrs); + setClickable(true); + } + + public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm, + boolean first, CharSequence newPermPrefix) { + mGroup = grp; + mPerm = perm; + + ImageView permGrpIcon = (ImageView) findViewById(R.id.perm_icon); + TextView permNameView = (TextView) findViewById(R.id.perm_name); + + PackageManager pm = getContext().getPackageManager(); + Drawable icon = null; + if (first) { + if (grp.icon != 0) { + icon = grp.loadIcon(pm); + } else { + ApplicationInfo appInfo; + try { + appInfo = pm.getApplicationInfo(grp.packageName, 0); + icon = appInfo.loadIcon(pm); + } catch (NameNotFoundException e) { + } + } + } + CharSequence label = perm.mLabel; + if (perm.mNew && newPermPrefix != null) { + // If this is a new permission, format it appropriately. + SpannableStringBuilder builder = new SpannableStringBuilder(); + Parcel parcel = Parcel.obtain(); + TextUtils.writeToParcel(newPermPrefix, parcel, 0); + parcel.setDataPosition(0); + CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); + parcel.recycle(); + builder.append(newStr); + builder.append(label); + label = builder; + } + + permGrpIcon.setImageDrawable(icon); + permNameView.setText(label); + setOnClickListener(this); + } + + @Override + public void onClick(View v) { + if (mGroup != null && mPerm != null) { + if (mDialog != null) { + mDialog.dismiss(); + } + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(mGroup.mLabel); + builder.setMessage(mPerm.loadDescription(getContext().getPackageManager())); + builder.setCancelable(true); + mDialog = builder.show(); + mDialog.setCanceledOnTouchOutside(true); + } + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + if (mDialog != null) { + mDialog.dismiss(); + } + } + } public AppSecurityPermissions(Context context, List permList) { mContext = context; mPm = mContext.getPackageManager(); + loadResources(); + mPermComparator = new PermissionInfoComparator(); + mPermGroupComparator = new PermissionGroupInfoComparator(); for (PermissionInfo pi : permList) { mPermsList.add(new MyPermissionInfo(pi)); } + setPermissions(mPermsList); } public AppSecurityPermissions(Context context, String packageName) { mContext = context; mPm = mContext.getPackageManager(); + loadResources(); + mPermComparator = new PermissionInfoComparator(); + mPermGroupComparator = new PermissionGroupInfoComparator(); mPermsList = new ArrayList(); Set permSet = new HashSet(); PackageInfo pkgInfo; @@ -155,11 +243,15 @@ public class AppSecurityPermissions implements View.OnClickListener { for(MyPermissionInfo tmpInfo : permSet) { mPermsList.add(tmpInfo); } + setPermissions(mPermsList); } - + public AppSecurityPermissions(Context context, PackageParser.Package pkg) { mContext = context; mPm = mContext.getPackageManager(); + loadResources(); + mPermComparator = new PermissionInfoComparator(); + mPermGroupComparator = new PermissionGroupInfoComparator(); mPermsList = new ArrayList(); Set permSet = new HashSet(); if(pkg == null) { @@ -193,10 +285,20 @@ public class AppSecurityPermissions implements View.OnClickListener { for (MyPermissionInfo tmpInfo : permSet) { mPermsList.add(tmpInfo); } + setPermissions(mPermsList); } - + + private void loadResources() { + // Pick up from framework resources instead. + mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix); + mNormalIcon = mContext.getResources().getDrawable(R.drawable.ic_text_dot); + mDangerousIcon = mContext.getResources().getDrawable(R.drawable.ic_bullet_key_permission); + } + /** - * Utility to retrieve a view displaying a single permission. + * Utility to retrieve a view displaying a single permission. This provides + * the old UI layout for permissions; it is only here for the device admin + * settings to continue to use. */ public static View getPermissionItemView(Context context, CharSequence grpName, CharSequence description, boolean dangerous) { @@ -204,10 +306,14 @@ public class AppSecurityPermissions implements View.OnClickListener { Context.LAYOUT_INFLATER_SERVICE); Drawable icon = context.getResources().getDrawable(dangerous ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot); - return getPermissionItemView(context, inflater, grpName, + return getPermissionItemViewOld(context, inflater, grpName, description, dangerous, icon); } + public PackageInfo getInstalledPackageInfo() { + return mInstalledPackageInfo; + } + private void getAllUsedPermissions(int sharedUid, Set permSet) { String sharedPkgList[] = mPm.getPackagesForUid(sharedUid); if(sharedPkgList == null || (sharedPkgList.length == 0)) { @@ -239,6 +345,7 @@ public class AppSecurityPermissions implements View.OnClickListener { if ((strList == null) || (strList.length == 0)) { return; } + mInstalledPackageInfo = installedPkgInfo; for (int i=0; i getPermissionList(MyPermissionGroupInfo grp, int which) { + if (which == WHICH_NEW) { + return grp.mNewPermissions; + } else if (which == WHICH_PERSONAL) { + return grp.mPersonalPermissions; + } else if (which == WHICH_DEVICE) { + return grp.mDevicePermissions; + } else { + return grp.mAllPermissions; + } } - /** - * Utility method that concatenates two strings defined by mPermFormat. - * a null value is returned if both str1 and str2 are null, if one of the strings - * is null the other non null value is returned without formatting - * this is to placate initial error checks - */ - private CharSequence formatPermissions(CharSequence groupDesc, CharSequence permDesc, - boolean newPerms) { - if (permDesc == null) { - return groupDesc; - } - // Sometimes people write permission names with a trailing period; - // strip that if it appears. - int len = permDesc.length(); - if (len > 0 && permDesc.charAt(len-1) == '.') { - permDesc = (permDesc.toString()).substring(0, len-1); - } - if (newPerms) { - if (true) { - // If this is a new permission, format it appropriately. - SpannableStringBuilder builder = new SpannableStringBuilder(); - if (groupDesc != null) { - // The previous permissions go in front, with a newline - // separating them. - builder.append(groupDesc); - builder.append("\n"); - } - Parcel parcel = Parcel.obtain(); - TextUtils.writeToParcel(mNewPermPrefix, parcel, 0); - parcel.setDataPosition(0); - CharSequence newStr = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); - parcel.recycle(); - builder.append(newStr); - builder.append(permDesc); - return builder; - } else { - // If this is a new permission, format it appropriately. - SpannableStringBuilder builder = new SpannableStringBuilder(permDesc); - builder.insert(0, mNewPermPrefix); - if (groupDesc != null) { - // The previous permissions go in front, with a newline - // separating them. - builder.insert(0, "\n"); - builder.insert(0, groupDesc); - } - return builder; - } - } - if (groupDesc == null) { - return permDesc; + public int getPermissionCount(int which) { + int N = 0; + for (int i=0; i permInfoMap, - LinearLayout permListView, boolean dangerous) { + private void displayPermissions(List groups, + LinearLayout permListView, int which) { permListView.removeAllViews(); - Set permInfoStrSet = permInfoMap.keySet(); - for (String loopPermGrpInfoStr : permInfoStrSet) { - CharSequence grpLabel = getGroupLabel(loopPermGrpInfoStr); - //guaranteed that grpLabel wont be null since permissions without groups - //will belong to the default group - if(localLOGV) Log.i(TAG, "Adding view group:" + grpLabel + ", desc:" - + permInfoMap.get(loopPermGrpInfoStr)); - permListView.addView(getPermissionItemView(grpLabel, - permInfoMap.get(loopPermGrpInfoStr), dangerous)); + int spacing = (int)(8*mContext.getResources().getDisplayMetrics().density); + + for (int i=0; i perms = getPermissionList(grp, which); + for (int j=0; j > map, - Map retMap, boolean newPerms) { - if(map == null) { - return; + + private static class PermissionGroupInfoComparator implements Comparator { + private final Collator sCollator = Collator.getInstance(); + PermissionGroupInfoComparator() { } - if(retMap == null) { - return; - } - Set grpNames = map.keySet(); - Iterator grpNamesIter = grpNames.iterator(); - while(grpNamesIter.hasNext()) { - CharSequence grpDesc = null; - String grpNameKey = grpNamesIter.next(); - List grpPermsList = map.get(grpNameKey); - if(grpPermsList == null) { - continue; - } - for(PermissionInfo permInfo: grpPermsList) { - CharSequence permDesc = permInfo.loadLabel(mPm); - grpDesc = formatPermissions(grpDesc, permDesc, newPerms); + public final int compare(MyPermissionGroupInfo a, MyPermissionGroupInfo b) { + if (((a.flags^b.flags)&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) { + return ((a.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) ? -1 : 1; } - // Insert grpDesc into map - if(grpDesc != null) { - if(localLOGV) Log.i(TAG, "Group:"+grpNameKey+" description:"+grpDesc.toString()); - retMap.put(grpNameKey, grpDesc); + if (a.priority != b.priority) { + return a.priority > b.priority ? -1 : 1; } + return sCollator.compare(a.mLabel, b.mLabel); } } - private static class PermissionInfoComparator implements Comparator { - private PackageManager mPm; + private static class PermissionInfoComparator implements Comparator { private final Collator sCollator = Collator.getInstance(); - PermissionInfoComparator(PackageManager pm) { - mPm = pm; + PermissionInfoComparator() { } - public final int compare(PermissionInfo a, PermissionInfo b) { - CharSequence sa = a.loadLabel(mPm); - CharSequence sb = b.loadLabel(mPm); - return sCollator.compare(sa, sb); + public final int compare(MyPermissionInfo a, MyPermissionInfo b) { + return sCollator.compare(a.mLabel, b.mLabel); } } - + + private void addPermToList(List permList, + MyPermissionInfo pInfo) { + if (pInfo.mLabel == null) { + pInfo.mLabel = pInfo.loadLabel(mPm); + } + int idx = Collections.binarySearch(permList, pInfo, mPermComparator); + if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+permList.size()); + if (idx < 0) { + idx = -idx-1; + permList.add(idx, pInfo); + } + } + private void setPermissions(List permList) { - mGroupLabelCache = new HashMap(); - //add the default label so that uncategorized permissions can go here - mGroupLabelCache.put(mDefaultGrpName, mDefaultGrpLabel); - - // Map containing group names and a list of permissions under that group - // that are new from the current install - mNewMap = new HashMap(); - // Map containing group names and a list of permissions under that group - // categorized as dangerous - mDangerousMap = new HashMap(); - // Map containing group names and a list of permissions under that group - // categorized as normal - mNormalMap = new HashMap(); - - // Additional structures needed to ensure that permissions are unique under - // each group - Map> newMap = - new HashMap>(); - Map> dangerousMap = - new HashMap>(); - Map > normalMap = - new HashMap>(); - PermissionInfoComparator permComparator = new PermissionInfoComparator(mPm); - if (permList != null) { // First pass to group permissions for (MyPermissionInfo pInfo : permList) { @@ -577,51 +594,26 @@ public class AppSecurityPermissions implements View.OnClickListener { if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable"); continue; } - Map > permInfoMap; - if (pInfo.mNew) { - permInfoMap = newMap; - } else if ((pInfo.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) - == PermissionInfo.PROTECTION_DANGEROUS) { - permInfoMap = dangerousMap; - } else { - permInfoMap = normalMap; - } - String grpName = (pInfo.group == null) ? mDefaultGrpName : pInfo.group; - if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" belongs to group:"+grpName); - List grpPermsList = permInfoMap.get(grpName); - if(grpPermsList == null) { - grpPermsList = new ArrayList(); - permInfoMap.put(grpName, grpPermsList); - grpPermsList.add(pInfo); - } else { - int idx = Collections.binarySearch(grpPermsList, pInfo, permComparator); - if(localLOGV) Log.i(TAG, "idx="+idx+", list.size="+grpPermsList.size()); - if (idx < 0) { - idx = -idx-1; - grpPermsList.add(idx, pInfo); + MyPermissionGroupInfo group = mPermGroups.get(pInfo.group); + if (group != null) { + pInfo.mLabel = pInfo.loadLabel(mPm); + addPermToList(group.mAllPermissions, pInfo); + if (pInfo.mNew) { + addPermToList(group.mNewPermissions, pInfo); + } + if ((group.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0) { + addPermToList(group.mPersonalPermissions, pInfo); + } else { + addPermToList(group.mDevicePermissions, pInfo); } } } - // Second pass to actually form the descriptions - // Look at dangerous permissions first - aggregateGroupDescs(newMap, mNewMap, true); - aggregateGroupDescs(dangerousMap, mDangerousMap, false); - aggregateGroupDescs(normalMap, mNormalMap, false); } - mCurrentState = State.NO_PERMS; - if (mNewMap.size() > 0 || mDangerousMap.size() > 0) { - mCurrentState = (mNormalMap.size() > 0) ? State.BOTH : State.DANGEROUS_ONLY; - } else if(mNormalMap.size() > 0) { - mCurrentState = State.NORMAL_ONLY; + for (MyPermissionGroupInfo pgrp : mPermGroups.values()) { + pgrp.mLabel = pgrp.loadLabel(mPm); + mPermGroupsList.add(pgrp); } - if(localLOGV) Log.i(TAG, "mCurrentState=" + mCurrentState); - showPermissions(); - } - - public void onClick(View v) { - if(localLOGV) Log.i(TAG, "mExpanded="+mExpanded); - mExpanded = !mExpanded; - showPermissions(); + Collections.sort(mPermGroupsList, mPermGroupComparator); } } diff --git a/core/res/res/layout/app_permission_item.xml b/core/res/res/layout/app_permission_item.xml index 1bd267f..9b8c5ae 100644 --- a/core/res/res/layout/app_permission_item.xml +++ b/core/res/res/layout/app_permission_item.xml @@ -19,37 +19,32 @@ Contains the group name and a list of permission labels under the group. --> - + android:layout_height="wrap_content" + android:orientation="horizontal" + android:background="?android:attr/selectableItemBackground"> - - + android:layout_height="match_parent" + android:background="?android:attr/dividerVertical" /> + android:layout_height="wrap_content" + android:layout_gravity="top|left" /> - + diff --git a/core/res/res/layout/app_permission_item_old.xml b/core/res/res/layout/app_permission_item_old.xml new file mode 100644 index 0000000..1bd267f --- /dev/null +++ b/core/res/res/layout/app_permission_item_old.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + diff --git a/core/res/res/layout/app_perms_summary.xml b/core/res/res/layout/app_perms_summary.xml index 77dbc2e..509c502 100755 --- a/core/res/res/layout/app_perms_summary.xml +++ b/core/res/res/layout/app_perms_summary.xml @@ -26,88 +26,17 @@ android:id="@+id/no_permissions" android:text="@string/no_permissions" android:textAppearance="?android:attr/textAppearanceMedium" - android:paddingLeft="16dip" - android:paddingRight="12dip" + android:paddingLeft="8dp" + android:paddingRight="8dp" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> - + - - - - - - - - - - - - - - - - - - - - - - - diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index 978e2a8..2387e7a 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -63,7 +63,6 @@ - @@ -117,9 +116,7 @@ - - @@ -127,7 +124,9 @@ + + @@ -163,9 +162,6 @@ - - - @@ -1044,6 +1040,7 @@ + diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index d779f72..3250507 100755 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2981,7 +2981,7 @@ %1$s, %2$s - NEW: + NEW: No permissions required -- cgit v1.1