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 | /** |
| 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 | */ |
| 28 | public final class PrintJob { |
| 29 | |
| 30 | private static final String LOG_TAG = "PrintJob"; |
| 31 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 32 | private final IPrintServiceClient mPrintServiceClient; |
| 33 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 34 | private final PrintDocument mDocument; |
| 35 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 36 | private PrintJobInfo mCachedInfo; |
| 37 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 38 | PrintJob(PrintJobInfo jobInfo, IPrintServiceClient client) { |
| 39 | mCachedInfo = jobInfo; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 40 | mPrintServiceClient = client; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 41 | mDocument = new PrintDocument(mCachedInfo.getId(), client, jobInfo.getDocumentInfo()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Gets the unique print job id. |
| 46 | * |
| 47 | * @return The id. |
| 48 | */ |
| 49 | public int getId() { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 50 | return mCachedInfo.getId(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 51 | } |
| 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 |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 58 | * info object that reflects the current print job state. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 59 | * </p> |
| 60 | * |
| 61 | * @return The print job info. |
| 62 | */ |
| 63 | public PrintJobInfo getInfo() { |
| 64 | PrintJobInfo info = null; |
| 65 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 66 | info = mPrintServiceClient.getPrintJobInfo(mCachedInfo.getId()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 67 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 68 | 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] | 69 | } |
| 70 | if (info != null) { |
| 71 | mCachedInfo = info; |
| 72 | } |
| 73 | return mCachedInfo; |
| 74 | } |
| 75 | |
| 76 | /** |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 77 | * Gets the document of this print job. |
| 78 | * |
| 79 | * @return The document. |
| 80 | */ |
| 81 | public PrintDocument getDocument() { |
| 82 | return mDocument; |
| 83 | } |
| 84 | |
| 85 | /** |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 86 | * 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() |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 105 | * @see #fail(CharSequence) |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 106 | */ |
| 107 | public boolean isStarted() { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 108 | return getInfo().getState() == PrintJobInfo.STATE_STARTED; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 109 | } |
| 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 | * |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 145 | * @param error The reason for the failure. |
| 146 | * @return Whether the job was failed. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 147 | * |
| 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 Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 186 | return mPrintServiceClient.setPrintJobTag(mCachedInfo.getId(), tag); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 187 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 188 | Log.e(LOG_TAG, "Error setting tag for job: " + mCachedInfo.getId(), re); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 193 | @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 Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 205 | return (mCachedInfo.getId() == other.mCachedInfo.getId()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public int hashCode() { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 210 | return mCachedInfo.getId(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private boolean setState(int state) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 214 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 215 | 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 222 | } catch (RemoteException re) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 223 | 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] | 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | } |