Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 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 | |
| 17 | package android.printservice; |
| 18 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 19 | import android.os.RemoteException; |
| 20 | import android.print.PrintJobInfo; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 24 | * This class represents a print job from the perspective of a print |
| 25 | * service. It provides APIs for observing the print job state and |
| 26 | * performing operations on the print job. |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 27 | * <p> |
| 28 | * <strong>Note: </strong> All methods of this class must be executed on the main |
| 29 | * application thread. |
| 30 | * </p> |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 31 | */ |
| 32 | public final class PrintJob { |
| 33 | |
| 34 | private static final String LOG_TAG = "PrintJob"; |
| 35 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 36 | private final IPrintServiceClient mPrintServiceClient; |
| 37 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 38 | private final PrintDocument mDocument; |
| 39 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 40 | private PrintJobInfo mCachedInfo; |
| 41 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 42 | PrintJob(PrintJobInfo jobInfo, IPrintServiceClient client) { |
| 43 | mCachedInfo = jobInfo; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 44 | mPrintServiceClient = client; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 45 | mDocument = new PrintDocument(mCachedInfo.getId(), client, |
| 46 | jobInfo.getDocumentInfo()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the unique print job id. |
| 51 | * |
| 52 | * @return The id. |
| 53 | */ |
| 54 | public int getId() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 55 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 56 | return mCachedInfo.getId(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets the {@link PrintJobInfo} that describes this job. |
| 61 | * <p> |
| 62 | * <strong>Node:</strong>The returned info object is a snapshot of the |
| 63 | * current print job state. Every call to this method returns a fresh |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 64 | * info object that reflects the current print job state. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 65 | * </p> |
| 66 | * |
| 67 | * @return The print job info. |
| 68 | */ |
| 69 | public PrintJobInfo getInfo() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 70 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 71 | if (isInImmutableState()) { |
| 72 | return mCachedInfo; |
| 73 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 74 | PrintJobInfo info = null; |
| 75 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 76 | info = mPrintServiceClient.getPrintJobInfo(mCachedInfo.getId()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 77 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 78 | Log.e(LOG_TAG, "Couldn't get info for job: " + mCachedInfo.getId(), re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 79 | } |
| 80 | if (info != null) { |
| 81 | mCachedInfo = info; |
| 82 | } |
| 83 | return mCachedInfo; |
| 84 | } |
| 85 | |
| 86 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 87 | * Gets the printed document. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 88 | * |
| 89 | * @return The document. |
| 90 | */ |
| 91 | public PrintDocument getDocument() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 92 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 93 | return mDocument; |
| 94 | } |
| 95 | |
| 96 | /** |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 97 | * Gets whether this print job is queued. Such a print job is |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 98 | * ready to be printed and can be started or cancelled. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 99 | * |
| 100 | * @return Whether the print job is queued. |
| 101 | * |
| 102 | * @see #start() |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 103 | * @see #cancel() |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 104 | */ |
| 105 | public boolean isQueued() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 106 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 107 | return getInfo().getState() == PrintJobInfo.STATE_QUEUED; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets whether this print job is started. Such a print job is |
| 112 | * being printed and can be completed or canceled or failed. |
| 113 | * |
| 114 | * @return Whether the print job is started. |
| 115 | * |
| 116 | * @see #complete() |
| 117 | * @see #cancel() |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 118 | * @see #fail(CharSequence) |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 119 | */ |
| 120 | public boolean isStarted() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 121 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 122 | return getInfo().getState() == PrintJobInfo.STATE_STARTED; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 126 | * Gets whether this print job is completed. Such a print job |
| 127 | * is successfully printed. This is a final state. |
| 128 | * |
| 129 | * @return Whether the print job is completed. |
| 130 | * |
| 131 | * @see #complete() |
| 132 | */ |
| 133 | public boolean isCompleted() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 134 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 135 | return getInfo().getState() == PrintJobInfo.STATE_COMPLETED; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Gets whether this print job is failed. Such a print job is |
| 140 | * not successfully printed due to an error. This is a final state. |
| 141 | * |
| 142 | * @return Whether the print job is failed. |
| 143 | * |
| 144 | * @see #fail(CharSequence) |
| 145 | */ |
| 146 | public boolean isFailed() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 147 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 148 | return getInfo().getState() == PrintJobInfo.STATE_FAILED; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Gets whether this print job is cancelled. Such a print job was |
| 153 | * cancelled as a result of a user request. This is a final state. |
| 154 | * |
| 155 | * @return Whether the print job is cancelled. |
| 156 | * |
| 157 | * @see #cancel() |
| 158 | */ |
| 159 | public boolean isCancelled() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 160 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 161 | return getInfo().getState() == PrintJobInfo.STATE_FAILED; |
| 162 | } |
| 163 | |
| 164 | /** |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 165 | * Starts the print job. You should call this method if {@link |
| 166 | * #isQueued()} returns true and you started printing. |
| 167 | * |
| 168 | * @return Whether the job as started. |
| 169 | * |
| 170 | * @see #isQueued() |
| 171 | */ |
| 172 | public boolean start() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 173 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 174 | if (isQueued()) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 175 | return setState(PrintJobInfo.STATE_STARTED, null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Completes the print job. You should call this method if {@link |
| 182 | * #isStarted()} returns true and you are done printing. |
| 183 | * |
| 184 | * @return Whether the job as completed. |
| 185 | * |
| 186 | * @see #isStarted() |
| 187 | */ |
| 188 | public boolean complete() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 189 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 190 | if (isStarted()) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 191 | return setState(PrintJobInfo.STATE_COMPLETED, null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Fails the print job. You should call this method if {@link |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 198 | * #isQueued()} or {@link #isStarted()} returns true you failed |
| 199 | * while printing. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 200 | * |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 201 | * @param error The human readable, short, and translated reason |
| 202 | * for the failure. |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 203 | * @return Whether the job was failed. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 204 | * |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 205 | * @see #isQueued() |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 206 | * @see #isStarted() |
| 207 | */ |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 208 | public boolean fail(String error) { |
| 209 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 210 | if (isQueued() || isStarted()) { |
| 211 | return setState(PrintJobInfo.STATE_FAILED, error); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Cancels the print job. You should call this method if {@link |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 218 | * #isQueued()} or {@link #isStarted()} returns true and you canceled |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 219 | * the print job as a response to a call to {@link |
| 220 | * PrintService#onRequestCancelPrintJob(PrintJob)}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 221 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 222 | * @return Whether the job is canceled. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 223 | * |
| 224 | * @see #isStarted() |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 225 | * @see #isQueued() |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 226 | */ |
| 227 | public boolean cancel() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 228 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 229 | if (isQueued() || isStarted()) { |
| 230 | return setState(PrintJobInfo.STATE_CANCELED, null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets a tag that is valid in the context of a {@link PrintService} |
| 237 | * and is not interpreted by the system. For example, a print service |
| 238 | * may set as a tag the key of the print job returned by a remote |
| 239 | * print server, if the printing is off handed to a cloud based service. |
| 240 | * |
| 241 | * @param tag The tag. |
| 242 | * @return True if the tag was set, false otherwise. |
| 243 | */ |
| 244 | public boolean setTag(String tag) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 245 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 246 | if (isInImmutableState()) { |
| 247 | return false; |
| 248 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 249 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 250 | return mPrintServiceClient.setPrintJobTag(mCachedInfo.getId(), tag); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 251 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 252 | Log.e(LOG_TAG, "Error setting tag for job: " + mCachedInfo.getId(), re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public boolean equals(Object obj) { |
| 259 | if (this == obj) { |
| 260 | return true; |
| 261 | } |
| 262 | if (obj == null) { |
| 263 | return false; |
| 264 | } |
| 265 | if (getClass() != obj.getClass()) { |
| 266 | return false; |
| 267 | } |
| 268 | PrintJob other = (PrintJob) obj; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 269 | return (mCachedInfo.getId() == other.mCachedInfo.getId()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public int hashCode() { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 274 | return mCachedInfo.getId(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 277 | private boolean isInImmutableState() { |
| 278 | final int state = mCachedInfo.getState(); |
| 279 | return state == PrintJobInfo.STATE_COMPLETED |
| 280 | || state == PrintJobInfo.STATE_CANCELED; |
| 281 | } |
| 282 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 283 | private boolean setState(int state, String error) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 284 | try { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 285 | if (mPrintServiceClient.setPrintJobState(mCachedInfo.getId(), state, error)) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 286 | // Best effort - update the state of the cached info since |
| 287 | // we may not be able to re-fetch it later if the job gets |
| 288 | // removed from the spooler as a result of the state change. |
| 289 | mCachedInfo.setState(state); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 290 | mCachedInfo.setFailureReason(error); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 291 | return true; |
| 292 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 293 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 294 | Log.e(LOG_TAG, "Error setting the state of job: " + mCachedInfo.getId(), re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | } |