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