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