blob: 25f383c09ad1c00ce676945cbc7fd7101cb91353 [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;
Mathew Inwood06bf4b72018-07-31 16:36:56 +010026import android.annotation.UnsupportedAppUsage;
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -070027import android.content.pm.PackageManager;
28import android.content.res.Resources;
Svetoslavb4fda132013-10-25 18:57:43 -070029import android.os.Bundle;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070030import android.os.Parcel;
31import android.os.Parcelable;
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070032import android.service.print.PrintJobInfoProto;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070033
Philip P. Moltmannb3078c22015-11-23 16:12:39 -080034import com.android.internal.util.Preconditions;
35
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070038import java.util.Arrays;
39
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070040/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070041 * This class represents the description of a print job. The print job
42 * state includes properties such as its id, print attributes used for
43 * generating the content, and so on. Note that the print jobs state may
44 * change over time and this class represents a snapshot of this state.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070045 */
46public final class PrintJobInfo implements Parcelable {
47
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080048 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070049 @IntDef(prefix = { "STATE_" }, value = {
50 STATE_CREATED,
51 STATE_QUEUED,
52 STATE_STARTED,
53 STATE_BLOCKED,
54 STATE_COMPLETED,
55 STATE_FAILED,
56 STATE_CANCELED
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080057 })
58 @Retention(RetentionPolicy.SOURCE)
59 public @interface State {
60 }
61
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070062 /**
63 * Constant for matching any print job state.
64 *
65 * @hide
66 */
67 public static final int STATE_ANY = -1;
68
69 /**
Svetoslav Ganov88d19912013-07-22 12:32:03 -070070 * Constant for matching any print job state.
71 *
72 * @hide
73 */
74 public static final int STATE_ANY_VISIBLE_TO_CLIENTS = -2;
75
76 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070077 * Constant for matching any active print job state.
78 *
79 * @hide
80 */
81 public static final int STATE_ANY_ACTIVE = -3;
82
83 /**
Svetoslav Ganov9b6d3a12013-10-12 12:35:41 -070084 * Constant for matching any scheduled, i.e. delivered to a print
85 * service, print job state.
86 *
87 * @hide
88 */
89 public static final int STATE_ANY_SCHEDULED = -4;
90
91 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070092 * Print job state: The print job is being created but not yet
93 * ready to be printed.
94 * <p>
95 * Next valid states: {@link #STATE_QUEUED}
96 * </p>
97 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070098 public static final int STATE_CREATED = PrintJobInfoProto.STATE_CREATED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070099
100 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700101 * Print job state: The print jobs is created, it is ready
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700102 * to be printed and should be processed.
103 * <p>
104 * Next valid states: {@link #STATE_STARTED}, {@link #STATE_FAILED},
105 * {@link #STATE_CANCELED}
106 * </p>
107 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700108 public static final int STATE_QUEUED = PrintJobInfoProto.STATE_QUEUED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700109
110 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700111 * Print job state: The print job is being printed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700112 * <p>
113 * Next valid states: {@link #STATE_COMPLETED}, {@link #STATE_FAILED},
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700114 * {@link #STATE_CANCELED}, {@link #STATE_BLOCKED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700115 * </p>
116 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700117 public static final int STATE_STARTED = PrintJobInfoProto.STATE_STARTED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700118
119 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700120 * Print job state: The print job is blocked.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700121 * <p>
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700122 * Next valid states: {@link #STATE_FAILED}, {@link #STATE_CANCELED},
123 * {@link #STATE_STARTED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700124 * </p>
125 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700126 public static final int STATE_BLOCKED = PrintJobInfoProto.STATE_BLOCKED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700127
128 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700129 * Print job state: The print job is successfully printed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700130 * This is a terminal state.
131 * <p>
132 * Next valid states: None
133 * </p>
134 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700135 public static final int STATE_COMPLETED = PrintJobInfoProto.STATE_COMPLETED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700136
137 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700138 * Print job state: The print job was printing but printing failed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700139 * <p>
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700140 * Next valid states: {@link #STATE_CANCELED}, {@link #STATE_STARTED}
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700141 * </p>
142 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700143 public static final int STATE_FAILED = PrintJobInfoProto.STATE_FAILED;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700144
145 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700146 * Print job state: The print job is canceled.
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700147 * This is a terminal state.
148 * <p>
149 * Next valid states: None
150 * </p>
151 */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -0700152 public static final int STATE_CANCELED = PrintJobInfoProto.STATE_CANCELED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700153
154 /** The unique print job id. */
Svetoslav2fbd2a72013-09-16 17:53:51 -0700155 private PrintJobId mId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700156
157 /** The human readable print job label. */
Svetoslav269403b2013-08-14 17:31:04 -0700158 private String mLabel;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700159
160 /** The unique id of the printer. */
161 private PrinterId mPrinterId;
162
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700163 /** The name of the printer - internally used */
164 private String mPrinterName;
165
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700166 /** The state of the print job. */
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700167 private int mState;
168
169 /** The id of the app that created the job. */
170 private int mAppId;
171
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700172 /** Optional tag assigned by a print service.*/
173 private String mTag;
174
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700175 /** The wall time when the print job was created. */
176 private long mCreationTime;
177
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700178 /** How many copies to print. */
179 private int mCopies;
180
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700181 /** The pages to print */
182 private PageRange[] mPageRanges;
183
184 /** The print job attributes size. */
185 private PrintAttributes mAttributes;
186
Svetoslav Ganova0027152013-06-25 14:59:53 -0700187 /** Information about the printed document. */
188 private PrintDocumentInfo mDocumentInfo;
189
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800190 /** The progress made on printing this job or -1 if not set. */
191 private float mProgress;
192
193 /** A short string describing the status of this job. */
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700194 private @Nullable CharSequence mStatus;
195
196 /** A string resource describing the status of this job. */
197 private @StringRes int mStatusRes;
198 private @Nullable CharSequence mStatusResAppPackageName;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800199
Svetoslavb4fda132013-10-25 18:57:43 -0700200 /** Advanced printer specific options. */
201 private Bundle mAdvancedOptions;
202
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700203 /** Whether we are trying to cancel this print job. */
204 private boolean mCanceling;
205
Svetoslav Ganova0027152013-06-25 14:59:53 -0700206 /** @hide*/
207 public PrintJobInfo() {
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800208 mProgress = -1;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700209 }
210
211 /** @hide */
212 public PrintJobInfo(PrintJobInfo other) {
213 mId = other.mId;
214 mLabel = other.mLabel;
215 mPrinterId = other.mPrinterId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700216 mPrinterName = other.mPrinterName;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700217 mState = other.mState;
218 mAppId = other.mAppId;
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700219 mTag = other.mTag;
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700220 mCreationTime = other.mCreationTime;
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700221 mCopies = other.mCopies;
222 mPageRanges = other.mPageRanges;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700223 mAttributes = other.mAttributes;
224 mDocumentInfo = other.mDocumentInfo;
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800225 mProgress = other.mProgress;
226 mStatus = other.mStatus;
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700227 mStatusRes = other.mStatusRes;
228 mStatusResAppPackageName = other.mStatusResAppPackageName;
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700229 mCanceling = other.mCanceling;
Svetoslavb4fda132013-10-25 18:57:43 -0700230 mAdvancedOptions = other.mAdvancedOptions;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700231 }
232
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800233 private PrintJobInfo(@NonNull Parcel parcel) {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700234 mId = parcel.readParcelable(null);
Svetoslav269403b2013-08-14 17:31:04 -0700235 mLabel = parcel.readString();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700236 mPrinterId = parcel.readParcelable(null);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700237 mPrinterName = parcel.readString();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700238 mState = parcel.readInt();
239 mAppId = parcel.readInt();
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700240 mTag = parcel.readString();
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700241 mCreationTime = parcel.readLong();
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700242 mCopies = parcel.readInt();
Svetoslavb4fda132013-10-25 18:57:43 -0700243 Parcelable[] parcelables = parcel.readParcelableArray(null);
244 if (parcelables != null) {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700245 mPageRanges = new PageRange[parcelables.length];
246 for (int i = 0; i < parcelables.length; i++) {
247 mPageRanges[i] = (PageRange) parcelables[i];
248 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700249 }
Svetoslavb4fda132013-10-25 18:57:43 -0700250 mAttributes = (PrintAttributes) parcel.readParcelable(null);
251 mDocumentInfo = (PrintDocumentInfo) parcel.readParcelable(null);
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800252 mProgress = parcel.readFloat();
253 mStatus = parcel.readCharSequence();
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700254 mStatusRes = parcel.readInt();
255 mStatusResAppPackageName = parcel.readCharSequence();
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700256 mCanceling = (parcel.readInt() == 1);
Svetoslavb4fda132013-10-25 18:57:43 -0700257 mAdvancedOptions = parcel.readBundle();
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700258
259 if (mAdvancedOptions != null) {
260 Preconditions.checkArgument(!mAdvancedOptions.containsKey(null));
261 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700262 }
263
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700264 /**
265 * Gets the unique print job id.
266 *
267 * @return The id.
268 */
Philip P. Moltmann22f86b42016-01-15 09:49:00 -0800269 public @Nullable PrintJobId getId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700270 return mId;
271 }
272
273 /**
274 * Sets the unique print job id.
275 *
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800276 * @param id The job id.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700277 *
278 * @hide
279 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800280 public void setId(@NonNull PrintJobId id) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700281 this.mId = id;
282 }
283
284 /**
285 * Gets the human readable job label.
286 *
287 * @return The label.
288 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800289 public @NonNull String getLabel() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700290 return mLabel;
291 }
292
293 /**
294 * Sets the human readable job label.
295 *
296 * @param label The label.
297 *
298 * @hide
299 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800300 public void setLabel(@NonNull String label) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700301 mLabel = label;
302 }
303
304 /**
305 * Gets the unique target printer id.
306 *
307 * @return The target printer id.
308 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800309 public @Nullable PrinterId getPrinterId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700310 return mPrinterId;
311 }
312
313 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800314 * Sets the unique target printer id.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700315 *
316 * @param printerId The target printer id.
317 *
318 * @hide
319 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800320 public void setPrinterId(@NonNull PrinterId printerId) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700321 mPrinterId = printerId;
322 }
323
324 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700325 * Gets the name of the target printer.
326 *
327 * @return The printer name.
328 *
329 * @hide
330 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800331 public @Nullable String getPrinterName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700332 return mPrinterName;
333 }
334
335 /**
336 * Sets the name of the target printer.
337 *
338 * @param printerName The printer name.
339 *
340 * @hide
341 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800342 public void setPrinterName(@NonNull String printerName) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700343 mPrinterName = printerName;
344 }
345
346 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700347 * Gets the current job state.
348 *
349 * @return The job state.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700350 *
351 * @see #STATE_CREATED
352 * @see #STATE_QUEUED
353 * @see #STATE_STARTED
354 * @see #STATE_COMPLETED
355 * @see #STATE_BLOCKED
356 * @see #STATE_FAILED
357 * @see #STATE_CANCELED
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700358 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800359 public @State int getState() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700360 return mState;
361 }
362
363 /**
364 * Sets the current job state.
365 *
366 * @param state The job state.
367 *
368 * @hide
369 */
370 public void setState(int state) {
371 mState = state;
372 }
373
374 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800375 * Sets the progress of the print job.
376 *
377 * @param progress the progress of the job
378 *
379 * @hide
380 */
381 public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
382 Preconditions.checkArgumentInRange(progress, 0, 1, "progress");
383
384 mProgress = progress;
385 }
386
387 /**
388 * Sets the status of the print job.
389 *
390 * @param status the status of the job, can be null
391 *
392 * @hide
393 */
394 public void setStatus(@Nullable CharSequence status) {
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700395 mStatusRes = 0;
396 mStatusResAppPackageName = null;
397
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800398 mStatus = status;
399 }
400
401 /**
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700402 * Sets the status of the print job.
403 *
404 * @param status The new status as a string resource
405 * @param appPackageName App package name the resource belongs to
406 *
407 * @hide
408 */
409 public void setStatus(@StringRes int status, @NonNull CharSequence appPackageName) {
410 mStatus = null;
411
412 mStatusRes = status;
413 mStatusResAppPackageName = appPackageName;
414 }
415
416 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700417 * Sets the owning application id.
418 *
419 * @return The owning app id.
420 *
421 * @hide
422 */
423 public int getAppId() {
424 return mAppId;
425 }
426
427 /**
428 * Sets the owning application id.
429 *
430 * @param appId The owning app id.
431 *
432 * @hide
433 */
434 public void setAppId(int appId) {
435 mAppId = appId;
436 }
437
438 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700439 * Gets the optional tag assigned by a print service.
440 *
441 * @return The tag.
Svetoslavb450d0d2013-10-04 16:20:00 -0700442 *
443 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700444 */
445 public String getTag() {
446 return mTag;
447 }
448
449 /**
450 * Sets the optional tag assigned by a print service.
451 *
452 * @param tag The tag.
453 *
454 * @hide
455 */
456 public void setTag(String tag) {
457 mTag = tag;
458 }
459
460 /**
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700461 * Gets the wall time in millisecond when this print job was created.
462 *
463 * @return The creation time in milliseconds.
464 */
465 public long getCreationTime() {
466 return mCreationTime;
467 }
468
469 /**
470 * Sets the wall time in milliseconds when this print job was created.
471 *
472 * @param creationTime The creation time in milliseconds.
473 *
474 * @hide
475 */
476 public void setCreationTime(long creationTime) {
477 if (creationTime < 0) {
478 throw new IllegalArgumentException("creationTime must be non-negative.");
479 }
480 mCreationTime = creationTime;
481 }
482
483 /**
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700484 * Gets the number of copies.
485 *
486 * @return The number of copies or zero if not set.
487 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800488 public @IntRange(from = 0) int getCopies() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700489 return mCopies;
490 }
491
492 /**
493 * Sets the number of copies.
494 *
495 * @param copyCount The number of copies.
496 *
497 * @hide
498 */
499 public void setCopies(int copyCount) {
500 if (copyCount < 1) {
501 throw new IllegalArgumentException("Copies must be more than one.");
502 }
503 mCopies = copyCount;
504 }
505
506 /**
Svetoslav53f57d12013-06-22 00:26:49 -0700507 * Gets the included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700508 *
Svetoslav53f57d12013-06-22 00:26:49 -0700509 * @return The included pages or <code>null</code> if not set.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700510 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800511 public @Nullable PageRange[] getPages() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700512 return mPageRanges;
513 }
514
515 /**
Svetoslav53f57d12013-06-22 00:26:49 -0700516 * Sets the included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700517 *
Svetoslav1c43fce2013-10-16 10:57:48 -0700518 * @param pageRanges The included pages.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700519 *
520 * @hide
521 */
Svetoslav53f57d12013-06-22 00:26:49 -0700522 public void setPages(PageRange[] pageRanges) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700523 mPageRanges = pageRanges;
524 }
525
526 /**
527 * Gets the print job attributes.
528 *
529 * @return The attributes.
530 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800531 public @NonNull PrintAttributes getAttributes() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700532 return mAttributes;
533 }
534
535 /**
536 * Sets the print job attributes.
537 *
538 * @param attributes The attributes.
539 *
540 * @hide
541 */
542 public void setAttributes(PrintAttributes attributes) {
543 mAttributes = attributes;
544 }
545
Svetoslav Ganova0027152013-06-25 14:59:53 -0700546 /**
547 * Gets the info describing the printed document.
548 *
549 * @return The document info.
550 *
551 * @hide
552 */
Mathew Inwood06bf4b72018-07-31 16:36:56 +0100553 @UnsupportedAppUsage
Svetoslav Ganova0027152013-06-25 14:59:53 -0700554 public PrintDocumentInfo getDocumentInfo() {
555 return mDocumentInfo;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700556 }
557
Svetoslav Ganova0027152013-06-25 14:59:53 -0700558 /**
559 * Sets the info describing the printed document.
560 *
561 * @param info The document info.
562 *
563 * @hide
564 */
565 public void setDocumentInfo(PrintDocumentInfo info) {
566 mDocumentInfo = info;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700567 }
568
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700569 /**
570 * Gets whether this print is being cancelled.
571 *
572 * @return True if the print job is being cancelled.
573 *
574 * @hide
575 */
576 public boolean isCancelling() {
577 return mCanceling;
578 }
579
580 /**
581 * Sets whether this print is being cancelled.
582 *
583 * @param cancelling True if the print job is being cancelled.
584 *
585 * @hide
586 */
587 public void setCancelling(boolean cancelling) {
588 mCanceling = cancelling;
589 }
590
Svetoslavb4fda132013-10-25 18:57:43 -0700591 /**
Philip P. Moltmann94620322018-05-15 12:59:38 -0700592 * If the print job is actively processed, i.e. the device needs to stay on.
593 *
594 * @hide
595 */
596 public boolean shouldStayAwake() {
597 return mCanceling || mState == STATE_STARTED || mState == STATE_QUEUED;
598 }
599
600 /**
Svetoslavb4fda132013-10-25 18:57:43 -0700601 * Gets whether this job has a given advanced (printer specific) print
602 * option.
603 *
604 * @param key The option key.
605 * @return Whether the option is present.
Svetoslavb4fda132013-10-25 18:57:43 -0700606 */
607 public boolean hasAdvancedOption(String key) {
608 return mAdvancedOptions != null && mAdvancedOptions.containsKey(key);
609 }
610
611 /**
612 * Gets the value of an advanced (printer specific) print option.
613 *
614 * @param key The option key.
615 * @return The option value.
Svetoslavb4fda132013-10-25 18:57:43 -0700616 */
617 public String getAdvancedStringOption(String key) {
618 if (mAdvancedOptions != null) {
619 return mAdvancedOptions.getString(key);
620 }
621 return null;
622 }
623
624 /**
625 * Gets the value of an advanced (printer specific) print option.
626 *
627 * @param key The option key.
628 * @return The option value.
Svetoslavb4fda132013-10-25 18:57:43 -0700629 */
630 public int getAdvancedIntOption(String key) {
631 if (mAdvancedOptions != null) {
632 return mAdvancedOptions.getInt(key);
633 }
634 return 0;
635 }
636
637 /**
638 * Gets the advanced options.
639 *
640 * @return The advanced options.
641 *
642 * @hide
643 */
Mathew Inwood06bf4b72018-07-31 16:36:56 +0100644 @UnsupportedAppUsage
Svetoslavb4fda132013-10-25 18:57:43 -0700645 public Bundle getAdvancedOptions() {
646 return mAdvancedOptions;
647 }
648
649 /**
650 * Sets the advanced options.
651 *
652 * @param options The advanced options.
653 *
654 * @hide
655 */
656 public void setAdvancedOptions(Bundle options) {
657 mAdvancedOptions = options;
658 }
659
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700660 @Override
661 public int describeContents() {
662 return 0;
663 }
664
665 @Override
666 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700667 parcel.writeParcelable(mId, flags);
Svetoslav269403b2013-08-14 17:31:04 -0700668 parcel.writeString(mLabel);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700669 parcel.writeParcelable(mPrinterId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700670 parcel.writeString(mPrinterName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700671 parcel.writeInt(mState);
672 parcel.writeInt(mAppId);
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700673 parcel.writeString(mTag);
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700674 parcel.writeLong(mCreationTime);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700675 parcel.writeInt(mCopies);
Svetoslavb4fda132013-10-25 18:57:43 -0700676 parcel.writeParcelableArray(mPageRanges, flags);
677 parcel.writeParcelable(mAttributes, flags);
678 parcel.writeParcelable(mDocumentInfo, 0);
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800679 parcel.writeFloat(mProgress);
680 parcel.writeCharSequence(mStatus);
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700681 parcel.writeInt(mStatusRes);
682 parcel.writeCharSequence(mStatusResAppPackageName);
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700683 parcel.writeInt(mCanceling ? 1 : 0);
Svetoslavb4fda132013-10-25 18:57:43 -0700684 parcel.writeBundle(mAdvancedOptions);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700685 }
686
687 @Override
688 public String toString() {
689 StringBuilder builder = new StringBuilder();
690 builder.append("PrintJobInfo{");
691 builder.append("label: ").append(mLabel);
692 builder.append(", id: ").append(mId);
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700693 builder.append(", state: ").append(stateToString(mState));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700694 builder.append(", printer: " + mPrinterId);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700695 builder.append(", tag: ").append(mTag);
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700696 builder.append(", creationTime: " + mCreationTime);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700697 builder.append(", copies: ").append(mCopies);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700698 builder.append(", attributes: " + (mAttributes != null
699 ? mAttributes.toString() : null));
700 builder.append(", documentInfo: " + (mDocumentInfo != null
701 ? mDocumentInfo.toString() : null));
Svetoslav Ganova18661d2013-10-09 22:55:49 -0700702 builder.append(", cancelling: " + mCanceling);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700703 builder.append(", pages: " + (mPageRanges != null
704 ? Arrays.toString(mPageRanges) : null));
Svetoslavb4fda132013-10-25 18:57:43 -0700705 builder.append(", hasAdvancedOptions: " + (mAdvancedOptions != null));
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800706 builder.append(", progress: " + mProgress);
707 builder.append(", status: " + (mStatus != null
708 ? mStatus.toString() : null));
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700709 builder.append(", statusRes: " + mStatusRes);
710 builder.append(", statusResAppPackageName: " + (mStatusResAppPackageName != null
711 ? mStatusResAppPackageName.toString() : null));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700712 builder.append("}");
713 return builder.toString();
714 }
715
716 /** @hide */
717 public static String stateToString(int state) {
718 switch (state) {
719 case STATE_CREATED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700720 return "STATE_CREATED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700721 }
722 case STATE_QUEUED: {
723 return "STATE_QUEUED";
724 }
725 case STATE_STARTED: {
726 return "STATE_STARTED";
727 }
Svetoslav Ganovd91cb3e2013-10-12 15:44:42 -0700728 case STATE_BLOCKED: {
729 return "STATE_BLOCKED";
730 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700731 case STATE_FAILED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700732 return "STATE_FAILED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700733 }
734 case STATE_COMPLETED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700735 return "STATE_COMPLETED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700736 }
737 case STATE_CANCELED: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700738 return "STATE_CANCELED";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700739 }
740 default: {
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700741 return "STATE_UNKNOWN";
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700742 }
743 }
744 }
745
Svetoslav1c43fce2013-10-16 10:57:48 -0700746 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800747 * Get the progress that has been made printing this job.
748 *
749 * @return the print progress or -1 if not set
750 * @hide
751 */
752 @TestApi
753 public float getProgress() {
754 return mProgress;
755 }
756
757 /**
758 * Get the status of this job.
759 *
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700760 * @param pm Package manager used to resolve the string
761 *
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800762 * @return the status of this job or null if not set
763 * @hide
764 */
765 @TestApi
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700766 public @Nullable CharSequence getStatus(@NonNull PackageManager pm) {
767 if (mStatusRes == 0) {
768 return mStatus;
769 } else {
770 try {
771 return pm.getResourcesForApplication(mStatusResAppPackageName.toString())
772 .getString(mStatusRes);
773 } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
774 return null;
775 }
776 }
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800777 }
778
779 /**
Svetoslav1c43fce2013-10-16 10:57:48 -0700780 * Builder for creating a {@link PrintJobInfo}.
781 */
782 public static final class Builder {
783 private final PrintJobInfo mPrototype;
784
785 /**
786 * Constructor.
787 *
788 * @param prototype Prototype to use as a starting point.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700789 * Can be <code>null</code>.
Svetoslav1c43fce2013-10-16 10:57:48 -0700790 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800791 public Builder(@Nullable PrintJobInfo prototype) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700792 mPrototype = (prototype != null)
793 ? new PrintJobInfo(prototype)
794 : new PrintJobInfo();
795 }
796
797 /**
798 * Sets the number of copies.
799 *
800 * @param copies The number of copies.
801 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800802 public void setCopies(@IntRange(from = 1) int copies) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700803 mPrototype.mCopies = copies;
804 }
805
806 /**
807 * Sets the print job attributes.
808 *
809 * @param attributes The attributes.
810 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800811 public void setAttributes(@NonNull PrintAttributes attributes) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700812 mPrototype.mAttributes = attributes;
813 }
814
815 /**
816 * Sets the included pages.
817 *
818 * @param pages The included pages.
819 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800820 public void setPages(@NonNull PageRange[] pages) {
Svetoslav1c43fce2013-10-16 10:57:48 -0700821 mPrototype.mPageRanges = pages;
822 }
823
824 /**
Philip P. Moltmannb3078c22015-11-23 16:12:39 -0800825 * Sets the progress of the print job.
826 *
827 * @param progress the progress of the job
828 * @hide
829 */
830 public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
831 Preconditions.checkArgumentInRange(progress, 0, 1, "progress");
832
833 mPrototype.mProgress = progress;
834 }
835
836 /**
837 * Sets the status of the print job.
838 *
839 * @param status the status of the job, can be null
840 * @hide
841 */
842 public void setStatus(@Nullable CharSequence status) {
843 mPrototype.mStatus = status;
844 }
845
846 /**
Svetoslav1c43fce2013-10-16 10:57:48 -0700847 * Puts an advanced (printer specific) option.
848 *
849 * @param key The option key.
850 * @param value The option value.
851 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800852 public void putAdvancedOption(@NonNull String key, @Nullable String value) {
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700853 Preconditions.checkNotNull(key, "key cannot be null");
854
Svetoslavb4fda132013-10-25 18:57:43 -0700855 if (mPrototype.mAdvancedOptions == null) {
856 mPrototype.mAdvancedOptions = new Bundle();
857 }
858 mPrototype.mAdvancedOptions.putString(key, value);
Svetoslav1c43fce2013-10-16 10:57:48 -0700859 }
860
861 /**
862 * Puts an advanced (printer specific) option.
863 *
864 * @param key The option key.
865 * @param value The option value.
866 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800867 public void putAdvancedOption(@NonNull String key, int value) {
Svetoslavb4fda132013-10-25 18:57:43 -0700868 if (mPrototype.mAdvancedOptions == null) {
869 mPrototype.mAdvancedOptions = new Bundle();
870 }
871 mPrototype.mAdvancedOptions.putInt(key, value);
Svetoslav1c43fce2013-10-16 10:57:48 -0700872 }
873
874 /**
875 * Creates a new {@link PrintJobInfo} instance.
876 *
877 * @return The new instance.
878 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800879 public @NonNull PrintJobInfo build() {
Svetoslav1c43fce2013-10-16 10:57:48 -0700880 return mPrototype;
881 }
882 }
883
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700884 public static final @android.annotation.NonNull Parcelable.Creator<PrintJobInfo> CREATOR =
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700885 new Creator<PrintJobInfo>() {
886 @Override
887 public PrintJobInfo createFromParcel(Parcel parcel) {
888 return new PrintJobInfo(parcel);
889 }
890
891 @Override
892 public PrintJobInfo[] newArray(int size) {
893 return new PrintJobInfo[size];
894 }
895 };
896}