AppOpsState.java revision df3f6d6c4722a7acc8189e47d0499aaf618969f9
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[] mOpNames; 51 52 List<AppOpEntry> mApps; 53 54 public AppOpsState(Context context) { 55 mContext = context; 56 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); 57 mPm = context.getPackageManager(); 58 mOpNames = context.getResources().getTextArray(R.array.app_ops_names); 59 } 60 61 public static class OpsTemplate implements Parcelable { 62 public final int[] ops; 63 64 public OpsTemplate(int[] _ops) { 65 ops = _ops; 66 } 67 68 OpsTemplate(Parcel src) { 69 ops = src.createIntArray(); 70 } 71 72 @Override 73 public int describeContents() { 74 return 0; 75 } 76 77 @Override 78 public void writeToParcel(Parcel dest, int flags) { 79 dest.writeIntArray(ops); 80 } 81 82 public static final Creator<OpsTemplate> CREATOR = new Creator<OpsTemplate>() { 83 @Override public OpsTemplate createFromParcel(Parcel source) { 84 return new OpsTemplate(source); 85 } 86 87 @Override public OpsTemplate[] newArray(int size) { 88 return new OpsTemplate[size]; 89 } 90 }; 91 } 92 93 public static final OpsTemplate LOCATION_TEMPLATE = new OpsTemplate( 94 new int[] { AppOpsManager.OP_COARSE_LOCATION, 95 AppOpsManager.OP_FINE_LOCATION, 96 AppOpsManager.OP_GPS, 97 AppOpsManager.OP_WIFI_SCAN } 98 ); 99 100 public static final OpsTemplate PERSONAL_TEMPLATE = new OpsTemplate( 101 new int[] { AppOpsManager.OP_READ_CONTACTS, 102 AppOpsManager.OP_WRITE_CONTACTS, 103 AppOpsManager.OP_READ_CALL_LOG, 104 AppOpsManager.OP_WRITE_CALL_LOG, 105 AppOpsManager.OP_READ_CALENDAR, 106 AppOpsManager.OP_WRITE_CALENDAR } 107 ); 108 109 public static final OpsTemplate DEVICE_TEMPLATE = new OpsTemplate( 110 new int[] { AppOpsManager.OP_VIBRATE, 111 AppOpsManager.OP_POST_NOTIFICATION } 112 ); 113 114 public static final OpsTemplate[] ALL_TEMPLATES = new OpsTemplate[] { 115 LOCATION_TEMPLATE, PERSONAL_TEMPLATE, DEVICE_TEMPLATE 116 }; 117 118 /** 119 * This class holds the per-item data in our Loader. 120 */ 121 public static class AppEntry { 122 private final AppOpsState mState; 123 private final ApplicationInfo mInfo; 124 private final File mApkFile; 125 private final SparseArray<AppOpsManager.OpEntry> mOps 126 = new SparseArray<AppOpsManager.OpEntry>(); 127 private String mLabel; 128 private Drawable mIcon; 129 private boolean mMounted; 130 131 public AppEntry(AppOpsState state, ApplicationInfo info) { 132 mState = state; 133 mInfo = info; 134 mApkFile = new File(info.sourceDir); 135 } 136 137 public void addOp(AppOpsManager.OpEntry op) { 138 mOps.put(op.getOp(), op); 139 } 140 141 public boolean hasOp(int op) { 142 return mOps.indexOfKey(op) >= 0; 143 } 144 145 public ApplicationInfo getApplicationInfo() { 146 return mInfo; 147 } 148 149 public String getLabel() { 150 return mLabel; 151 } 152 153 public Drawable getIcon() { 154 if (mIcon == null) { 155 if (mApkFile.exists()) { 156 mIcon = mInfo.loadIcon(mState.mPm); 157 return mIcon; 158 } else { 159 mMounted = false; 160 } 161 } else if (!mMounted) { 162 // If the app wasn't mounted but is now mounted, reload 163 // its icon. 164 if (mApkFile.exists()) { 165 mMounted = true; 166 mIcon = mInfo.loadIcon(mState.mPm); 167 return mIcon; 168 } 169 } else { 170 return mIcon; 171 } 172 173 return mState.mContext.getResources().getDrawable( 174 android.R.drawable.sym_def_app_icon); 175 } 176 177 @Override public String toString() { 178 return mLabel; 179 } 180 181 void loadLabel(Context context) { 182 if (mLabel == null || !mMounted) { 183 if (!mApkFile.exists()) { 184 mMounted = false; 185 mLabel = mInfo.packageName; 186 } else { 187 mMounted = true; 188 CharSequence label = mInfo.loadLabel(context.getPackageManager()); 189 mLabel = label != null ? label.toString() : mInfo.packageName; 190 } 191 } 192 } 193 } 194 195 /** 196 * This class holds the per-item data in our Loader. 197 */ 198 public static class AppOpEntry { 199 private final AppOpsManager.PackageOps mPkgOps; 200 private final ArrayList<AppOpsManager.OpEntry> mOps 201 = new ArrayList<AppOpsManager.OpEntry>(); 202 private final AppEntry mApp; 203 204 public AppOpEntry(AppOpsManager.PackageOps pkg, AppOpsManager.OpEntry op, AppEntry app) { 205 mPkgOps = pkg; 206 mApp = app; 207 mApp.addOp(op); 208 mOps.add(op); 209 } 210 211 public void addOp(AppOpsManager.OpEntry op) { 212 mApp.addOp(op); 213 for (int i=0; i<mOps.size(); i++) { 214 AppOpsManager.OpEntry pos = mOps.get(i); 215 if (pos.isRunning() != op.isRunning()) { 216 if (op.isRunning()) { 217 mOps.add(i, op); 218 return; 219 } 220 continue; 221 } 222 if (pos.getTime() < op.getTime()) { 223 mOps.add(i, op); 224 return; 225 } 226 } 227 mOps.add(op); 228 } 229 230 public AppEntry getAppEntry() { 231 return mApp; 232 } 233 234 public AppOpsManager.PackageOps getPackageOps() { 235 return mPkgOps; 236 } 237 238 public int getNumOpEntry() { 239 return mOps.size(); 240 } 241 242 public AppOpsManager.OpEntry getOpEntry(int pos) { 243 return mOps.get(pos); 244 } 245 246 public CharSequence getLabelText(AppOpsState state) { 247 if (getNumOpEntry() == 1) { 248 return state.mOpNames[getOpEntry(0).getOp()]; 249 } else { 250 StringBuilder builder = new StringBuilder(); 251 for (int i=0; i<getNumOpEntry(); i++) { 252 if (i > 0) { 253 builder.append(", "); 254 } 255 builder.append(state.mOpNames[getOpEntry(i).getOp()]); 256 } 257 return builder.toString(); 258 } 259 } 260 261 public CharSequence getTimeText(Resources res) { 262 if (isRunning()) { 263 return res.getText(R.string.app_ops_running); 264 } 265 if (getTime() > 0) { 266 return DateUtils.getRelativeTimeSpanString(getTime(), 267 System.currentTimeMillis(), 268 DateUtils.MINUTE_IN_MILLIS, 269 DateUtils.FORMAT_ABBREV_RELATIVE); 270 } 271 return ""; 272 } 273 274 public boolean isRunning() { 275 return mOps.get(0).isRunning(); 276 } 277 278 public long getTime() { 279 return mOps.get(0).getTime(); 280 } 281 282 @Override public String toString() { 283 return mApp.getLabel(); 284 } 285 } 286 287 /** 288 * Perform alphabetical comparison of application entry objects. 289 */ 290 public static final Comparator<AppOpEntry> APP_OP_COMPARATOR = new Comparator<AppOpEntry>() { 291 private final Collator sCollator = Collator.getInstance(); 292 @Override 293 public int compare(AppOpEntry object1, AppOpEntry object2) { 294 if (object1.isRunning() != object2.isRunning()) { 295 // Currently running ops go first. 296 return object1.isRunning() ? -1 : 1; 297 } 298 if (object1.getTime() != object2.getTime()) { 299 // More recent times go first. 300 return object1.getTime() > object2.getTime() ? -1 : 1; 301 } 302 return sCollator.compare(object1.getAppEntry().getLabel(), 303 object2.getAppEntry().getLabel()); 304 } 305 }; 306 307 private void addOp(List<AppOpEntry> entries, AppOpsManager.PackageOps pkgOps, 308 AppEntry appEntry, AppOpsManager.OpEntry opEntry) { 309 if (entries.size() > 0) { 310 AppOpEntry last = entries.get(entries.size()-1); 311 if (last.getAppEntry() == appEntry) { 312 boolean lastExe = last.getTime() != 0; 313 boolean entryExe = opEntry.getTime() != 0; 314 if (lastExe == entryExe) { 315 if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package " 316 + pkgOps.getPackageName() + ": append to " + last); 317 last.addOp(opEntry); 318 return; 319 } 320 } 321 } 322 AppOpEntry entry = new AppOpEntry(pkgOps, opEntry, appEntry); 323 if (DEBUG) Log.d(TAG, "Add op " + opEntry.getOp() + " to package " 324 + pkgOps.getPackageName() + ": making new " + entry); 325 entries.add(entry); 326 } 327 328 public List<AppOpEntry> buildState(OpsTemplate tpl) { 329 return buildState(tpl, 0, null); 330 } 331 332 private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries, 333 final String packageName, ApplicationInfo appInfo) { 334 AppEntry appEntry = appEntries.get(packageName); 335 if (appEntry == null) { 336 if (appInfo == null) { 337 try { 338 appInfo = mPm.getApplicationInfo(packageName, 339 PackageManager.GET_DISABLED_COMPONENTS 340 | PackageManager.GET_UNINSTALLED_PACKAGES); 341 } catch (PackageManager.NameNotFoundException e) { 342 Log.w(TAG, "Unable to find info for package " + packageName); 343 return null; 344 } 345 } 346 appEntry = new AppEntry(this, appInfo); 347 appEntry.loadLabel(context); 348 appEntries.put(packageName, appEntry); 349 } 350 return appEntry; 351 } 352 353 public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName) { 354 final Context context = mContext; 355 356 final HashMap<String, AppEntry> appEntries = new HashMap<String, AppEntry>(); 357 List<AppOpEntry> entries = new ArrayList<AppOpEntry>(); 358 359 ArrayList<String> perms = new ArrayList<String>(); 360 ArrayList<Integer> permOps = new ArrayList<Integer>(); 361 for (int i=0; i<tpl.ops.length; i++) { 362 String perm = AppOpsManager.opToPermission(tpl.ops[i]); 363 if (perm != null && !perms.contains(perm)) { 364 perms.add(perm); 365 permOps.add(tpl.ops[i]); 366 } 367 } 368 369 List<AppOpsManager.PackageOps> pkgs; 370 if (packageName != null) { 371 pkgs = mAppOps.getOpsForPackage(uid, packageName, tpl.ops); 372 } else { 373 pkgs = mAppOps.getPackagesForOps(tpl.ops); 374 } 375 376 if (pkgs != null) { 377 for (int i=0; i<pkgs.size(); i++) { 378 AppOpsManager.PackageOps pkgOps = pkgs.get(i); 379 AppEntry appEntry = getAppEntry(context, appEntries, pkgOps.getPackageName(), null); 380 if (appEntry == null) { 381 continue; 382 } 383 for (int j=0; j<pkgOps.getOps().size(); j++) { 384 AppOpsManager.OpEntry opEntry = pkgOps.getOps().get(j); 385 addOp(entries, pkgOps, appEntry, opEntry); 386 } 387 } 388 } 389 390 List<PackageInfo> apps; 391 if (packageName != null) { 392 apps = new ArrayList<PackageInfo>(); 393 try { 394 PackageInfo pi = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); 395 apps.add(pi); 396 } catch (NameNotFoundException e) { 397 } 398 } else { 399 String[] permsArray = new String[perms.size()]; 400 perms.toArray(permsArray); 401 apps = mPm.getPackagesHoldingPermissions(permsArray, 0); 402 } 403 for (int i=0; i<apps.size(); i++) { 404 PackageInfo appInfo = apps.get(i); 405 AppEntry appEntry = getAppEntry(context, appEntries, appInfo.packageName, 406 appInfo.applicationInfo); 407 if (appEntry == null) { 408 continue; 409 } 410 List<AppOpsManager.OpEntry> dummyOps = null; 411 AppOpsManager.PackageOps pkgOps = null; 412 if (appInfo.requestedPermissions != null) { 413 for (int j=0; j<appInfo.requestedPermissions.length; j++) { 414 if (appInfo.requestedPermissionsFlags != null) { 415 if ((appInfo.requestedPermissionsFlags[j] 416 & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) { 417 if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm " 418 + appInfo.requestedPermissions[j] + " not granted; skipping"); 419 break; 420 } 421 } 422 if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + ": requested perm " 423 + appInfo.requestedPermissions[j]); 424 for (int k=0; k<perms.size(); k++) { 425 if (!perms.get(k).equals(appInfo.requestedPermissions[j])) { 426 continue; 427 } 428 if (DEBUG) Log.d(TAG, "Pkg " + appInfo.packageName + " perm " + perms.get(k) 429 + " has op " + permOps.get(k) + ": " + appEntry.hasOp(permOps.get(k))); 430 if (appEntry.hasOp(permOps.get(k))) { 431 continue; 432 } 433 if (dummyOps == null) { 434 dummyOps = new ArrayList<AppOpsManager.OpEntry>(); 435 pkgOps = new AppOpsManager.PackageOps( 436 appInfo.packageName, appInfo.applicationInfo.uid, dummyOps); 437 438 } 439 AppOpsManager.OpEntry opEntry = new AppOpsManager.OpEntry( 440 permOps.get(k), AppOpsManager.MODE_ALLOWED, 0, 0, 0); 441 dummyOps.add(opEntry); 442 addOp(entries, pkgOps, appEntry, opEntry); 443 } 444 } 445 } 446 } 447 448 // Sort the list. 449 Collections.sort(entries, APP_OP_COMPARATOR); 450 451 // Done! 452 return entries; 453 } 454 455 public CharSequence getLabelText(AppOpsManager.OpEntry op) { 456 return mOpNames[op.getOp()]; 457 } 458 459 public CharSequence getTimeText(AppOpsManager.OpEntry op) { 460 if (op.isRunning()) { 461 return mContext.getResources().getText(R.string.app_ops_running); 462 } 463 if (op.getTime() > 0) { 464 return DateUtils.getRelativeTimeSpanString(op.getTime(), 465 System.currentTimeMillis(), 466 DateUtils.MINUTE_IN_MILLIS, 467 DateUtils.FORMAT_ABBREV_RELATIVE); 468 } 469 return mContext.getResources().getText(R.string.app_ops_never_used); 470 } 471 472} 473