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