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