| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.print; |
| 18 |
|
| 19 |
import android.annotation.DrawableRes; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.app.PendingIntent; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.pm.ApplicationInfo; |
| 27 |
import android.content.pm.PackageInfo; |
| 28 |
import android.content.pm.PackageManager; |
| 29 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 30 |
import android.graphics.drawable.Drawable; |
| 31 |
import android.graphics.drawable.Icon; |
| 32 |
import android.os.Parcel; |
| 33 |
import android.os.Parcelable; |
| 34 |
import android.service.print.PrinterInfoProto; |
| 35 |
import android.text.TextUtils; |
| 36 |
|
| 37 |
import com.android.internal.util.Preconditions; |
| 38 |
|
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public final class PrinterInfo implements Parcelable { |
| 54 |
|
| 55 |
|
| 56 |
@IntDef(prefix = { "STATUS_" }, value = { |
| 57 |
STATUS_IDLE, |
| 58 |
STATUS_BUSY, |
| 59 |
STATUS_UNAVAILABLE |
| 60 |
}) |
| 61 |
@Retention(RetentionPolicy.SOURCE) |
| 62 |
public @interface Status { |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public static final int STATUS_IDLE = PrinterInfoProto.STATUS_IDLE; |
| 67 |
|
| 68 |
|
| 69 |
public static final int STATUS_BUSY = PrinterInfoProto.STATUS_BUSY; |
| 70 |
|
| 71 |
|
| 72 |
public static final int STATUS_UNAVAILABLE = PrinterInfoProto.STATUS_UNAVAILABLE; |
| 73 |
|
| 74 |
private final @NonNull PrinterId mId; |
| 75 |
|
| 76 |
|
| 77 |
private final int mIconResourceId; |
| 78 |
|
| 79 |
|
| 80 |
private final boolean mHasCustomPrinterIcon; |
| 81 |
|
| 82 |
|
| 83 |
private final int mCustomPrinterIconGen; |
| 84 |
|
| 85 |
|
| 86 |
private final @Nullable PendingIntent mInfoIntent; |
| 87 |
|
| 88 |
private final @NonNull String mName; |
| 89 |
|
| 90 |
private final @Status int mStatus; |
| 91 |
|
| 92 |
private final @Nullable String mDescription; |
| 93 |
|
| 94 |
private final @Nullable PrinterCapabilitiesInfo mCapabilities; |
| 95 |
|
| 96 |
private PrinterInfo(@NonNull PrinterId printerId, @NonNull String name, @Status int status, |
| 97 |
int iconResourceId, boolean hasCustomPrinterIcon, String description, |
| 98 |
PendingIntent infoIntent, PrinterCapabilitiesInfo capabilities, |
| 99 |
int customPrinterIconGen) { |
| 100 |
mId = printerId; |
| 101 |
mName = name; |
| 102 |
mStatus = status; |
| 103 |
mIconResourceId = iconResourceId; |
| 104 |
mHasCustomPrinterIcon = hasCustomPrinterIcon; |
| 105 |
mDescription = description; |
| 106 |
mInfoIntent = infoIntent; |
| 107 |
mCapabilities = capabilities; |
| 108 |
mCustomPrinterIconGen = customPrinterIconGen; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public @NonNull PrinterId getId() { |
| 117 |
return mId; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@TestApi |
| 131 |
public @Nullable Drawable loadIcon(@NonNull Context context) { |
| 132 |
Drawable drawable = null; |
| 133 |
PackageManager packageManager = context.getPackageManager(); |
| 134 |
|
| 135 |
if (mHasCustomPrinterIcon) { |
| 136 |
PrintManager printManager = (PrintManager) context |
| 137 |
.getSystemService(Context.PRINT_SERVICE); |
| 138 |
|
| 139 |
Icon icon = printManager.getCustomPrinterIcon(mId); |
| 140 |
|
| 141 |
if (icon != null) { |
| 142 |
drawable = icon.loadDrawable(context); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (drawable == null) { |
| 147 |
try { |
| 148 |
String packageName = mId.getServiceName().getPackageName(); |
| 149 |
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0); |
| 150 |
ApplicationInfo appInfo = packageInfo.applicationInfo; |
| 151 |
|
| 152 |
|
| 153 |
if (mIconResourceId != 0) { |
| 154 |
drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (drawable == null) { |
| 159 |
drawable = appInfo.loadIcon(packageManager); |
| 160 |
} |
| 161 |
} catch (NameNotFoundException e) { |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return drawable; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public boolean getHasCustomPrinterIcon() { |
| 176 |
return mHasCustomPrinterIcon; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public @NonNull String getName() { |
| 185 |
return mName; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public @Status int getStatus() { |
| 198 |
return mStatus; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public @Nullable String getDescription() { |
| 207 |
return mDescription; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public @Nullable PendingIntent getInfoIntent() { |
| 219 |
return mInfoIntent; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public @Nullable PrinterCapabilitiesInfo getCapabilities() { |
| 228 |
return mCapabilities; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private static @NonNull PrinterId checkPrinterId(PrinterId printerId) { |
| 239 |
return Preconditions.checkNotNull(printerId, "printerId cannot be null."); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
private static @Status int checkStatus(int status) { |
| 250 |
if (!(status == STATUS_IDLE |
| 251 |
|| status == STATUS_BUSY |
| 252 |
|| status == STATUS_UNAVAILABLE)) { |
| 253 |
throw new IllegalArgumentException("status is invalid."); |
| 254 |
} |
| 255 |
|
| 256 |
return status; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private static @NonNull String checkName(String name) { |
| 267 |
return Preconditions.checkStringNotEmpty(name, "name cannot be empty."); |
| 268 |
} |
| 269 |
|
| 270 |
private PrinterInfo(Parcel parcel) { |
| 271 |
|
| 272 |
|
| 273 |
mId = checkPrinterId((PrinterId) parcel.readParcelable(null, android.print.PrinterId.class)); |
| 274 |
mName = checkName(parcel.readString()); |
| 275 |
mStatus = checkStatus(parcel.readInt()); |
| 276 |
mDescription = parcel.readString(); |
| 277 |
mCapabilities = parcel.readParcelable(null, android.print.PrinterCapabilitiesInfo.class); |
| 278 |
mIconResourceId = parcel.readInt(); |
| 279 |
mHasCustomPrinterIcon = parcel.readByte() != 0; |
| 280 |
mCustomPrinterIconGen = parcel.readInt(); |
| 281 |
mInfoIntent = parcel.readParcelable(null, android.app.PendingIntent.class); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public int describeContents() { |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
@Override |
| 290 |
public void writeToParcel(Parcel parcel, int flags) { |
| 291 |
parcel.writeParcelable(mId, flags); |
| 292 |
parcel.writeString(mName); |
| 293 |
parcel.writeInt(mStatus); |
| 294 |
parcel.writeString(mDescription); |
| 295 |
parcel.writeParcelable(mCapabilities, flags); |
| 296 |
parcel.writeInt(mIconResourceId); |
| 297 |
parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0)); |
| 298 |
parcel.writeInt(mCustomPrinterIconGen); |
| 299 |
parcel.writeParcelable(mInfoIntent, flags); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public int hashCode() { |
| 304 |
final int prime = 31; |
| 305 |
int result = 1; |
| 306 |
result = prime * result + mId.hashCode(); |
| 307 |
result = prime * result + mName.hashCode(); |
| 308 |
result = prime * result + mStatus; |
| 309 |
result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0); |
| 310 |
result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0); |
| 311 |
result = prime * result + mIconResourceId; |
| 312 |
result = prime * result + (mHasCustomPrinterIcon ? 1 : 0); |
| 313 |
result = prime * result + mCustomPrinterIconGen; |
| 314 |
result = prime * result + ((mInfoIntent != null) ? mInfoIntent.hashCode() : 0); |
| 315 |
return result; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public boolean equalsIgnoringStatus(PrinterInfo other) { |
| 327 |
if (!mId.equals(other.mId)) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
if (!mName.equals(other.mName)) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
if (!TextUtils.equals(mDescription, other.mDescription)) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
if (mCapabilities == null) { |
| 337 |
if (other.mCapabilities != null) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
} else if (!mCapabilities.equals(other.mCapabilities)) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
if (mIconResourceId != other.mIconResourceId) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
if (mHasCustomPrinterIcon != other.mHasCustomPrinterIcon) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
if (mCustomPrinterIconGen != other.mCustomPrinterIconGen) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
if (mInfoIntent == null) { |
| 353 |
if (other.mInfoIntent != null) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
} else if (!mInfoIntent.equals(other.mInfoIntent)) { |
| 357 |
return false; |
| 358 |
} |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
@Override |
| 363 |
public boolean equals(@Nullable Object obj) { |
| 364 |
if (this == obj) { |
| 365 |
return true; |
| 366 |
} |
| 367 |
if (obj == null) { |
| 368 |
return false; |
| 369 |
} |
| 370 |
if (getClass() != obj.getClass()) { |
| 371 |
return false; |
| 372 |
} |
| 373 |
PrinterInfo other = (PrinterInfo) obj; |
| 374 |
if (!equalsIgnoringStatus(other)) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
if (mStatus != other.mStatus) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
return true; |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public String toString() { |
| 385 |
StringBuilder builder = new StringBuilder(); |
| 386 |
builder.append("PrinterInfo{"); |
| 387 |
builder.append("id=").append(mId); |
| 388 |
builder.append(", name=").append(mName); |
| 389 |
builder.append(", status=").append(mStatus); |
| 390 |
builder.append(", description=").append(mDescription); |
| 391 |
builder.append(", capabilities=").append(mCapabilities); |
| 392 |
builder.append(", iconResId=").append(mIconResourceId); |
| 393 |
builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon); |
| 394 |
builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen); |
| 395 |
builder.append(", infoIntent=").append(mInfoIntent); |
| 396 |
builder.append("\"}"); |
| 397 |
return builder.toString(); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public static final class Builder { |
| 404 |
private @NonNull PrinterId mPrinterId; |
| 405 |
private @NonNull String mName; |
| 406 |
private @Status int mStatus; |
| 407 |
private int mIconResourceId; |
| 408 |
private boolean mHasCustomPrinterIcon; |
| 409 |
private String mDescription; |
| 410 |
private PendingIntent mInfoIntent; |
| 411 |
private PrinterCapabilitiesInfo mCapabilities; |
| 412 |
private int mCustomPrinterIconGen; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) { |
| 424 |
mPrinterId = checkPrinterId(printerId); |
| 425 |
mName = checkName(name); |
| 426 |
mStatus = checkStatus(status); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public Builder(@NonNull PrinterInfo other) { |
| 435 |
mPrinterId = other.mId; |
| 436 |
mName = other.mName; |
| 437 |
mStatus = other.mStatus; |
| 438 |
mIconResourceId = other.mIconResourceId; |
| 439 |
mHasCustomPrinterIcon = other.mHasCustomPrinterIcon; |
| 440 |
mDescription = other.mDescription; |
| 441 |
mInfoIntent = other.mInfoIntent; |
| 442 |
mCapabilities = other.mCapabilities; |
| 443 |
mCustomPrinterIconGen = other.mCustomPrinterIconGen; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public @NonNull Builder setStatus(@Status int status) { |
| 456 |
mStatus = checkStatus(status); |
| 457 |
return this; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) { |
| 469 |
mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId, |
| 470 |
"iconResourceId can't be negative"); |
| 471 |
return this; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) { |
| 490 |
mHasCustomPrinterIcon = hasCustomPrinterIcon; |
| 491 |
return this; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public @NonNull Builder setName(@NonNull String name) { |
| 502 |
mName = checkName(name); |
| 503 |
return this; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public @NonNull Builder setDescription(@NonNull String description) { |
| 514 |
mDescription = description; |
| 515 |
return this; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public @NonNull Builder setInfoIntent(@NonNull PendingIntent infoIntent) { |
| 526 |
mInfoIntent = infoIntent; |
| 527 |
return this; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) { |
| 537 |
mCapabilities = capabilities; |
| 538 |
return this; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public @NonNull PrinterInfo build() { |
| 547 |
return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId, |
| 548 |
mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities, |
| 549 |
mCustomPrinterIconGen); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public @NonNull Builder incCustomPrinterIconGen() { |
| 561 |
mCustomPrinterIconGen++; |
| 562 |
return this; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
public static final @android.annotation.NonNull Parcelable.Creator<PrinterInfo> CREATOR = |
| 567 |
new Parcelable.Creator<PrinterInfo>() { |
| 568 |
@Override |
| 569 |
public PrinterInfo createFromParcel(Parcel parcel) { |
| 570 |
return new PrinterInfo(parcel); |
| 571 |
} |
| 572 |
|
| 573 |
@Override |
| 574 |
public PrinterInfo[] newArray(int size) { |
| 575 |
return new PrinterInfo[size]; |
| 576 |
} |
| 577 |
}; |
| 578 |
} |