PrintJobInfo.java revision 704697b6197262678e930daa831a1916ddee4dcf
1/* 2 * Copyright (C) 2013 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 17package android.print; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21 22import java.util.Arrays; 23 24/** 25 * This class represents the description of a print job. 26 */ 27public final class PrintJobInfo implements Parcelable { 28 29 /** Undefined print job id. */ 30 public static final int PRINT_JOB_ID_UNDEFINED = -1; 31 32 /** 33 * Constant for matching any print job state. 34 * 35 * @hide 36 */ 37 public static final int STATE_ANY = -1; 38 39 /** 40 * Constant for matching any print job state. 41 * 42 * @hide 43 */ 44 public static final int STATE_ANY_VISIBLE_TO_CLIENTS = -2; 45 46 /** 47 * Constant for matching any active print job state. 48 * 49 * @hide 50 */ 51 public static final int STATE_ANY_ACTIVE = -3; 52 53 /** 54 * Print job state: The print job is being created but not yet 55 * ready to be printed. 56 * <p> 57 * Next valid states: {@link #STATE_QUEUED} 58 * </p> 59 */ 60 public static final int STATE_CREATED = 1; 61 62 /** 63 * Print job state: The print jobs is created, it is ready 64 * to be printed and should be processed. 65 * <p> 66 * Next valid states: {@link #STATE_STARTED}, {@link #STATE_FAILED}, 67 * {@link #STATE_CANCELED} 68 * </p> 69 */ 70 public static final int STATE_QUEUED = 2; 71 72 /** 73 * Print job state: The print job is being printed. 74 * <p> 75 * Next valid states: {@link #STATE_COMPLETED}, {@link #STATE_FAILED}, 76 * {@link #STATE_CANCELED}, {@link #STATE_BLOCKED} 77 * </p> 78 */ 79 public static final int STATE_STARTED = 3; 80 81 /** 82 * Print job state: The print job is blocked. 83 * <p> 84 * Next valid states: {@link #STATE_FAILED}, {@link #STATE_CANCELED}, 85 * {@link #STATE_STARTED} 86 * </p> 87 */ 88 public static final int STATE_BLOCKED = 4; 89 90 /** 91 * Print job state: The print job was successfully printed. 92 * This is a terminal state. 93 * <p> 94 * Next valid states: None 95 * </p> 96 */ 97 public static final int STATE_COMPLETED = 5; 98 99 /** 100 * Print job state: The print job was printing but printing failed. 101 * This is a terminal state. 102 * <p> 103 * Next valid states: None 104 * </p> 105 */ 106 public static final int STATE_FAILED = 6; 107 108 /** 109 * Print job state: The print job was canceled. 110 * This is a terminal state. 111 * <p> 112 * Next valid states: None 113 * </p> 114 */ 115 public static final int STATE_CANCELED = 7; 116 117 /** The unique print job id. */ 118 private PrintJobId mId; 119 120 /** The human readable print job label. */ 121 private String mLabel; 122 123 /** The unique id of the printer. */ 124 private PrinterId mPrinterId; 125 126 /** The name of the printer - internally used */ 127 private String mPrinterName; 128 129 /** The status of the print job. */ 130 private int mState; 131 132 /** The id of the app that created the job. */ 133 private int mAppId; 134 135 /** The id of the user that created the job. */ 136 private int mUserId; 137 138 /** Optional tag assigned by a print service.*/ 139 private String mTag; 140 141 /** The wall time when the print job was created. */ 142 private long mCreationTime; 143 144 /** How many copies to print. */ 145 private int mCopies; 146 147 /** Reason for the print job being in its current state. */ 148 private String mStateReason; 149 150 /** The pages to print */ 151 private PageRange[] mPageRanges; 152 153 /** The print job attributes size. */ 154 private PrintAttributes mAttributes; 155 156 /** Information about the printed document. */ 157 private PrintDocumentInfo mDocumentInfo; 158 159 /** @hide*/ 160 public PrintJobInfo() { 161 /* do nothing */ 162 } 163 164 /** @hide */ 165 public PrintJobInfo(PrintJobInfo other) { 166 mId = other.mId; 167 mLabel = other.mLabel; 168 mPrinterId = other.mPrinterId; 169 mPrinterName = other.mPrinterName; 170 mState = other.mState; 171 mAppId = other.mAppId; 172 mUserId = other.mUserId; 173 mTag = other.mTag; 174 mCreationTime = other.mCreationTime; 175 mCopies = other.mCopies; 176 mStateReason = other.mStateReason; 177 mPageRanges = other.mPageRanges; 178 mAttributes = other.mAttributes; 179 mDocumentInfo = other.mDocumentInfo; 180 } 181 182 private PrintJobInfo(Parcel parcel) { 183 mId = parcel.readParcelable(null); 184 mLabel = parcel.readString(); 185 mPrinterId = parcel.readParcelable(null); 186 mPrinterName = parcel.readString(); 187 mState = parcel.readInt(); 188 mAppId = parcel.readInt(); 189 mUserId = parcel.readInt(); 190 mTag = parcel.readString(); 191 mCreationTime = parcel.readLong(); 192 mCopies = parcel.readInt(); 193 mStateReason = parcel.readString(); 194 if (parcel.readInt() == 1) { 195 Parcelable[] parcelables = parcel.readParcelableArray(null); 196 mPageRanges = new PageRange[parcelables.length]; 197 for (int i = 0; i < parcelables.length; i++) { 198 mPageRanges[i] = (PageRange) parcelables[i]; 199 } 200 } 201 if (parcel.readInt() == 1) { 202 mAttributes = PrintAttributes.CREATOR.createFromParcel(parcel); 203 } 204 if (parcel.readInt() == 1) { 205 mDocumentInfo = PrintDocumentInfo.CREATOR.createFromParcel(parcel); 206 } 207 } 208 209 /** 210 * Gets the unique print job id. 211 * 212 * @return The id. 213 */ 214 public PrintJobId getId() { 215 return mId; 216 } 217 218 /** 219 * Sets the unique print job id. 220 * 221 * @param The job id. 222 * 223 * @hide 224 */ 225 public void setId(PrintJobId id) { 226 this.mId = id; 227 } 228 229 /** 230 * Gets the human readable job label. 231 * 232 * @return The label. 233 */ 234 public String getLabel() { 235 return mLabel; 236 } 237 238 /** 239 * Sets the human readable job label. 240 * 241 * @param label The label. 242 * 243 * @hide 244 */ 245 public void setLabel(String label) { 246 mLabel = label; 247 } 248 249 /** 250 * Gets the unique target printer id. 251 * 252 * @return The target printer id. 253 */ 254 public PrinterId getPrinterId() { 255 return mPrinterId; 256 } 257 258 /** 259 * Sets the unique target pritner id. 260 * 261 * @param printerId The target printer id. 262 * 263 * @hide 264 */ 265 public void setPrinterId(PrinterId printerId) { 266 mPrinterId = printerId; 267 } 268 269 /** 270 * Gets the name of the target printer. 271 * 272 * @return The printer name. 273 * 274 * @hide 275 */ 276 public String getPrinterName() { 277 return mPrinterName; 278 } 279 280 /** 281 * Sets the name of the target printer. 282 * 283 * @param printerName The printer name. 284 * 285 * @hide 286 */ 287 public void setPrinterName(String printerName) { 288 mPrinterName = printerName; 289 } 290 291 /** 292 * Gets the current job state. 293 * 294 * @return The job state. 295 */ 296 public int getState() { 297 return mState; 298 } 299 300 /** 301 * Sets the current job state. 302 * 303 * @param state The job state. 304 * 305 * @hide 306 */ 307 public void setState(int state) { 308 mState = state; 309 } 310 311 /** 312 * Sets the owning application id. 313 * 314 * @return The owning app id. 315 * 316 * @hide 317 */ 318 public int getAppId() { 319 return mAppId; 320 } 321 322 /** 323 * Sets the owning application id. 324 * 325 * @param appId The owning app id. 326 * 327 * @hide 328 */ 329 public void setAppId(int appId) { 330 mAppId = appId; 331 } 332 333 /** 334 * Gets the owning user id. 335 * 336 * @return The user id. 337 * 338 * @hide 339 */ 340 public int getUserId() { 341 return mUserId; 342 } 343 344 /** 345 * Sets the owning user id. 346 * 347 * @param userId The user id. 348 * 349 * @hide 350 */ 351 public void setUserId(int userId) { 352 mUserId = userId; 353 } 354 355 /** 356 * Gets the optional tag assigned by a print service. 357 * 358 * @return The tag. 359 */ 360 public String getTag() { 361 return mTag; 362 } 363 364 /** 365 * Sets the optional tag assigned by a print service. 366 * 367 * @param tag The tag. 368 * 369 * @hide 370 */ 371 public void setTag(String tag) { 372 mTag = tag; 373 } 374 375 /** 376 * Gets the wall time in millisecond when this print job was created. 377 * 378 * @return The creation time in milliseconds. 379 */ 380 public long getCreationTime() { 381 return mCreationTime; 382 } 383 384 /** 385 * Sets the wall time in milliseconds when this print job was created. 386 * 387 * @param creationTime The creation time in milliseconds. 388 * 389 * @hide 390 */ 391 public void setCreationTime(long creationTime) { 392 if (creationTime < 0) { 393 throw new IllegalArgumentException("creationTime must be non-negative."); 394 } 395 mCreationTime = creationTime; 396 } 397 398 /** 399 * Gets the number of copies. 400 * 401 * @return The number of copies or zero if not set. 402 */ 403 public int getCopies() { 404 return mCopies; 405 } 406 407 /** 408 * Sets the number of copies. 409 * 410 * @param copyCount The number of copies. 411 * 412 * @hide 413 */ 414 public void setCopies(int copyCount) { 415 if (copyCount < 1) { 416 throw new IllegalArgumentException("Copies must be more than one."); 417 } 418 mCopies = copyCount; 419 } 420 421 /** 422 * Gets the reason for the print job being in the current state. 423 * 424 * @return The reason, or null if there is no reason or the 425 * reason is unknown. 426 * 427 * @hide 428 */ 429 public String getStateReason() { 430 return mStateReason; 431 } 432 433 /** 434 * Sets the reason for the print job being in the current state. 435 * 436 * @param stateReason The reason, or null if there is no reason 437 * or the reason is unknown. 438 * 439 * @hide 440 */ 441 public void setStateReason(String stateReason) { 442 mStateReason = stateReason; 443 } 444 445 /** 446 * Gets the included pages. 447 * 448 * @return The included pages or <code>null</code> if not set. 449 */ 450 public PageRange[] getPages() { 451 return mPageRanges; 452 } 453 454 /** 455 * Sets the included pages. 456 * 457 * @return The included pages. 458 * 459 * @hide 460 */ 461 public void setPages(PageRange[] pageRanges) { 462 mPageRanges = pageRanges; 463 } 464 465 /** 466 * Gets the print job attributes. 467 * 468 * @return The attributes. 469 */ 470 public PrintAttributes getAttributes() { 471 return mAttributes; 472 } 473 474 /** 475 * Sets the print job attributes. 476 * 477 * @param attributes The attributes. 478 * 479 * @hide 480 */ 481 public void setAttributes(PrintAttributes attributes) { 482 mAttributes = attributes; 483 } 484 485 /** 486 * Gets the info describing the printed document. 487 * 488 * @return The document info. 489 * 490 * @hide 491 */ 492 public PrintDocumentInfo getDocumentInfo() { 493 return mDocumentInfo; 494 } 495 496 /** 497 * Sets the info describing the printed document. 498 * 499 * @param info The document info. 500 * 501 * @hide 502 */ 503 public void setDocumentInfo(PrintDocumentInfo info) { 504 mDocumentInfo = info; 505 } 506 507 @Override 508 public int describeContents() { 509 return 0; 510 } 511 512 @Override 513 public void writeToParcel(Parcel parcel, int flags) { 514 parcel.writeParcelable(mId, flags); 515 parcel.writeString(mLabel); 516 parcel.writeParcelable(mPrinterId, flags); 517 parcel.writeString(mPrinterName); 518 parcel.writeInt(mState); 519 parcel.writeInt(mAppId); 520 parcel.writeInt(mUserId); 521 parcel.writeString(mTag); 522 parcel.writeLong(mCreationTime); 523 parcel.writeInt(mCopies); 524 parcel.writeString(mStateReason); 525 if (mPageRanges != null) { 526 parcel.writeInt(1); 527 parcel.writeParcelableArray(mPageRanges, flags); 528 } else { 529 parcel.writeInt(0); 530 } 531 if (mAttributes != null) { 532 parcel.writeInt(1); 533 mAttributes.writeToParcel(parcel, flags); 534 } else { 535 parcel.writeInt(0); 536 } 537 if (mDocumentInfo != null) { 538 parcel.writeInt(1); 539 mDocumentInfo.writeToParcel(parcel, flags); 540 } else { 541 parcel.writeInt(0); 542 } 543 } 544 545 @Override 546 public String toString() { 547 StringBuilder builder = new StringBuilder(); 548 builder.append("PrintJobInfo{"); 549 builder.append("label: ").append(mLabel); 550 builder.append(", id: ").append(mId); 551 builder.append(", status: ").append(stateToString(mState)); 552 builder.append(", printer: " + mPrinterId); 553 builder.append(", tag: ").append(mTag); 554 builder.append(", creationTime: " + mCreationTime); 555 builder.append(", copies: ").append(mCopies); 556 builder.append(", attributes: " + (mAttributes != null 557 ? mAttributes.toString() : null)); 558 builder.append(", documentInfo: " + (mDocumentInfo != null 559 ? mDocumentInfo.toString() : null)); 560 builder.append(", pages: " + (mPageRanges != null 561 ? Arrays.toString(mPageRanges) : null)); 562 builder.append("}"); 563 return builder.toString(); 564 } 565 566 /** @hide */ 567 public static String stateToString(int state) { 568 switch (state) { 569 case STATE_CREATED: { 570 return "STATE_CREATED"; 571 } 572 case STATE_QUEUED: { 573 return "STATE_QUEUED"; 574 } 575 case STATE_STARTED: { 576 return "STATE_STARTED"; 577 } 578 case STATE_FAILED: { 579 return "STATE_FAILED"; 580 } 581 case STATE_COMPLETED: { 582 return "STATE_COMPLETED"; 583 } 584 case STATE_CANCELED: { 585 return "STATE_CANCELED"; 586 } 587 default: { 588 return "STATE_UNKNOWN"; 589 } 590 } 591 } 592 593 public static final Parcelable.Creator<PrintJobInfo> CREATOR = 594 new Creator<PrintJobInfo>() { 595 @Override 596 public PrintJobInfo createFromParcel(Parcel parcel) { 597 return new PrintJobInfo(parcel); 598 } 599 600 @Override 601 public PrintJobInfo[] newArray(int size) { 602 return new PrintJobInfo[size]; 603 } 604 }; 605} 606