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