PackageItemInfo.java revision 2aba11f6b9b7b202742a11797084a23d0f940c35
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        if (name != null) name = name.trim();
82        packageName = orig.packageName;
83        labelRes = orig.labelRes;
84        nonLocalizedLabel = orig.nonLocalizedLabel;
85        if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim();
86        icon = orig.icon;
87        metaData = orig.metaData;
88    }
89
90    /**
91     * Retrieve the current textual label associated with this item.  This
92     * will call back on the given PackageManager to load the label from
93     * the application.
94     *
95     * @param pm A PackageManager from which the label can be loaded; usually
96     * the PackageManager from which you originally retrieved this item.
97     *
98     * @return Returns a CharSequence containing the item's label.  If the
99     * item does not have a label, its name is returned.
100     */
101    public CharSequence loadLabel(PackageManager pm) {
102        if (nonLocalizedLabel != null) {
103            return nonLocalizedLabel;
104        }
105        if (labelRes != 0) {
106            CharSequence label = pm.getText(packageName, labelRes, null);
107            if (label != null) {
108                return label.toString().trim();
109            }
110        }
111        if (name != null) {
112            return name;
113        }
114        return packageName;
115    }
116
117    /**
118     * Retrieve the current graphical icon associated with this item.  This
119     * will call back on the given PackageManager to load the icon from
120     * the application.
121     *
122     * @param pm A PackageManager from which the icon can be loaded; usually
123     * the PackageManager from which you originally retrieved this item.
124     *
125     * @return Returns a Drawable containing the item's icon.  If the
126     * item does not have an icon, the default activity icon is returned.
127     */
128    public Drawable loadIcon(PackageManager pm) {
129        if (icon != 0) {
130            Drawable dr = pm.getDrawable(packageName, icon, null);
131            if (dr != null) {
132                return dr;
133            }
134        }
135        return pm.getDefaultActivityIcon();
136    }
137
138    /**
139     * Load an XML resource attached to the meta-data of this item.  This will
140     * retrieved the name meta-data entry, and if defined call back on the
141     * given PackageManager to load its XML file from the application.
142     *
143     * @param pm A PackageManager from which the XML can be loaded; usually
144     * the PackageManager from which you originally retrieved this item.
145     * @param name Name of the meta-date you would like to load.
146     *
147     * @return Returns an XmlPullParser you can use to parse the XML file
148     * assigned as the given meta-data.  If the meta-data name is not defined
149     * or the XML resource could not be found, null is returned.
150     */
151    public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
152        if (metaData != null) {
153            int resid = metaData.getInt(name);
154            if (resid != 0) {
155                return pm.getXml(packageName, resid, null);
156            }
157        }
158        return null;
159    }
160
161    protected void dumpFront(Printer pw, String prefix) {
162        if (name != null) {
163            pw.println(prefix + "name=" + name);
164        }
165        pw.println(prefix + "packageName=" + packageName);
166        if (labelRes != 0 || nonLocalizedLabel != null || icon != 0) {
167            pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes)
168                    + " nonLocalizedLabel=" + nonLocalizedLabel
169                    + " icon=0x" + Integer.toHexString(icon));
170        }
171    }
172
173    protected void dumpBack(Printer pw, String prefix) {
174        // no back here
175    }
176
177    public void writeToParcel(Parcel dest, int parcelableFlags) {
178        dest.writeString(name);
179        dest.writeString(packageName);
180        dest.writeInt(labelRes);
181        TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);
182        dest.writeInt(icon);
183        dest.writeBundle(metaData);
184    }
185
186    protected PackageItemInfo(Parcel source) {
187        name = source.readString();
188        packageName = source.readString();
189        labelRes = source.readInt();
190        nonLocalizedLabel
191                = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
192        icon = source.readInt();
193        metaData = source.readBundle();
194    }
195
196    public static class DisplayNameComparator
197            implements Comparator<PackageItemInfo> {
198        public DisplayNameComparator(PackageManager pm) {
199            mPM = pm;
200        }
201
202        public final int compare(PackageItemInfo aa, PackageItemInfo ab) {
203            CharSequence  sa = aa.loadLabel(mPM);
204            if (sa == null) sa = aa.name;
205            CharSequence  sb = ab.loadLabel(mPM);
206            if (sb == null) sb = ab.name;
207            return sCollator.compare(sa.toString(), sb.toString());
208        }
209
210        private final Collator   sCollator = Collator.getInstance();
211        private PackageManager   mPM;
212    }
213}
214