PackageItemInfo.java revision 12527f9fb1cb0a1ad3be8149c1c88a0e731cb4d6
1package android.content.pm;
2
3import android.content.res.XmlResourceParser;
4
5import android.graphics.drawable.Drawable;
6import android.os.Bundle;
7import android.os.Parcel;
8import android.text.TextUtils;
9import android.util.Printer;
10
11import java.text.Collator;
12import java.util.Comparator;
13
14/**
15 * Base class containing information common to all package items held by
16 * the package manager.  This provides a very common basic set of attributes:
17 * a label, icon, and meta-data.  This class is not intended
18 * to be used by itself; it is simply here to share common definitions
19 * between all items returned by the package manager.  As such, it does not
20 * itself implement Parcelable, but does provide convenience methods to assist
21 * in the implementation of Parcelable in subclasses.
22 */
23public class PackageItemInfo {
24    /**
25     * Public name of this item. From the "android:name" attribute.
26     */
27    public String name;
28
29    /**
30     * Name of the package that this item is in.
31     */
32    public String packageName;
33
34    /**
35     * A string resource identifier (in the package's resources) of this
36     * component's label.  From the "label" attribute or, if not set, 0.
37     */
38    public int labelRes;
39
40    /**
41     * The string provided in the AndroidManifest file, if any.  You
42     * probably don't want to use this.  You probably want
43     * {@link PackageManager#getApplicationLabel}
44     */
45    public CharSequence nonLocalizedLabel;
46
47    /**
48     * A drawable resource identifier (in the package's resources) of this
49     * component's icon.  From the "icon" attribute or, if not set, 0.
50     */
51    public int icon;
52
53    /**
54     * Additional meta-data associated with this component.  This field
55     * will only be filled in if you set the
56     * {@link PackageManager#GET_META_DATA} flag when requesting the info.
57     */
58    public Bundle metaData;
59
60    public PackageItemInfo() {
61    }
62
63    public PackageItemInfo(PackageItemInfo orig) {
64        name = orig.name;
65        packageName = orig.packageName;
66        labelRes = orig.labelRes;
67        nonLocalizedLabel = orig.nonLocalizedLabel;
68        icon = orig.icon;
69        metaData = orig.metaData;
70    }
71
72    /**
73     * Retrieve the current textual label associated with this item.  This
74     * will call back on the given PackageManager to load the label from
75     * the application.
76     *
77     * @param pm A PackageManager from which the label can be loaded; usually
78     * the PackageManager from which you originally retrieved this item.
79     *
80     * @return Returns a CharSequence containing the item's label.  If the
81     * item does not have a label, its name is returned.
82     */
83    public CharSequence loadLabel(PackageManager pm) {
84        if (nonLocalizedLabel != null) {
85            return nonLocalizedLabel;
86        }
87        if (labelRes != 0) {
88            CharSequence label = pm.getText(packageName, labelRes, null);
89            if (label != null) {
90                return label;
91            }
92        }
93        if(name != null) {
94            return name;
95        }
96        return packageName;
97    }
98
99    /**
100     * Retrieve the current graphical icon associated with this item.  This
101     * will call back on the given PackageManager to load the icon from
102     * the application.
103     *
104     * @param pm A PackageManager from which the icon can be loaded; usually
105     * the PackageManager from which you originally retrieved this item.
106     *
107     * @return Returns a Drawable containing the item's icon.  If the
108     * item does not have an icon, the default activity icon is returned.
109     */
110    public Drawable loadIcon(PackageManager pm) {
111        if (icon != 0) {
112            Drawable dr = pm.getDrawable(packageName, icon, null);
113            if (dr != null) {
114                return dr;
115            }
116        }
117        return pm.getDefaultActivityIcon();
118    }
119
120    /**
121     * Load an XML resource attached to the meta-data of this item.  This will
122     * retrieved the name meta-data entry, and if defined call back on the
123     * given PackageManager to load its XML file from the application.
124     *
125     * @param pm A PackageManager from which the XML can be loaded; usually
126     * the PackageManager from which you originally retrieved this item.
127     * @param name Name of the meta-date you would like to load.
128     *
129     * @return Returns an XmlPullParser you can use to parse the XML file
130     * assigned as the given meta-data.  If the meta-data name is not defined
131     * or the XML resource could not be found, null is returned.
132     */
133    public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
134        if (metaData != null) {
135            int resid = metaData.getInt(name);
136            if (resid != 0) {
137                return pm.getXml(packageName, resid, null);
138            }
139        }
140        return null;
141    }
142
143    protected void dumpFront(Printer pw, String prefix) {
144        if (name != null) {
145            pw.println(prefix + "name=" + name);
146        }
147        pw.println(prefix + "packageName=" + packageName);
148        if (labelRes != 0 || nonLocalizedLabel != null || icon != 0) {
149            pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes)
150                    + " nonLocalizedLabel=" + nonLocalizedLabel
151                    + " icon=0x" + Integer.toHexString(icon));
152        }
153    }
154
155    protected void dumpBack(Printer pw, String prefix) {
156        // no back here
157    }
158
159    public void writeToParcel(Parcel dest, int parcelableFlags) {
160        dest.writeString(name);
161        dest.writeString(packageName);
162        dest.writeInt(labelRes);
163        TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);
164        dest.writeInt(icon);
165        dest.writeBundle(metaData);
166    }
167
168    protected PackageItemInfo(Parcel source) {
169        name = source.readString();
170        packageName = source.readString();
171        labelRes = source.readInt();
172        nonLocalizedLabel
173                = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
174        icon = source.readInt();
175        metaData = source.readBundle();
176    }
177
178    public static class DisplayNameComparator
179            implements Comparator<PackageItemInfo> {
180        public DisplayNameComparator(PackageManager pm) {
181            mPM = pm;
182        }
183
184        public final int compare(PackageItemInfo aa, PackageItemInfo ab) {
185            CharSequence  sa = aa.loadLabel(mPM);
186            if (sa == null) sa = aa.name;
187            CharSequence  sb = ab.loadLabel(mPM);
188            if (sb == null) sb = ab.name;
189            return sCollator.compare(sa.toString(), sb.toString());
190        }
191
192        private final Collator   sCollator = Collator.getInstance();
193        private PackageManager   mPM;
194    }
195}
196