blob: 777baabd955a842fce048c648e0ce725f33bccc4 [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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
21
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070022/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070023 * This class represents a print job from the perspective of an
24 * application. It contains behavior methods for performing operations
25 * on it as well as methods for querying its state. A snapshot of the
26 * print job state is represented by the {@link PrintJobInfo} class.
27 * The state of a print job may change over time. An application receives
28 * instances of this class when creating a print job or querying for
29 * its print jobs.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070030 */
31public final class PrintJob {
32
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070033 private final PrintManager mPrintManager;
34
35 private PrintJobInfo mCachedInfo;
36
37 PrintJob(PrintJobInfo info, PrintManager printManager) {
38 mCachedInfo = info;
39 mPrintManager = printManager;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070040 }
41
42 /**
43 * Gets the unique print job id.
44 *
45 * @return The id.
46 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080047 public @NonNull PrintJobId getId() {
Svetoslav2fbd2a72013-09-16 17:53:51 -070048 return mCachedInfo.getId();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070049 }
50
51 /**
52 * Gets the {@link PrintJobInfo} that describes this job.
53 * <p>
54 * <strong>Node:</strong>The returned info object is a snapshot of the
55 * current print job state. Every call to this method returns a fresh
56 * info object that reflects the current print job state.
57 * </p>
58 *
59 * @return The print job info.
60 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080061 public @Nullable PrintJobInfo getInfo() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070062 if (isInImmutableState()) {
63 return mCachedInfo;
64 }
Svetoslav2fbd2a72013-09-16 17:53:51 -070065 PrintJobInfo info = mPrintManager.getPrintJobInfo(mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070066 if (info != null) {
67 mCachedInfo = info;
68 }
69 return mCachedInfo;
70 }
71
72 /**
Svetoslav Ganov704697b2013-09-21 20:30:24 -070073 * Cancels this print job. You can request cancellation of a
74 * queued, started, blocked, or failed print job.
75 *
76 * @see #isQueued()
77 * @see #isStarted()
78 * @see #isBlocked()
79 * @see #isFailed()
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070080 */
81 public void cancel() {
Svetoslav Ganov704697b2013-09-21 20:30:24 -070082 final int state = getInfo().getState();
83 if (state == PrintJobInfo.STATE_QUEUED
84 || state == PrintJobInfo.STATE_STARTED
85 || state == PrintJobInfo.STATE_BLOCKED
86 || state == PrintJobInfo.STATE_FAILED) {
Svetoslav2fbd2a72013-09-16 17:53:51 -070087 mPrintManager.cancelPrintJob(mCachedInfo.getId());
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070088 }
89 }
90
Svetoslav Ganov704697b2013-09-21 20:30:24 -070091 /**
92 * Restarts this print job. You can request restart of a failed
93 * print job.
94 *
95 * @see #isFailed()
96 */
97 public void restart() {
98 if (isFailed()) {
99 mPrintManager.restartPrintJob(mCachedInfo.getId());
100 }
101 }
102
103 /**
104 * Gets whether this print job is queued. Such a print job is
105 * ready to be printed. You can request a cancellation via
106 * {@link #cancel()}.
107 *
108 * @return Whether the print job is queued.
109 *
110 * @see #cancel()
111 */
112 public boolean isQueued() {
113 return getInfo().getState() == PrintJobInfo.STATE_QUEUED;
114 }
115
116 /**
117 * Gets whether this print job is started. Such a print job is
118 * being printed. You can request a cancellation via
119 * {@link #cancel()}.
120 *
121 * @return Whether the print job is started.
122 *
123 * @see #cancel()
124 */
125 public boolean isStarted() {
126 return getInfo().getState() == PrintJobInfo.STATE_STARTED;
127 }
128
129 /**
130 * Gets whether this print job is blocked. Such a print job is halted
131 * due to an abnormal condition. You can request a cancellation via
132 * {@link #cancel()}.
133 *
134 * @return Whether the print job is blocked.
135 *
136 * @see #cancel()
137 */
138 public boolean isBlocked() {
139 return getInfo().getState() == PrintJobInfo.STATE_BLOCKED;
140 }
141
142 /**
143 * Gets whether this print job is completed. Such a print job
144 * is successfully printed. You can neither cancel nor restart
145 * such a print job.
146 *
147 * @return Whether the print job is completed.
148 */
149 public boolean isCompleted() {
150 return getInfo().getState() == PrintJobInfo.STATE_COMPLETED;
151 }
152
153 /**
154 * Gets whether this print job is failed. Such a print job is
155 * not successfully printed due to an error. You can request
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700156 * a restart via {@link #restart()} or cancel via {@link #cancel()}.
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700157 *
158 * @return Whether the print job is failed.
159 *
160 * @see #restart()
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700161 * @see #cancel()
Svetoslav Ganov704697b2013-09-21 20:30:24 -0700162 */
163 public boolean isFailed() {
164 return getInfo().getState() == PrintJobInfo.STATE_FAILED;
165 }
166
167 /**
168 * Gets whether this print job is cancelled. Such a print job was
169 * cancelled as a result of a user request. This is a final state.
170 * You cannot restart such a print job.
171 *
172 * @return Whether the print job is cancelled.
173 */
174 public boolean isCancelled() {
175 return getInfo().getState() == PrintJobInfo.STATE_CANCELED;
176 }
177
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700178 private boolean isInImmutableState() {
179 final int state = mCachedInfo.getState();
180 return state == PrintJobInfo.STATE_COMPLETED
181 || state == PrintJobInfo.STATE_CANCELED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (this == obj) {
187 return true;
188 }
189 if (obj == null) {
190 return false;
191 }
192 if (getClass() != obj.getClass()) {
193 return false;
194 }
195 PrintJob other = (PrintJob) obj;
Svetoslav2fbd2a72013-09-16 17:53:51 -0700196 return mCachedInfo.getId().equals(other.mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700197 }
198
199 @Override
200 public int hashCode() {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700201 return mCachedInfo.getId().hashCode();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700202 }
203}