Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.content.pm; |
| 18 | |
| 19 | import android.content.res.XmlResourceParser; |
| 20 | |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.os.Bundle; |
| 23 | import android.os.Parcel; |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 24 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | import android.util.Printer; |
| 27 | |
| 28 | import java.text.Collator; |
| 29 | import 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 | */ |
| 40 | public 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 Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 71 | * A drawable resource identifier (in the package's resources) of this |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 72 | * 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 Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 78 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | * 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 Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 90 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | public PackageItemInfo() { |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 98 | showUserIcon = UserHandle.USER_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | public PackageItemInfo(PackageItemInfo orig) { |
| 102 | name = orig.name; |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 103 | if (name != null) name = name.trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | packageName = orig.packageName; |
| 105 | labelRes = orig.labelRes; |
| 106 | nonLocalizedLabel = orig.nonLocalizedLabel; |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 107 | if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | icon = orig.icon; |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 109 | banner = orig.banner; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 110 | logo = orig.logo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | metaData = orig.metaData; |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 112 | showUserIcon = orig.showUserIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 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 Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 131 | CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | if (label != null) { |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 133 | return label.toString().trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 136 | if (name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | return name; |
| 138 | } |
| 139 | return packageName; |
| 140 | } |
Benjamin Franz | ec2d48b | 2014-10-01 15:38:43 +0100 | [diff] [blame] | 141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | /** |
| 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 Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 151 | * item does not have an icon, the item's default icon is returned |
| 152 | * such as the default activity icon. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | */ |
| 154 | public Drawable loadIcon(PackageManager pm) { |
Alexandra Gherghina | a71e390 | 2014-07-25 20:03:47 +0100 | [diff] [blame] | 155 | return pm.loadItemIcon(this, getApplicationInfo()); |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 156 | } |
Alexandra Gherghina | a71e390 | 2014-07-25 20:03:47 +0100 | [diff] [blame] | 157 | |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 158 | /** |
Benjamin Franz | ec2d48b | 2014-10-01 15:38:43 +0100 | [diff] [blame] | 159 | * 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 Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 176 | * 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 Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 197 | * 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 Gherghina | a709314 | 2014-07-30 13:43:39 +0100 | [diff] [blame] | 207 | public Drawable loadDefaultIcon(PackageManager pm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | return pm.getDefaultActivityIcon(); |
| 209 | } |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 210 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | /** |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 227 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * 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 Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 279 | return pm.getXml(packageName, resid, getApplicationInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | return null; |
| 283 | } |
Amith Yamasani | 64442c1 | 2012-10-07 08:17:46 -0700 | [diff] [blame] | 284 | |
Dianne Hackborn | 6ac42ae | 2015-12-08 17:22:10 -0800 | [diff] [blame^] | 285 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | protected void dumpFront(Printer pw, String prefix) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 301 | if (name != null) { |
| 302 | pw.println(prefix + "name=" + name); |
| 303 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | pw.println(prefix + "packageName=" + packageName); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 305 | if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 306 | pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes) |
| 307 | + " nonLocalizedLabel=" + nonLocalizedLabel |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 308 | + " icon=0x" + Integer.toHexString(icon) |
| 309 | + " banner=0x" + Integer.toHexString(banner)); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 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 Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 323 | dest.writeInt(logo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | dest.writeBundle(metaData); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 325 | dest.writeInt(banner); |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 326 | dest.writeInt(showUserIcon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | 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 Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 336 | logo = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | metaData = source.readBundle(); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 338 | banner = source.readInt(); |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 339 | showUserIcon = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 342 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | 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 | } |