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