OSDN Git Service

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