PrintJobInfo.java revision 88d199130d44c6bacb383a7757e782cf97483c68
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
22/**
23 * This class represents the description of a print job.
24 */
25public final class PrintJobInfo implements Parcelable {
26
27    /** Undefined print job id. */
28    public static final int PRINT_JOB_ID_UNDEFINED = -1;
29
30    /**
31     * Constant for matching any print job state.
32     *
33     * @hide
34     */
35    public static final int STATE_ANY = -1;
36
37    /**
38     * Constant for matching any print job state.
39     *
40     * @hide
41     */
42    public static final int STATE_ANY_VISIBLE_TO_CLIENTS = -2;
43
44    /**
45     * Print job state: The print job is being created but not yet
46     * ready to be printed.
47     * <p>
48     * Next valid states: {@link #STATE_QUEUED}
49     * </p>
50     *
51     * @hide
52     */
53    public static final int STATE_CREATED = 1;
54
55    /**
56     * Print job status: The print jobs is created, it is ready
57     * to be printed and should be processed.
58     * <p>
59     * Next valid states: {@link #STATE_STARTED}, {@link #STATE_FAILED},
60     * {@link #STATE_CANCELED}
61     * </p>
62     */
63    public static final int STATE_QUEUED = 2;
64
65    /**
66     * Print job status: The print job is being printed.
67     * <p>
68     * Next valid states: {@link #STATE_COMPLETED}, {@link #STATE_FAILED},
69     * {@link #STATE_CANCELED}
70     * </p>
71     */
72    public static final int STATE_STARTED = 3;
73
74    /**
75     * Print job status: The print job was successfully printed.
76     * This is a terminal state.
77     * <p>
78     * Next valid states: None
79     * </p>
80     */
81    public static final int STATE_COMPLETED = 4;
82
83    /**
84     * Print job status: The print job was printing but printing failed.
85     * This is a terminal state.
86     * <p>
87     * Next valid states: None
88     * </p>
89     */
90    public static final int STATE_FAILED = 5;
91
92    /**
93     * Print job status: The print job was canceled.
94     * This is a terminal state.
95     * <p>
96     * Next valid states: None
97     * </p>
98     */
99    public static final int STATE_CANCELED = 6;
100
101    /** The unique print job id. */
102    private int mId;
103
104    /** The human readable print job label. */
105    private CharSequence mLabel;
106
107    /** The unique id of the printer. */
108    private PrinterId mPrinterId;
109
110    /** The status of the print job. */
111    private int mState;
112
113    /** The id of the app that created the job. */
114    private int mAppId;
115
116    /** The id of the user that created the job. */
117    private int mUserId;
118
119    /** Optional tag assigned by a print service.*/
120    private String mTag;
121
122    /** The pages to print */
123    private PageRange[] mPageRanges;
124
125    /** The print job attributes size. */
126    private PrintAttributes mAttributes;
127
128    /** Information about the printed document. */
129    private PrintDocumentInfo mDocumentInfo;
130
131    /** @hide*/
132    public PrintJobInfo() {
133        /* do nothing */
134    }
135
136    /** @hide */
137    public PrintJobInfo(PrintJobInfo other) {
138        mId = other.mId;
139        mLabel = other.mLabel;
140        mPrinterId = other.mPrinterId;
141        mState = other.mState;
142        mAppId = other.mAppId;
143        mUserId = other.mUserId;
144        mTag = other.mTag;
145        mAttributes = other.mAttributes;
146        mDocumentInfo = other.mDocumentInfo;
147    }
148
149    private PrintJobInfo(Parcel parcel) {
150        mId = parcel.readInt();
151        mLabel = parcel.readCharSequence();
152        mPrinterId = parcel.readParcelable(null);
153        mState = parcel.readInt();
154        mAppId = parcel.readInt();
155        mUserId = parcel.readInt();
156        mTag = parcel.readString();
157        if (parcel.readInt() == 1) {
158            mPageRanges = (PageRange[]) parcel.readParcelableArray(null);
159        }
160        if (parcel.readInt() == 1) {
161            mAttributes = PrintAttributes.CREATOR.createFromParcel(parcel);
162        }
163        if (parcel.readInt() == 1) {
164            mDocumentInfo = PrintDocumentInfo.CREATOR.createFromParcel(parcel);
165        }
166    }
167
168    /**
169     * Gets the unique print job id.
170     *
171     * @return The id.
172     */
173    public int getId() {
174        return mId;
175    }
176
177    /**
178     * Sets the unique print job id.
179     *
180     * @param The job id.
181     *
182     * @hide
183     */
184    public void setId(int id) {
185        this.mId = id;
186    }
187
188    /**
189     * Gets the human readable job label.
190     *
191     * @return The label.
192     */
193    public CharSequence getLabel() {
194        return mLabel;
195    }
196
197    /**
198     * Sets the human readable job label.
199     *
200     * @param label The label.
201     *
202     * @hide
203     */
204    public void setLabel(CharSequence label) {
205        mLabel = label;
206    }
207
208    /**
209     * Gets the unique target printer id.
210     *
211     * @return The target printer id.
212     */
213    public PrinterId getPrinterId() {
214        return mPrinterId;
215    }
216
217    /**
218     * Sets the unique target pritner id.
219     *
220     * @param printerId The target printer id.
221     *
222     * @hide
223     */
224    public void setPrinterId(PrinterId printerId) {
225        mPrinterId = printerId;
226    }
227
228    /**
229     * Gets the current job state.
230     *
231     * @return The job state.
232     */
233    public int getState() {
234        return mState;
235    }
236
237    /**
238     * Sets the current job state.
239     *
240     * @param state The job state.
241     *
242     * @hide
243     */
244    public void setState(int state) {
245        mState = state;
246    }
247
248    /**
249     * Sets the owning application id.
250     *
251     * @return The owning app id.
252     *
253     * @hide
254     */
255    public int getAppId() {
256        return mAppId;
257    }
258
259    /**
260     * Sets the owning application id.
261     *
262     * @param appId The owning app id.
263     *
264     * @hide
265     */
266    public void setAppId(int appId) {
267        mAppId = appId;
268    }
269
270    /**
271     * Gets the owning user id.
272     *
273     * @return The user id.
274     *
275     * @hide
276     */
277    public int getUserId() {
278        return mUserId;
279    }
280
281    /**
282     * Sets the owning user id.
283     *
284     * @param userId The user id.
285     *
286     * @hide
287     */
288    public void setUserId(int userId) {
289        mUserId = userId;
290    }
291
292    /**
293     * Gets the optional tag assigned by a print service.
294     *
295     * @return The tag.
296     */
297    public String getTag() {
298        return mTag;
299    }
300
301    /**
302     * Sets the optional tag assigned by a print service.
303     *
304     * @param tag The tag.
305     *
306     * @hide
307     */
308    public void setTag(String tag) {
309        mTag = tag;
310    }
311
312    /**
313     * Gets the included pages.
314     *
315     * @return The included pages or <code>null</code> if not set.
316     */
317    public PageRange[] getPages() {
318        return mPageRanges;
319    }
320
321    /**
322     * Sets the included pages.
323     *
324     * @return The included pages.
325     *
326     * @hide
327     */
328    public void setPages(PageRange[] pageRanges) {
329        mPageRanges = pageRanges;
330    }
331
332    /**
333     * Gets the print job attributes.
334     *
335     * @return The attributes.
336     */
337    public PrintAttributes getAttributes() {
338        return mAttributes;
339    }
340
341    /**
342     * Sets the print job attributes.
343     *
344     * @param attributes The attributes.
345     *
346     * @hide
347     */
348    public void setAttributes(PrintAttributes attributes) {
349        mAttributes = attributes;
350    }
351
352    /**
353     * Gets the info describing the printed document.
354     *
355     * @return The document info.
356     *
357     * @hide
358     */
359    public PrintDocumentInfo getDocumentInfo() {
360        return mDocumentInfo;
361    }
362
363    /**
364     * Sets the info describing the printed document.
365     *
366     * @param info The document info.
367     *
368     * @hide
369     */
370    public void setDocumentInfo(PrintDocumentInfo info) {
371        mDocumentInfo = info;
372    }
373
374    @Override
375    public int describeContents() {
376        return 0;
377    }
378
379    @Override
380    public void writeToParcel(Parcel parcel, int flags) {
381        parcel.writeInt(mId);
382        parcel.writeCharSequence(mLabel);
383        parcel.writeParcelable(mPrinterId, flags);
384        parcel.writeInt(mState);
385        parcel.writeInt(mAppId);
386        parcel.writeInt(mUserId);
387        parcel.writeString(mTag);
388        if (mPageRanges != null) {
389            parcel.writeInt(1);
390            parcel.writeParcelableArray(mPageRanges, flags);
391        } else {
392            parcel.writeInt(0);
393        }
394        if (mAttributes != null) {
395            parcel.writeInt(1);
396            mAttributes.writeToParcel(parcel, flags);
397        } else {
398            parcel.writeInt(0);
399        }
400        if (mDocumentInfo != null) {
401            parcel.writeInt(1);
402            mDocumentInfo.writeToParcel(parcel, flags);
403        } else {
404            parcel.writeInt(0);
405        }
406    }
407
408    @Override
409    public String toString() {
410        StringBuilder builder = new StringBuilder();
411        builder.append("PrintJobInfo{");
412        builder.append("label: ").append(mLabel);
413        builder.append(", id: ").append(mId);
414        builder.append(", status: ").append(stateToString(mState));
415        builder.append(", printer: " + mPrinterId);
416        builder.append(", attributes: " + (mAttributes != null
417                ? mAttributes.toString() : null));
418        builder.append(", documentInfo: " + (mDocumentInfo != null
419                ? mDocumentInfo.toString() : null));
420        builder.append("}");
421        return builder.toString();
422    }
423
424    /** @hide */
425    public static String stateToString(int state) {
426        switch (state) {
427            case STATE_CREATED: {
428                return "STATUS_CREATED";
429            }
430            case STATE_QUEUED: {
431                return "STATE_QUEUED";
432            }
433            case STATE_STARTED: {
434                return "STATE_STARTED";
435            }
436            case STATE_FAILED: {
437                return "STATUS_FAILED";
438            }
439            case STATE_COMPLETED: {
440                return "STATUS_COMPLETED";
441            }
442            case STATE_CANCELED: {
443                return "STATUS_CANCELED";
444            }
445            default: {
446                return "STATUS_UNKNOWN";
447            }
448        }
449    }
450
451
452    public static final Parcelable.Creator<PrintJobInfo> CREATOR =
453            new Creator<PrintJobInfo>() {
454        @Override
455        public PrintJobInfo createFromParcel(Parcel parcel) {
456            return new PrintJobInfo(parcel);
457        }
458
459        @Override
460        public PrintJobInfo[] newArray(int size) {
461            return new PrintJobInfo[size];
462        }
463    };
464}
465