blob: 80530a7cb8d692e9da394479ee17e28ceefc833f [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() {
64 PrintJobInfo info = null;
65 try {
Svetoslav Ganova0027152013-06-25 14:59:53 -070066 info = mPrintServiceClient.getPrintJobInfo(mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070067 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -070068 Log.e(LOG_TAG, "Couldn't get info for job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070069 }
70 if (info != null) {
71 mCachedInfo = info;
72 }
73 return mCachedInfo;
74 }
75
76 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -070077 * Gets the document of this print job.
78 *
79 * @return The document.
80 */
81 public PrintDocument getDocument() {
82 return mDocument;
83 }
84
85 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070086 * Gets whether this print job is queued. Such a print job is
87 * ready to be printed and can be started.
88 *
89 * @return Whether the print job is queued.
90 *
91 * @see #start()
92 */
93 public boolean isQueued() {
94 return getInfo().getState() == PrintJobInfo.STATE_QUEUED;
95 }
96
97 /**
98 * Gets whether this print job is started. Such a print job is
99 * being printed and can be completed or canceled or failed.
100 *
101 * @return Whether the print job is started.
102 *
103 * @see #complete()
104 * @see #cancel()
Svetoslavfd906512013-06-24 09:04:48 -0700105 * @see #fail(CharSequence)
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700106 */
107 public boolean isStarted() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700108 return getInfo().getState() == PrintJobInfo.STATE_STARTED;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700109 }
110
111 /**
112 * Starts the print job. You should call this method if {@link
113 * #isQueued()} returns true and you started printing.
114 *
115 * @return Whether the job as started.
116 *
117 * @see #isQueued()
118 */
119 public boolean start() {
120 if (isQueued()) {
121 return setState(PrintJobInfo.STATE_STARTED);
122 }
123 return false;
124 }
125
126 /**
127 * Completes the print job. You should call this method if {@link
128 * #isStarted()} returns true and you are done printing.
129 *
130 * @return Whether the job as completed.
131 *
132 * @see #isStarted()
133 */
134 public boolean complete() {
135 if (isStarted()) {
136 return setState(PrintJobInfo.STATE_COMPLETED);
137 }
138 return false;
139 }
140
141 /**
142 * Fails the print job. You should call this method if {@link
143 * #isStarted()} returns true you filed while printing.
144 *
Svetoslavfd906512013-06-24 09:04:48 -0700145 * @param error The reason for the failure.
146 * @return Whether the job was failed.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700147 *
148 * @see #isStarted()
149 */
150 public boolean fail(CharSequence error) {
151 // TODO: Propagate the error message to the UI.
152 if (isStarted()) {
153 return setState(PrintJobInfo.STATE_FAILED);
154 }
155 return false;
156 }
157
158 /**
159 * Cancels the print job. You should call this method if {@link
160 * #isStarted()} returns true and you canceled the print job as a
161 * response to a call to {@link PrintService#onRequestCancelPrintJob(
162 * PrintJob)}.
163 *
164 * @return Whether the job as canceled.
165 *
166 * @see #isStarted()
167 */
168 public boolean cancel() {
169 if (isStarted()) {
170 return setState(PrintJobInfo.STATE_CANCELED);
171 }
172 return false;
173 }
174
175 /**
176 * Sets a tag that is valid in the context of a {@link PrintService}
177 * and is not interpreted by the system. For example, a print service
178 * may set as a tag the key of the print job returned by a remote
179 * print server, if the printing is off handed to a cloud based service.
180 *
181 * @param tag The tag.
182 * @return True if the tag was set, false otherwise.
183 */
184 public boolean setTag(String tag) {
185 try {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700186 return mPrintServiceClient.setPrintJobTag(mCachedInfo.getId(), tag);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700187 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700188 Log.e(LOG_TAG, "Error setting tag for job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700189 }
190 return false;
191 }
192
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700193 @Override
194 public boolean equals(Object obj) {
195 if (this == obj) {
196 return true;
197 }
198 if (obj == null) {
199 return false;
200 }
201 if (getClass() != obj.getClass()) {
202 return false;
203 }
204 PrintJob other = (PrintJob) obj;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700205 return (mCachedInfo.getId() == other.mCachedInfo.getId());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700206 }
207
208 @Override
209 public int hashCode() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700210 return mCachedInfo.getId();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700211 }
212
213 private boolean setState(int state) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700214 try {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700215 if (mPrintServiceClient.setPrintJobState(mCachedInfo.getId(), state)) {
216 // Best effort - update the state of the cached info since
217 // we may not be able to re-fetch it later if the job gets
218 // removed from the spooler as a result of the state change.
219 mCachedInfo.setState(state);
220 return true;
221 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700222 } catch (RemoteException re) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700223 Log.e(LOG_TAG, "Error setting the state of job: " + mCachedInfo.getId(), re);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700224 }
225 return false;
226 }
227}