Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2008 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.ComponentName;
     20 import android.graphics.drawable.Drawable;
     21 import android.os.Parcel;
     22 import android.os.Parcelable;
     23 import android.util.Printer;
     24 
     25 /**
     26  * Base class containing information common to all application components
     27  * ({@link ActivityInfo}, {@link ServiceInfo}).  This class is not intended
     28  * to be used by itself; it is simply here to share common definitions
     29  * between all application components.  As such, it does not itself
     30  * implement Parcelable, but does provide convenience methods to assist
     31  * in the implementation of Parcelable in subclasses.
     32  */
     33 public class ComponentInfo extends PackageItemInfo {
     34     /**
     35      * Global information about the application/package this component is a
     36      * part of.
     37      */
     38     public ApplicationInfo applicationInfo;
     39 
     40     /**
     41      * The name of the process this component should run in.
     42      * From the "android:process" attribute or, if not set, the same
     43      * as <var>applicationInfo.processName</var>.
     44      */
     45     public String processName;
     46 
     47     /**
     48      * The name of the split in which this component is declared.
     49      * Null if the component was declared in the base APK.
     50      */
     51     public String splitName;
     52 
     53     /**
     54      * A string resource identifier (in the package's resources) containing
     55      * a user-readable description of the component.  From the "description"
     56      * attribute or, if not set, 0.
     57      */
     58     public int descriptionRes;
     59 
     60     /**
     61      * Indicates whether or not this component may be instantiated.  Note that this value can be
     62      * overridden by the one in its parent {@link ApplicationInfo}.
     63      */
     64     public boolean enabled = true;
     65 
     66     /**
     67      * Set to true if this component is available for use by other applications.
     68      * Comes from {@link android.R.attr#exported android:exported} of the
     69      * &lt;activity&gt;, &lt;receiver&gt;, &lt;service&gt;, or
     70      * &lt;provider&gt; tag.
     71      */
     72     public boolean exported = false;
     73 
     74     /**
     75      * Indicates if this component is aware of direct boot lifecycle, and can be
     76      * safely run before the user has entered their credentials (such as a lock
     77      * pattern or PIN).
     78      */
     79     public boolean directBootAware = false;
     80 
     81     /** @removed */
     82     @Deprecated
     83     public boolean encryptionAware = false;
     84 
     85     public ComponentInfo() {
     86     }
     87 
     88     public ComponentInfo(ComponentInfo orig) {
     89         super(orig);
     90         applicationInfo = orig.applicationInfo;
     91         processName = orig.processName;
     92         splitName = orig.splitName;
     93         descriptionRes = orig.descriptionRes;
     94         enabled = orig.enabled;
     95         exported = orig.exported;
     96         encryptionAware = directBootAware = orig.directBootAware;
     97     }
     98 
     99     /** @hide */
    100     @Override public CharSequence loadUnsafeLabel(PackageManager pm) {
    101         if (nonLocalizedLabel != null) {
    102             return nonLocalizedLabel;
    103         }
    104         ApplicationInfo ai = applicationInfo;
    105         CharSequence label;
    106         if (labelRes != 0) {
    107             label = pm.getText(packageName, labelRes, ai);
    108             if (label != null) {
    109                 return label;
    110             }
    111         }
    112         if (ai.nonLocalizedLabel != null) {
    113             return ai.nonLocalizedLabel;
    114         }
    115         if (ai.labelRes != 0) {
    116             label = pm.getText(packageName, ai.labelRes, ai);
    117             if (label != null) {
    118                 return label;
    119             }
    120         }
    121         return name;
    122     }
    123 
    124     /**
    125      * Return whether this component and its enclosing application are enabled.
    126      */
    127     public boolean isEnabled() {
    128         return enabled && applicationInfo.enabled;
    129     }
    130 
    131     /**
    132      * Return the icon resource identifier to use for this component.  If
    133      * the component defines an icon, that is used; else, the application
    134      * icon is used.
    135      *
    136      * @return The icon associated with this component.
    137      */
    138     public final int getIconResource() {
    139         return icon != 0 ? icon : applicationInfo.icon;
    140     }
    141 
    142     /**
    143      * Return the logo resource identifier to use for this component.  If
    144      * the component defines a logo, that is used; else, the application
    145      * logo is used.
    146      *
    147      * @return The logo associated with this component.
    148      */
    149     public final int getLogoResource() {
    150         return logo != 0 ? logo : applicationInfo.logo;
    151     }
    152 
    153     /**
    154      * Return the banner resource identifier to use for this component. If the
    155      * component defines a banner, that is used; else, the application banner is
    156      * used.
    157      *
    158      * @return The banner associated with this component.
    159      */
    160     public final int getBannerResource() {
    161         return banner != 0 ? banner : applicationInfo.banner;
    162     }
    163 
    164     /** {@hide} */
    165     public ComponentName getComponentName() {
    166         return new ComponentName(packageName, name);
    167     }
    168 
    169     protected void dumpFront(Printer pw, String prefix) {
    170         super.dumpFront(pw, prefix);
    171         if (processName != null && !packageName.equals(processName)) {
    172             pw.println(prefix + "processName=" + processName);
    173         }
    174         if (splitName != null) {
    175             pw.println(prefix + "splitName=" + splitName);
    176         }
    177         pw.println(prefix + "enabled=" + enabled + " exported=" + exported
    178                 + " directBootAware=" + directBootAware);
    179         if (descriptionRes != 0) {
    180             pw.println(prefix + "description=" + descriptionRes);
    181         }
    182     }
    183 
    184     protected void dumpBack(Printer pw, String prefix) {
    185         dumpBack(pw, prefix, DUMP_FLAG_ALL);
    186     }
    187 
    188     void dumpBack(Printer pw, String prefix, int dumpFlags) {
    189         if ((dumpFlags & DUMP_FLAG_APPLICATION) != 0) {
    190             if (applicationInfo != null) {
    191                 pw.println(prefix + "ApplicationInfo:");
    192                 applicationInfo.dump(pw, prefix + "  ", dumpFlags);
    193             } else {
    194                 pw.println(prefix + "ApplicationInfo: null");
    195             }
    196         }
    197         super.dumpBack(pw, prefix);
    198     }
    199 
    200     public void writeToParcel(Parcel dest, int parcelableFlags) {
    201         super.writeToParcel(dest, parcelableFlags);
    202         if ((parcelableFlags & Parcelable.PARCELABLE_ELIDE_DUPLICATES) != 0) {
    203             dest.writeInt(0);
    204         } else {
    205             dest.writeInt(1);
    206             applicationInfo.writeToParcel(dest, parcelableFlags);
    207         }
    208         dest.writeString(processName);
    209         dest.writeString(splitName);
    210         dest.writeInt(descriptionRes);
    211         dest.writeInt(enabled ? 1 : 0);
    212         dest.writeInt(exported ? 1 : 0);
    213         dest.writeInt(directBootAware ? 1 : 0);
    214     }
    215 
    216     protected ComponentInfo(Parcel source) {
    217         super(source);
    218         final boolean hasApplicationInfo = (source.readInt() != 0);
    219         if (hasApplicationInfo) {
    220             applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);
    221         }
    222         processName = source.readString();
    223         splitName = source.readString();
    224         descriptionRes = source.readInt();
    225         enabled = (source.readInt() != 0);
    226         exported = (source.readInt() != 0);
    227         encryptionAware = directBootAware = (source.readInt() != 0);
    228     }
    229 
    230     /**
    231      * @hide
    232      */
    233     @Override
    234     public Drawable loadDefaultIcon(PackageManager pm) {
    235         return applicationInfo.loadIcon(pm);
    236     }
    237 
    238     /**
    239      * @hide
    240      */
    241     @Override protected Drawable loadDefaultBanner(PackageManager pm) {
    242         return applicationInfo.loadBanner(pm);
    243     }
    244 
    245     /**
    246      * @hide
    247      */
    248     @Override
    249     protected Drawable loadDefaultLogo(PackageManager pm) {
    250         return applicationInfo.loadLogo(pm);
    251     }
    252 
    253     /**
    254      * @hide
    255      */
    256     @Override protected ApplicationInfo getApplicationInfo() {
    257         return applicationInfo;
    258     }
    259 }
    260