PrintJobInfo.java revision d26d4898fcc9b78f4b66118895c375384098205e
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 * @hide 61 */ 62 public static final int STATE_CREATED = 1; 63 64 /** 65 * Print job state: The print jobs is created, it is ready 66 * to be printed and should be processed. 67 * <p> 68 * Next valid states: {@link #STATE_STARTED}, {@link #STATE_FAILED}, 69 * {@link #STATE_CANCELED} 70 * </p> 71 */ 72 public static final int STATE_QUEUED = 2; 73 74 /** 75 * Print job state: The print job is being printed. 76 * <p> 77 * Next valid states: {@link #STATE_COMPLETED}, {@link #STATE_FAILED}, 78 * {@link #STATE_CANCELED}, {@link #STATE_BLOCKED} 79 * </p> 80 */ 81 public static final int STATE_STARTED = 3; 82 83 /** 84 * Print job state: The print job is blocked. 85 * <p> 86 * Next valid states: {@link #STATE_FAILED}, {@link #STATE_CANCELED}, 87 * {@link #STATE_STARTED} 88 * </p> 89 */ 90 public static final int STATE_BLOCKED = 4; 91 92 /** 93 * Print job state: The print job was successfully printed. 94 * This is a terminal state. 95 * <p> 96 * Next valid states: None 97 * </p> 98 */ 99 public static final int STATE_COMPLETED = 5; 100 101 /** 102 * Print job state: The print job was printing but printing failed. 103 * This is a terminal state. 104 * <p> 105 * Next valid states: None 106 * </p> 107 */ 108 public static final int STATE_FAILED = 6; 109 110 /** 111 * Print job state: The print job was canceled. 112 * This is a terminal state. 113 * <p> 114 * Next valid states: None 115 * </p> 116 */ 117 public static final int STATE_CANCELED = 7; 118 119 /** The unique print job id. */ 120 private int mId; 121 122 /** The human readable print job label. */ 123 private String mLabel; 124 125 /** The unique id of the printer. */ 126 private PrinterId mPrinterId; 127 128 /** The name of the printer - internally used */ 129 private String mPrinterName; 130 131 /** The status of the print job. */ 132 private int mState; 133 134 /** The id of the app that created the job. */ 135 private int mAppId; 136 137 /** The id of the user that created the job. */ 138 private int mUserId; 139 140 /** Optional tag assigned by a print service.*/ 141 private String mTag; 142 143 /** How many copies to print. */ 144 private int mCopies; 145 146 /** Reason for the print job being in its current state. */ 147 private String mStateReason; 148 149 /** The pages to print */ 150 private PageRange[] mPageRanges; 151 152 /** The print job attributes size. */ 153 private PrintAttributes mAttributes; 154 155 /** Information about the printed document. */ 156 private PrintDocumentInfo mDocumentInfo; 157 158 /** @hide*/ 159 public PrintJobInfo() { 160 /* do nothing */ 161 } 162 163 /** @hide */ 164 public PrintJobInfo(PrintJobInfo other) { 165 mId = other.mId; 166 mLabel = other.mLabel; 167 mPrinterId = other.mPrinterId; 168 mPrinterName = other.mPrinterName; 169 mState = other.mState; 170 mAppId = other.mAppId; 171 mUserId = other.mUserId; 172 mTag = other.mTag; 173 mCopies = other.mCopies; 174 mStateReason = other.mStateReason; 175 mPageRanges = other.mPageRanges; 176 mAttributes = other.mAttributes; 177 mDocumentInfo = other.mDocumentInfo; 178 } 179 180 private PrintJobInfo(Parcel parcel) { 181 mId = parcel.readInt(); 182 mLabel = parcel.readString(); 183 mPrinterId = parcel.readParcelable(null); 184 mPrinterName = parcel.readString(); 185 mState = parcel.readInt(); 186 mAppId = parcel.readInt(); 187 mUserId = parcel.readInt(); 188 mTag = parcel.readString(); 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 int 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(int 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 public String getTag() { 358 return mTag; 359 } 360 361 /** 362 * Sets the optional tag assigned by a print service. 363 * 364 * @param tag The tag. 365 * 366 * @hide 367 */ 368 public void setTag(String tag) { 369 mTag = tag; 370 } 371 372 /** 373 * Gets the number of copies. 374 * 375 * @return The number of copies or zero if not set. 376 */ 377 public int getCopies() { 378 return mCopies; 379 } 380 381 /** 382 * Sets the number of copies. 383 * 384 * @param copyCount The number of copies. 385 * 386 * @hide 387 */ 388 public void setCopies(int copyCount) { 389 if (copyCount < 1) { 390 throw new IllegalArgumentException("Copies must be more than one."); 391 } 392 mCopies = copyCount; 393 } 394 395 /** 396 * Gets the reason for the print job being in the current state. 397 * 398 * @return The reason, or null if there is no reason or the 399 * reason is unknown. 400 * 401 * @hide 402 */ 403 public String getStateReason() { 404 return mStateReason; 405 } 406 407 /** 408 * Sets the reason for the print job being in the current state. 409 * 410 * @param stateReason The reason, or null if there is no reason 411 * or the reason is unknown. 412 * 413 * @hide 414 */ 415 public void setStateReason(String stateReason) { 416 mStateReason = stateReason; 417 } 418 419 /** 420 * Gets the included pages. 421 * 422 * @return The included pages or <code>null</code> if not set. 423 */ 424 public PageRange[] getPages() { 425 return mPageRanges; 426 } 427 428 /** 429 * Sets the included pages. 430 * 431 * @return The included pages. 432 * 433 * @hide 434 */ 435 public void setPages(PageRange[] pageRanges) { 436 mPageRanges = pageRanges; 437 } 438 439 /** 440 * Gets the print job attributes. 441 * 442 * @return The attributes. 443 */ 444 public PrintAttributes getAttributes() { 445 return mAttributes; 446 } 447 448 /** 449 * Sets the print job attributes. 450 * 451 * @param attributes The attributes. 452 * 453 * @hide 454 */ 455 public void setAttributes(PrintAttributes attributes) { 456 mAttributes = attributes; 457 } 458 459 /** 460 * Gets the info describing the printed document. 461 * 462 * @return The document info. 463 * 464 * @hide 465 */ 466 public PrintDocumentInfo getDocumentInfo() { 467 return mDocumentInfo; 468 } 469 470 /** 471 * Sets the info describing the printed document. 472 * 473 * @param info The document info. 474 * 475 * @hide 476 */ 477 public void setDocumentInfo(PrintDocumentInfo info) { 478 mDocumentInfo = info; 479 } 480 481 @Override 482 public int describeContents() { 483 return 0; 484 } 485 486 @Override 487 public void writeToParcel(Parcel parcel, int flags) { 488 parcel.writeInt(mId); 489 parcel.writeString(mLabel); 490 parcel.writeParcelable(mPrinterId, flags); 491 parcel.writeString(mPrinterName); 492 parcel.writeInt(mState); 493 parcel.writeInt(mAppId); 494 parcel.writeInt(mUserId); 495 parcel.writeString(mTag); 496 parcel.writeInt(mCopies); 497 parcel.writeString(mStateReason); 498 if (mPageRanges != null) { 499 parcel.writeInt(1); 500 parcel.writeParcelableArray(mPageRanges, flags); 501 } else { 502 parcel.writeInt(0); 503 } 504 if (mAttributes != null) { 505 parcel.writeInt(1); 506 mAttributes.writeToParcel(parcel, flags); 507 } else { 508 parcel.writeInt(0); 509 } 510 if (mDocumentInfo != null) { 511 parcel.writeInt(1); 512 mDocumentInfo.writeToParcel(parcel, flags); 513 } else { 514 parcel.writeInt(0); 515 } 516 } 517 518 @Override 519 public String toString() { 520 StringBuilder builder = new StringBuilder(); 521 builder.append("PrintJobInfo{"); 522 builder.append("label: ").append(mLabel); 523 builder.append(", id: ").append(mId); 524 builder.append(", status: ").append(stateToString(mState)); 525 builder.append(", printer: " + mPrinterId); 526 builder.append(", tag: ").append(mTag); 527 builder.append(", copies: ").append(mCopies); 528 builder.append(", attributes: " + (mAttributes != null 529 ? mAttributes.toString() : null)); 530 builder.append(", documentInfo: " + (mDocumentInfo != null 531 ? mDocumentInfo.toString() : null)); 532 builder.append(", pages: " + (mPageRanges != null 533 ? Arrays.toString(mPageRanges) : null)); 534 builder.append("}"); 535 return builder.toString(); 536 } 537 538 /** @hide */ 539 public static String stateToString(int state) { 540 switch (state) { 541 case STATE_CREATED: { 542 return "STATUS_CREATED"; 543 } 544 case STATE_QUEUED: { 545 return "STATE_QUEUED"; 546 } 547 case STATE_STARTED: { 548 return "STATE_STARTED"; 549 } 550 case STATE_FAILED: { 551 return "STATUS_FAILED"; 552 } 553 case STATE_COMPLETED: { 554 return "STATUS_COMPLETED"; 555 } 556 case STATE_CANCELED: { 557 return "STATUS_CANCELED"; 558 } 559 default: { 560 return "STATUS_UNKNOWN"; 561 } 562 } 563 } 564 565 566 public static final Parcelable.Creator<PrintJobInfo> CREATOR = 567 new Creator<PrintJobInfo>() { 568 @Override 569 public PrintJobInfo createFromParcel(Parcel parcel) { 570 return new PrintJobInfo(parcel); 571 } 572 573 @Override 574 public PrintJobInfo[] newArray(int size) { 575 return new PrintJobInfo[size]; 576 } 577 }; 578} 579