1 /**
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 
17 package com.android.settings.applications;
18 
19 import android.app.AppOpsManager;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.content.res.Resources;
26 import android.graphics.drawable.Drawable;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.text.format.DateUtils;
30 import android.util.Log;
31 import android.util.SparseArray;
32 
33 import com.android.settings.R;
34 
35 import java.io.File;
36 import java.text.Collator;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.HashMap;
41 import java.util.List;
42 
43 public class AppOpsState {
44     static final String TAG = "AppOpsState";
45     static final boolean DEBUG = false;
46 
47     final Context mContext;
48     final AppOpsManager mAppOps;
49     final PackageManager mPm;
50     final CharSequence[] mOpSummaries;
51     final CharSequence[] mOpLabels;
52 
53     List<AppOpEntry> mApps;
54 
AppOpsState(Context context)55     public AppOpsState(Context context) {
56         mContext = context;
57         mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
58         mPm = context.getPackageManager();
59         mOpSummaries = context.getResources().getTextArray(R.array.app_ops_summaries);
60         mOpLabels = context.getResources().getTextArray(R.array.app_ops_labels);
61     }
62 
63     public static class OpsTemplate implements Parcelable {
64         public final int[] ops;
65         public final boolean[] showPerms;
66 
OpsTemplate(int[] _ops, boolean[] _showPerms)67         public OpsTemplate(int[] _ops, boolean[] _showPerms) {
68             ops = _ops;
69             showPerms = _showPerms;
70         }
71 
OpsTemplate(Parcel src)72         OpsTemplate(Parcel src) {
73             ops = src.createIntArray();
74             showPerms = src.createBooleanArray();
75         }
76 
77         @Override
describeContents()78         public int describeContents() {
79             return 0;
80         }
81 
82         @Override
writeToParcel(Parcel dest, int flags)83         public void writeToParcel(Parcel dest, int flags) {
84             dest.writeIntArray(ops);
85             dest.writeBooleanArray(showPerms);
86         }
87 
88         public static final Creator<OpsTemplate> CREATOR = new Creator<OpsTemplate>() {
89             @Override public OpsTemplate createFromParcel(Parcel source) {
90                 return new OpsTemplate(source);
91             }
92 
93             @Override public OpsTemplate[] newArray(int size) {
94                 return new OpsTemplate[size];
95             }
96         };
97     }
98 
99     public static final OpsTemplate LOCATION_TEMPLATE = new OpsTemplate(
100             new int[] { AppOpsManager.OP_COARSE_LOCATION,
101                     AppOpsManager.OP_FINE_LOCATION,
102                     AppOpsManager.OP_GPS,
103                     AppOpsManager.OP_WIFI_SCAN,
104                     AppOpsManager.OP_NEIGHBORING_CELLS,
105                     AppOpsManager.OP_MONITOR_LOCATION,
106                     AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION },
107             new boolean[] { true,
108                     true,
109                     false,
110                     false,
111                     false,
112                     false,
113                     false }
114             );
115 
116     public static final OpsTemplate PERSONAL_TEMPLATE = new OpsTemplate(
117             new int[] { AppOpsManager.OP_READ_CONTACTS,
118                     AppOpsManager.OP_WRITE_CONTACTS,
119                     AppOpsManager.OP_READ_CALL_LOG,
120                     AppOpsManager.OP_WRITE_CALL_LOG,
121                     AppOpsManager.OP_READ_CALENDAR,
122                     AppOpsManager.OP_WRITE_CALENDAR,
123                     AppOpsManager.OP_READ_CLIPBOARD,
124                     AppOpsManager.OP_WRITE_CLIPBOARD },
125             new boolean[] { true,
126                     true,
127                     true,
128                     true,
129                     true,
130                     true,
131                     false,
132                     false }
133             );
134 
135     public static final OpsTemplate MESSAGING_TEMPLATE = new OpsTemplate(
136             new int[] { AppOpsManager.OP_READ_SMS,
137                     AppOpsManager.OP_RECEIVE_SMS,
138                     AppOpsManager.OP_RECEIVE_EMERGECY_SMS,
139                     AppOpsManager.OP_RECEIVE_MMS,
140                     AppOpsManager.OP_RECEIVE_WAP_PUSH,
141                     AppOpsManager.OP_WRITE_SMS,
142                     AppOpsManager.OP_SEND_SMS,
143                     AppOpsManager.OP_READ_ICC_SMS,
144                     AppOpsManager.OP_WRITE_ICC_SMS },
145             new boolean[] { true,
146                     true,
147                     true,
148                     true,
149                     true,
150                     true,
151                     true,
152                     true,
153                     true }
154             );
155 
156     public static final OpsTemplate MEDIA_TEMPLATE = new OpsTemplate(
157             new int[] { AppOpsManager.OP_VIBRATE,
158                     AppOpsManager.OP_CAMERA,
159                     AppOpsManager.OP_RECORD_AUDIO,
160                     AppOpsManager.OP_PLAY_AUDIO,
161                     AppOpsManager.OP_TAKE_MEDIA_BUTTONS,
162                     AppOpsManager.OP_TAKE_AUDIO_FOCUS,
163                     AppOpsManager.OP_AUDIO_MASTER_VOLUME,
164                     AppOpsManager.OP_AUDIO_VOICE_VOLUME,
165                     AppOpsManager.OP_AUDIO_RING_VOLUME,
166                     AppOpsManager.OP_AUDIO_MEDIA_VOLUME,
167                     AppOpsManager.OP_AUDIO_ALARM_VOLUME,
168                     AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME,
169                     AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME,
170                     AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME,
171                     AppOpsManager.OP_MUTE_MICROPHONE},
172             new boolean[] { false,
173                     true,
174                     true,
175                     false,
176                     false,
177                     false,
178                     false,
179                     false,
180                     false,
181                     false,
182                     false,
183                     false,
184                     false,
185                     false }
186             );
187 
188     public static final OpsTemplate DEVICE_TEMPLATE = new OpsTemplate(
189             new int[] { AppOpsManager.OP_POST_NOTIFICATION,
190                     AppOpsManager.OP_ACCESS_NOTIFICATIONS,
191                     AppOpsManager.OP_CALL_PHONE,
192                     AppOpsManager.OP_WRITE_SETTINGS,
193                     AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
194                     AppOpsManager.OP_WAKE_LOCK,
195                     AppOpsManager.OP_PROJECT_MEDIA,
196                     AppOpsManager.OP_ACTIVATE_VPN,
197                     AppOpsManager.OP_ASSIST_STRUCTURE,
198                     AppOpsManager.OP_ASSIST_SCREENSHOT},
199             new boolean[] { false,
200                     true,
201                     true,
202                     true,
203                     true,
204                     true,
205                     false,
206                     false,
207                     false,
208                     false }
209             );
210 
211     public static final OpsTemplate RUN_IN_BACKGROUND_TEMPLATE = new OpsTemplate(
212             new int[] { AppOpsManager.OP_RUN_IN_BACKGROUND },
213             new boolean[] { false }
214             );
215 
216     public static final OpsTemplate[] ALL_TEMPLATES = new OpsTemplate[] {
217             LOCATION_TEMPLATE, PERSONAL_TEMPLATE, MESSAGING_TEMPLATE,
218             MEDIA_TEMPLATE, DEVICE_TEMPLATE, RUN_IN_BACKGROUND_TEMPLATE
219     };
220 
221     /**
222      * This class holds the per-item data in our Loader.
223      */
224     public static class AppEntry {
225         private final AppOpsState mState;
226         private final ApplicationInfo mInfo;
227         private final File mApkFile;
228         private final SparseArray<AppOpsManager.OpEntry> mOps
229                 = new SparseArray<AppOpsManager.OpEntry>();
230         private final SparseArray<AppOpEntry> mOpSwitches
231                 = new SparseArray<AppOpEntry>();
232         private String mLabel;
233         private Drawable mIcon;
234         private boolean mMounted;
235 
AppEntry(AppOpsState state, ApplicationInfo info)236         public AppEntry(AppOpsState state, ApplicationInfo info) {
237             mState = state;
238             mInfo = info;
239             mApkFile = new File(info.sourceDir);
240         }
241 
addOp(AppOpEntry entry, AppOpsManager.OpEntry op)242         public void addOp(AppOpEntry entry, AppOpsManager.OpEntry op) {
243             mOps.put(op.getOp(), op);
244             mOpSwitches.put(AppOpsManager.opToSwitch(op.getOp()), entry);
245         }
246 
hasOp(int op)247         public boolean hasOp(int op) {
248             return mOps.indexOfKey(op) >= 0;
249         }
250 
getOpSwitch(int op)251         public AppOpEntry getOpSwitch(int op) {
252             return mOpSwitches.get(AppOpsManager.opToSwitch(op));
253         }
254 
getApplicationInfo()255         public ApplicationInfo getApplicationInfo() {
256             return mInfo;
257         }
258 
getLabel()259         public String getLabel() {
260             return mLabel;
261         }
262 
getIcon()263         public Drawable getIcon() {
264             if (mIcon == null) {
265                 if (mApkFile.exists()) {
266                     mIcon = mInfo.loadIcon(mState.mPm);
267                     return mIcon;
268                 } else {
269                     mMounted = false;
270                 }
271             } else if (!mMounted) {
272                 // If the app wasn't mounted but is now mounted, reload
273                 // its icon.
274                 if (mApkFile.exists()) {
275                     mMounted = true;
276                     mIcon = mInfo.loadIcon(mState.mPm);
277                     return mIcon;
278                 }
279             } else {
280                 return mIcon;
281             }
282 
283             return mState.mContext.getDrawable(
284                     android.R.drawable.sym_def_app_icon);
285         }
286 
toString()287         @Override public String toString() {
288             return mLabel;
289         }
290 
loadLabel(Context context)291         void loadLabel(Context context) {
292             if (mLabel == null || !mMounted) {
293                 if (!mApkFile.exists()) {
294                     mMounted = false;
295                     mLabel = mInfo.packageName;
296                 } else {
297                     mMounted = true;
298                     CharSequence label = mInfo.loadLabel(context.getPackageManager());
299                     mLabel = label != null ? label.toString() : mInfo.packageName;
300                 }
301             }
302         }
303     }
304 
305     /**
306      * This class holds the per-item data in our Loader.
307      */
308     public static class AppOpEntry {
309         private final AppOpsManager.PackageOps mPkgOps;
310         private final ArrayList<AppOpsManager.OpEntry> mOps
311                 = new ArrayList<AppOpsManager.OpEntry>();
312         private final ArrayList<AppOpsManager.OpEntry> mSwitchOps
313                 = new ArrayList<AppOpsManager.OpEntry>();
314         private final AppEntry mApp;
315         private final int mSwitchOrder;
316         private int mOverriddenPrimaryMode = -1;
317 
AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app, int switchOrder)318         public AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app,
319                 int switchOrder) {
320             mPkgOps = pkg;
321             mApp = app;
322             mSwitchOrder = switchOrder;
323             mApp.addOp(this, op);
324             mOps.add(op);
325             mSwitchOps.add(op);
326         }
327 
addOp(ArrayList<AppOpsManager.OpEntry> list, AppOpsManager.OpEntry op)328         private static void addOp(ArrayList<AppOpsManager.OpEntry> list, AppOpsManager.OpEntry op) {
329             for (int i=0; i<list.size(); i++) {
330                 AppOpsManager.OpEntry pos = list.get(i);
331                 if (pos.isRunning() != op.isRunning()) {
332                     if (op.isRunning()) {
333                         list.add(i, op);
334                         return;
335                     }
336                     continue;
337                 }
338                 if (pos.getTime() < op.getTime()) {
339                     list.add(i, op);
340                     return;
341                 }
342             }
343             list.add(op);
344         }
345 
addOp(AppOpsManager.OpEntry op)346         public void addOp(AppOpsManager.OpEntry op) {
347             mApp.addOp(this, op);
348             addOp(mOps, op);
349             if (mApp.getOpSwitch(AppOpsManager.opToSwitch(op.getOp())) == null) {
350                 addOp(mSwitchOps, op);
351             }
352         }
353 
getAppEntry()354         public AppEntry getAppEntry() {
355             return mApp;
356         }
357 
getSwitchOrder()358         public int getSwitchOrder() {
359             return mSwitchOrder;
360         }
361 
getPackageOps()362         public AppOpsManager.PackageOps getPackageOps() {
363             return mPkgOps;
364         }
365 
getNumOpEntry()366         public int getNumOpEntry() {
367             return mOps.size();
368         }
369 
getOpEntry(int pos)370         public AppOpsManager.OpEntry getOpEntry(int pos) {
371             return mOps.get(pos);
372         }
373 
getPrimaryOpMode()374         public int getPrimaryOpMode() {
375             return mOverriddenPrimaryMode >= 0 ? mOverriddenPrimaryMode : mOps.get(0).getMode();
376         }
377 
overridePrimaryOpMode(int mode)378         public void overridePrimaryOpMode(int mode) {
379             mOverriddenPrimaryMode = mode;
380         }
381 
getCombinedText(ArrayList<AppOpsManager.OpEntry> ops, CharSequence[] items)382         private CharSequence getCombinedText(ArrayList<AppOpsManager.OpEntry> ops,
383                 CharSequence[] items) {
384             if (ops.size() == 1) {
385                 return items[ops.get(0).getOp()];
386             } else {
387                 StringBuilder builder = new StringBuilder();
388                 for (int i=0; i<ops.size(); i++) {
389                     if (i > 0) {
390                         builder.append(", ");
391                     }
392                     builder.append(items[ops.get(i).getOp()]);
393                 }
394                 return builder.toString();
395             }
396         }
397 
getSummaryText(AppOpsState state)398         public CharSequence getSummaryText(AppOpsState state) {
399             return getCombinedText(mOps, state.mOpSummaries);
400         }
401 
getSwitchText(AppOpsState state)402         public CharSequence getSwitchText(AppOpsState state) {
403             if (mSwitchOps.size() > 0) {
404                 return getCombinedText(mSwitchOps, state.mOpLabels);
405             } else {
406                 return getCombinedText(mOps, state.mOpLabels);
407             }
408         }
409 
getTimeText(Resources res, boolean showEmptyText)410         public CharSequence getTimeText(Resources res, boolean showEmptyText) {
411             if (isRunning()) {
412                 return res.getText(R.string.app_ops_running);
413             }
414             if (getTime() > 0) {
415                 return DateUtils.getRelativeTimeSpanString(getTime(),
416                         System.currentTimeMillis(),
417                         DateUtils.MINUTE_IN_MILLIS,
418                         DateUtils.FORMAT_ABBREV_RELATIVE);
419             }
420             return showEmptyText ? res.getText(R.string.app_ops_never_used) : "";
421         }
422 
isRunning()423         public boolean isRunning() {
424             return mOps.get(0).isRunning();
425         }
426 
getTime()427         public long getTime() {
428             return mOps.get(0).getTime();
429         }
430 
toString()431         @Override public String toString() {
432             return mApp.getLabel();
433         }
434     }
435 
436     /**
437      * Perform app op state comparison of application entry objects.
438      */
439     public static final Comparator<AppOpEntry> RECENCY_COMPARATOR = new Comparator<AppOpEntry>() {
440         private final Collator sCollator = Collator.getInstance();
441         @Override
442         public int compare(AppOpEntry object1, AppOpEntry object2) {
443             if (object1.getSwitchOrder() != object2.getSwitchOrder()) {
444                 return object1.getSwitchOrder() < object2.getSwitchOrder() ? -1 : 1;
445             }
446             if (object1.isRunning() != object2.isRunning()) {
447                 // Currently running ops go first.
448                 return object1.isRunning() ? -1 : 1;
449             }
450             if (object1.getTime() != object2.getTime()) {
451                 // More recent times go first.
452                 return object1.getTime() > object2.getTime() ? -1 : 1;
453             }
454             return sCollator.compare(object1.getAppEntry().getLabel(),
455                     object2.getAppEntry().getLabel());
456         }
457     };
458 
459     /**
460      * Perform alphabetical comparison of application entry objects.
461      */
462     public static final Comparator<AppOpEntry> LABEL_COMPARATOR = new Comparator<AppOpEntry>() {
463         private final Collator sCollator = Collator.getInstance();
464         @Override
465         public int compare(AppOpEntry object1, AppOpEntry object2) {
466             return sCollator.compare(object1.getAppEntry().getLabel(),
467                     object2.getAppEntry().getLabel());
468         }
469     };
470 
addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps, AppEntry appEntry, AppOpsManager.OpEntry opEntry, boolean allowMerge, int switchOrder)471     private void addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps,
472             AppEntry appEntry, AppOpsManager.OpEntry opEntry, boolean allowMerge, int switchOrder) {
473         if (allowMerge && entries.size() > 0) {
474             AppOpEntry last = entries.get(entries.size()-1);
475             if (last.getAppEntry() == appEntry) {
476                 boolean lastExe = last.getTime() != 0;
477                 boolean entryExe = opEntry.getTime() != 0;
478                 if (lastExe == entryExe) {
479                     if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package "
480                             + pkgOps.getPackageName() + ": append to " + last);
481                     last.addOp(opEntry);
482                     return;
483                 }
484             }
485         }
486         AppOpEntry entry = appEntry.getOpSwitch(opEntry.getOp());
487         if (entry != null) {
488             entry.addOp(opEntry);
489             return;
490         }
491         entry = new AppOpEntry(pkgOps, opEntry, appEntry, switchOrder);
492         if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package "
493                 + pkgOps.getPackageName() + ": making new " + entry);
494         entries.add(entry);
495     }
496 
getAppOpsManager()497     public AppOpsManager getAppOpsManager() {
498         return mAppOps;
499     }
500 
buildState(OpsTemplate tpl)501     public List<AppOpEntry> buildState(OpsTemplate tpl) {
502         return buildState(tpl, 0, null, RECENCY_COMPARATOR);
503     }
504 
getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries, final String packageName, ApplicationInfo appInfo)505     private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries,
506             final String packageName, ApplicationInfo appInfo) {
507         AppEntry appEntry = appEntries.get(packageName);
508         if (appEntry == null) {
509             if (appInfo == null) {
510                 try {
511                     appInfo = mPm.getApplicationInfo(packageName,
512                             PackageManager.MATCH_DISABLED_COMPONENTS
513                             | PackageManager.MATCH_ANY_USER);
514                 } catch (PackageManager.NameNotFoundException e) {
515                     Log.w(TAG, "Unable to find info for package " + packageName);
516                     return null;
517                 }
518             }
519             appEntry = new AppEntry(this, appInfo);
520             appEntry.loadLabel(context);
521             appEntries.put(packageName, appEntry);
522         }
523         return appEntry;
524     }
525 
buildState(OpsTemplate tpl, int uid, String packageName)526     public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName) {
527         return buildState(tpl, uid, packageName, RECENCY_COMPARATOR);
528     }
529 
buildState(OpsTemplate tpl, int uid, String packageName, Comparator<AppOpEntry> comparator)530     public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName,
531             Comparator<AppOpEntry> comparator) {
532         final Context context = mContext;
533 
534         final HashMap<String, AppEntry> appEntries = new HashMap<String, AppEntry>();
535         final List<AppOpEntry> entries = new ArrayList<AppOpEntry>();
536 
537         final ArrayList<String> perms = new ArrayList<String>();
538         final ArrayList<Integer> permOps = new ArrayList<Integer>();
539         final int[] opToOrder = new int[AppOpsManager._NUM_OP];
540         for (int i=0; i<tpl.ops.length; i++) {
541             if (tpl.showPerms[i]) {
542                 String perm = AppOpsManager.opToPermission(tpl.ops[i]);
543                 if (perm != null && !perms.contains(perm)) {
544                     perms.add(perm);
545                     permOps.add(tpl.ops[i]);
546                     opToOrder[tpl.ops[i]] = i;
547                 }
548             }
549         }
550 
551         List<AppOpsManager.PackageOps> pkgs;
552         if (packageName != null) {
553             pkgs = mAppOps.getOpsForPackage(uid, packageName, tpl.ops);
554         } else {
555             pkgs = mAppOps.getPackagesForOps(tpl.ops);
556         }
557 
558         if (pkgs != null) {
559             for (int i=0; i<pkgs.size(); i++) {
560                 AppOpsManager.PackageOps pkgOps = pkgs.get(i);
561                 AppEntry appEntry = getAppEntry(context, appEntries, pkgOps.getPackageName(), null);
562                 if (appEntry == null) {
563                     continue;
564                 }
565                 for (int j=0; j<pkgOps.getOps().size(); j++) {
566                     AppOpsManager.OpEntry opEntry = pkgOps.getOps().get(j);
567                     addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
568                             packageName == null ? 0 : opToOrder[opEntry.getOp()]);
569                 }
570             }
571         }
572 
573         List<PackageInfo> apps;
574         if (packageName != null) {
575             apps = new ArrayList<PackageInfo>();
576             try {
577                 PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
578                 apps.add(pi);
579             } catch (NameNotFoundException e) {
580             }
581         } else {
582             String[] permsArray = new String[perms.size()];
583             perms.toArray(permsArray);
584             apps = mPm.getPackagesHoldingPermissions(permsArray, 0);
585         }
586         for (int i=0; i<apps.size(); i++) {
587             PackageInfo appInfo = apps.get(i);
588             AppEntry appEntry = getAppEntry(context, appEntries, appInfo.packageName,
589                     appInfo.applicationInfo);
590             if (appEntry == null) {
591                 continue;
592             }
593             List<AppOpsManager.OpEntry> dummyOps = null;
594             AppOpsManager.PackageOps pkgOps = null;
595             if (appInfo.requestedPermissions != null) {
596                 for (int j=0; j<appInfo.requestedPermissions.length; j++) {
597                     if (appInfo.requestedPermissionsFlags != null) {
598                         if ((appInfo.requestedPermissionsFlags[j]
599                                 & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
600                             if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm "
601                                     + appInfo.requestedPermissions[j] + " not granted; skipping");
602                             continue;
603                         }
604                     }
605                     if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + ": requested perm "
606                             + appInfo.requestedPermissions[j]);
607                     for (int k=0; k<perms.size(); k++) {
608                         if (!perms.get(k).equals(appInfo.requestedPermissions[j])) {
609                             continue;
610                         }
611                         if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm " + perms.get(k)
612                                 + " has op " + permOps.get(k) + ": " + appEntry.hasOp(permOps.get(k)));
613                         if (appEntry.hasOp(permOps.get(k))) {
614                             continue;
615                         }
616                         if (dummyOps == null) {
617                             dummyOps = new ArrayList<AppOpsManager.OpEntry>();
618                             pkgOps = new AppOpsManager.PackageOps(
619                                     appInfo.packageName, appInfo.applicationInfo.uid, dummyOps);
620 
621                         }
622                         AppOpsManager.OpEntry opEntry = new AppOpsManager.OpEntry(
623                                 permOps.get(k), AppOpsManager.MODE_ALLOWED, 0, 0, 0, -1, null);
624                         dummyOps.add(opEntry);
625                         addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
626                                 packageName == null ? 0 : opToOrder[opEntry.getOp()]);
627                     }
628                 }
629             }
630         }
631 
632         // Sort the list.
633         Collections.sort(entries, comparator);
634 
635         // Done!
636         return entries;
637     }
638 }
639