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