ApplicationInfo.java revision 4a627c71ff53a4fca1f961f4b1dcc0461df18a06
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content.pm; 18 19import android.content.pm.PackageManager.NameNotFoundException; 20import android.content.res.Resources; 21import android.graphics.drawable.Drawable; 22import android.os.Parcel; 23import android.os.Parcelable; 24import android.util.Printer; 25 26import java.text.Collator; 27import java.util.Comparator; 28 29/** 30 * Information you can retrieve about a particular application. This 31 * corresponds to information collected from the AndroidManifest.xml's 32 * <application> tag. 33 */ 34public class ApplicationInfo extends PackageItemInfo implements Parcelable { 35 36 /** 37 * Default task affinity of all activities in this application. See 38 * {@link ActivityInfo#taskAffinity} for more information. This comes 39 * from the "taskAffinity" attribute. 40 */ 41 public String taskAffinity; 42 43 /** 44 * Optional name of a permission required to be able to access this 45 * application's components. From the "permission" attribute. 46 */ 47 public String permission; 48 49 /** 50 * The name of the process this application should run in. From the 51 * "process" attribute or, if not set, the same as 52 * <var>packageName</var>. 53 */ 54 public String processName; 55 56 /** 57 * Class implementing the Application object. From the "class" 58 * attribute. 59 */ 60 public String className; 61 62 /** 63 * A style resource identifier (in the package's resources) of the 64 * description of an application. From the "description" attribute 65 * or, if not set, 0. 66 */ 67 public int descriptionRes; 68 69 /** 70 * A style resource identifier (in the package's resources) of the 71 * default visual theme of the application. From the "theme" attribute 72 * or, if not set, 0. 73 */ 74 public int theme; 75 76 /** 77 * Class implementing the Application's manage space 78 * functionality. From the "manageSpaceActivity" 79 * attribute. This is an optional attribute and will be null if 80 * applications don't specify it in their manifest 81 */ 82 public String manageSpaceActivityName; 83 84 /** 85 * Class implementing the Application's backup functionality. From 86 * the "backupAgent" attribute. This is an optional attribute and 87 * will be null if the application does not specify it in its manifest. 88 * 89 * <p>If android:allowBackup is set to false, this attribute is ignored. 90 */ 91 public String backupAgentName; 92 93 /** 94 * Class implementing the package's *full* backup functionality. This 95 * is not usable except by system-installed packages. It can be the same 96 * as the backupAgent. 97 * 98 * @hide 99 */ 100 public String fullBackupAgentName; 101 102 /** 103 * Value for {@link #flags}: if set, this application is installed in the 104 * device's system image. 105 */ 106 public static final int FLAG_SYSTEM = 1<<0; 107 108 /** 109 * Value for {@link #flags}: set to true if this application would like to 110 * allow debugging of its 111 * code, even when installed on a non-development system. Comes 112 * from {@link android.R.styleable#AndroidManifestApplication_debuggable 113 * android:debuggable} of the <application> tag. 114 */ 115 public static final int FLAG_DEBUGGABLE = 1<<1; 116 117 /** 118 * Value for {@link #flags}: set to true if this application has code 119 * associated with it. Comes 120 * from {@link android.R.styleable#AndroidManifestApplication_hasCode 121 * android:hasCode} of the <application> tag. 122 */ 123 public static final int FLAG_HAS_CODE = 1<<2; 124 125 /** 126 * Value for {@link #flags}: set to true if this application is persistent. 127 * Comes from {@link android.R.styleable#AndroidManifestApplication_persistent 128 * android:persistent} of the <application> tag. 129 */ 130 public static final int FLAG_PERSISTENT = 1<<3; 131 132 /** 133 * Value for {@link #flags}: set to true if this application holds the 134 * {@link android.Manifest.permission#FACTORY_TEST} permission and the 135 * device is running in factory test mode. 136 */ 137 public static final int FLAG_FACTORY_TEST = 1<<4; 138 139 /** 140 * Value for {@link #flags}: default value for the corresponding ActivityInfo flag. 141 * Comes from {@link android.R.styleable#AndroidManifestApplication_allowTaskReparenting 142 * android:allowTaskReparenting} of the <application> tag. 143 */ 144 public static final int FLAG_ALLOW_TASK_REPARENTING = 1<<5; 145 146 /** 147 * Value for {@link #flags}: default value for the corresponding ActivityInfo flag. 148 * Comes from {@link android.R.styleable#AndroidManifestApplication_allowClearUserData 149 * android:allowClearUserData} of the <application> tag. 150 */ 151 public static final int FLAG_ALLOW_CLEAR_USER_DATA = 1<<6; 152 153 /** 154 * Value for {@link #flags}: this is set if this application has been 155 * install as an update to a built-in system application. 156 */ 157 public static final int FLAG_UPDATED_SYSTEM_APP = 1<<7; 158 159 /** 160 * Value for {@link #flags}: this is set of the application has specified 161 * {@link android.R.styleable#AndroidManifestApplication_testOnly 162 * android:testOnly} to be true. 163 */ 164 public static final int FLAG_TEST_ONLY = 1<<8; 165 166 /** 167 * Value for {@link #flags}: true when the application's window can be 168 * reduced in size for smaller screens. Corresponds to 169 * {@link android.R.styleable#AndroidManifestSupportsScreens_smallScreens 170 * android:smallScreens}. 171 */ 172 public static final int FLAG_SUPPORTS_SMALL_SCREENS = 1<<9; 173 174 /** 175 * Value for {@link #flags}: true when the application's window can be 176 * displayed on normal screens. Corresponds to 177 * {@link android.R.styleable#AndroidManifestSupportsScreens_normalScreens 178 * android:normalScreens}. 179 */ 180 public static final int FLAG_SUPPORTS_NORMAL_SCREENS = 1<<10; 181 182 /** 183 * Value for {@link #flags}: true when the application's window can be 184 * increased in size for larger screens. Corresponds to 185 * {@link android.R.styleable#AndroidManifestSupportsScreens_largeScreens 186 * android:largeScreens}. 187 */ 188 public static final int FLAG_SUPPORTS_LARGE_SCREENS = 1<<11; 189 190 /** 191 * Value for {@link #flags}: true when the application knows how to adjust 192 * its UI for different screen sizes. Corresponds to 193 * {@link android.R.styleable#AndroidManifestSupportsScreens_resizeable 194 * android:resizeable}. 195 */ 196 public static final int FLAG_RESIZEABLE_FOR_SCREENS = 1<<12; 197 198 /** 199 * Value for {@link #flags}: true when the application knows how to 200 * accomodate different screen densities. Corresponds to 201 * {@link android.R.styleable#AndroidManifestSupportsScreens_anyDensity 202 * android:anyDensity}. 203 */ 204 public static final int FLAG_SUPPORTS_SCREEN_DENSITIES = 1<<13; 205 206 /** 207 * Value for {@link #flags}: set to true if this application would like to 208 * request the VM to operate under the safe mode. Comes from 209 * {@link android.R.styleable#AndroidManifestApplication_vmSafeMode 210 * android:vmSafeMode} of the <application> tag. 211 */ 212 public static final int FLAG_VM_SAFE_MODE = 1<<14; 213 214 /** 215 * Value for {@link #flags}: set to <code>false</code> if the application does not wish 216 * to permit any OS-driven backups of its data; <code>true</code> otherwise. 217 * 218 * <p>Comes from the 219 * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 220 * attribute of the <application> tag. 221 */ 222 public static final int FLAG_ALLOW_BACKUP = 1<<15; 223 224 /** 225 * Value for {@link #flags}: set to <code>false</code> if the application must be kept 226 * in memory following a full-system restore operation; <code>true</code> otherwise. 227 * Ordinarily, during a full system restore operation each application is shut down 228 * following execution of its agent's onRestore() method. Setting this attribute to 229 * <code>false</code> prevents this. Most applications will not need to set this attribute. 230 * 231 * <p>If 232 * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 233 * is set to <code>false</code> or no 234 * {@link android.R.styleable#AndroidManifestApplication_backupAgent android:backupAgent} 235 * is specified, this flag will be ignored. 236 * 237 * <p>Comes from the 238 * {@link android.R.styleable#AndroidManifestApplication_killAfterRestore android:killAfterRestore} 239 * attribute of the <application> tag. 240 */ 241 public static final int FLAG_KILL_AFTER_RESTORE = 1<<16; 242 243 /** 244 * Value for {@link #flags}: Set to <code>true</code> if the application's backup 245 * agent claims to be able to handle restore data even "from the future," 246 * i.e. from versions of the application with a versionCode greater than 247 * the one currently installed on the device. <i>Use with caution!</i> By default 248 * this attribute is <code>false</code> and the Backup Manager will ensure that data 249 * from "future" versions of the application are never supplied during a restore operation. 250 * 251 * <p>If 252 * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 253 * is set to <code>false</code> or no 254 * {@link android.R.styleable#AndroidManifestApplication_backupAgent android:backupAgent} 255 * is specified, this flag will be ignored. 256 * 257 * <p>Comes from the 258 * {@link android.R.styleable#AndroidManifestApplication_restoreAnyVersion android:restoreAnyVersion} 259 * attribute of the <application> tag. 260 */ 261 public static final int FLAG_RESTORE_ANY_VERSION = 1<<17; 262 263 /** 264 * Value for {@link #flags}: Set to true if the application is 265 * currently installed on external/removable/unprotected storage. Such 266 * applications may not be available if their storage is not currently 267 * mounted. When the storage it is on is not available, it will look like 268 * the application has been uninstalled (its .apk is no longer available) 269 * but its persistent data is not removed. 270 */ 271 public static final int FLAG_EXTERNAL_STORAGE = 1<<18; 272 273 /** 274 * Value for {@link #flags}: true when the application's window can be 275 * increased in size for extra large screens. Corresponds to 276 * {@link android.R.styleable#AndroidManifestSupportsScreens_xlargeScreens 277 * android:xlargeScreens}. 278 */ 279 public static final int FLAG_SUPPORTS_XLARGE_SCREENS = 1<<19; 280 281 /** 282 * Value for {@link #flags}: true when the application has requested a 283 * large heap for its processes. Corresponds to 284 * {@link android.R.styleable#AndroidManifestApplication_largeHeap 285 * android:largeHeap}. 286 */ 287 public static final int FLAG_LARGE_HEAP = 1<<20; 288 289 /** 290 * Value for {@link #flags}: true if this application's package is in 291 * the stopped state. 292 */ 293 public static final int FLAG_STOPPED = 1<<21; 294 295 /** 296 * Value for {@link #flags}: Set to true if the application has been 297 * installed using the forward lock option. 298 * 299 * NOTE: DO NOT CHANGE THIS VALUE! It is saved in packages.xml. 300 * 301 * {@hide} 302 */ 303 public static final int FLAG_FORWARD_LOCK = 1<<29; 304 305 /** 306 * Value for {@link #flags}: set to <code>true</code> if the application 307 * has reported that it is heavy-weight, and thus can not participate in 308 * the normal application lifecycle. 309 * 310 * <p>Comes from the 311 * {@link android.R.styleable#AndroidManifestApplication_cantSaveState android:cantSaveState} 312 * attribute of the <application> tag. 313 * 314 * {@hide} 315 */ 316 public static final int FLAG_CANT_SAVE_STATE = 1<<28; 317 318 /** 319 * Flags associated with the application. Any combination of 320 * {@link #FLAG_SYSTEM}, {@link #FLAG_DEBUGGABLE}, {@link #FLAG_HAS_CODE}, 321 * {@link #FLAG_PERSISTENT}, {@link #FLAG_FACTORY_TEST}, and 322 * {@link #FLAG_ALLOW_TASK_REPARENTING} 323 * {@link #FLAG_ALLOW_CLEAR_USER_DATA}, {@link #FLAG_UPDATED_SYSTEM_APP}, 324 * {@link #FLAG_TEST_ONLY}, {@link #FLAG_SUPPORTS_SMALL_SCREENS}, 325 * {@link #FLAG_SUPPORTS_NORMAL_SCREENS}, 326 * {@link #FLAG_SUPPORTS_LARGE_SCREENS}, {@link #FLAG_SUPPORTS_XLARGE_SCREENS}, 327 * {@link #FLAG_RESIZEABLE_FOR_SCREENS}, 328 * {@link #FLAG_SUPPORTS_SCREEN_DENSITIES}, {@link #FLAG_VM_SAFE_MODE} 329 */ 330 public int flags = 0; 331 332 /** 333 * Full path to the location of this package. 334 */ 335 public String sourceDir; 336 337 /** 338 * Full path to the location of the publicly available parts of this 339 * package (i.e. the primary resource package and manifest). For 340 * non-forward-locked apps this will be the same as {@link #sourceDir). 341 */ 342 public String publicSourceDir; 343 344 /** 345 * Full paths to the locations of extra resource packages this application 346 * uses. This field is only used if there are extra resource packages, 347 * otherwise it is null. 348 * 349 * {@hide} 350 */ 351 public String[] resourceDirs; 352 353 /** 354 * Paths to all shared libraries this application is linked against. This 355 * field is only set if the {@link PackageManager#GET_SHARED_LIBRARY_FILES 356 * PackageManager.GET_SHARED_LIBRARY_FILES} flag was used when retrieving 357 * the structure. 358 */ 359 public String[] sharedLibraryFiles; 360 361 /** 362 * Full path to a directory assigned to the package for its persistent 363 * data. 364 */ 365 public String dataDir; 366 367 /** 368 * Full path to the directory where native JNI libraries are stored. 369 */ 370 public String nativeLibraryDir; 371 372 /** 373 * The kernel user-ID that has been assigned to this application; 374 * currently this is not a unique ID (multiple applications can have 375 * the same uid). 376 */ 377 public int uid; 378 379 /** 380 * The minimum SDK version this application targets. It may run on earlier 381 * versions, but it knows how to work with any new behavior added at this 382 * version. Will be {@link android.os.Build.VERSION_CODES#CUR_DEVELOPMENT} 383 * if this is a development build and the app is targeting that. You should 384 * compare that this number is >= the SDK version number at which your 385 * behavior was introduced. 386 */ 387 public int targetSdkVersion; 388 389 /** 390 * When false, indicates that all components within this application are 391 * considered disabled, regardless of their individually set enabled status. 392 */ 393 public boolean enabled = true; 394 395 /** 396 * For convenient access to package's install location. 397 * @hide 398 */ 399 public int installLocation = PackageInfo.INSTALL_LOCATION_UNSPECIFIED; 400 401 public void dump(Printer pw, String prefix) { 402 super.dumpFront(pw, prefix); 403 if (className != null) { 404 pw.println(prefix + "className=" + className); 405 } 406 if (permission != null) { 407 pw.println(prefix + "permission=" + permission); 408 } 409 pw.println(prefix + "processName=" + processName); 410 pw.println(prefix + "taskAffinity=" + taskAffinity); 411 pw.println(prefix + "uid=" + uid + " flags=0x" + Integer.toHexString(flags) 412 + " theme=0x" + Integer.toHexString(theme)); 413 pw.println(prefix + "sourceDir=" + sourceDir); 414 if (sourceDir == null) { 415 if (publicSourceDir != null) { 416 pw.println(prefix + "publicSourceDir=" + publicSourceDir); 417 } 418 } else if (!sourceDir.equals(publicSourceDir)) { 419 pw.println(prefix + "publicSourceDir=" + publicSourceDir); 420 } 421 if (resourceDirs != null) { 422 pw.println(prefix + "resourceDirs=" + resourceDirs); 423 } 424 pw.println(prefix + "dataDir=" + dataDir); 425 if (sharedLibraryFiles != null) { 426 pw.println(prefix + "sharedLibraryFiles=" + sharedLibraryFiles); 427 } 428 pw.println(prefix + "enabled=" + enabled + " targetSdkVersion=" + targetSdkVersion); 429 if (manageSpaceActivityName != null) { 430 pw.println(prefix + "manageSpaceActivityName="+manageSpaceActivityName); 431 } 432 if (descriptionRes != 0) { 433 pw.println(prefix + "description=0x"+Integer.toHexString(descriptionRes)); 434 } 435 super.dumpBack(pw, prefix); 436 } 437 438 public static class DisplayNameComparator 439 implements Comparator<ApplicationInfo> { 440 public DisplayNameComparator(PackageManager pm) { 441 mPM = pm; 442 } 443 444 public final int compare(ApplicationInfo aa, ApplicationInfo ab) { 445 CharSequence sa = mPM.getApplicationLabel(aa); 446 if (sa == null) { 447 sa = aa.packageName; 448 } 449 CharSequence sb = mPM.getApplicationLabel(ab); 450 if (sb == null) { 451 sb = ab.packageName; 452 } 453 454 return sCollator.compare(sa.toString(), sb.toString()); 455 } 456 457 private final Collator sCollator = Collator.getInstance(); 458 private PackageManager mPM; 459 } 460 461 public ApplicationInfo() { 462 } 463 464 public ApplicationInfo(ApplicationInfo orig) { 465 super(orig); 466 taskAffinity = orig.taskAffinity; 467 permission = orig.permission; 468 processName = orig.processName; 469 className = orig.className; 470 theme = orig.theme; 471 flags = orig.flags; 472 sourceDir = orig.sourceDir; 473 publicSourceDir = orig.publicSourceDir; 474 nativeLibraryDir = orig.nativeLibraryDir; 475 resourceDirs = orig.resourceDirs; 476 sharedLibraryFiles = orig.sharedLibraryFiles; 477 dataDir = orig.dataDir; 478 uid = orig.uid; 479 targetSdkVersion = orig.targetSdkVersion; 480 enabled = orig.enabled; 481 installLocation = orig.installLocation; 482 manageSpaceActivityName = orig.manageSpaceActivityName; 483 descriptionRes = orig.descriptionRes; 484 } 485 486 487 public String toString() { 488 return "ApplicationInfo{" 489 + Integer.toHexString(System.identityHashCode(this)) 490 + " " + packageName + "}"; 491 } 492 493 public int describeContents() { 494 return 0; 495 } 496 497 public void writeToParcel(Parcel dest, int parcelableFlags) { 498 super.writeToParcel(dest, parcelableFlags); 499 dest.writeString(taskAffinity); 500 dest.writeString(permission); 501 dest.writeString(processName); 502 dest.writeString(className); 503 dest.writeInt(theme); 504 dest.writeInt(flags); 505 dest.writeString(sourceDir); 506 dest.writeString(publicSourceDir); 507 dest.writeString(nativeLibraryDir); 508 dest.writeStringArray(resourceDirs); 509 dest.writeStringArray(sharedLibraryFiles); 510 dest.writeString(dataDir); 511 dest.writeInt(uid); 512 dest.writeInt(targetSdkVersion); 513 dest.writeInt(enabled ? 1 : 0); 514 dest.writeInt(installLocation); 515 dest.writeString(manageSpaceActivityName); 516 dest.writeString(backupAgentName); 517 dest.writeString(fullBackupAgentName); 518 dest.writeInt(descriptionRes); 519 } 520 521 public static final Parcelable.Creator<ApplicationInfo> CREATOR 522 = new Parcelable.Creator<ApplicationInfo>() { 523 public ApplicationInfo createFromParcel(Parcel source) { 524 return new ApplicationInfo(source); 525 } 526 public ApplicationInfo[] newArray(int size) { 527 return new ApplicationInfo[size]; 528 } 529 }; 530 531 private ApplicationInfo(Parcel source) { 532 super(source); 533 taskAffinity = source.readString(); 534 permission = source.readString(); 535 processName = source.readString(); 536 className = source.readString(); 537 theme = source.readInt(); 538 flags = source.readInt(); 539 sourceDir = source.readString(); 540 publicSourceDir = source.readString(); 541 nativeLibraryDir = source.readString(); 542 resourceDirs = source.readStringArray(); 543 sharedLibraryFiles = source.readStringArray(); 544 dataDir = source.readString(); 545 uid = source.readInt(); 546 targetSdkVersion = source.readInt(); 547 enabled = source.readInt() != 0; 548 installLocation = source.readInt(); 549 manageSpaceActivityName = source.readString(); 550 backupAgentName = source.readString(); 551 fullBackupAgentName = source.readString(); 552 descriptionRes = source.readInt(); 553 } 554 555 /** 556 * Retrieve the textual description of the application. This 557 * will call back on the given PackageManager to load the description from 558 * the application. 559 * 560 * @param pm A PackageManager from which the label can be loaded; usually 561 * the PackageManager from which you originally retrieved this item. 562 * 563 * @return Returns a CharSequence containing the application's description. 564 * If there is no description, null is returned. 565 */ 566 public CharSequence loadDescription(PackageManager pm) { 567 if (descriptionRes != 0) { 568 CharSequence label = pm.getText(packageName, descriptionRes, this); 569 if (label != null) { 570 return label; 571 } 572 } 573 return null; 574 } 575 576 /** 577 * Disable compatibility mode 578 * 579 * @hide 580 */ 581 public void disableCompatibilityMode() { 582 flags |= (FLAG_SUPPORTS_LARGE_SCREENS | FLAG_SUPPORTS_NORMAL_SCREENS | 583 FLAG_SUPPORTS_SMALL_SCREENS | FLAG_RESIZEABLE_FOR_SCREENS | 584 FLAG_SUPPORTS_SCREEN_DENSITIES | FLAG_SUPPORTS_XLARGE_SCREENS); 585 } 586 587 /** 588 * @hide 589 */ 590 @Override protected Drawable loadDefaultIcon(PackageManager pm) { 591 if ((flags & FLAG_EXTERNAL_STORAGE) != 0 592 && isPackageUnavailable(pm)) { 593 return Resources.getSystem().getDrawable( 594 com.android.internal.R.drawable.sym_app_on_sd_unavailable_icon); 595 } 596 return pm.getDefaultActivityIcon(); 597 } 598 599 private boolean isPackageUnavailable(PackageManager pm) { 600 try { 601 return pm.getPackageInfo(packageName, 0) == null; 602 } catch (NameNotFoundException ex) { 603 return true; 604 } 605 } 606 607 /** 608 * @hide 609 */ 610 @Override protected ApplicationInfo getApplicationInfo() { 611 return this; 612 } 613} 614