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