blob: 94686a8ee456f2a11665c6eac6099fe25b309fb9 [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
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
Philip P. Moltmannb3078c22015-11-23 16:12:39 -080019import android.annotation.FloatRange;
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080020import android.annotation.IntDef;
21import android.annotation.IntRange;
22import android.annotation.NonNull;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -080023import android.annotation.Nullable;
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -070024import android.annotation.StringRes;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -080025import android.annotation.TestApi;
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -070026import android.content.pm.PackageManager;
27import android.content.res.Resources;
Svetoslavb4fda132013-10-25 18:57:43 -070028import android.os.Bundle;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070029import android.os.Parcel;
30import android.os.Parcelable;
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070031import android.service.print.PrintJobInfoProto;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070032
Philip P. Moltmannb3078c22015-11-23 16:12:39 -080033import com.android.internal.util.Preconditions;
34
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080035import java.lang.annotation.Retention;
36import java.lang.annotation.RetentionPolicy;
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070037import java.util.Arrays;
38
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070039/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070040 * This class represents the description of a print job. The print job
41 * state includes properties such as its id, print attributes used for
42 * generating the content, and so on. Note that the print jobs state may
43 * change over time and this class represents a snapshot of this state.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070044 */
45public final class PrintJobInfo implements Parcelable {
46
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080047 /** @hide */
48 @IntDef({
49 STATE_CREATED, STATE_QUEUED, STATE_STARTED, STATE_BLOCKED, STATE_COMPLETED,
50 STATE_FAILED, STATE_CANCELED
51 })
52 @Retention(RetentionPolicy.SOURCE)
53 public @interface State {
54 }
55
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070056 /**
57 * Constant for matching any print job state.
58 *
59 * @hide
60 */
61 public static final int STATE_ANY = -1;
62
63 /**
Svetoslav Ganov88d19912013-07-22 12:32:03 -070064 * Constant for matching any print job state.
65 *
66 * @hide
67 */
68 public static final int STATE_ANY_VISIBLE_TO_CLIENTS = -2;
69
70 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070071 * Constant for matching any active print job state.
72 *
73 * @hide
74 */
75 public static final int STATE_ANY_ACTIVE = -3;
76
77 /**
Svetoslav Ganov9b6d3a12013-10-12 12:35:41 -070078 * Constant for matching any scheduled, i.e. delivered to a print
79 * service, print job state.
80 *
81 * @hide
82 */
83 public static final int STATE_ANY_SCHEDULED = -4;
84
85 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070086 * Print job state: The print job is being created but not yet
87 * ready to be printed.
88 * <p>
89 * Next valid states: {@link #STATE_QUEUED}
90 * </p>
91 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070092 public static final int STATE_CREATED = PrintJobInfoProto.STATE_CREATED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070093
94 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070095 * Print job state: The print jobs is created, it is ready
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070096 * to be printed and should be processed.
97 * <p>
98 * Next valid states: {@link #STATE_STARTED}, {@link #STATE_FAILED},
99 * {@link #STATE_CANCELED}
100 * </p>
101 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700102 public static final int STATE_QUEUED = PrintJobInfoProto.STATE_QUEUED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700103
104 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700105 * Print job state: The print job is being printed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700106 * <p>
107 * Next valid states: {@link #STATE_COMPLETED}, {@link #STATE_FAILED},
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700108 * {@link #STATE_CANCELED}, {@link #STATE_BLOCKED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700109 * </p>
110 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700111 public static final int STATE_STARTED = PrintJobInfoProto.STATE_STARTED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700112
113 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700114 * Print job state: The print job is blocked.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700115 * <p>
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700116 * Next valid states: {@link #STATE_FAILED}, {@link #STATE_CANCELED},
117 * {@link #STATE_STARTED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700118 * </p>
119 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700120 public static final int STATE_BLOCKED = PrintJobInfoProto.STATE_BLOCKED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700121
122 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700123 * Print job state: The print job is successfully printed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700124 * This is a terminal state.
125 * <p>
126 * Next valid states: None
127 * </p>
128 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700129 public static final int STATE_COMPLETED = PrintJobInfoProto.STATE_COMPLETED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700130
131 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700132 * Print job state: The print job was printing but printing failed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700133 * <p>
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700134 * Next valid states: {@link #STATE_CANCELED}, {@link #STATE_STARTED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700135 * </p>
136 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700137 public static final int STATE_FAILED = PrintJobInfoProto.STATE_FAILED;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700138
139 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700140 * Print job state: The print job is canceled.
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700141 * This is a terminal state.
142 * <p>
143 * Next valid states: None
144 * </p>
145 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700146 public static final int STATE_CANCELED = PrintJobInfoProto.STATE_CANCELED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700147
148 /** The unique print job id. */
Svetoslav2fbd2a72013-09-16 17:53:51 -0700149 private PrintJobId mId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700150
151 /** The human readable print job label. */
Svetoslav269403b2013-08-14 17:31:04 -0700152 private String mLabel;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700153
154 /** The unique id of the printer. */
155 private PrinterId mPrinterId;
156
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700157 /** The name of the printer - internally used */
158 private String mPrinterName;
159
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700160 /** The state of the print job. */
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700161 private int mState;
162
163 /** The id of the app that created the job. */
164 private int mAppId;
165
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700166 /** Optional tag assigned by a print service.*/
167 private String mTag;
168
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700169 /** The wall time when the print job was created. */
170 private long mCreationTime;
171
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700172 /** How many copies to print. */
173 private int mCopies;
174
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700175 /** The pages to print */
176 private PageRange[] mPageRanges;
177
178 /** The print job attributes size. */
179 private PrintAttributes mAttributes;
180
Svetoslav Ganova0027152013-06-25 14:59:53 -0700181 /** Information about the printed document. */
182 private PrintDocumentInfo mDocumentInfo;
183
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800184 /** The progress made on printing this job or -1 if not set. */
185 private float mProgress;
186
187 /** A short string describing the status of this job. */
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700188 private @Nullable CharSequence mStatus;
189
190 /** A string resource describing the status of this job. */
191 private @StringRes int mStatusRes;
192 private @Nullable CharSequence mStatusResAppPackageName;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800193
Svetoslavb4fda132013-10-25 18:57:43 -0700194 /** Advanced printer specific options. */
195 private Bundle mAdvancedOptions;
196
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700197 /** Whether we are trying to cancel this print job. */
198 private boolean mCanceling;
199
Svetoslav Ganova0027152013-06-25 14:59:53 -0700200 /** @hide*/
201 public PrintJobInfo() {
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800202 mProgress = -1;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700203 }
204
205 /** @hide */
206 public PrintJobInfo(PrintJobInfo other) {
207 mId = other.mId;
208 mLabel = other.mLabel;
209 mPrinterId = other.mPrinterId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700210 mPrinterName = other.mPrinterName;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700211 mState = other.mState;
212 mAppId = other.mAppId;
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700213 mTag = other.mTag;
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700214 mCreationTime = other.mCreationTime;
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700215 mCopies = other.mCopies;
216 mPageRanges = other.mPageRanges;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700217 mAttributes = other.mAttributes;
218 mDocumentInfo = other.mDocumentInfo;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800219 mProgress = other.mProgress;
220 mStatus = other.mStatus;
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700221 mStatusRes = other.mStatusRes;
222 mStatusResAppPackageName = other.mStatusResAppPackageName;
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700223 mCanceling = other.mCanceling;
Svetoslavb4fda132013-10-25 18:57:43 -0700224 mAdvancedOptions = other.mAdvancedOptions;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700225 }
226
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800227 private PrintJobInfo(@NonNull Parcel parcel) {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700228 mId = parcel.readParcelable(null);
Svetoslav269403b2013-08-14 17:31:04 -0700229 mLabel = parcel.readString();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700230 mPrinterId = parcel.readParcelable(null);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700231 mPrinterName = parcel.readString();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700232 mState = parcel.readInt();
233 mAppId = parcel.readInt();
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700234 mTag = parcel.readString();
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700235 mCreationTime = parcel.readLong();
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700236 mCopies = parcel.readInt();
Svetoslavb4fda132013-10-25 18:57:43 -0700237 Parcelable[] parcelables = parcel.readParcelableArray(null);
238 if (parcelables != null) {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700239 mPageRanges = new PageRange[parcelables.length];
240 for (int i = 0; i < parcelables.length; i++) {
241 mPageRanges[i] = (PageRange) parcelables[i];
242 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700243 }
Svetoslavb4fda132013-10-25 18:57:43 -0700244 mAttributes = (PrintAttributes) parcel.readParcelable(null);
245 mDocumentInfo = (PrintDocumentInfo) parcel.readParcelable(null);
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800246 mProgress = parcel.readFloat();
247 mStatus = parcel.readCharSequence();
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700248 mStatusRes = parcel.readInt();
249 mStatusResAppPackageName = parcel.readCharSequence();
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700250 mCanceling = (parcel.readInt() == 1);
Svetoslavb4fda132013-10-25 18:57:43 -0700251 mAdvancedOptions = parcel.readBundle();
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700252
253 if (mAdvancedOptions != null) {
254 Preconditions.checkArgument(!mAdvancedOptions.containsKey(null));
255 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700256 }
257
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700258 /**
259 * Gets the unique print job id.
260 *
261 * @return The id.
262 */
Philip P. Moltmann22f86b42016-01-15 09:49:00 -0800263 public @Nullable PrintJobId getId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700264 return mId;
265 }
266
267 /**
268 * Sets the unique print job id.
269 *
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800270 * @param id The job id.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700271 *
272 * @hide
273 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800274 public void setId(@NonNull PrintJobId id) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700275 this.mId = id;
276 }
277
278 /**
279 * Gets the human readable job label.
280 *
281 * @return The label.
282 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800283 public @NonNull String getLabel() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700284 return mLabel;
285 }
286
287 /**
288 * Sets the human readable job label.
289 *
290 * @param label The label.
291 *
292 * @hide
293 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800294 public void setLabel(@NonNull String label) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700295 mLabel = label;
296 }
297
298 /**
299 * Gets the unique target printer id.
300 *
301 * @return The target printer id.
302 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800303 public @Nullable PrinterId getPrinterId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700304 return mPrinterId;
305 }
306
307 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800308 * Sets the unique target printer id.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700309 *
310 * @param printerId The target printer id.
311 *
312 * @hide
313 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800314 public void setPrinterId(@NonNull PrinterId printerId) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700315 mPrinterId = printerId;
316 }
317
318 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700319 * Gets the name of the target printer.
320 *
321 * @return The printer name.
322 *
323 * @hide
324 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800325 public @Nullable String getPrinterName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700326 return mPrinterName;
327 }
328
329 /**
330 * Sets the name of the target printer.
331 *
332 * @param printerName The printer name.
333 *
334 * @hide
335 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800336 public void setPrinterName(@NonNull String printerName) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700337 mPrinterName = printerName;
338 }
339
340 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700341 * Gets the current job state.
342 *
343 * @return The job state.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700344 *
345 * @see #STATE_CREATED
346 * @see #STATE_QUEUED
347 * @see #STATE_STARTED
348 * @see #STATE_COMPLETED
349 * @see #STATE_BLOCKED
350 * @see #STATE_FAILED
351 * @see #STATE_CANCELED
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700352 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800353 public @State int getState() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700354 return mState;
355 }
356
357 /**
358 * Sets the current job state.
359 *
360 * @param state The job state.
361 *
362 * @hide
363 */
364 public void setState(int state) {
365 mState = state;
366 }
367
368 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800369 * Sets the progress of the print job.
370 *
371 * @param progress the progress of the job
372 *
373 * @hide
374 */
375 public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
376 Preconditions.checkArgumentInRange(progress, 0, 1, "progress");
377
378 mProgress = progress;
379 }
380
381 /**
382 * Sets the status of the print job.
383 *
384 * @param status the status of the job, can be null
385 *
386 * @hide
387 */
388 public void setStatus(@Nullable CharSequence status) {
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700389 mStatusRes = 0;
390 mStatusResAppPackageName = null;
391
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800392 mStatus = status;
393 }
394
395 /**
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700396 * Sets the status of the print job.
397 *
398 * @param status The new status as a string resource
399 * @param appPackageName App package name the resource belongs to
400 *
401 * @hide
402 */
403 public void setStatus(@StringRes int status, @NonNull CharSequence appPackageName) {
404 mStatus = null;
405
406 mStatusRes = status;
407 mStatusResAppPackageName = appPackageName;
408 }
409
410 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700411 * Sets the owning application id.
412 *
413 * @return The owning app id.
414 *
415 * @hide
416 */
417 public int getAppId() {
418 return mAppId;
419 }
420
421 /**
422 * Sets the owning application id.
423 *
424 * @param appId The owning app id.
425 *
426 * @hide
427 */
428 public void setAppId(int appId) {
429 mAppId = appId;
430 }
431
432 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700433 * Gets the optional tag assigned by a print service.
434 *
435 * @return The tag.
Svetoslavb450d0d2013-10-04 16:20:00 -0700436 *
437 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700438 */
439 public String getTag() {
440 return mTag;
441 }
442
443 /**
444 * Sets the optional tag assigned by a print service.
445 *
446 * @param tag The tag.
447 *
448 * @hide
449 */
450 public void setTag(String tag) {
451 mTag = tag;
452 }
453
454 /**
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700455 * Gets the wall time in millisecond when this print job was created.
456 *
457 * @return The creation time in milliseconds.
458 */
459 public long getCreationTime() {
460 return mCreationTime;
461 }
462
463 /**
464 * Sets the wall time in milliseconds when this print job was created.
465 *
466 * @param creationTime The creation time in milliseconds.
467 *
468 * @hide
469 */
470 public void setCreationTime(long creationTime) {
471 if (creationTime < 0) {
472 throw new IllegalArgumentException("creationTime must be non-negative.");
473 }
474 mCreationTime = creationTime;
475 }
476
477 /**
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700478 * Gets the number of copies.
479 *
480 * @return The number of copies or zero if not set.
481 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800482 public @IntRange(from = 0) int getCopies() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700483 return mCopies;
484 }
485
486 /**
487 * Sets the number of copies.
488 *
489 * @param copyCount The number of copies.
490 *
491 * @hide
492 */
493 public void setCopies(int copyCount) {
494 if (copyCount < 1) {
495 throw new IllegalArgumentException("Copies must be more than one.");
496 }
497 mCopies = copyCount;
498 }
499
500 /**
Svetoslav53f57d12013-06-22 00:26:49 -0700501 * Gets the included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700502 *
Svetoslav53f57d12013-06-22 00:26:49 -0700503 * @return The included pages or <code>null</code> if not set.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700504 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800505 public @Nullable PageRange[] getPages() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700506 return mPageRanges;
507 }
508
509 /**
Svetoslav53f57d12013-06-22 00:26:49 -0700510 * Sets the included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700511 *
Svetoslav1c43fce2013-10-16 10:57:48 -0700512 * @param pageRanges The included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700513 *
514 * @hide
515 */
Svetoslav53f57d12013-06-22 00:26:49 -0700516 public void setPages(PageRange[] pageRanges) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700517 mPageRanges = pageRanges;
518 }
519
520 /**
521 * Gets the print job attributes.
522 *
523 * @return The attributes.
524 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800525 public @NonNull PrintAttributes getAttributes() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700526 return mAttributes;
527 }
528
529 /**
530 * Sets the print job attributes.
531 *
532 * @param attributes The attributes.
533 *
534 * @hide
535 */
536 public void setAttributes(PrintAttributes attributes) {
537 mAttributes = attributes;
538 }
539
Svetoslav Ganova0027152013-06-25 14:59:53 -0700540 /**
541 * Gets the info describing the printed document.
542 *
543 * @return The document info.
544 *
545 * @hide
546 */
547 public PrintDocumentInfo getDocumentInfo() {
548 return mDocumentInfo;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700549 }
550
Svetoslav Ganova0027152013-06-25 14:59:53 -0700551 /**
552 * Sets the info describing the printed document.
553 *
554 * @param info The document info.
555 *
556 * @hide
557 */
558 public void setDocumentInfo(PrintDocumentInfo info) {
559 mDocumentInfo = info;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700560 }
561
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700562 /**
563 * Gets whether this print is being cancelled.
564 *
565 * @return True if the print job is being cancelled.
566 *
567 * @hide
568 */
569 public boolean isCancelling() {
570 return mCanceling;
571 }
572
573 /**
574 * Sets whether this print is being cancelled.
575 *
576 * @param cancelling True if the print job is being cancelled.
577 *
578 * @hide
579 */
580 public void setCancelling(boolean cancelling) {
581 mCanceling = cancelling;
582 }
583
Svetoslavb4fda132013-10-25 18:57:43 -0700584 /**
585 * Gets whether this job has a given advanced (printer specific) print
586 * option.
587 *
588 * @param key The option key.
589 * @return Whether the option is present.
Svetoslavb4fda132013-10-25 18:57:43 -0700590 */
591 public boolean hasAdvancedOption(String key) {
592 return mAdvancedOptions != null && mAdvancedOptions.containsKey(key);
593 }
594
595 /**
596 * Gets the value of an advanced (printer specific) print option.
597 *
598 * @param key The option key.
599 * @return The option value.
Svetoslavb4fda132013-10-25 18:57:43 -0700600 */
601 public String getAdvancedStringOption(String key) {
602 if (mAdvancedOptions != null) {
603 return mAdvancedOptions.getString(key);
604 }
605 return null;
606 }
607
608 /**
609 * Gets the value of an advanced (printer specific) print option.
610 *
611 * @param key The option key.
612 * @return The option value.
Svetoslavb4fda132013-10-25 18:57:43 -0700613 */
614 public int getAdvancedIntOption(String key) {
615 if (mAdvancedOptions != null) {
616 return mAdvancedOptions.getInt(key);
617 }
618 return 0;
619 }
620
621 /**
622 * Gets the advanced options.
623 *
624 * @return The advanced options.
625 *
626 * @hide
627 */
628 public Bundle getAdvancedOptions() {
629 return mAdvancedOptions;
630 }
631
632 /**
633 * Sets the advanced options.
634 *
635 * @param options The advanced options.
636 *
637 * @hide
638 */
639 public void setAdvancedOptions(Bundle options) {
640 mAdvancedOptions = options;
641 }
642
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700643 @Override
644 public int describeContents() {
645 return 0;
646 }
647
648 @Override
649 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700650 parcel.writeParcelable(mId, flags);
Svetoslav269403b2013-08-14 17:31:04 -0700651 parcel.writeString(mLabel);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700652 parcel.writeParcelable(mPrinterId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700653 parcel.writeString(mPrinterName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700654 parcel.writeInt(mState);
655 parcel.writeInt(mAppId);
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700656 parcel.writeString(mTag);
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700657 parcel.writeLong(mCreationTime);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700658 parcel.writeInt(mCopies);
Svetoslavb4fda132013-10-25 18:57:43 -0700659 parcel.writeParcelableArray(mPageRanges, flags);
660 parcel.writeParcelable(mAttributes, flags);
661 parcel.writeParcelable(mDocumentInfo, 0);
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800662 parcel.writeFloat(mProgress);
663 parcel.writeCharSequence(mStatus);
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700664 parcel.writeInt(mStatusRes);
665 parcel.writeCharSequence(mStatusResAppPackageName);
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700666 parcel.writeInt(mCanceling ? 1 : 0);
Svetoslavb4fda132013-10-25 18:57:43 -0700667 parcel.writeBundle(mAdvancedOptions);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700668 }
669
670 @Override
671 public String toString() {
672 StringBuilder builder = new StringBuilder();
673 builder.append("PrintJobInfo{");
674 builder.append("label: ").append(mLabel);
675 builder.append(", id: ").append(mId);
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700676 builder.append(", state: ").append(stateToString(mState));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700677 builder.append(", printer: " + mPrinterId);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700678 builder.append(", tag: ").append(mTag);
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700679 builder.append(", creationTime: " + mCreationTime);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700680 builder.append(", copies: ").append(mCopies);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700681 builder.append(", attributes: " + (mAttributes != null
682 ? mAttributes.toString() : null));
683 builder.append(", documentInfo: " + (mDocumentInfo != null
684 ? mDocumentInfo.toString() : null));
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700685 builder.append(", cancelling: " + mCanceling);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700686 builder.append(", pages: " + (mPageRanges != null
687 ? Arrays.toString(mPageRanges) : null));
Svetoslavb4fda132013-10-25 18:57:43 -0700688 builder.append(", hasAdvancedOptions: " + (mAdvancedOptions != null));
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800689 builder.append(", progress: " + mProgress);
690 builder.append(", status: " + (mStatus != null
691 ? mStatus.toString() : null));
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700692 builder.append(", statusRes: " + mStatusRes);
693 builder.append(", statusResAppPackageName: " + (mStatusResAppPackageName != null
694 ? mStatusResAppPackageName.toString() : null));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700695 builder.append("}");
696 return builder.toString();
697 }
698
699 /** @hide */
700 public static String stateToString(int state) {
701 switch (state) {
702 case STATE_CREATED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700703 return "STATE_CREATED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700704 }
705 case STATE_QUEUED: {
706 return "STATE_QUEUED";
707 }
708 case STATE_STARTED: {
709 return "STATE_STARTED";
710 }
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700711 case STATE_BLOCKED: {
712 return "STATE_BLOCKED";
713 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700714 case STATE_FAILED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700715 return "STATE_FAILED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700716 }
717 case STATE_COMPLETED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700718 return "STATE_COMPLETED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700719 }
720 case STATE_CANCELED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700721 return "STATE_CANCELED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700722 }
723 default: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700724 return "STATE_UNKNOWN";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700725 }
726 }
727 }
728
Svetoslav1c43fce2013-10-16 10:57:48 -0700729 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800730 * Get the progress that has been made printing this job.
731 *
732 * @return the print progress or -1 if not set
733 * @hide
734 */
735 @TestApi
736 public float getProgress() {
737 return mProgress;
738 }
739
740 /**
741 * Get the status of this job.
742 *
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700743 * @param pm Package manager used to resolve the string
744 *
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800745 * @return the status of this job or null if not set
746 * @hide
747 */
748 @TestApi
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700749 public @Nullable CharSequence getStatus(@NonNull PackageManager pm) {
750 if (mStatusRes == 0) {
751 return mStatus;
752 } else {
753 try {
754 return pm.getResourcesForApplication(mStatusResAppPackageName.toString())
755 .getString(mStatusRes);
756 } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
757 return null;
758 }
759 }
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800760 }
761
762 /**
Svetoslav1c43fce2013-10-16 10:57:48 -0700763 * Builder for creating a {@link PrintJobInfo}.
764 */
765 public static final class Builder {
766 private final PrintJobInfo mPrototype;
767
768 /**
769 * Constructor.
770 *
771 * @param prototype Prototype to use as a starting point.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700772 * Can be <code>null</code>.
Svetoslav1c43fce2013-10-16 10:57:48 -0700773 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800774 public Builder(@Nullable PrintJobInfo prototype) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700775 mPrototype = (prototype != null)
776 ? new PrintJobInfo(prototype)
777 : new PrintJobInfo();
778 }
779
780 /**
781 * Sets the number of copies.
782 *
783 * @param copies The number of copies.
784 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800785 public void setCopies(@IntRange(from = 1) int copies) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700786 mPrototype.mCopies = copies;
787 }
788
789 /**
790 * Sets the print job attributes.
791 *
792 * @param attributes The attributes.
793 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800794 public void setAttributes(@NonNull PrintAttributes attributes) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700795 mPrototype.mAttributes = attributes;
796 }
797
798 /**
799 * Sets the included pages.
800 *
801 * @param pages The included pages.
802 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800803 public void setPages(@NonNull PageRange[] pages) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700804 mPrototype.mPageRanges = pages;
805 }
806
807 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800808 * Sets the progress of the print job.
809 *
810 * @param progress the progress of the job
811 * @hide
812 */
813 public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
814 Preconditions.checkArgumentInRange(progress, 0, 1, "progress");
815
816 mPrototype.mProgress = progress;
817 }
818
819 /**
820 * Sets the status of the print job.
821 *
822 * @param status the status of the job, can be null
823 * @hide
824 */
825 public void setStatus(@Nullable CharSequence status) {
826 mPrototype.mStatus = status;
827 }
828
829 /**
Svetoslav1c43fce2013-10-16 10:57:48 -0700830 * Puts an advanced (printer specific) option.
831 *
832 * @param key The option key.
833 * @param value The option value.
834 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800835 public void putAdvancedOption(@NonNull String key, @Nullable String value) {
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700836 Preconditions.checkNotNull(key, "key cannot be null");
837
Svetoslavb4fda132013-10-25 18:57:43 -0700838 if (mPrototype.mAdvancedOptions == null) {
839 mPrototype.mAdvancedOptions = new Bundle();
840 }
841 mPrototype.mAdvancedOptions.putString(key, value);
Svetoslav1c43fce2013-10-16 10:57:48 -0700842 }
843
844 /**
845 * Puts an advanced (printer specific) option.
846 *
847 * @param key The option key.
848 * @param value The option value.
849 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800850 public void putAdvancedOption(@NonNull String key, int value) {
Svetoslavb4fda132013-10-25 18:57:43 -0700851 if (mPrototype.mAdvancedOptions == null) {
852 mPrototype.mAdvancedOptions = new Bundle();
853 }
854 mPrototype.mAdvancedOptions.putInt(key, value);
Svetoslav1c43fce2013-10-16 10:57:48 -0700855 }
856
857 /**
858 * Creates a new {@link PrintJobInfo} instance.
859 *
860 * @return The new instance.
861 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800862 public @NonNull PrintJobInfo build() {
Svetoslav1c43fce2013-10-16 10:57:48 -0700863 return mPrototype;
864 }
865 }
866
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700867 public static final Parcelable.Creator<PrintJobInfo> CREATOR =
868 new Creator<PrintJobInfo>() {
869 @Override
870 public PrintJobInfo createFromParcel(Parcel parcel) {
871 return new PrintJobInfo(parcel);
872 }
873
874 @Override
875 public PrintJobInfo[] newArray(int size) {
876 return new PrintJobInfo[size];
877 }
878 };
879}