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