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 | |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SystemApi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.res.XmlResourceParser; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Parcel; |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 25 | import android.os.UserHandle; |
fionaxu | e630f35 | 2016-05-31 14:47:14 -0700 | [diff] [blame] | 26 | import android.text.Html; |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 27 | import android.text.TextPaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.text.TextUtils; |
| 29 | import android.util.Printer; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 30 | import android.util.proto.ProtoOutputStream; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import java.text.Collator; |
| 33 | import java.util.Comparator; |
| 34 | |
| 35 | /** |
| 36 | * Base class containing information common to all package items held by |
| 37 | * the package manager. This provides a very common basic set of attributes: |
| 38 | * a label, icon, and meta-data. This class is not intended |
| 39 | * to be used by itself; it is simply here to share common definitions |
| 40 | * between all items returned by the package manager. As such, it does not |
| 41 | * itself implement Parcelable, but does provide convenience methods to assist |
| 42 | * in the implementation of Parcelable in subclasses. |
| 43 | */ |
| 44 | public class PackageItemInfo { |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 45 | private static final float MAX_LABEL_SIZE_PX = 500f; |
Jeff Sharkey | b0613dc | 2018-02-27 14:38:04 -0700 | [diff] [blame] | 46 | |
| 47 | private static volatile boolean sForceSafeLabels = false; |
| 48 | |
| 49 | /** {@hide} */ |
| 50 | public static void setForceSafeLabels(boolean forceSafeLabels) { |
| 51 | sForceSafeLabels = forceSafeLabels; |
| 52 | } |
| 53 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Public name of this item. From the "android:name" attribute. |
| 56 | */ |
| 57 | public String name; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Name of the package that this item is in. |
| 61 | */ |
| 62 | public String packageName; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
| 65 | * A string resource identifier (in the package's resources) of this |
| 66 | * component's label. From the "label" attribute or, if not set, 0. |
| 67 | */ |
| 68 | public int labelRes; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /** |
| 71 | * The string provided in the AndroidManifest file, if any. You |
| 72 | * probably don't want to use this. You probably want |
| 73 | * {@link PackageManager#getApplicationLabel} |
| 74 | */ |
| 75 | public CharSequence nonLocalizedLabel; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | /** |
| 78 | * A drawable resource identifier (in the package's resources) of this |
| 79 | * component's icon. From the "icon" attribute or, if not set, 0. |
| 80 | */ |
| 81 | public int icon; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 84 | * A drawable resource identifier (in the package's resources) of this |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 85 | * component's banner. From the "banner" attribute or, if not set, 0. |
| 86 | */ |
| 87 | public int banner; |
| 88 | |
| 89 | /** |
| 90 | * A drawable resource identifier (in the package's resources) of this |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 91 | * component's logo. Logos may be larger/wider than icons and are |
| 92 | * displayed by certain UI elements in place of a name or name/icon |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 93 | * combination. From the "logo" attribute or, if not set, 0. |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 94 | */ |
| 95 | public int logo; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 96 | |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 97 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * Additional meta-data associated with this component. This field |
| 99 | * will only be filled in if you set the |
| 100 | * {@link PackageManager#GET_META_DATA} flag when requesting the info. |
| 101 | */ |
| 102 | public Bundle metaData; |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * If different of UserHandle.USER_NULL, The icon of this item will be the one of that user. |
| 106 | * @hide |
| 107 | */ |
| 108 | public int showUserIcon; |
| 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | public PackageItemInfo() { |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 111 | showUserIcon = UserHandle.USER_NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | public PackageItemInfo(PackageItemInfo orig) { |
| 115 | name = orig.name; |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 116 | if (name != null) name = name.trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | packageName = orig.packageName; |
| 118 | labelRes = orig.labelRes; |
| 119 | nonLocalizedLabel = orig.nonLocalizedLabel; |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 120 | if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | icon = orig.icon; |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 122 | banner = orig.banner; |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 123 | logo = orig.logo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | metaData = orig.metaData; |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 125 | showUserIcon = orig.showUserIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieve the current textual label associated with this item. This |
| 130 | * will call back on the given PackageManager to load the label from |
| 131 | * the application. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 132 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | * @param pm A PackageManager from which the label can be loaded; usually |
| 134 | * the PackageManager from which you originally retrieved this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 135 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | * @return Returns a CharSequence containing the item's label. If the |
| 137 | * item does not have a label, its name is returned. |
| 138 | */ |
Jeff Sharkey | b0613dc | 2018-02-27 14:38:04 -0700 | [diff] [blame] | 139 | public @NonNull CharSequence loadLabel(@NonNull PackageManager pm) { |
| 140 | if (sForceSafeLabels) { |
| 141 | return loadSafeLabel(pm); |
| 142 | } else { |
| 143 | return loadUnsafeLabel(pm); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** {@hide} */ |
| 148 | public CharSequence loadUnsafeLabel(PackageManager pm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | if (nonLocalizedLabel != null) { |
| 150 | return nonLocalizedLabel; |
| 151 | } |
| 152 | if (labelRes != 0) { |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 153 | CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | if (label != null) { |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 155 | return label.toString().trim(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
Romain Guy | 2aba11f | 2010-03-29 16:03:01 -0700 | [diff] [blame] | 158 | if (name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | return name; |
| 160 | } |
| 161 | return packageName; |
| 162 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | /** |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 165 | * Same as {@link #loadLabel(PackageManager)} with the addition that |
| 166 | * the returned label is safe for being presented in the UI since it |
| 167 | * will not contain new lines and the length will be limited to a |
| 168 | * reasonable amount. This prevents a malicious party to influence UI |
| 169 | * layout via the app label misleading the user into performing a |
| 170 | * detrimental for them action. If the label is too long it will be |
| 171 | * truncated and ellipsized at the end. |
| 172 | * |
| 173 | * @param pm A PackageManager from which the label can be loaded; usually |
| 174 | * the PackageManager from which you originally retrieved this item |
| 175 | * @return Returns a CharSequence containing the item's label. If the |
| 176 | * item does not have a label, its name is returned. |
| 177 | * |
| 178 | * @hide |
| 179 | */ |
| 180 | @SystemApi |
| 181 | public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm) { |
| 182 | // loadLabel() always returns non-null |
Jeff Sharkey | b0613dc | 2018-02-27 14:38:04 -0700 | [diff] [blame] | 183 | String label = loadUnsafeLabel(pm).toString(); |
fionaxu | e630f35 | 2016-05-31 14:47:14 -0700 | [diff] [blame] | 184 | // strip HTML tags to avoid <br> and other tags overwriting original message |
| 185 | String labelStr = Html.fromHtml(label).toString(); |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 186 | |
| 187 | // If the label contains new line characters it may push the UI |
| 188 | // down to hide a part of it. Labels shouldn't have new line |
| 189 | // characters, so just truncate at the first time one is seen. |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 190 | final int labelLength = labelStr.length(); |
| 191 | int offset = 0; |
| 192 | while (offset < labelLength) { |
| 193 | final int codePoint = labelStr.codePointAt(offset); |
| 194 | final int type = Character.getType(codePoint); |
| 195 | if (type == Character.LINE_SEPARATOR |
| 196 | || type == Character.CONTROL |
| 197 | || type == Character.PARAGRAPH_SEPARATOR) { |
| 198 | labelStr = labelStr.substring(0, offset); |
| 199 | break; |
| 200 | } |
fionaxu | e630f35 | 2016-05-31 14:47:14 -0700 | [diff] [blame] | 201 | // replace all non-break space to " " in order to be trimmed |
| 202 | if (type == Character.SPACE_SEPARATOR) { |
| 203 | labelStr = labelStr.substring(0, offset) + " " + labelStr.substring(offset + |
| 204 | Character.charCount(codePoint)); |
| 205 | } |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 206 | offset += Character.charCount(codePoint); |
| 207 | } |
| 208 | |
fionaxu | e630f35 | 2016-05-31 14:47:14 -0700 | [diff] [blame] | 209 | labelStr = labelStr.trim(); |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 210 | if (labelStr.isEmpty()) { |
fionaxu | e630f35 | 2016-05-31 14:47:14 -0700 | [diff] [blame] | 211 | return packageName; |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 212 | } |
Svetoslav Ganov | aa8b0d7 | 2016-03-01 14:12:43 -0800 | [diff] [blame] | 213 | TextPaint paint = new TextPaint(); |
| 214 | paint.setTextSize(42); |
| 215 | |
| 216 | return TextUtils.ellipsize(labelStr, paint, MAX_LABEL_SIZE_PX, |
| 217 | TextUtils.TruncateAt.END); |
| 218 | } |
| 219 | |
| 220 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | * Retrieve the current graphical icon associated with this item. This |
| 222 | * will call back on the given PackageManager to load the icon from |
| 223 | * the application. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 224 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | * @param pm A PackageManager from which the icon can be loaded; usually |
| 226 | * the PackageManager from which you originally retrieved this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 227 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | * @return Returns a Drawable containing the item's icon. If the |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 229 | * item does not have an icon, the item's default icon is returned |
| 230 | * such as the default activity icon. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | */ |
| 232 | public Drawable loadIcon(PackageManager pm) { |
Alexandra Gherghina | a71e390 | 2014-07-25 20:03:47 +0100 | [diff] [blame] | 233 | return pm.loadItemIcon(this, getApplicationInfo()); |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 234 | } |
Alexandra Gherghina | a71e390 | 2014-07-25 20:03:47 +0100 | [diff] [blame] | 235 | |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 236 | /** |
Benjamin Franz | ec2d48b | 2014-10-01 15:38:43 +0100 | [diff] [blame] | 237 | * Retrieve the current graphical icon associated with this item without |
| 238 | * the addition of a work badge if applicable. |
| 239 | * This will call back on the given PackageManager to load the icon from |
| 240 | * the application. |
| 241 | * |
| 242 | * @param pm A PackageManager from which the icon can be loaded; usually |
| 243 | * the PackageManager from which you originally retrieved this item. |
| 244 | * |
| 245 | * @return Returns a Drawable containing the item's icon. If the |
| 246 | * item does not have an icon, the item's default icon is returned |
| 247 | * such as the default activity icon. |
| 248 | */ |
| 249 | public Drawable loadUnbadgedIcon(PackageManager pm) { |
| 250 | return pm.loadUnbadgedItemIcon(this, getApplicationInfo()); |
| 251 | } |
| 252 | |
| 253 | /** |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 254 | * Retrieve the current graphical banner associated with this item. This |
| 255 | * will call back on the given PackageManager to load the banner from |
| 256 | * the application. |
| 257 | * |
| 258 | * @param pm A PackageManager from which the banner can be loaded; usually |
| 259 | * the PackageManager from which you originally retrieved this item. |
| 260 | * |
| 261 | * @return Returns a Drawable containing the item's banner. If the item |
| 262 | * does not have a banner, this method will return null. |
| 263 | */ |
| 264 | public Drawable loadBanner(PackageManager pm) { |
| 265 | if (banner != 0) { |
| 266 | Drawable dr = pm.getDrawable(packageName, banner, getApplicationInfo()); |
| 267 | if (dr != null) { |
| 268 | return dr; |
| 269 | } |
| 270 | } |
| 271 | return loadDefaultBanner(pm); |
| 272 | } |
| 273 | |
| 274 | /** |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 275 | * Retrieve the default graphical icon associated with this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 276 | * |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 277 | * @param pm A PackageManager from which the icon can be loaded; usually |
| 278 | * the PackageManager from which you originally retrieved this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 279 | * |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 280 | * @return Returns a Drawable containing the item's default icon |
| 281 | * such as the default activity icon. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 282 | * |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 283 | * @hide |
| 284 | */ |
Alexandra Gherghina | a709314 | 2014-07-30 13:43:39 +0100 | [diff] [blame] | 285 | public Drawable loadDefaultIcon(PackageManager pm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | return pm.getDefaultActivityIcon(); |
| 287 | } |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Retrieve the default graphical banner associated with this item. |
| 291 | * |
| 292 | * @param pm A PackageManager from which the banner can be loaded; usually |
| 293 | * the PackageManager from which you originally retrieved this item. |
| 294 | * |
| 295 | * @return Returns a Drawable containing the item's default banner |
| 296 | * or null if no default logo is available. |
| 297 | * |
| 298 | * @hide |
| 299 | */ |
| 300 | protected Drawable loadDefaultBanner(PackageManager pm) { |
| 301 | return null; |
| 302 | } |
| 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | /** |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 305 | * Retrieve the current graphical logo associated with this item. This |
| 306 | * will call back on the given PackageManager to load the logo from |
| 307 | * the application. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 308 | * |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 309 | * @param pm A PackageManager from which the logo can be loaded; usually |
| 310 | * the PackageManager from which you originally retrieved this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 311 | * |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 312 | * @return Returns a Drawable containing the item's logo. If the item |
| 313 | * does not have a logo, this method will return null. |
| 314 | */ |
| 315 | public Drawable loadLogo(PackageManager pm) { |
| 316 | if (logo != 0) { |
| 317 | Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo()); |
| 318 | if (d != null) { |
| 319 | return d; |
| 320 | } |
| 321 | } |
| 322 | return loadDefaultLogo(pm); |
| 323 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 324 | |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Retrieve the default graphical logo associated with this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 327 | * |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 328 | * @param pm A PackageManager from which the logo can be loaded; usually |
| 329 | * the PackageManager from which you originally retrieved this item. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 330 | * |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 331 | * @return Returns a Drawable containing the item's default logo |
| 332 | * or null if no default logo is available. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 333 | * |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 334 | * @hide |
| 335 | */ |
| 336 | protected Drawable loadDefaultLogo(PackageManager pm) { |
| 337 | return null; |
| 338 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 339 | |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 340 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | * Load an XML resource attached to the meta-data of this item. This will |
| 342 | * retrieved the name meta-data entry, and if defined call back on the |
| 343 | * given PackageManager to load its XML file from the application. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 344 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * @param pm A PackageManager from which the XML can be loaded; usually |
| 346 | * the PackageManager from which you originally retrieved this item. |
| 347 | * @param name Name of the meta-date you would like to load. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 348 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * @return Returns an XmlPullParser you can use to parse the XML file |
| 350 | * assigned as the given meta-data. If the meta-data name is not defined |
| 351 | * or the XML resource could not be found, null is returned. |
| 352 | */ |
| 353 | public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) { |
| 354 | if (metaData != null) { |
| 355 | int resid = metaData.getInt(name); |
| 356 | if (resid != 0) { |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 357 | return pm.getXml(packageName, resid, getApplicationInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | return null; |
| 361 | } |
Amith Yamasani | 64442c1 | 2012-10-07 08:17:46 -0700 | [diff] [blame] | 362 | |
Dianne Hackborn | 6ac42ae | 2015-12-08 17:22:10 -0800 | [diff] [blame] | 363 | /** |
| 364 | * @hide Flag for dumping: include all details. |
| 365 | */ |
| 366 | public static final int DUMP_FLAG_DETAILS = 1<<0; |
| 367 | |
| 368 | /** |
| 369 | * @hide Flag for dumping: include nested ApplicationInfo. |
| 370 | */ |
| 371 | public static final int DUMP_FLAG_APPLICATION = 1<<1; |
| 372 | |
| 373 | /** |
| 374 | * @hide Flag for dumping: all flags to dump everything. |
| 375 | */ |
| 376 | public static final int DUMP_FLAG_ALL = DUMP_FLAG_DETAILS | DUMP_FLAG_APPLICATION; |
| 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | protected void dumpFront(Printer pw, String prefix) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 379 | if (name != null) { |
| 380 | pw.println(prefix + "name=" + name); |
| 381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | pw.println(prefix + "packageName=" + packageName); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 383 | if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 384 | pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes) |
| 385 | + " nonLocalizedLabel=" + nonLocalizedLabel |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 386 | + " icon=0x" + Integer.toHexString(icon) |
| 387 | + " banner=0x" + Integer.toHexString(banner)); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | protected void dumpBack(Printer pw, String prefix) { |
| 392 | // no back here |
| 393 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 396 | dest.writeString(name); |
| 397 | dest.writeString(packageName); |
| 398 | dest.writeInt(labelRes); |
| 399 | TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags); |
| 400 | dest.writeInt(icon); |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 401 | dest.writeInt(logo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | dest.writeBundle(metaData); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 403 | dest.writeInt(banner); |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 404 | dest.writeInt(showUserIcon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 406 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 407 | /** |
| 408 | * @hide |
| 409 | */ |
| 410 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 411 | long token = proto.start(fieldId); |
| 412 | if (name != null) { |
| 413 | proto.write(PackageItemInfoProto.NAME, name); |
| 414 | } |
| 415 | proto.write(PackageItemInfoProto.PACKAGE_NAME, packageName); |
| 416 | if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) { |
| 417 | proto.write(PackageItemInfoProto.LABEL_RES, labelRes); |
| 418 | proto.write(PackageItemInfoProto.NON_LOCALIZED_LABEL, nonLocalizedLabel.toString()); |
| 419 | proto.write(PackageItemInfoProto.ICON, icon); |
| 420 | proto.write(PackageItemInfoProto.BANNER, banner); |
| 421 | } |
| 422 | proto.end(token); |
| 423 | } |
| 424 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | protected PackageItemInfo(Parcel source) { |
| 426 | name = source.readString(); |
| 427 | packageName = source.readString(); |
| 428 | labelRes = source.readInt(); |
| 429 | nonLocalizedLabel |
| 430 | = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 431 | icon = source.readInt(); |
Adam Powell | 81cd2e9 | 2010-04-21 16:35:18 -0700 | [diff] [blame] | 432 | logo = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | metaData = source.readBundle(); |
Jose Lima | f78e312 | 2014-03-06 12:13:15 -0800 | [diff] [blame] | 434 | banner = source.readInt(); |
Nicolas Prevot | 7f7b0c7 | 2014-06-23 15:59:38 +0100 | [diff] [blame] | 435 | showUserIcon = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 438 | /** |
| 439 | * Get the ApplicationInfo for the application to which this item belongs, |
| 440 | * if available, otherwise returns null. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 441 | * |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 442 | * @return Returns the ApplicationInfo of this item, or null if not known. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 443 | * |
Jeff Brown | 0733079 | 2010-03-30 19:57:08 -0700 | [diff] [blame] | 444 | * @hide |
| 445 | */ |
| 446 | protected ApplicationInfo getApplicationInfo() { |
| 447 | return null; |
| 448 | } |
| 449 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | public static class DisplayNameComparator |
| 451 | implements Comparator<PackageItemInfo> { |
| 452 | public DisplayNameComparator(PackageManager pm) { |
| 453 | mPM = pm; |
| 454 | } |
| 455 | |
| 456 | public final int compare(PackageItemInfo aa, PackageItemInfo ab) { |
| 457 | CharSequence sa = aa.loadLabel(mPM); |
| 458 | if (sa == null) sa = aa.name; |
| 459 | CharSequence sb = ab.loadLabel(mPM); |
| 460 | if (sb == null) sb = ab.name; |
| 461 | return sCollator.compare(sa.toString(), sb.toString()); |
| 462 | } |
| 463 | |
| 464 | private final Collator sCollator = Collator.getInstance(); |
| 465 | private PackageManager mPM; |
| 466 | } |
| 467 | } |