blob: 4df83036daaf5dde4414d2bb1e5ff653b45abf06 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.content.pm;
18
19import android.content.res.XmlResourceParser;
20
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.os.Parcel;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010024import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.TextUtils;
26import android.util.Printer;
27
28import java.text.Collator;
29import java.util.Comparator;
30
31/**
32 * Base class containing information common to all package items held by
33 * the package manager. This provides a very common basic set of attributes:
34 * a label, icon, and meta-data. This class is not intended
35 * to be used by itself; it is simply here to share common definitions
36 * between all items returned by the package manager. As such, it does not
37 * itself implement Parcelable, but does provide convenience methods to assist
38 * in the implementation of Parcelable in subclasses.
39 */
40public class PackageItemInfo {
41 /**
42 * Public name of this item. From the "android:name" attribute.
43 */
44 public String name;
45
46 /**
47 * Name of the package that this item is in.
48 */
49 public String packageName;
50
51 /**
52 * A string resource identifier (in the package's resources) of this
53 * component's label. From the "label" attribute or, if not set, 0.
54 */
55 public int labelRes;
56
57 /**
58 * The string provided in the AndroidManifest file, if any. You
59 * probably don't want to use this. You probably want
60 * {@link PackageManager#getApplicationLabel}
61 */
62 public CharSequence nonLocalizedLabel;
63
64 /**
65 * A drawable resource identifier (in the package's resources) of this
66 * component's icon. From the "icon" attribute or, if not set, 0.
67 */
68 public int icon;
69
70 /**
Adam Powell81cd2e92010-04-21 16:35:18 -070071 * A drawable resource identifier (in the package's resources) of this
Jose Limaf78e3122014-03-06 12:13:15 -080072 * component's banner. From the "banner" attribute or, if not set, 0.
73 */
74 public int banner;
75
76 /**
77 * A drawable resource identifier (in the package's resources) of this
Adam Powell81cd2e92010-04-21 16:35:18 -070078 * component's logo. Logos may be larger/wider than icons and are
79 * displayed by certain UI elements in place of a name or name/icon
80 * combination. From the "logo" attribute or, if not set, 0.
81 */
82 public int logo;
83
84 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * Additional meta-data associated with this component. This field
86 * will only be filled in if you set the
87 * {@link PackageManager#GET_META_DATA} flag when requesting the info.
88 */
89 public Bundle metaData;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010090
91 /**
92 * If different of UserHandle.USER_NULL, The icon of this item will be the one of that user.
93 * @hide
94 */
95 public int showUserIcon;
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public PackageItemInfo() {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010098 showUserIcon = UserHandle.USER_NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100
101 public PackageItemInfo(PackageItemInfo orig) {
102 name = orig.name;
Romain Guy2aba11f2010-03-29 16:03:01 -0700103 if (name != null) name = name.trim();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 packageName = orig.packageName;
105 labelRes = orig.labelRes;
106 nonLocalizedLabel = orig.nonLocalizedLabel;
Romain Guy2aba11f2010-03-29 16:03:01 -0700107 if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 icon = orig.icon;
Jose Limaf78e3122014-03-06 12:13:15 -0800109 banner = orig.banner;
Adam Powell81cd2e92010-04-21 16:35:18 -0700110 logo = orig.logo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 metaData = orig.metaData;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100112 showUserIcon = orig.showUserIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
114
115 /**
116 * Retrieve the current textual label associated with this item. This
117 * will call back on the given PackageManager to load the label from
118 * the application.
119 *
120 * @param pm A PackageManager from which the label can be loaded; usually
121 * the PackageManager from which you originally retrieved this item.
122 *
123 * @return Returns a CharSequence containing the item's label. If the
124 * item does not have a label, its name is returned.
125 */
126 public CharSequence loadLabel(PackageManager pm) {
127 if (nonLocalizedLabel != null) {
128 return nonLocalizedLabel;
129 }
130 if (labelRes != 0) {
Jeff Brown07330792010-03-30 19:57:08 -0700131 CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 if (label != null) {
Romain Guy2aba11f2010-03-29 16:03:01 -0700133 return label.toString().trim();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135 }
Romain Guy2aba11f2010-03-29 16:03:01 -0700136 if (name != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 return name;
138 }
139 return packageName;
140 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
143 * Retrieve the current graphical icon associated with this item. This
144 * will call back on the given PackageManager to load the icon from
145 * the application.
146 *
147 * @param pm A PackageManager from which the icon can be loaded; usually
148 * the PackageManager from which you originally retrieved this item.
149 *
150 * @return Returns a Drawable containing the item's icon. If the
Jeff Brown07330792010-03-30 19:57:08 -0700151 * item does not have an icon, the item's default icon is returned
152 * such as the default activity icon.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
154 public Drawable loadIcon(PackageManager pm) {
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +0100155 return pm.loadItemIcon(this, getApplicationInfo());
Jeff Brown07330792010-03-30 19:57:08 -0700156 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +0100157
Jeff Brown07330792010-03-30 19:57:08 -0700158 /**
Benjamin Franzec2d48b2014-10-01 15:38:43 +0100159 * Retrieve the current graphical icon associated with this item without
160 * the addition of a work badge if applicable.
161 * This will call back on the given PackageManager to load the icon from
162 * the application.
163 *
164 * @param pm A PackageManager from which the icon can be loaded; usually
165 * the PackageManager from which you originally retrieved this item.
166 *
167 * @return Returns a Drawable containing the item's icon. If the
168 * item does not have an icon, the item's default icon is returned
169 * such as the default activity icon.
170 */
171 public Drawable loadUnbadgedIcon(PackageManager pm) {
172 return pm.loadUnbadgedItemIcon(this, getApplicationInfo());
173 }
174
175 /**
Jose Limaf78e3122014-03-06 12:13:15 -0800176 * Retrieve the current graphical banner associated with this item. This
177 * will call back on the given PackageManager to load the banner from
178 * the application.
179 *
180 * @param pm A PackageManager from which the banner can be loaded; usually
181 * the PackageManager from which you originally retrieved this item.
182 *
183 * @return Returns a Drawable containing the item's banner. If the item
184 * does not have a banner, this method will return null.
185 */
186 public Drawable loadBanner(PackageManager pm) {
187 if (banner != 0) {
188 Drawable dr = pm.getDrawable(packageName, banner, getApplicationInfo());
189 if (dr != null) {
190 return dr;
191 }
192 }
193 return loadDefaultBanner(pm);
194 }
195
196 /**
Jeff Brown07330792010-03-30 19:57:08 -0700197 * Retrieve the default graphical icon associated with this item.
198 *
199 * @param pm A PackageManager from which the icon can be loaded; usually
200 * the PackageManager from which you originally retrieved this item.
201 *
202 * @return Returns a Drawable containing the item's default icon
203 * such as the default activity icon.
204 *
205 * @hide
206 */
Alexandra Gherghinaa7093142014-07-30 13:43:39 +0100207 public Drawable loadDefaultIcon(PackageManager pm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 return pm.getDefaultActivityIcon();
209 }
Jose Limaf78e3122014-03-06 12:13:15 -0800210
211 /**
212 * Retrieve the default graphical banner associated with this item.
213 *
214 * @param pm A PackageManager from which the banner can be loaded; usually
215 * the PackageManager from which you originally retrieved this item.
216 *
217 * @return Returns a Drawable containing the item's default banner
218 * or null if no default logo is available.
219 *
220 * @hide
221 */
222 protected Drawable loadDefaultBanner(PackageManager pm) {
223 return null;
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Adam Powell81cd2e92010-04-21 16:35:18 -0700227 * Retrieve the current graphical logo associated with this item. This
228 * will call back on the given PackageManager to load the logo from
229 * the application.
230 *
231 * @param pm A PackageManager from which the logo can be loaded; usually
232 * the PackageManager from which you originally retrieved this item.
233 *
234 * @return Returns a Drawable containing the item's logo. If the item
235 * does not have a logo, this method will return null.
236 */
237 public Drawable loadLogo(PackageManager pm) {
238 if (logo != 0) {
239 Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo());
240 if (d != null) {
241 return d;
242 }
243 }
244 return loadDefaultLogo(pm);
245 }
246
247 /**
248 * Retrieve the default graphical logo associated with this item.
249 *
250 * @param pm A PackageManager from which the logo can be loaded; usually
251 * the PackageManager from which you originally retrieved this item.
252 *
253 * @return Returns a Drawable containing the item's default logo
254 * or null if no default logo is available.
255 *
256 * @hide
257 */
258 protected Drawable loadDefaultLogo(PackageManager pm) {
259 return null;
260 }
261
262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * Load an XML resource attached to the meta-data of this item. This will
264 * retrieved the name meta-data entry, and if defined call back on the
265 * given PackageManager to load its XML file from the application.
266 *
267 * @param pm A PackageManager from which the XML can be loaded; usually
268 * the PackageManager from which you originally retrieved this item.
269 * @param name Name of the meta-date you would like to load.
270 *
271 * @return Returns an XmlPullParser you can use to parse the XML file
272 * assigned as the given meta-data. If the meta-data name is not defined
273 * or the XML resource could not be found, null is returned.
274 */
275 public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
276 if (metaData != null) {
277 int resid = metaData.getInt(name);
278 if (resid != 0) {
Jeff Brown07330792010-03-30 19:57:08 -0700279 return pm.getXml(packageName, resid, getApplicationInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 }
282 return null;
283 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700284
Dianne Hackborn6ac42ae2015-12-08 17:22:10 -0800285 /**
286 * @hide Flag for dumping: include all details.
287 */
288 public static final int DUMP_FLAG_DETAILS = 1<<0;
289
290 /**
291 * @hide Flag for dumping: include nested ApplicationInfo.
292 */
293 public static final int DUMP_FLAG_APPLICATION = 1<<1;
294
295 /**
296 * @hide Flag for dumping: all flags to dump everything.
297 */
298 public static final int DUMP_FLAG_ALL = DUMP_FLAG_DETAILS | DUMP_FLAG_APPLICATION;
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 protected void dumpFront(Printer pw, String prefix) {
Dianne Hackborn12527f92009-11-11 17:39:50 -0800301 if (name != null) {
302 pw.println(prefix + "name=" + name);
303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 pw.println(prefix + "packageName=" + packageName);
Jose Limaf78e3122014-03-06 12:13:15 -0800305 if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) {
Dianne Hackborn12527f92009-11-11 17:39:50 -0800306 pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes)
307 + " nonLocalizedLabel=" + nonLocalizedLabel
Jose Limaf78e3122014-03-06 12:13:15 -0800308 + " icon=0x" + Integer.toHexString(icon)
309 + " banner=0x" + Integer.toHexString(banner));
Dianne Hackborn12527f92009-11-11 17:39:50 -0800310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312
313 protected void dumpBack(Printer pw, String prefix) {
314 // no back here
315 }
316
317 public void writeToParcel(Parcel dest, int parcelableFlags) {
318 dest.writeString(name);
319 dest.writeString(packageName);
320 dest.writeInt(labelRes);
321 TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);
322 dest.writeInt(icon);
Adam Powell81cd2e92010-04-21 16:35:18 -0700323 dest.writeInt(logo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 dest.writeBundle(metaData);
Jose Limaf78e3122014-03-06 12:13:15 -0800325 dest.writeInt(banner);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100326 dest.writeInt(showUserIcon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
Jeff Brown07330792010-03-30 19:57:08 -0700328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 protected PackageItemInfo(Parcel source) {
330 name = source.readString();
331 packageName = source.readString();
332 labelRes = source.readInt();
333 nonLocalizedLabel
334 = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
335 icon = source.readInt();
Adam Powell81cd2e92010-04-21 16:35:18 -0700336 logo = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 metaData = source.readBundle();
Jose Limaf78e3122014-03-06 12:13:15 -0800338 banner = source.readInt();
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100339 showUserIcon = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341
Jeff Brown07330792010-03-30 19:57:08 -0700342 /**
343 * Get the ApplicationInfo for the application to which this item belongs,
344 * if available, otherwise returns null.
345 *
346 * @return Returns the ApplicationInfo of this item, or null if not known.
347 *
348 * @hide
349 */
350 protected ApplicationInfo getApplicationInfo() {
351 return null;
352 }
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 public static class DisplayNameComparator
355 implements Comparator<PackageItemInfo> {
356 public DisplayNameComparator(PackageManager pm) {
357 mPM = pm;
358 }
359
360 public final int compare(PackageItemInfo aa, PackageItemInfo ab) {
361 CharSequence sa = aa.loadLabel(mPM);
362 if (sa == null) sa = aa.name;
363 CharSequence sb = ab.loadLabel(mPM);
364 if (sb == null) sb = ab.name;
365 return sCollator.compare(sa.toString(), sb.toString());
366 }
367
368 private final Collator sCollator = Collator.getInstance();
369 private PackageManager mPM;
370 }
371}