blob: 64c079e0055bf97261dcc36e466bbaf130428287 [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.printservice;
18
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070019import android.os.RemoteException;
20import android.print.PrintJobInfo;
21import android.util.Log;
22
23/**
24 * This class represents a print job from the perspective of a
25 * print service. It provides APIs for observing the print job
26 * state and performing operations on the print job.
27 */
28public final class PrintJob {
29
30 private static final String LOG_TAG = "PrintJob";
31
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070032 private final IPrintServiceClient mPrintServiceClient;
33
Svetoslav Ganova0027152013-06-25 14:59:53 -070034 private final PrintDocument mDocument;
35
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070036 private PrintJobInfo mCachedInfo;
37
Svetoslav Ganova0027152013-06-25 14:59:53 -070038 PrintJob(PrintJobInfo jobInfo, IPrintServiceClient client) {
39 mCachedInfo = jobInfo;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070040 mPrintServiceClient = client;
Svetoslav Ganova0027152013-06-25 14:59:53 -070041 mDocument = new PrintDocument(mCachedInfo.getId(), client, jobInfo.getDocumentInfo());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070042 }
43
44 /**
45 * Gets the unique print job id.
46 *
47 * @return The id.
48 */
49 public int getId() {
Svetoslav Ganova0027152013-06-25 14:59:53 -070050 return mCachedInfo.getId();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070051 }
52
53 /**
54 * Gets the {@link PrintJobInfo} that describes this job.
55 * <p>
56 * <strong>Node:</strong>The returned info object is a snapshot of the
57 * current print job state. Every call to this method returns a fresh
Svetoslavfd906512013-06-24 09:04:48 -070058 * info object that reflects the current print job state.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070059 * </p>
60 *
61 * @return The print job info.
62 */
63 public PrintJobInfo getInfo() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -070064 if (isInImmutableState()) {
65 return mCachedInfo;
66 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070067 PrintJobInfo info = null;
68 try {
Svetoslav Ganova0027152013-06-25 14:59:53 -070069 info = mPrintServiceClient.getPrintJobInfo(mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070070 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -070071 Log.e(LOG_TAG, "Couldn't get info for job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070072 }
73 if (info != null) {
74 mCachedInfo = info;
75 }
76 return mCachedInfo;
77 }
78
79 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -070080 * Gets the document of this print job.
81 *
82 * @return The document.
83 */
84 public PrintDocument getDocument() {
85 return mDocument;
86 }
87
88 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070089 * Gets whether this print job is queued. Such a print job is
90 * ready to be printed and can be started.
91 *
92 * @return Whether the print job is queued.
93 *
94 * @see #start()
95 */
96 public boolean isQueued() {
97 return getInfo().getState() == PrintJobInfo.STATE_QUEUED;
98 }
99
100 /**
101 * Gets whether this print job is started. Such a print job is
102 * being printed and can be completed or canceled or failed.
103 *
104 * @return Whether the print job is started.
105 *
106 * @see #complete()
107 * @see #cancel()
Svetoslavfd906512013-06-24 09:04:48 -0700108 * @see #fail(CharSequence)
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700109 */
110 public boolean isStarted() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700111 return getInfo().getState() == PrintJobInfo.STATE_STARTED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700112 }
113
114 /**
115 * Starts the print job. You should call this method if {@link
116 * #isQueued()} returns true and you started printing.
117 *
118 * @return Whether the job as started.
119 *
120 * @see #isQueued()
121 */
122 public boolean start() {
123 if (isQueued()) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700124 return setState(PrintJobInfo.STATE_STARTED, null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700125 }
126 return false;
127 }
128
129 /**
130 * Completes the print job. You should call this method if {@link
131 * #isStarted()} returns true and you are done printing.
132 *
133 * @return Whether the job as completed.
134 *
135 * @see #isStarted()
136 */
137 public boolean complete() {
138 if (isStarted()) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700139 return setState(PrintJobInfo.STATE_COMPLETED, null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700140 }
141 return false;
142 }
143
144 /**
145 * Fails the print job. You should call this method if {@link
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700146 * #isQueued()} or {@link #isStarted()} returns true you failed
147 * while printing.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700148 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700149 * @param error The human readable, short, and translated reason
150 * for the failure.
Svetoslavfd906512013-06-24 09:04:48 -0700151 * @return Whether the job was failed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700152 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700153 * @see #isQueued()
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700154 * @see #isStarted()
155 */
156 public boolean fail(CharSequence error) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700157 if (isQueued() || isStarted()) {
158 return setState(PrintJobInfo.STATE_FAILED, error);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700159 }
160 return false;
161 }
162
163 /**
164 * Cancels the print job. You should call this method if {@link
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700165 * #isQueued()} or {@link #isStarted()} returns true and you canceled
166 * the print job as a response to a call to {@link PrintService
167 * #onRequestCancelPrintJob(PrintJob)}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700168 *
169 * @return Whether the job as canceled.
170 *
171 * @see #isStarted()
172 */
173 public boolean cancel() {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700174 if (isQueued() || isStarted()) {
175 return setState(PrintJobInfo.STATE_CANCELED, null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700176 }
177 return false;
178 }
179
180 /**
181 * Sets a tag that is valid in the context of a {@link PrintService}
182 * and is not interpreted by the system. For example, a print service
183 * may set as a tag the key of the print job returned by a remote
184 * print server, if the printing is off handed to a cloud based service.
185 *
186 * @param tag The tag.
187 * @return True if the tag was set, false otherwise.
188 */
189 public boolean setTag(String tag) {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700190 if (isInImmutableState()) {
191 return false;
192 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700193 try {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700194 return mPrintServiceClient.setPrintJobTag(mCachedInfo.getId(), tag);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700195 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700196 Log.e(LOG_TAG, "Error setting tag for job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700197 }
198 return false;
199 }
200
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700201 @Override
202 public boolean equals(Object obj) {
203 if (this == obj) {
204 return true;
205 }
206 if (obj == null) {
207 return false;
208 }
209 if (getClass() != obj.getClass()) {
210 return false;
211 }
212 PrintJob other = (PrintJob) obj;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700213 return (mCachedInfo.getId() == other.mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700214 }
215
216 @Override
217 public int hashCode() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700218 return mCachedInfo.getId();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700219 }
220
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700221 private boolean isInImmutableState() {
222 final int state = mCachedInfo.getState();
223 return state == PrintJobInfo.STATE_COMPLETED
224 || state == PrintJobInfo.STATE_CANCELED;
225 }
226
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700227 private boolean setState(int state, CharSequence error) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700228 try {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700229 if (mPrintServiceClient.setPrintJobState(mCachedInfo.getId(), state, error)) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700230 // Best effort - update the state of the cached info since
231 // we may not be able to re-fetch it later if the job gets
232 // removed from the spooler as a result of the state change.
233 mCachedInfo.setState(state);
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700234 mCachedInfo.setFailureReason(error);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700235 return true;
236 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700237 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700238 Log.e(LOG_TAG, "Error setting the state of job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700239 }
240 return false;
241 }
242}