| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.job; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.app.ActivityManager; |
| 23 |
import android.app.usage.UsageStatsManager; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.content.ClipData; |
| 26 |
import android.content.pm.PackageManager; |
| 27 |
import android.net.Network; |
| 28 |
import android.net.NetworkRequest; |
| 29 |
import android.net.Uri; |
| 30 |
import android.os.Bundle; |
| 31 |
import android.os.IBinder; |
| 32 |
import android.os.Parcel; |
| 33 |
import android.os.Parcelable; |
| 34 |
import android.os.PersistableBundle; |
| 35 |
import android.os.RemoteException; |
| 36 |
|
| 37 |
import java.lang.annotation.Retention; |
| 38 |
import java.lang.annotation.RetentionPolicy; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class JobParameters implements Parcelable { |
| 45 |
|
| 46 |
|
| 47 |
public static final int INTERNAL_STOP_REASON_UNKNOWN = -1; |
| 48 |
|
| 49 |
|
| 50 |
public static final int INTERNAL_STOP_REASON_CANCELED = |
| 51 |
JobProtoEnums.INTERNAL_STOP_REASON_CANCELLED; |
| 52 |
|
| 53 |
public static final int INTERNAL_STOP_REASON_CONSTRAINTS_NOT_SATISFIED = |
| 54 |
JobProtoEnums.INTERNAL_STOP_REASON_CONSTRAINTS_NOT_SATISFIED; |
| 55 |
|
| 56 |
public static final int INTERNAL_STOP_REASON_PREEMPT = |
| 57 |
JobProtoEnums.INTERNAL_STOP_REASON_PREEMPT; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final int INTERNAL_STOP_REASON_TIMEOUT = |
| 63 |
JobProtoEnums.INTERNAL_STOP_REASON_TIMEOUT; |
| 64 |
|
| 65 |
public static final int INTERNAL_STOP_REASON_DEVICE_IDLE = |
| 66 |
JobProtoEnums.INTERNAL_STOP_REASON_DEVICE_IDLE; |
| 67 |
|
| 68 |
public static final int INTERNAL_STOP_REASON_DEVICE_THERMAL = |
| 69 |
JobProtoEnums.INTERNAL_STOP_REASON_DEVICE_THERMAL; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static final int INTERNAL_STOP_REASON_RESTRICTED_BUCKET = |
| 77 |
JobProtoEnums.INTERNAL_STOP_REASON_RESTRICTED_BUCKET; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static final int INTERNAL_STOP_REASON_UNINSTALL = |
| 83 |
JobProtoEnums.INTERNAL_STOP_REASON_UNINSTALL; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static final int INTERNAL_STOP_REASON_DATA_CLEARED = |
| 89 |
JobProtoEnums.INTERNAL_STOP_REASON_DATA_CLEARED; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static final int INTERNAL_STOP_REASON_RTC_UPDATED = |
| 94 |
JobProtoEnums.INTERNAL_STOP_REASON_RTC_UPDATED; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final int INTERNAL_STOP_REASON_SUCCESSFUL_FINISH = |
| 100 |
JobProtoEnums.INTERNAL_STOP_REASON_SUCCESSFUL_FINISH; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static final int[] JOB_STOP_REASON_CODES = { |
| 112 |
INTERNAL_STOP_REASON_UNKNOWN, |
| 113 |
INTERNAL_STOP_REASON_CANCELED, |
| 114 |
INTERNAL_STOP_REASON_CONSTRAINTS_NOT_SATISFIED, |
| 115 |
INTERNAL_STOP_REASON_PREEMPT, |
| 116 |
INTERNAL_STOP_REASON_TIMEOUT, |
| 117 |
INTERNAL_STOP_REASON_DEVICE_IDLE, |
| 118 |
INTERNAL_STOP_REASON_DEVICE_THERMAL, |
| 119 |
INTERNAL_STOP_REASON_RESTRICTED_BUCKET, |
| 120 |
INTERNAL_STOP_REASON_UNINSTALL, |
| 121 |
INTERNAL_STOP_REASON_DATA_CLEARED, |
| 122 |
INTERNAL_STOP_REASON_RTC_UPDATED, |
| 123 |
INTERNAL_STOP_REASON_SUCCESSFUL_FINISH, |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@NonNull |
| 131 |
public static String getInternalReasonCodeDescription(int reasonCode) { |
| 132 |
switch (reasonCode) { |
| 133 |
case INTERNAL_STOP_REASON_CANCELED: return "canceled"; |
| 134 |
case INTERNAL_STOP_REASON_CONSTRAINTS_NOT_SATISFIED: return "constraints"; |
| 135 |
case INTERNAL_STOP_REASON_PREEMPT: return "preempt"; |
| 136 |
case INTERNAL_STOP_REASON_TIMEOUT: return "timeout"; |
| 137 |
case INTERNAL_STOP_REASON_DEVICE_IDLE: return "device_idle"; |
| 138 |
case INTERNAL_STOP_REASON_DEVICE_THERMAL: return "thermal"; |
| 139 |
case INTERNAL_STOP_REASON_RESTRICTED_BUCKET: return "restricted_bucket"; |
| 140 |
case INTERNAL_STOP_REASON_UNINSTALL: return "uninstall"; |
| 141 |
case INTERNAL_STOP_REASON_DATA_CLEARED: return "data_cleared"; |
| 142 |
case INTERNAL_STOP_REASON_RTC_UPDATED: return "rtc_updated"; |
| 143 |
case INTERNAL_STOP_REASON_SUCCESSFUL_FINISH: return "successful_finish"; |
| 144 |
default: return "unknown:" + reasonCode; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
@NonNull |
| 150 |
public static int[] getJobStopReasonCodes() { |
| 151 |
return JOB_STOP_REASON_CODES; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static final int STOP_REASON_UNDEFINED = 0; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public static final int STOP_REASON_CANCELLED_BY_APP = 1; |
| 165 |
|
| 166 |
public static final int STOP_REASON_PREEMPT = 2; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static final int STOP_REASON_TIMEOUT = 3; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static final int STOP_REASON_DEVICE_STATE = 4; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static final int STOP_REASON_CONSTRAINT_BATTERY_NOT_LOW = 5; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static final int STOP_REASON_CONSTRAINT_CHARGING = 6; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static final int STOP_REASON_CONSTRAINT_CONNECTIVITY = 7; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static final int STOP_REASON_CONSTRAINT_DEVICE_IDLE = 8; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static final int STOP_REASON_CONSTRAINT_STORAGE_NOT_LOW = 9; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static final int STOP_REASON_QUOTA = 10; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public static final int STOP_REASON_BACKGROUND_RESTRICTION = 11; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static final int STOP_REASON_APP_STANDBY = 12; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public static final int STOP_REASON_USER = 13; |
| 236 |
|
| 237 |
public static final int STOP_REASON_SYSTEM_PROCESSING = 14; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public static final int STOP_REASON_ESTIMATED_APP_LAUNCH_TIME_CHANGED = 15; |
| 246 |
|
| 247 |
|
| 248 |
@IntDef(prefix = {"STOP_REASON_"}, value = { |
| 249 |
STOP_REASON_UNDEFINED, |
| 250 |
STOP_REASON_CANCELLED_BY_APP, |
| 251 |
STOP_REASON_PREEMPT, |
| 252 |
STOP_REASON_TIMEOUT, |
| 253 |
STOP_REASON_DEVICE_STATE, |
| 254 |
STOP_REASON_CONSTRAINT_BATTERY_NOT_LOW, |
| 255 |
STOP_REASON_CONSTRAINT_CHARGING, |
| 256 |
STOP_REASON_CONSTRAINT_CONNECTIVITY, |
| 257 |
STOP_REASON_CONSTRAINT_DEVICE_IDLE, |
| 258 |
STOP_REASON_CONSTRAINT_STORAGE_NOT_LOW, |
| 259 |
STOP_REASON_QUOTA, |
| 260 |
STOP_REASON_BACKGROUND_RESTRICTION, |
| 261 |
STOP_REASON_APP_STANDBY, |
| 262 |
STOP_REASON_USER, |
| 263 |
STOP_REASON_SYSTEM_PROCESSING, |
| 264 |
STOP_REASON_ESTIMATED_APP_LAUNCH_TIME_CHANGED, |
| 265 |
}) |
| 266 |
@Retention(RetentionPolicy.SOURCE) |
| 267 |
public @interface StopReason { |
| 268 |
} |
| 269 |
|
| 270 |
@UnsupportedAppUsage |
| 271 |
private final int jobId; |
| 272 |
private final PersistableBundle extras; |
| 273 |
private final Bundle transientExtras; |
| 274 |
private final ClipData clipData; |
| 275 |
private final int clipGrantFlags; |
| 276 |
@UnsupportedAppUsage |
| 277 |
private final IBinder callback; |
| 278 |
private final boolean overrideDeadlineExpired; |
| 279 |
private final boolean mIsExpedited; |
| 280 |
private final Uri[] mTriggeredContentUris; |
| 281 |
private final String[] mTriggeredContentAuthorities; |
| 282 |
private final Network network; |
| 283 |
|
| 284 |
private int mStopReason = STOP_REASON_UNDEFINED; |
| 285 |
private int mInternalStopReason = INTERNAL_STOP_REASON_UNKNOWN; |
| 286 |
private String debugStopReason; |
| 287 |
|
| 288 |
|
| 289 |
public JobParameters(IBinder callback, int jobId, PersistableBundle extras, |
| 290 |
Bundle transientExtras, ClipData clipData, int clipGrantFlags, |
| 291 |
boolean overrideDeadlineExpired, boolean isExpedited, Uri[] triggeredContentUris, |
| 292 |
String[] triggeredContentAuthorities, Network network) { |
| 293 |
this.jobId = jobId; |
| 294 |
this.extras = extras; |
| 295 |
this.transientExtras = transientExtras; |
| 296 |
this.clipData = clipData; |
| 297 |
this.clipGrantFlags = clipGrantFlags; |
| 298 |
this.callback = callback; |
| 299 |
this.overrideDeadlineExpired = overrideDeadlineExpired; |
| 300 |
this.mIsExpedited = isExpedited; |
| 301 |
this.mTriggeredContentUris = triggeredContentUris; |
| 302 |
this.mTriggeredContentAuthorities = triggeredContentAuthorities; |
| 303 |
this.network = network; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public int getJobId() { |
| 310 |
return jobId; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
@StopReason |
| 319 |
public int getStopReason() { |
| 320 |
return mStopReason; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
public int getInternalStopReasonCode() { |
| 325 |
return mInternalStopReason; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public String getDebugStopReason() { |
| 334 |
return debugStopReason; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public @NonNull PersistableBundle getExtras() { |
| 343 |
return extras; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public @NonNull Bundle getTransientExtras() { |
| 352 |
return transientExtras; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public @Nullable ClipData getClipData() { |
| 361 |
return clipData; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public int getClipGrantFlags() { |
| 370 |
return clipGrantFlags; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public boolean isExpeditedJob() { |
| 383 |
return mIsExpedited; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public boolean isOverrideDeadlineExpired() { |
| 393 |
return overrideDeadlineExpired; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public @Nullable Uri[] getTriggeredContentUris() { |
| 405 |
return mTriggeredContentUris; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public @Nullable String[] getTriggeredContentAuthorities() { |
| 417 |
return mTriggeredContentAuthorities; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public @Nullable Network getNetwork() { |
| 440 |
return network; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public @Nullable JobWorkItem dequeueWork() { |
| 477 |
try { |
| 478 |
return getCallback().dequeueWork(getJobId()); |
| 479 |
} catch (RemoteException e) { |
| 480 |
throw e.rethrowFromSystemServer(); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public void completeWork(@NonNull JobWorkItem work) { |
| 501 |
try { |
| 502 |
if (!getCallback().completeWork(getJobId(), work.getWorkId())) { |
| 503 |
throw new IllegalArgumentException("Given work is not active: " + work); |
| 504 |
} |
| 505 |
} catch (RemoteException e) { |
| 506 |
throw e.rethrowFromSystemServer(); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
@UnsupportedAppUsage |
| 512 |
public IJobCallback getCallback() { |
| 513 |
return IJobCallback.Stub.asInterface(callback); |
| 514 |
} |
| 515 |
|
| 516 |
private JobParameters(Parcel in) { |
| 517 |
jobId = in.readInt(); |
| 518 |
extras = in.readPersistableBundle(); |
| 519 |
transientExtras = in.readBundle(); |
| 520 |
if (in.readInt() != 0) { |
| 521 |
clipData = ClipData.CREATOR.createFromParcel(in); |
| 522 |
clipGrantFlags = in.readInt(); |
| 523 |
} else { |
| 524 |
clipData = null; |
| 525 |
clipGrantFlags = 0; |
| 526 |
} |
| 527 |
callback = in.readStrongBinder(); |
| 528 |
overrideDeadlineExpired = in.readInt() == 1; |
| 529 |
mIsExpedited = in.readBoolean(); |
| 530 |
mTriggeredContentUris = in.createTypedArray(Uri.CREATOR); |
| 531 |
mTriggeredContentAuthorities = in.createStringArray(); |
| 532 |
if (in.readInt() != 0) { |
| 533 |
network = Network.CREATOR.createFromParcel(in); |
| 534 |
} else { |
| 535 |
network = null; |
| 536 |
} |
| 537 |
mStopReason = in.readInt(); |
| 538 |
mInternalStopReason = in.readInt(); |
| 539 |
debugStopReason = in.readString(); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
public void setStopReason(@StopReason int reason, int internalStopReason, |
| 544 |
String debugStopReason) { |
| 545 |
mStopReason = reason; |
| 546 |
mInternalStopReason = internalStopReason; |
| 547 |
this.debugStopReason = debugStopReason; |
| 548 |
} |
| 549 |
|
| 550 |
@Override |
| 551 |
public int describeContents() { |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
@Override |
| 556 |
public void writeToParcel(Parcel dest, int flags) { |
| 557 |
dest.writeInt(jobId); |
| 558 |
dest.writePersistableBundle(extras); |
| 559 |
dest.writeBundle(transientExtras); |
| 560 |
if (clipData != null) { |
| 561 |
dest.writeInt(1); |
| 562 |
clipData.writeToParcel(dest, flags); |
| 563 |
dest.writeInt(clipGrantFlags); |
| 564 |
} else { |
| 565 |
dest.writeInt(0); |
| 566 |
} |
| 567 |
dest.writeStrongBinder(callback); |
| 568 |
dest.writeInt(overrideDeadlineExpired ? 1 : 0); |
| 569 |
dest.writeBoolean(mIsExpedited); |
| 570 |
dest.writeTypedArray(mTriggeredContentUris, flags); |
| 571 |
dest.writeStringArray(mTriggeredContentAuthorities); |
| 572 |
if (network != null) { |
| 573 |
dest.writeInt(1); |
| 574 |
network.writeToParcel(dest, flags); |
| 575 |
} else { |
| 576 |
dest.writeInt(0); |
| 577 |
} |
| 578 |
dest.writeInt(mStopReason); |
| 579 |
dest.writeInt(mInternalStopReason); |
| 580 |
dest.writeString(debugStopReason); |
| 581 |
} |
| 582 |
|
| 583 |
public static final @android.annotation.NonNull Creator<JobParameters> CREATOR = new Creator<JobParameters>() { |
| 584 |
@Override |
| 585 |
public JobParameters createFromParcel(Parcel in) { |
| 586 |
return new JobParameters(in); |
| 587 |
} |
| 588 |
|
| 589 |
@Override |
| 590 |
public JobParameters[] newArray(int size) { |
| 591 |
return new JobParameters[size]; |
| 592 |
} |
| 593 |
}; |
| 594 |
} |