PrintJob.java revision 704697b6197262678e930daa831a1916ddee4dcf
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
19/**
20 * This class represents a print job from the perspective of
21 * an application.
22 */
23public final class PrintJob {
24
25    private final PrintManager mPrintManager;
26
27    private PrintJobInfo mCachedInfo;
28
29    PrintJob(PrintJobInfo info, PrintManager printManager) {
30        mCachedInfo = info;
31        mPrintManager = printManager;
32    }
33
34    /**
35     * Gets the unique print job id.
36     *
37     * @return The id.
38     */
39    public PrintJobId getId() {
40        return mCachedInfo.getId();
41    }
42
43    /**
44     * Gets the {@link PrintJobInfo} that describes this job.
45     * <p>
46     * <strong>Node:</strong>The returned info object is a snapshot of the
47     * current print job state. Every call to this method returns a fresh
48     * info object that reflects the current print job state.
49     * </p>
50     *
51     * @return The print job info.
52     */
53    public PrintJobInfo getInfo() {
54        if (isInImmutableState()) {
55            return mCachedInfo;
56        }
57        PrintJobInfo info = mPrintManager.getPrintJobInfo(mCachedInfo.getId());
58        if (info != null) {
59            mCachedInfo = info;
60        }
61        return mCachedInfo;
62    }
63
64    /**
65     * Cancels this print job. You can request cancellation of a
66     * queued, started, blocked, or failed print job.
67     *
68     * @see #isQueued()
69     * @see #isStarted()
70     * @see #isBlocked()
71     * @see #isFailed()
72     */
73    public void cancel() {
74        final int state = getInfo().getState();
75        if (state == PrintJobInfo.STATE_QUEUED
76                || state == PrintJobInfo.STATE_STARTED
77                || state == PrintJobInfo.STATE_BLOCKED
78                || state == PrintJobInfo.STATE_FAILED) {
79            mPrintManager.cancelPrintJob(mCachedInfo.getId());
80        }
81    }
82
83    /**
84     * Restarts this print job. You can request restart of a failed
85     * print job.
86     *
87     * @see #isFailed()
88     */
89    public void restart() {
90        if (isFailed()) {
91            mPrintManager.restartPrintJob(mCachedInfo.getId());
92        }
93    }
94
95    /**
96     * Gets whether this print job is queued. Such a print job is
97     * ready to be printed. You can request a cancellation via
98     * {@link #cancel()}.
99     *
100     * @return Whether the print job is queued.
101     *
102     * @see #cancel()
103     */
104    public boolean isQueued() {
105        return getInfo().getState() == PrintJobInfo.STATE_QUEUED;
106    }
107
108    /**
109     * Gets whether this print job is started. Such a print job is
110     * being printed. You can request a cancellation via
111     * {@link #cancel()}.
112     *
113     * @return Whether the print job is started.
114     *
115     * @see #cancel()
116     */
117    public boolean isStarted() {
118        return getInfo().getState() == PrintJobInfo.STATE_STARTED;
119    }
120
121    /**
122     * Gets whether this print job is blocked. Such a print job is halted
123     * due to an abnormal condition. You can request a cancellation via
124     * {@link #cancel()}.
125     *
126     * @return Whether the print job is blocked.
127     *
128     * @see #cancel()
129     */
130    public boolean isBlocked() {
131        return getInfo().getState() == PrintJobInfo.STATE_BLOCKED;
132    }
133
134    /**
135     * Gets whether this print job is completed. Such a print job
136     * is successfully printed. You can neither cancel nor restart
137     * such a print job.
138     *
139     * @return Whether the print job is completed.
140     */
141    public boolean isCompleted() {
142        return getInfo().getState() == PrintJobInfo.STATE_COMPLETED;
143    }
144
145    /**
146     * Gets whether this print job is failed. Such a print job is
147     * not successfully printed due to an error. You can request
148     * a restart via {@link #restart()}.
149     *
150     * @return Whether the print job is failed.
151     *
152     * @see #restart()
153     */
154    public boolean isFailed() {
155        return getInfo().getState() == PrintJobInfo.STATE_FAILED;
156    }
157
158    /**
159     * Gets whether this print job is cancelled. Such a print job was
160     * cancelled as a result of a user request. This is a final state.
161     * You cannot restart such a print job.
162     *
163     * @return Whether the print job is cancelled.
164     */
165    public boolean isCancelled() {
166        return getInfo().getState() == PrintJobInfo.STATE_CANCELED;
167    }
168
169    private boolean isInImmutableState() {
170        final int state = mCachedInfo.getState();
171        return state == PrintJobInfo.STATE_COMPLETED
172                || state == PrintJobInfo.STATE_CANCELED;
173    }
174
175    @Override
176    public boolean equals(Object obj) {
177        if (this == obj) {
178            return true;
179        }
180        if (obj == null) {
181            return false;
182        }
183        if (getClass() != obj.getClass()) {
184            return false;
185        }
186        PrintJob other = (PrintJob) obj;
187        return mCachedInfo.getId().equals(other.mCachedInfo.getId());
188    }
189
190    @Override
191    public int hashCode() {
192        return mCachedInfo.getId().hashCode();
193    }
194}
195