| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.printservice; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.MainThread; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.StringRes; |
| 24 |
import android.content.Context; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.print.PrintJobId; |
| 27 |
import android.print.PrintJobInfo; |
| 28 |
import android.text.TextUtils; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
import com.android.internal.util.Preconditions; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public final class PrintJob { |
| 43 |
|
| 44 |
private static final String LOG_TAG = "PrintJob"; |
| 45 |
|
| 46 |
private final IPrintServiceClient mPrintServiceClient; |
| 47 |
|
| 48 |
private final PrintDocument mDocument; |
| 49 |
|
| 50 |
private PrintJobInfo mCachedInfo; |
| 51 |
|
| 52 |
|
| 53 |
private final Context mContext; |
| 54 |
|
| 55 |
PrintJob(@NonNull Context context, @NonNull PrintJobInfo jobInfo, |
| 56 |
@NonNull IPrintServiceClient client) { |
| 57 |
mContext = context; |
| 58 |
mCachedInfo = jobInfo; |
| 59 |
mPrintServiceClient = client; |
| 60 |
mDocument = new PrintDocument(mCachedInfo.getId(), client, |
| 61 |
jobInfo.getDocumentInfo()); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@MainThread |
| 70 |
public PrintJobId getId() { |
| 71 |
PrintService.throwIfNotCalledOnMainThread(); |
| 72 |
return mCachedInfo.getId(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@MainThread |
| 86 |
public @NonNull PrintJobInfo getInfo() { |
| 87 |
PrintService.throwIfNotCalledOnMainThread(); |
| 88 |
if (isInImmutableState()) { |
| 89 |
return mCachedInfo; |
| 90 |
} |
| 91 |
PrintJobInfo info = null; |
| 92 |
try { |
| 93 |
info = mPrintServiceClient.getPrintJobInfo(mCachedInfo.getId()); |
| 94 |
} catch (RemoteException re) { |
| 95 |
Log.e(LOG_TAG, "Couldn't get info for job: " + mCachedInfo.getId(), re); |
| 96 |
} |
| 97 |
if (info != null) { |
| 98 |
mCachedInfo = info; |
| 99 |
} |
| 100 |
return mCachedInfo; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@MainThread |
| 109 |
public @NonNull PrintDocument getDocument() { |
| 110 |
PrintService.throwIfNotCalledOnMainThread(); |
| 111 |
return mDocument; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@MainThread |
| 124 |
public boolean isQueued() { |
| 125 |
PrintService.throwIfNotCalledOnMainThread(); |
| 126 |
return getInfo().getState() == PrintJobInfo.STATE_QUEUED; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@MainThread |
| 140 |
public boolean isStarted() { |
| 141 |
PrintService.throwIfNotCalledOnMainThread(); |
| 142 |
return getInfo().getState() == PrintJobInfo.STATE_STARTED; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@MainThread |
| 156 |
public boolean isBlocked() { |
| 157 |
PrintService.throwIfNotCalledOnMainThread(); |
| 158 |
return getInfo().getState() == PrintJobInfo.STATE_BLOCKED; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@MainThread |
| 170 |
public boolean isCompleted() { |
| 171 |
PrintService.throwIfNotCalledOnMainThread(); |
| 172 |
return getInfo().getState() == PrintJobInfo.STATE_COMPLETED; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@MainThread |
| 184 |
public boolean isFailed() { |
| 185 |
PrintService.throwIfNotCalledOnMainThread(); |
| 186 |
return getInfo().getState() == PrintJobInfo.STATE_FAILED; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@MainThread |
| 198 |
public boolean isCancelled() { |
| 199 |
PrintService.throwIfNotCalledOnMainThread(); |
| 200 |
return getInfo().getState() == PrintJobInfo.STATE_CANCELED; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@MainThread |
| 217 |
public boolean start() { |
| 218 |
PrintService.throwIfNotCalledOnMainThread(); |
| 219 |
final int state = getInfo().getState(); |
| 220 |
if (state == PrintJobInfo.STATE_QUEUED |
| 221 |
|| state == PrintJobInfo.STATE_BLOCKED) { |
| 222 |
return setState(PrintJobInfo.STATE_STARTED, null); |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
@MainThread |
| 240 |
public boolean block(@Nullable String reason) { |
| 241 |
PrintService.throwIfNotCalledOnMainThread(); |
| 242 |
PrintJobInfo info = getInfo(); |
| 243 |
final int state = info.getState(); |
| 244 |
if (state == PrintJobInfo.STATE_STARTED || state == PrintJobInfo.STATE_BLOCKED) { |
| 245 |
return setState(PrintJobInfo.STATE_BLOCKED, reason); |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
@MainThread |
| 259 |
public boolean complete() { |
| 260 |
PrintService.throwIfNotCalledOnMainThread(); |
| 261 |
if (isStarted()) { |
| 262 |
return setState(PrintJobInfo.STATE_COMPLETED, null); |
| 263 |
} |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@MainThread |
| 281 |
public boolean fail(@Nullable String error) { |
| 282 |
PrintService.throwIfNotCalledOnMainThread(); |
| 283 |
if (!isInImmutableState()) { |
| 284 |
return setState(PrintJobInfo.STATE_FAILED, error); |
| 285 |
} |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
@MainThread |
| 302 |
public boolean cancel() { |
| 303 |
PrintService.throwIfNotCalledOnMainThread(); |
| 304 |
if (!isInImmutableState()) { |
| 305 |
return setState(PrintJobInfo.STATE_CANCELED, null); |
| 306 |
} |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@MainThread |
| 316 |
public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) { |
| 317 |
PrintService.throwIfNotCalledOnMainThread(); |
| 318 |
|
| 319 |
try { |
| 320 |
mPrintServiceClient.setProgress(mCachedInfo.getId(), progress); |
| 321 |
} catch (RemoteException re) { |
| 322 |
Log.e(LOG_TAG, "Error setting progress for job: " + mCachedInfo.getId(), re); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
@MainThread |
| 336 |
public void setStatus(@Nullable CharSequence status) { |
| 337 |
PrintService.throwIfNotCalledOnMainThread(); |
| 338 |
|
| 339 |
try { |
| 340 |
mPrintServiceClient.setStatus(mCachedInfo.getId(), status); |
| 341 |
} catch (RemoteException re) { |
| 342 |
Log.e(LOG_TAG, "Error setting status for job: " + mCachedInfo.getId(), re); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
@MainThread |
| 355 |
public void setStatus(@StringRes int statusResId) { |
| 356 |
PrintService.throwIfNotCalledOnMainThread(); |
| 357 |
|
| 358 |
try { |
| 359 |
mPrintServiceClient.setStatusRes(mCachedInfo.getId(), statusResId, |
| 360 |
mContext.getPackageName()); |
| 361 |
} catch (RemoteException re) { |
| 362 |
Log.e(LOG_TAG, "Error setting status for job: " + mCachedInfo.getId(), re); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
@MainThread |
| 376 |
public boolean setTag(@NonNull String tag) { |
| 377 |
PrintService.throwIfNotCalledOnMainThread(); |
| 378 |
if (isInImmutableState()) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
try { |
| 382 |
return mPrintServiceClient.setPrintJobTag(mCachedInfo.getId(), tag); |
| 383 |
} catch (RemoteException re) { |
| 384 |
Log.e(LOG_TAG, "Error setting tag for job: " + mCachedInfo.getId(), re); |
| 385 |
} |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
@MainThread |
| 397 |
public @Nullable String getTag() { |
| 398 |
PrintService.throwIfNotCalledOnMainThread(); |
| 399 |
return getInfo().getTag(); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
@MainThread |
| 409 |
public String getAdvancedStringOption(String key) { |
| 410 |
PrintService.throwIfNotCalledOnMainThread(); |
| 411 |
return getInfo().getAdvancedStringOption(key); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
@MainThread |
| 422 |
public boolean hasAdvancedOption(String key) { |
| 423 |
PrintService.throwIfNotCalledOnMainThread(); |
| 424 |
return getInfo().hasAdvancedOption(key); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
@MainThread |
| 434 |
public int getAdvancedIntOption(String key) { |
| 435 |
PrintService.throwIfNotCalledOnMainThread(); |
| 436 |
return getInfo().getAdvancedIntOption(key); |
| 437 |
} |
| 438 |
|
| 439 |
@Override |
| 440 |
public boolean equals(@Nullable Object obj) { |
| 441 |
if (this == obj) { |
| 442 |
return true; |
| 443 |
} |
| 444 |
if (obj == null) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
if (getClass() != obj.getClass()) { |
| 448 |
return false; |
| 449 |
} |
| 450 |
PrintJob other = (PrintJob) obj; |
| 451 |
return (mCachedInfo.getId().equals(other.mCachedInfo.getId())); |
| 452 |
} |
| 453 |
|
| 454 |
@Override |
| 455 |
public int hashCode() { |
| 456 |
return mCachedInfo.getId().hashCode(); |
| 457 |
} |
| 458 |
|
| 459 |
private boolean isInImmutableState() { |
| 460 |
final int state = mCachedInfo.getState(); |
| 461 |
return state == PrintJobInfo.STATE_COMPLETED |
| 462 |
|| state == PrintJobInfo.STATE_CANCELED |
| 463 |
|| state == PrintJobInfo.STATE_FAILED; |
| 464 |
} |
| 465 |
|
| 466 |
private boolean setState(int state, @Nullable String error) { |
| 467 |
try { |
| 468 |
if (mPrintServiceClient.setPrintJobState(mCachedInfo.getId(), state, error)) { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
mCachedInfo.setState(state); |
| 473 |
mCachedInfo.setStatus(error); |
| 474 |
return true; |
| 475 |
} |
| 476 |
} catch (RemoteException re) { |
| 477 |
Log.e(LOG_TAG, "Error setting the state of job: " + mCachedInfo.getId(), re); |
| 478 |
} |
| 479 |
return false; |
| 480 |
} |
| 481 |
} |