| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 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; |
| 23 |
|
| 24 |
import android.annotation.FloatRange; |
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.SystemApi; |
| 27 |
import android.app.ActivityThread; |
| 28 |
import android.content.res.XmlResourceParser; |
| 29 |
import android.graphics.drawable.Drawable; |
| 30 |
import android.os.Bundle; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.UserHandle; |
| 33 |
import android.text.TextUtils; |
| 34 |
import android.util.Printer; |
| 35 |
import android.util.proto.ProtoOutputStream; |
| 36 |
|
| 37 |
|
| 38 |
import java.text.Collator; |
| 39 |
import java.util.Comparator; |
| 40 |
import java.util.Objects; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class PackageItemInfo { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int MAX_SAFE_LABEL_LENGTH = 1000; |
| 62 |
|
| 63 |
|
| 64 |
public static final float DEFAULT_MAX_LABEL_SIZE_PX = 1000f; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@Deprecated |
| 77 |
@SystemApi |
| 78 |
public static final int SAFE_LABEL_FLAG_TRIM = SAFE_STRING_FLAG_TRIM; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
@Deprecated |
| 91 |
@SystemApi |
| 92 |
public static final int SAFE_LABEL_FLAG_SINGLE_LINE = SAFE_STRING_FLAG_SINGLE_LINE; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Deprecated |
| 105 |
@SystemApi |
| 106 |
public static final int SAFE_LABEL_FLAG_FIRST_LINE = SAFE_STRING_FLAG_FIRST_LINE; |
| 107 |
|
| 108 |
private static volatile boolean sForceSafeLabels = false; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@SystemApi |
| 116 |
public static void forceSafeLabels() { |
| 117 |
sForceSafeLabels = true; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public String name; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public String packageName; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int labelRes; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public CharSequence nonLocalizedLabel; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public int icon; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public int banner; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public int logo; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public Bundle metaData; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public int showUserIcon; |
| 175 |
|
| 176 |
public PackageItemInfo() { |
| 177 |
showUserIcon = UserHandle.USER_NULL; |
| 178 |
} |
| 179 |
|
| 180 |
public PackageItemInfo(PackageItemInfo orig) { |
| 181 |
name = orig.name; |
| 182 |
if (name != null) name = name.trim(); |
| 183 |
packageName = orig.packageName; |
| 184 |
labelRes = orig.labelRes; |
| 185 |
nonLocalizedLabel = orig.nonLocalizedLabel; |
| 186 |
if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim(); |
| 187 |
icon = orig.icon; |
| 188 |
banner = orig.banner; |
| 189 |
logo = orig.logo; |
| 190 |
metaData = orig.metaData; |
| 191 |
showUserIcon = orig.showUserIcon; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public @NonNull CharSequence loadLabel(@NonNull PackageManager pm) { |
| 206 |
if (sForceSafeLabels && !Objects.equals(packageName, ActivityThread.currentPackageName())) { |
| 207 |
return loadSafeLabel(pm, DEFAULT_MAX_LABEL_SIZE_PX, SAFE_STRING_FLAG_TRIM |
| 208 |
| SAFE_STRING_FLAG_FIRST_LINE); |
| 209 |
} else { |
| 210 |
|
| 211 |
|
| 212 |
return TextUtils.trimToSize(loadUnsafeLabel(pm), MAX_SAFE_LABEL_LENGTH); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public CharSequence loadUnsafeLabel(PackageManager pm) { |
| 218 |
if (nonLocalizedLabel != null) { |
| 219 |
return nonLocalizedLabel; |
| 220 |
} |
| 221 |
if (labelRes != 0) { |
| 222 |
CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo()); |
| 223 |
if (label != null) { |
| 224 |
return label.toString().trim(); |
| 225 |
} |
| 226 |
} |
| 227 |
if (name != null) { |
| 228 |
return name; |
| 229 |
} |
| 230 |
return packageName; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
@SystemApi |
| 238 |
@Deprecated |
| 239 |
public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm) { |
| 240 |
return loadSafeLabel(pm, DEFAULT_MAX_LABEL_SIZE_PX, SAFE_STRING_FLAG_TRIM |
| 241 |
| SAFE_STRING_FLAG_FIRST_LINE); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@SystemApi |
| 252 |
public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm, |
| 253 |
@FloatRange(from = 0) float ellipsizeDip, @TextUtils.SafeStringFlags int flags) { |
| 254 |
Objects.requireNonNull(pm); |
| 255 |
|
| 256 |
return makeSafeForPresentation(loadUnsafeLabel(pm).toString(), MAX_SAFE_LABEL_LENGTH, |
| 257 |
ellipsizeDip, flags); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public Drawable loadIcon(PackageManager pm) { |
| 273 |
return pm.loadItemIcon(this, getApplicationInfo()); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public Drawable loadUnbadgedIcon(PackageManager pm) { |
| 290 |
return pm.loadUnbadgedItemIcon(this, getApplicationInfo()); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public Drawable loadBanner(PackageManager pm) { |
| 305 |
if (banner != 0) { |
| 306 |
Drawable dr = pm.getDrawable(packageName, banner, getApplicationInfo()); |
| 307 |
if (dr != null) { |
| 308 |
return dr; |
| 309 |
} |
| 310 |
} |
| 311 |
return loadDefaultBanner(pm); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public Drawable loadDefaultIcon(PackageManager pm) { |
| 326 |
return pm.getDefaultActivityIcon(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
protected Drawable loadDefaultBanner(PackageManager pm) { |
| 341 |
return null; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public Drawable loadLogo(PackageManager pm) { |
| 356 |
if (logo != 0) { |
| 357 |
Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo()); |
| 358 |
if (d != null) { |
| 359 |
return d; |
| 360 |
} |
| 361 |
} |
| 362 |
return loadDefaultLogo(pm); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
protected Drawable loadDefaultLogo(PackageManager pm) { |
| 377 |
return null; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) { |
| 394 |
if (metaData != null) { |
| 395 |
int resid = metaData.getInt(name); |
| 396 |
if (resid != 0) { |
| 397 |
return pm.getXml(packageName, resid, getApplicationInfo()); |
| 398 |
} |
| 399 |
} |
| 400 |
return null; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public static final int DUMP_FLAG_DETAILS = 1<<0; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public static final int DUMP_FLAG_APPLICATION = 1<<1; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public static final int DUMP_FLAG_ALL = DUMP_FLAG_DETAILS | DUMP_FLAG_APPLICATION; |
| 417 |
|
| 418 |
protected void dumpFront(Printer pw, String prefix) { |
| 419 |
if (name != null) { |
| 420 |
pw.println(prefix + "name=" + name); |
| 421 |
} |
| 422 |
pw.println(prefix + "packageName=" + packageName); |
| 423 |
if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) { |
| 424 |
pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes) |
| 425 |
+ " nonLocalizedLabel=" + nonLocalizedLabel |
| 426 |
+ " icon=0x" + Integer.toHexString(icon) |
| 427 |
+ " banner=0x" + Integer.toHexString(banner)); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
protected void dumpBack(Printer pw, String prefix) { |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 436 |
dest.writeString8(name); |
| 437 |
dest.writeString8(packageName); |
| 438 |
dest.writeInt(labelRes); |
| 439 |
TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags); |
| 440 |
dest.writeInt(icon); |
| 441 |
dest.writeInt(logo); |
| 442 |
dest.writeBundle(metaData); |
| 443 |
dest.writeInt(banner); |
| 444 |
dest.writeInt(showUserIcon); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public void dumpDebug(ProtoOutputStream proto, long fieldId, int dumpFlags) { |
| 451 |
long token = proto.start(fieldId); |
| 452 |
if (name != null) { |
| 453 |
proto.write(PackageItemInfoProto.NAME, name); |
| 454 |
} |
| 455 |
proto.write(PackageItemInfoProto.PACKAGE_NAME, packageName); |
| 456 |
proto.write(PackageItemInfoProto.LABEL_RES, labelRes); |
| 457 |
if (nonLocalizedLabel != null) { |
| 458 |
proto.write(PackageItemInfoProto.NON_LOCALIZED_LABEL, nonLocalizedLabel.toString()); |
| 459 |
} |
| 460 |
proto.write(PackageItemInfoProto.ICON, icon); |
| 461 |
proto.write(PackageItemInfoProto.BANNER, banner); |
| 462 |
proto.end(token); |
| 463 |
} |
| 464 |
|
| 465 |
protected PackageItemInfo(Parcel source) { |
| 466 |
name = source.readString8(); |
| 467 |
packageName = source.readString8(); |
| 468 |
labelRes = source.readInt(); |
| 469 |
nonLocalizedLabel |
| 470 |
= TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 471 |
icon = source.readInt(); |
| 472 |
logo = source.readInt(); |
| 473 |
metaData = source.readBundle(); |
| 474 |
banner = source.readInt(); |
| 475 |
showUserIcon = source.readInt(); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
protected ApplicationInfo getApplicationInfo() { |
| 487 |
return null; |
| 488 |
} |
| 489 |
|
| 490 |
public static class DisplayNameComparator |
| 491 |
implements Comparator<PackageItemInfo> { |
| 492 |
public DisplayNameComparator(PackageManager pm) { |
| 493 |
mPM = pm; |
| 494 |
} |
| 495 |
|
| 496 |
public final int compare(PackageItemInfo aa, PackageItemInfo ab) { |
| 497 |
CharSequence sa = aa.loadLabel(mPM); |
| 498 |
if (sa == null) sa = aa.name; |
| 499 |
CharSequence sb = ab.loadLabel(mPM); |
| 500 |
if (sb == null) sb = ab.name; |
| 501 |
return sCollator.compare(sa.toString(), sb.toString()); |
| 502 |
} |
| 503 |
|
| 504 |
private final Collator sCollator = Collator.getInstance(); |
| 505 |
private PackageManager mPM; |
| 506 |
} |
| 507 |
} |