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