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