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_MUTE_MICROPHONE},
171             new boolean[] { false,
172                     true,
173                     true,
174                     false,
175                     false,
176                     false,
177                     false,
178                     false,
179                     false,
180                     false,
181                     false,
182                     false,
183                     false,
184                     false }
185             );
186 
187     public static final OpsTemplate DEVICE_TEMPLATE = new OpsTemplate(
188             new int[] { AppOpsManager.OP_POST_NOTIFICATION,
189                     AppOpsManager.OP_ACCESS_NOTIFICATIONS,
190                     AppOpsManager.OP_CALL_PHONE,
191                     AppOpsManager.OP_WRITE_SETTINGS,
192                     AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
193                     AppOpsManager.OP_WAKE_LOCK,
194                     AppOpsManager.OP_PROJECT_MEDIA,
195                     AppOpsManager.OP_ACTIVATE_VPN,
196                     AppOpsManager.OP_ASSIST_STRUCTURE,
197                     AppOpsManager.OP_ASSIST_SCREENSHOT},
198             new boolean[] { false,
199                     true,
200                     true,
201                     true,
202                     true,
203                     true,
204                     false,
205                     false,
206                     false,
207                     false }
208             );
209 
210     public static final OpsTemplate RUN_IN_BACKGROUND_TEMPLATE = new OpsTemplate(
211             new int[] { AppOpsManager.OP_RUN_IN_BACKGROUND },
212             new boolean[] { false }
213             );
214 
215     public static final OpsTemplate[] ALL_TEMPLATES = new OpsTemplate[] {
216             LOCATION_TEMPLATE, PERSONAL_TEMPLATE, MESSAGING_TEMPLATE,
217             MEDIA_TEMPLATE, DEVICE_TEMPLATE, RUN_IN_BACKGROUND_TEMPLATE
218     };
219 
220     /**
221      * This class holds the per-item data in our Loader.
222      */
223     public static class AppEntry {
224         private final AppOpsState mState;
225         private final ApplicationInfo mInfo;
226         private final File mApkFile;
227         private final SparseArray<AppOpsManager.OpEntry> mOps
228                 = new SparseArray<AppOpsManager.OpEntry>();
229         private final SparseArray<AppOpEntry> mOpSwitches
230                 = new SparseArray<AppOpEntry>();
231         private String mLabel;
232         private Drawable mIcon;
233         private boolean mMounted;
234 
AppEntry(AppOpsState state, ApplicationInfo info)235         public AppEntry(AppOpsState state, ApplicationInfo info) {
236             mState = state;
237             mInfo = info;
238             mApkFile = new File(info.sourceDir);
239         }
240 
addOp(AppOpEntry entry, AppOpsManager.OpEntry op)241         public void addOp(AppOpEntry entry, AppOpsManager.OpEntry op) {
242             mOps.put(op.getOp(), op);
243             mOpSwitches.put(AppOpsManager.opToSwitch(op.getOp()), entry);
244         }
245 
hasOp(int op)246         public boolean hasOp(int op) {
247             return mOps.indexOfKey(op) >= 0;
248         }
249 
getOpSwitch(int op)250         public AppOpEntry getOpSwitch(int op) {
251             return mOpSwitches.get(AppOpsManager.opToSwitch(op));
252         }
253 
getApplicationInfo()254         public ApplicationInfo getApplicationInfo() {
255             return mInfo;
256         }
257 
getLabel()258         public String getLabel() {
259             return mLabel;
260         }
261 
getIcon()262         public Drawable getIcon() {
263             if (mIcon == null) {
264                 if (mApkFile.exists()) {
265                     mIcon = mInfo.loadIcon(mState.mPm);
266                     return mIcon;
267                 } else {
268                     mMounted = false;
269                 }
270             } else if (!mMounted) {
271                 // If the app wasn't mounted but is now mounted, reload
272                 // its icon.
273                 if (mApkFile.exists()) {
274                     mMounted = true;
275                     mIcon = mInfo.loadIcon(mState.mPm);
276                     return mIcon;
277                 }
278             } else {
279                 return mIcon;
280             }
281 
282             return mState.mContext.getDrawable(
283                     android.R.drawable.sym_def_app_icon);
284         }
285 
toString()286         @Override public String toString() {
287             return mLabel;
288         }
289 
loadLabel(Context context)290         void loadLabel(Context context) {
291             if (mLabel == null || !mMounted) {
292                 if (!mApkFile.exists()) {
293                     mMounted = false;
294                     mLabel = mInfo.packageName;
295                 } else {
296                     mMounted = true;
297                     CharSequence label = mInfo.loadLabel(context.getPackageManager());
298                     mLabel = label != null ? label.toString() : mInfo.packageName;
299                 }
300             }
301         }
302     }
303 
304     /**
305      * This class holds the per-item data in our Loader.
306      */
307     public static class AppOpEntry {
308         private final AppOpsManager.PackageOps mPkgOps;
309         private final ArrayList<AppOpsManager.OpEntry> mOps
310                 = new ArrayList<AppOpsManager.OpEntry>();
311         private final ArrayList<AppOpsManager.OpEntry> mSwitchOps
312                 = new ArrayList<AppOpsManager.OpEntry>();
313         private final AppEntry mApp;
314         private final int mSwitchOrder;
315         private int mOverriddenPrimaryMode = -1;
316 
AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app, int switchOrder)317         public AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app,
318                 int switchOrder) {
319             mPkgOps = pkg;
320             mApp = app;
321             mSwitchOrder = switchOrder;
322             mApp.addOp(this, op);
323             mOps.add(op);
324             mSwitchOps.add(op);
325         }
326 
addOp(ArrayList<AppOpsManager.OpEntry> list, AppOpsManager.OpEntry op)327         private static void addOp(ArrayList<AppOpsManager.OpEntry> list, AppOpsManager.OpEntry op) {
328             for (int i=0; i<list.size(); i++) {
329                 AppOpsManager.OpEntry pos = list.get(i);
330                 if (pos.isRunning() != op.isRunning()) {
331                     if (op.isRunning()) {
332                         list.add(i, op);
333                         return;
334                     }
335                     continue;
336                 }
337                 if (pos.getTime() < op.getTime()) {
338                     list.add(i, op);
339                     return;
340                 }
341             }
342             list.add(op);
343         }
344 
addOp(AppOpsManager.OpEntry op)345         public void addOp(AppOpsManager.OpEntry op) {
346             mApp.addOp(this, op);
347             addOp(mOps, op);
348             if (mApp.getOpSwitch(AppOpsManager.opToSwitch(op.getOp())) == null) {
349                 addOp(mSwitchOps, op);
350             }
351         }
352 
getAppEntry()353         public AppEntry getAppEntry() {
354             return mApp;
355         }
356 
getSwitchOrder()357         public int getSwitchOrder() {
358             return mSwitchOrder;
359         }
360 
getPackageOps()361         public AppOpsManager.PackageOps getPackageOps() {
362             return mPkgOps;
363         }
364 
getNumOpEntry()365         public int getNumOpEntry() {
366             return mOps.size();
367         }
368 
getOpEntry(int pos)369         public AppOpsManager.OpEntry getOpEntry(int pos) {
370             return mOps.get(pos);
371         }
372 
getPrimaryOpMode()373         public int getPrimaryOpMode() {
374             return mOverriddenPrimaryMode >= 0 ? mOverriddenPrimaryMode : mOps.get(0).getMode();
375         }
376 
overridePrimaryOpMode(int mode)377         public void overridePrimaryOpMode(int mode) {
378             mOverriddenPrimaryMode = mode;
379         }
380 
getCombinedText(ArrayList<AppOpsManager.OpEntry> ops, CharSequence[] items)381         private CharSequence getCombinedText(ArrayList<AppOpsManager.OpEntry> ops,
382                 CharSequence[] items) {
383             if (ops.size() == 1) {
384                 return items[ops.get(0).getOp()];
385             } else {
386                 StringBuilder builder = new StringBuilder();
387                 for (int i=0; i<ops.size(); i++) {
388                     if (i > 0) {
389                         builder.append(", ");
390                     }
391                     builder.append(items[ops.get(i).getOp()]);
392                 }
393                 return builder.toString();
394             }
395         }
396 
getSummaryText(AppOpsState state)397         public CharSequence getSummaryText(AppOpsState state) {
398             return getCombinedText(mOps, state.mOpSummaries);
399         }
400 
getSwitchText(AppOpsState state)401         public CharSequence getSwitchText(AppOpsState state) {
402             if (mSwitchOps.size() > 0) {
403                 return getCombinedText(mSwitchOps, state.mOpLabels);
404             } else {
405                 return getCombinedText(mOps, state.mOpLabels);
406             }
407         }
408 
getTimeText(Resources res, boolean showEmptyText)409         public CharSequence getTimeText(Resources res, boolean showEmptyText) {
410             if (isRunning()) {
411                 return res.getText(R.string.app_ops_running);
412             }
413             if (getTime() > 0) {
414                 return DateUtils.getRelativeTimeSpanString(getTime(),
415                         System.currentTimeMillis(),
416                         DateUtils.MINUTE_IN_MILLIS,
417                         DateUtils.FORMAT_ABBREV_RELATIVE);
418             }
419             return showEmptyText ? res.getText(R.string.app_ops_never_used) : "";
420         }
421 
isRunning()422         public boolean isRunning() {
423             return mOps.get(0).isRunning();
424         }
425 
getTime()426         public long getTime() {
427             return mOps.get(0).getTime();
428         }
429 
toString()430         @Override public String toString() {
431             return mApp.getLabel();
432         }
433     }
434 
435     /**
436      * Perform app op state comparison of application entry objects.
437      */
438     public static final Comparator<AppOpEntry> RECENCY_COMPARATOR = new Comparator<AppOpEntry>() {
439         private final Collator sCollator = Collator.getInstance();
440         @Override
441         public int compare(AppOpEntry object1, AppOpEntry object2) {
442             if (object1.getSwitchOrder() != object2.getSwitchOrder()) {
443                 return object1.getSwitchOrder() < object2.getSwitchOrder() ? -1 : 1;
444             }
445             if (object1.isRunning() != object2.isRunning()) {
446                 // Currently running ops go first.
447                 return object1.isRunning() ? -1 : 1;
448             }
449             if (object1.getTime() != object2.getTime()) {
450                 // More recent times go first.
451                 return object1.getTime() > object2.getTime() ? -1 : 1;
452             }
453             return sCollator.compare(object1.getAppEntry().getLabel(),
454                     object2.getAppEntry().getLabel());
455         }
456     };
457 
458     /**
459      * Perform alphabetical comparison of application entry objects.
460      */
461     public static final Comparator<AppOpEntry> LABEL_COMPARATOR = new Comparator<AppOpEntry>() {
462         private final Collator sCollator = Collator.getInstance();
463         @Override
464         public int compare(AppOpEntry object1, AppOpEntry object2) {
465             return sCollator.compare(object1.getAppEntry().getLabel(),
466                     object2.getAppEntry().getLabel());
467         }
468     };
469 
addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps, AppEntry appEntry, AppOpsManager.OpEntry opEntry, boolean allowMerge, int switchOrder)470     private void addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps,
471             AppEntry appEntry, AppOpsManager.OpEntry opEntry, boolean allowMerge, int switchOrder) {
472         if (allowMerge && entries.size() > 0) {
473             AppOpEntry last = entries.get(entries.size()-1);
474             if (last.getAppEntry() == appEntry) {
475                 boolean lastExe = last.getTime() != 0;
476                 boolean entryExe = opEntry.getTime() != 0;
477                 if (lastExe == entryExe) {
478                     if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package "
479                             + pkgOps.getPackageName() + ": append to " + last);
480                     last.addOp(opEntry);
481                     return;
482                 }
483             }
484         }
485         AppOpEntry entry = appEntry.getOpSwitch(opEntry.getOp());
486         if (entry != null) {
487             entry.addOp(opEntry);
488             return;
489         }
490         entry = new AppOpEntry(pkgOps, opEntry, appEntry, switchOrder);
491         if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package "
492                 + pkgOps.getPackageName() + ": making new " + entry);
493         entries.add(entry);
494     }
495 
getAppOpsManager()496     public AppOpsManager getAppOpsManager() {
497         return mAppOps;
498     }
499 
buildState(OpsTemplate tpl)500     public List<AppOpEntry> buildState(OpsTemplate tpl) {
501         return buildState(tpl, 0, null, RECENCY_COMPARATOR);
502     }
503 
getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries, final String packageName, ApplicationInfo appInfo)504     private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries,
505             final String packageName, ApplicationInfo appInfo) {
506         AppEntry appEntry = appEntries.get(packageName);
507         if (appEntry == null) {
508             if (appInfo == null) {
509                 try {
510                     appInfo = mPm.getApplicationInfo(packageName,
511                             PackageManager.GET_DISABLED_COMPONENTS
512                             | PackageManager.GET_UNINSTALLED_PACKAGES);
513                 } catch (PackageManager.NameNotFoundException e) {
514                     Log.w(TAG, "Unable to find info for package " + packageName);
515                     return null;
516                 }
517             }
518             appEntry = new AppEntry(this, appInfo);
519             appEntry.loadLabel(context);
520             appEntries.put(packageName, appEntry);
521         }
522         return appEntry;
523     }
524 
buildState(OpsTemplate tpl, int uid, String packageName)525     public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName) {
526         return buildState(tpl, uid, packageName, RECENCY_COMPARATOR);
527     }
528 
buildState(OpsTemplate tpl, int uid, String packageName, Comparator<AppOpEntry> comparator)529     public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName,
530             Comparator<AppOpEntry> comparator) {
531         final Context context = mContext;
532 
533         final HashMap<String, AppEntry> appEntries = new HashMap<String, AppEntry>();
534         final List<AppOpEntry> entries = new ArrayList<AppOpEntry>();
535 
536         final ArrayList<String> perms = new ArrayList<String>();
537         final ArrayList<Integer> permOps = new ArrayList<Integer>();
538         final int[] opToOrder = new int[AppOpsManager._NUM_OP];
539         for (int i=0; i<tpl.ops.length; i++) {
540             if (tpl.showPerms[i]) {
541                 String perm = AppOpsManager.opToPermission(tpl.ops[i]);
542                 if (perm != null && !perms.contains(perm)) {
543                     perms.add(perm);
544                     permOps.add(tpl.ops[i]);
545                     opToOrder[tpl.ops[i]] = i;
546                 }
547             }
548         }
549 
550         List<AppOpsManager.PackageOps> pkgs;
551         if (packageName != null) {
552             pkgs = mAppOps.getOpsForPackage(uid, packageName, tpl.ops);
553         } else {
554             pkgs = mAppOps.getPackagesForOps(tpl.ops);
555         }
556 
557         if (pkgs != null) {
558             for (int i=0; i<pkgs.size(); i++) {
559                 AppOpsManager.PackageOps pkgOps = pkgs.get(i);
560                 AppEntry appEntry = getAppEntry(context, appEntries, pkgOps.getPackageName(), null);
561                 if (appEntry == null) {
562                     continue;
563                 }
564                 for (int j=0; j<pkgOps.getOps().size(); j++) {
565                     AppOpsManager.OpEntry opEntry = pkgOps.getOps().get(j);
566                     addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
567                             packageName == null ? 0 : opToOrder[opEntry.getOp()]);
568                 }
569             }
570         }
571 
572         List<PackageInfo> apps;
573         if (packageName != null) {
574             apps = new ArrayList<PackageInfo>();
575             try {
576                 PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
577                 apps.add(pi);
578             } catch (NameNotFoundException e) {
579             }
580         } else {
581             String[] permsArray = new String[perms.size()];
582             perms.toArray(permsArray);
583             apps = mPm.getPackagesHoldingPermissions(permsArray, 0);
584         }
585         for (int i=0; i<apps.size(); i++) {
586             PackageInfo appInfo = apps.get(i);
587             AppEntry appEntry = getAppEntry(context, appEntries, appInfo.packageName,
588                     appInfo.applicationInfo);
589             if (appEntry == null) {
590                 continue;
591             }
592             List<AppOpsManager.OpEntry> dummyOps = null;
593             AppOpsManager.PackageOps pkgOps = null;
594             if (appInfo.requestedPermissions != null) {
595                 for (int j=0; j<appInfo.requestedPermissions.length; j++) {
596                     if (appInfo.requestedPermissionsFlags != null) {
597                         if ((appInfo.requestedPermissionsFlags[j]
598                                 & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
599                             if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm "
600                                     + appInfo.requestedPermissions[j] + " not granted; skipping");
601                             continue;
602                         }
603                     }
604                     if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + ": requested perm "
605                             + appInfo.requestedPermissions[j]);
606                     for (int k=0; k<perms.size(); k++) {
607                         if (!perms.get(k).equals(appInfo.requestedPermissions[j])) {
608                             continue;
609                         }
610                         if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm " + perms.get(k)
611                                 + " has op " + permOps.get(k) + ": " + appEntry.hasOp(permOps.get(k)));
612                         if (appEntry.hasOp(permOps.get(k))) {
613                             continue;
614                         }
615                         if (dummyOps == null) {
616                             dummyOps = new ArrayList<AppOpsManager.OpEntry>();
617                             pkgOps = new AppOpsManager.PackageOps(
618                                     appInfo.packageName, appInfo.applicationInfo.uid, dummyOps);
619 
620                         }
621                         AppOpsManager.OpEntry opEntry = new AppOpsManager.OpEntry(
622                                 permOps.get(k), AppOpsManager.MODE_ALLOWED, 0, 0, 0, -1, null);
623                         dummyOps.add(opEntry);
624                         addOp(entries, pkgOps, appEntry, opEntry, packageName == null,
625                                 packageName == null ? 0 : opToOrder[opEntry.getOp()]);
626                     }
627                 }
628             }
629         }
630 
631         // Sort the list.
632         Collections.sort(entries, comparator);
633 
634         // Done!
635         return entries;
636     }
637 }
638