PrintJobInfo.java revision 54adee86af495311fc9ef62d65e035605043413f
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    /** Optional tag assigned by a print service.*/
133    private String mTag;
134
135    /** The wall time when the print job was created. */
136    private long mCreationTime;
137
138    /** How many copies to print. */
139    private int mCopies;
140
141    /** Reason for the print job being in its current state. */
142    private String mStateReason;
143
144    /** The pages to print */
145    private PageRange[] mPageRanges;
146
147    /** The print job attributes size. */
148    private PrintAttributes mAttributes;
149
150    /** Information about the printed document. */
151    private PrintDocumentInfo mDocumentInfo;
152
153    /** Whether we are trying to cancel this print job. */
154    private boolean mCanceling;
155
156    /** @hide*/
157    public PrintJobInfo() {
158        /* do nothing */
159    }
160
161    /** @hide */
162    public PrintJobInfo(PrintJobInfo other) {
163        mId = other.mId;
164        mLabel = other.mLabel;
165        mPrinterId = other.mPrinterId;
166        mPrinterName = other.mPrinterName;
167        mState = other.mState;
168        mAppId = other.mAppId;
169        mTag = other.mTag;
170        mCreationTime = other.mCreationTime;
171        mCopies = other.mCopies;
172        mStateReason = other.mStateReason;
173        mPageRanges = other.mPageRanges;
174        mAttributes = other.mAttributes;
175        mDocumentInfo = other.mDocumentInfo;
176        mCanceling = other.mCanceling;
177    }
178
179    private PrintJobInfo(Parcel parcel) {
180        mId = parcel.readParcelable(null);
181        mLabel = parcel.readString();
182        mPrinterId = parcel.readParcelable(null);
183        mPrinterName = parcel.readString();
184        mState = parcel.readInt();
185        mAppId = parcel.readInt();
186        mTag = parcel.readString();
187        mCreationTime = parcel.readLong();
188        mCopies = parcel.readInt();
189        mStateReason = parcel.readString();
190        if (parcel.readInt() == 1) {
191            Parcelable[] parcelables = parcel.readParcelableArray(null);
192            mPageRanges = new PageRange[parcelables.length];
193            for (int i = 0; i < parcelables.length; i++) {
194                mPageRanges[i] = (PageRange) parcelables[i];
195            }
196        }
197        if (parcel.readInt() == 1) {
198            mAttributes = PrintAttributes.CREATOR.createFromParcel(parcel);
199        }
200        if (parcel.readInt() == 1) {
201            mDocumentInfo = PrintDocumentInfo.CREATOR.createFromParcel(parcel);
202        }
203        mCanceling = (parcel.readInt() == 1);
204    }
205
206    /**
207     * Gets the unique print job id.
208     *
209     * @return The id.
210     */
211    public PrintJobId getId() {
212        return mId;
213    }
214
215    /**
216     * Sets the unique print job id.
217     *
218     * @param The job id.
219     *
220     * @hide
221     */
222    public void setId(PrintJobId id) {
223        this.mId = id;
224    }
225
226    /**
227     * Gets the human readable job label.
228     *
229     * @return The label.
230     */
231    public String getLabel() {
232        return mLabel;
233    }
234
235    /**
236     * Sets the human readable job label.
237     *
238     * @param label The label.
239     *
240     * @hide
241     */
242    public void setLabel(String label) {
243        mLabel = label;
244    }
245
246    /**
247     * Gets the unique target printer id.
248     *
249     * @return The target printer id.
250     */
251    public PrinterId getPrinterId() {
252        return mPrinterId;
253    }
254
255    /**
256     * Sets the unique target pritner id.
257     *
258     * @param printerId The target printer id.
259     *
260     * @hide
261     */
262    public void setPrinterId(PrinterId printerId) {
263        mPrinterId = printerId;
264    }
265
266    /**
267     * Gets the name of the target printer.
268     *
269     * @return The printer name.
270     *
271     * @hide
272     */
273    public String getPrinterName() {
274        return mPrinterName;
275    }
276
277    /**
278     * Sets the name of the target printer.
279     *
280     * @param printerName The printer name.
281     *
282     * @hide
283     */
284    public void setPrinterName(String printerName) {
285        mPrinterName = printerName;
286    }
287
288    /**
289     * Gets the current job state.
290     *
291     * @return The job state.
292     */
293    public int getState() {
294        return mState;
295    }
296
297    /**
298     * Sets the current job state.
299     *
300     * @param state The job state.
301     *
302     * @hide
303     */
304    public void setState(int state) {
305        mState = state;
306    }
307
308    /**
309     * Sets the owning application id.
310     *
311     * @return The owning app id.
312     *
313     * @hide
314     */
315    public int getAppId() {
316        return mAppId;
317    }
318
319    /**
320     * Sets the owning application id.
321     *
322     * @param appId The owning app id.
323     *
324     * @hide
325     */
326    public void setAppId(int appId) {
327        mAppId = appId;
328    }
329
330    /**
331     * Gets the optional tag assigned by a print service.
332     *
333     * @return The tag.
334     *
335     * @hide
336     */
337    public String getTag() {
338        return mTag;
339    }
340
341    /**
342     * Sets the optional tag assigned by a print service.
343     *
344     * @param tag The tag.
345     *
346     * @hide
347     */
348    public void setTag(String tag) {
349        mTag = tag;
350    }
351
352    /**
353     * Gets the wall time in millisecond when this print job was created.
354     *
355     * @return The creation time in milliseconds.
356     */
357    public long getCreationTime() {
358        return mCreationTime;
359    }
360
361    /**
362     * Sets the wall time in milliseconds when this print job was created.
363     *
364     * @param creationTime The creation time in milliseconds.
365     *
366     * @hide
367     */
368    public void setCreationTime(long creationTime) {
369        if (creationTime < 0) {
370            throw new IllegalArgumentException("creationTime must be non-negative.");
371        }
372        mCreationTime = creationTime;
373    }
374
375    /**
376     * Gets the number of copies.
377     *
378     * @return The number of copies or zero if not set.
379     */
380    public int getCopies() {
381        return mCopies;
382    }
383
384    /**
385     * Sets the number of copies.
386     *
387     * @param copyCount The number of copies.
388     *
389     * @hide
390     */
391    public void setCopies(int copyCount) {
392        if (copyCount < 1) {
393            throw new IllegalArgumentException("Copies must be more than one.");
394        }
395        mCopies = copyCount;
396    }
397
398    /**
399     * Gets the reason for the print job being in the current state.
400     *
401     * @return The reason, or null if there is no reason or the
402     * reason is unknown.
403     *
404     * @hide
405     */
406    public String getStateReason() {
407        return mStateReason;
408    }
409
410    /**
411     * Sets the reason for the print job being in the current state.
412     *
413     * @param stateReason The reason, or null if there is no reason
414     * or the reason is unknown.
415     *
416     * @hide
417     */
418    public void setStateReason(String stateReason) {
419        mStateReason = stateReason;
420    }
421
422    /**
423     * Gets the included pages.
424     *
425     * @return The included pages or <code>null</code> if not set.
426     */
427    public PageRange[] getPages() {
428        return mPageRanges;
429    }
430
431    /**
432     * Sets the included pages.
433     *
434     * @return The included pages.
435     *
436     * @hide
437     */
438    public void setPages(PageRange[] pageRanges) {
439        mPageRanges = pageRanges;
440    }
441
442    /**
443     * Gets the print job attributes.
444     *
445     * @return The attributes.
446     */
447    public PrintAttributes getAttributes() {
448        return mAttributes;
449    }
450
451    /**
452     * Sets the print job attributes.
453     *
454     * @param attributes The attributes.
455     *
456     * @hide
457     */
458    public void setAttributes(PrintAttributes attributes) {
459        mAttributes = attributes;
460    }
461
462    /**
463     * Gets the info describing the printed document.
464     *
465     * @return The document info.
466     *
467     * @hide
468     */
469    public PrintDocumentInfo getDocumentInfo() {
470        return mDocumentInfo;
471    }
472
473    /**
474     * Sets the info describing the printed document.
475     *
476     * @param info The document info.
477     *
478     * @hide
479     */
480    public void setDocumentInfo(PrintDocumentInfo info) {
481        mDocumentInfo = info;
482    }
483
484    /**
485     * Gets whether this print is being cancelled.
486     *
487     * @return True if the print job is being cancelled.
488     *
489     * @hide
490     */
491    public boolean isCancelling() {
492        return mCanceling;
493    }
494
495    /**
496     * Sets whether this print is being cancelled.
497     *
498     * @param cancelling True if the print job is being cancelled.
499     *
500     * @hide
501     */
502    public void setCancelling(boolean cancelling) {
503        mCanceling = cancelling;
504    }
505
506    @Override
507    public int describeContents() {
508        return 0;
509    }
510
511    @Override
512    public void writeToParcel(Parcel parcel, int flags) {
513        parcel.writeParcelable(mId, flags);
514        parcel.writeString(mLabel);
515        parcel.writeParcelable(mPrinterId, flags);
516        parcel.writeString(mPrinterName);
517        parcel.writeInt(mState);
518        parcel.writeInt(mAppId);
519        parcel.writeString(mTag);
520        parcel.writeLong(mCreationTime);
521        parcel.writeInt(mCopies);
522        parcel.writeString(mStateReason);
523        if (mPageRanges != null) {
524            parcel.writeInt(1);
525            parcel.writeParcelableArray(mPageRanges, flags);
526        } else {
527            parcel.writeInt(0);
528        }
529        if (mAttributes != null) {
530            parcel.writeInt(1);
531            mAttributes.writeToParcel(parcel, flags);
532        } else {
533            parcel.writeInt(0);
534        }
535        if (mDocumentInfo != null) {
536            parcel.writeInt(1);
537            mDocumentInfo.writeToParcel(parcel, flags);
538        } else {
539            parcel.writeInt(0);
540        }
541        parcel.writeInt(mCanceling ? 1 : 0);
542    }
543
544    @Override
545    public String toString() {
546        StringBuilder builder = new StringBuilder();
547        builder.append("PrintJobInfo{");
548        builder.append("label: ").append(mLabel);
549        builder.append(", id: ").append(mId);
550        builder.append(", status: ").append(stateToString(mState));
551        builder.append(", printer: " + mPrinterId);
552        builder.append(", tag: ").append(mTag);
553        builder.append(", creationTime: " + mCreationTime);
554        builder.append(", copies: ").append(mCopies);
555        builder.append(", attributes: " + (mAttributes != null
556                ? mAttributes.toString() : null));
557        builder.append(", documentInfo: " + (mDocumentInfo != null
558                ? mDocumentInfo.toString() : null));
559        builder.append(", cancelling: " + mCanceling);
560        builder.append(", pages: " + (mPageRanges != null
561                ? Arrays.toString(mPageRanges) : null));
562        builder.append("}");
563        return builder.toString();
564    }
565
566    /** @hide */
567    public static String stateToString(int state) {
568        switch (state) {
569            case STATE_CREATED: {
570                return "STATE_CREATED";
571            }
572            case STATE_QUEUED: {
573                return "STATE_QUEUED";
574            }
575            case STATE_STARTED: {
576                return "STATE_STARTED";
577            }
578            case STATE_FAILED: {
579                return "STATE_FAILED";
580            }
581            case STATE_COMPLETED: {
582                return "STATE_COMPLETED";
583            }
584            case STATE_CANCELED: {
585                return "STATE_CANCELED";
586            }
587            default: {
588                return "STATE_UNKNOWN";
589            }
590        }
591    }
592
593    public static final Parcelable.Creator<PrintJobInfo> CREATOR =
594            new Creator<PrintJobInfo>() {
595        @Override
596        public PrintJobInfo createFromParcel(Parcel parcel) {
597            return new PrintJobInfo(parcel);
598        }
599
600        @Override
601        public PrintJobInfo[] newArray(int size) {
602            return new PrintJobInfo[size];
603        }
604    };
605}
606