ComponentInfo.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1package android.content.pm;
2
3import android.graphics.drawable.Drawable;
4import android.os.Parcel;
5import android.util.Printer;
6
7/**
8 * Base class containing information common to all application components
9 * ({@link ActivityInfo}, {@link ServiceInfo}).  This class is not intended
10 * to be used by itself; it is simply here to share common definitions
11 * between all application components.  As such, it does not itself
12 * implement Parcelable, but does provide convenience methods to assist
13 * in the implementation of Parcelable in subclasses.
14 */
15public class ComponentInfo extends PackageItemInfo {
16    /**
17     * Global information about the application/package this component is a
18     * part of.
19     */
20    public ApplicationInfo applicationInfo;
21
22    /**
23     * The name of the process this component should run in.
24     * From the "android:process" attribute or, if not set, the same
25     * as <var>applicationInfo.processName</var>.
26     */
27    public String processName;
28
29    /**
30     * Indicates whether or not this component may be instantiated.  Note that this value can be
31     * overriden by the one in its parent {@link ApplicationInfo}.
32     */
33    public boolean enabled = true;
34
35    /**
36     * Set to true if this component is available for use by other applications.
37     * Comes from {@link android.R.attr#exported android:exported} of the
38     * &lt;activity&gt;, &lt;receiver&gt;, &lt;service&gt;, or
39     * &lt;provider&gt; tag.
40     */
41    public boolean exported = false;
42
43    public ComponentInfo() {
44    }
45
46    public ComponentInfo(ComponentInfo orig) {
47        super(orig);
48        applicationInfo = orig.applicationInfo;
49        processName = orig.processName;
50        enabled = orig.enabled;
51        exported = orig.exported;
52    }
53
54    @Override public CharSequence loadLabel(PackageManager pm) {
55        if (nonLocalizedLabel != null) {
56            return nonLocalizedLabel;
57        }
58        ApplicationInfo ai = applicationInfo;
59        CharSequence label;
60        if (labelRes != 0) {
61            label = pm.getText(packageName, labelRes, ai);
62            if (label != null) {
63                return label;
64            }
65        }
66        if (ai.nonLocalizedLabel != null) {
67            return ai.nonLocalizedLabel;
68        }
69        if (ai.labelRes != 0) {
70            label = pm.getText(packageName, ai.labelRes, ai);
71            if (label != null) {
72                return label;
73            }
74        }
75        return name;
76    }
77
78    @Override public Drawable loadIcon(PackageManager pm) {
79        ApplicationInfo ai = applicationInfo;
80        Drawable dr;
81        if (icon != 0) {
82            dr = pm.getDrawable(packageName, icon, ai);
83            if (dr != null) {
84                return dr;
85            }
86        }
87        if (ai.icon != 0) {
88            dr = pm.getDrawable(packageName, ai.icon, ai);
89            if (dr != null) {
90                return dr;
91            }
92        }
93        return pm.getDefaultActivityIcon();
94    }
95
96    /**
97     * Return the icon resource identifier to use for this component.  If
98     * the component defines an icon, that is used; else, the application
99     * icon is used.
100     *
101     * @return The icon associated with this component.
102     */
103    public final int getIconResource() {
104        return icon != 0 ? icon : applicationInfo.icon;
105    }
106
107    protected void dumpFront(Printer pw, String prefix) {
108        super.dumpFront(pw, prefix);
109        pw.println(prefix + "enabled=" + enabled + " exported=" + exported
110                + " processName=" + processName);
111    }
112
113    protected void dumpBack(Printer pw, String prefix) {
114        if (applicationInfo != null) {
115            pw.println(prefix + "ApplicationInfo:");
116            applicationInfo.dump(pw, prefix + "  ");
117        } else {
118            pw.println(prefix + "ApplicationInfo: null");
119        }
120        super.dumpBack(pw, prefix);
121    }
122
123    public void writeToParcel(Parcel dest, int parcelableFlags) {
124        super.writeToParcel(dest, parcelableFlags);
125        applicationInfo.writeToParcel(dest, parcelableFlags);
126        dest.writeString(processName);
127        dest.writeInt(enabled ? 1 : 0);
128        dest.writeInt(exported ? 1 : 0);
129    }
130
131    protected ComponentInfo(Parcel source) {
132        super(source);
133        applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);
134        processName = source.readString();
135        enabled = (source.readInt() != 0);
136        exported = (source.readInt() != 0);
137    }
138}
139