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