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