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