Home | History | Annotate | Download | only in widget
      1 /*
      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 */
     17 package android.widget;
     18 
     19 import com.android.internal.R;
     20 
     21 import android.app.AlertDialog;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.content.pm.ApplicationInfo;
     25 import android.content.pm.PackageInfo;
     26 import android.content.pm.PackageManager;
     27 import android.content.pm.PackageManager.NameNotFoundException;
     28 import android.content.pm.PermissionGroupInfo;
     29 import android.content.pm.PermissionInfo;
     30 import android.graphics.drawable.Drawable;
     31 import android.os.Parcel;
     32 import android.text.SpannableStringBuilder;
     33 import android.text.TextUtils;
     34 import android.util.AttributeSet;
     35 import android.util.Log;
     36 import android.view.LayoutInflater;
     37 import android.view.View;
     38 import android.view.ViewGroup;
     39 
     40 import java.text.Collator;
     41 import java.util.ArrayList;
     42 import java.util.Collections;
     43 import java.util.Comparator;
     44 import java.util.HashMap;
     45 import java.util.HashSet;
     46 import java.util.List;
     47 import java.util.Map;
     48 import 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  */
     61 public class AppSecurityPermissions {
     62 
     63     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;
     67 
     68     private final static String TAG = "AppSecurityPermissions";
     69     private final static boolean localLOGV = false;
     70     private final Context mContext;
     71     private final LayoutInflater mInflater;
     72     private final PackageManager mPm;
     73     private final Map<String, MyPermissionGroupInfo> mPermGroups
     74             = new HashMap<String, MyPermissionGroupInfo>();
     75     private final List<MyPermissionGroupInfo> mPermGroupsList
     76             = new ArrayList<MyPermissionGroupInfo>();
     77     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;
     81     private String mPackageName;
     82 
     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 
    115     private static class MyPermissionInfo extends PermissionInfo {
    116         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 
    134         MyPermissionInfo(PermissionInfo info) {
    135             super(info);
    136         }
    137     }
    138 
    139     public static class PermissionItemView extends LinearLayout implements View.OnClickListener {
    140         MyPermissionGroupInfo mGroup;
    141         MyPermissionInfo mPerm;
    142         AlertDialog mDialog;
    143         private boolean mShowRevokeUI = false;
    144         private String mPackageName;
    145 
    146         public PermissionItemView(Context context, AttributeSet attrs) {
    147             super(context, attrs);
    148             setClickable(true);
    149         }
    150 
    151         public void setPermission(MyPermissionGroupInfo grp, MyPermissionInfo perm,
    152                 boolean first, CharSequence newPermPrefix, String packageName,
    153                 boolean showRevokeUI) {
    154             mGroup = grp;
    155             mPerm = perm;
    156             mShowRevokeUI = showRevokeUI;
    157             mPackageName = packageName;
    158 
    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);
    184             if (localLOGV) Log.i(TAG, "Made perm item " + perm.name
    185                     + ": " + label + " in group " + grp.name);
    186         }
    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));
    216                 addRevokeUIIfNecessary(builder);
    217                 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         }
    229 
    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);
    247                     PermissionItemView.this.setVisibility(View.GONE);
    248                 }
    249             };
    250             builder.setNegativeButton(R.string.revoke, ocl);
    251             builder.setPositiveButton(R.string.ok, null);
    252         }
    253     }
    254 
    255     private AppSecurityPermissions(Context context) {
    256         mContext = context;
    257         mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    258         mPm = mContext.getPackageManager();
    259         // Pick up from framework resources instead.
    260         mNewPermPrefix = mContext.getText(R.string.perms_new_perm_prefix);
    261     }
    262 
    263     public AppSecurityPermissions(Context context, String packageName) {
    264         this(context);
    265         mPackageName = packageName;
    266         Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
    267         PackageInfo pkgInfo;
    268         try {
    269             pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
    270         } catch (NameNotFoundException e) {
    271             Log.w(TAG, "Couldn't retrieve permissions for package:"+packageName);
    272             return;
    273         }
    274         // Extract all user permissions
    275         if((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
    276             getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
    277         }
    278         mPermsList.addAll(permSet);
    279         setPermissions(mPermsList);
    280     }
    281 
    282     public AppSecurityPermissions(Context context, PackageInfo info) {
    283         this(context);
    284         Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
    285         if(info == null) {
    286             return;
    287         }
    288         mPackageName = info.packageName;
    289 
    290         // Convert to a PackageInfo
    291         PackageInfo installedPkgInfo = null;
    292         // Get requested permissions
    293         if (info.requestedPermissions != null) {
    294             try {
    295                 installedPkgInfo = mPm.getPackageInfo(info.packageName,
    296                         PackageManager.GET_PERMISSIONS);
    297             } catch (NameNotFoundException e) {
    298             }
    299             extractPerms(info, permSet, installedPkgInfo);
    300         }
    301         // Get permissions related to  shared user if any
    302         if (info.sharedUserId != null) {
    303             int sharedUid;
    304             try {
    305                 sharedUid = mPm.getUidForSharedUser(info.sharedUserId);
    306                 getAllUsedPermissions(sharedUid, permSet);
    307             } catch (NameNotFoundException e) {
    308                 Log.w(TAG, "Couldn't retrieve shared user id for: " + info.packageName);
    309             }
    310         }
    311         // Retrieve list of permissions
    312         mPermsList.addAll(permSet);
    313         setPermissions(mPermsList);
    314     }
    315 
    316     /**
    317      * 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.
    320      */
    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);
    325         Drawable icon = context.getResources().getDrawable(dangerous
    326                 ? R.drawable.ic_bullet_key_permission : R.drawable.ic_text_dot);
    327         return getPermissionItemViewOld(context, inflater, grpName,
    328                 description, dangerous, icon);
    329     }
    330 
    331     private void getAllUsedPermissions(int sharedUid, Set<MyPermissionInfo> permSet) {
    332         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 
    341     private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
    342         try {
    343             PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
    344             extractPerms(pkgInfo, permSet, pkgInfo);
    345         } catch (NameNotFoundException e) {
    346             Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
    347         }
    348     }
    349 
    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)) {
    355             return;
    356         }
    357         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             }
    366             try {
    367                 PermissionInfo tmpPermInfo = mPm.getPermissionInfo(permName, 0);
    368                 if (tmpPermInfo == null) {
    369                     continue;
    370                 }
    371                 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);
    425             } catch (NameNotFoundException e) {
    426                 Log.i(TAG, "Ignoring unknown permission:"+permName);
    427             }
    428         }
    429     }
    430 
    431     public int getPermissionCount() {
    432         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;
    453     }
    454 
    455     public View getPermissionsView() {
    456         return getPermissionsView(WHICH_ALL, false);
    457     }
    458 
    459     public View getPermissionsViewWithRevokeButtons() {
    460         return getPermissionsView(WHICH_ALL, true);
    461     }
    462 
    463     public View getPermissionsView(int which) {
    464         return getPermissionsView(which, false);
    465     }
    466 
    467     private View getPermissionsView(int which, boolean showRevokeUI) {
    468         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);
    471 
    472         displayPermissions(mPermGroupsList, displayList, which, showRevokeUI);
    473         if (displayList.getChildCount() <= 0) {
    474             noPermsView.setVisibility(View.VISIBLE);
    475         }
    476 
    477         return permsView;
    478     }
    479 
    480     /**
    481      * Utility method that displays permissions from a map containing group name and
    482      * list of permission descriptions.
    483      */
    484     private void displayPermissions(List<MyPermissionGroupInfo> groups,
    485             LinearLayout permListView, int which, boolean showRevokeUI) {
    486         permListView.removeAllViews();
    487 
    488         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,
    496                         which != WHICH_NEW ? mNewPermPrefix : null, showRevokeUI);
    497                 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             }
    511         }
    512     }
    513 
    514     private PermissionItemView getPermissionItemView(MyPermissionGroupInfo grp,
    515             MyPermissionInfo perm, boolean first, CharSequence newPermPrefix, boolean showRevokeUI) {
    516         return getPermissionItemView(mContext, mInflater, grp, perm, first, newPermPrefix,
    517                 mPackageName, showRevokeUI);
    518     }
    519 
    520     private static PermissionItemView getPermissionItemView(Context context, LayoutInflater inflater,
    521             MyPermissionGroupInfo grp, MyPermissionInfo perm, boolean first,
    522             CharSequence newPermPrefix, String packageName, boolean showRevokeUI) {
    523             PermissionItemView permView = (PermissionItemView)inflater.inflate(
    524                 (perm.flags & PermissionInfo.FLAG_COSTS_MONEY) != 0
    525                         ? R.layout.app_permission_item_money : R.layout.app_permission_item,
    526                 null);
    527         permView.setPermission(grp, perm, first, newPermPrefix, packageName, showRevokeUI);
    528         return permView;
    529     }
    530 
    531     private static View getPermissionItemViewOld(Context context, LayoutInflater inflater,
    532             CharSequence grpName, CharSequence permList, boolean dangerous, Drawable icon) {
    533         View permView = inflater.inflate(R.layout.app_permission_item_old, null);
    534 
    535         TextView permGrpView = (TextView) permView.findViewById(R.id.permission_group);
    536         TextView permDescView = (TextView) permView.findViewById(R.id.permission_list);
    537 
    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 
    550     private boolean isDisplayablePermission(PermissionInfo pInfo, int newReqFlags,
    551             int existingReqFlags) {
    552         final int base = pInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
    553         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);
    561         final boolean isGranted =
    562                 ((newReqFlags&PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);
    563 
    564         // Dangerous and normal permissions are always shown to the user if the permission
    565         // is required, or it was previously granted
    566         if ((isNormal || isDangerous) && (isRequired || wasGranted || isGranted)) {
    567             return true;
    568         }
    569 
    570         // 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.
    573         if (isDevelopment && wasGranted) {
    574             if (localLOGV) Log.i(TAG, "Special perm " + pInfo.name
    575                     + ": protlevel=0x" + Integer.toHexString(pInfo.protectionLevel));
    576             return true;
    577         }
    578         return false;
    579     }
    580 
    581     private static class PermissionGroupInfoComparator implements Comparator<MyPermissionGroupInfo> {
    582         private final Collator sCollator = Collator.getInstance();
    583         PermissionGroupInfoComparator() {
    584         }
    585         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);
    593         }
    594     }
    595 
    596     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) {
    619         if (permList != null) {
    620             // First pass to group permissions
    621             for (MyPermissionInfo pInfo : permList) {
    622                 if(localLOGV) Log.i(TAG, "Processing permission:"+pInfo.name);
    623                 if(!isDisplayablePermission(pInfo, pInfo.mNewReqFlags, pInfo.mExistingReqFlags)) {
    624                     if(localLOGV) Log.i(TAG, "Permission:"+pInfo.name+" is not displayable");
    625                     continue;
    626                 }
    627                 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);
    638                     }
    639                 }
    640             }
    641         }
    642 
    643         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);
    656         }
    657         Collections.sort(mPermGroupsList, mPermGroupComparator);
    658         if (localLOGV) {
    659             for (MyPermissionGroupInfo grp : mPermGroupsList) {
    660                 Log.i(TAG, "Group " + grp.name + " personal="
    661                         + ((grp.flags&PermissionGroupInfo.FLAG_PERSONAL_INFO) != 0)
    662                         + " priority=" + grp.priority);
    663             }
    664         }
    665     }
    666 }
    667