blob: 0abe2193249ee8bafefc15a03b98bf581ba4974e [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
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
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070019/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070020 * 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.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070027 */
28public final class PrintJob {
29
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070030 private final PrintManager mPrintManager;
31
32 private PrintJobInfo mCachedInfo;
33
34 PrintJob(PrintJobInfo info, PrintManager printManager) {
35 mCachedInfo = info;
36 mPrintManager = printManager;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070037 }
38
39 /**
40 * Gets the unique print job id.
41 *
42 * @return The id.
43 */
Svetoslav2fbd2a72013-09-16 17:53:51 -070044 public PrintJobId getId() {
45 return mCachedInfo.getId();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070046 }
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() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070059 if (isInImmutableState()) {
60 return mCachedInfo;
61 }
Svetoslav2fbd2a72013-09-16 17:53:51 -070062 PrintJobInfo info = mPrintManager.getPrintJobInfo(mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070063 if (info != null) {
64 mCachedInfo = info;
65 }
66 return mCachedInfo;
67 }
68
69 /**
Svetoslav Ganov704697b2013-09-21 20:30:24 -070070 * 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()
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070077 */
78 public void cancel() {
Svetoslav Ganov704697b2013-09-21 20:30:24 -070079 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) {
Svetoslav2fbd2a72013-09-16 17:53:51 -070084 mPrintManager.cancelPrintJob(mCachedInfo.getId());
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070085 }
86 }
87
Svetoslav Ganov704697b2013-09-21 20:30:24 -070088 /**
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
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700153 * a restart via {@link #restart()} or cancel via {@link #cancel()}.
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700154 *
155 * @return Whether the print job is failed.
156 *
157 * @see #restart()
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700158 * @see #cancel()
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700159 */
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
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700175 private boolean isInImmutableState() {
176 final int state = mCachedInfo.getState();
177 return state == PrintJobInfo.STATE_COMPLETED
178 || state == PrintJobInfo.STATE_CANCELED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700179 }
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;
Svetoslav2fbd2a72013-09-16 17:53:51 -0700193 return mCachedInfo.getId().equals(other.mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700194 }
195
196 @Override
197 public int hashCode() {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700198 return mCachedInfo.getId().hashCode();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700199 }
200}