|
23 | 23 | // This allows using ether a IntentService even for Android 8.0 (API 26) when needed.
|
24 | 24 | // - This allows starting NotificationExtenderService on a high priority FCM message
|
25 | 25 | // - Even when the device is in doze mode.
|
| 26 | +// Has fallback handling to job service if IllegalStateException is thrown when starting service. |
| 27 | +// ==== Testing ==== |
26 | 28 | // - The following can be used to confirm;
|
27 | 29 | // - adb shell dumpsys deviceidle force-idle
|
28 | 30 | // - And the following to undo
|
@@ -314,7 +316,7 @@ public boolean onStartJob(JobParameters params) {
|
314 | 316 |
|
315 | 317 | @Override
|
316 | 318 | public boolean onStopJob(JobParameters params) {
|
317 |
| - if (DEBUG) Log.d(TAG, "onStartJob: " + params); |
| 319 | + if (DEBUG) Log.d(TAG, "onStopJob: " + params); |
318 | 320 | boolean result = mService.doStopCurrentWork();
|
319 | 321 | synchronized (mLock) {
|
320 | 322 | // Once we return, the job is stopped, so its JobParameters are no
|
@@ -531,7 +533,21 @@ public static void enqueueWork(@NonNull Context context, @NonNull ComponentName
|
531 | 533 | synchronized (sLock) {
|
532 | 534 | WorkEnqueuer we = getWorkEnqueuer(context, component, true, jobId, useWakefulService);
|
533 | 535 | we.ensureJobId(jobId);
|
534 |
| - we.enqueueWork(work); |
| 536 | + |
| 537 | + // Can throw on API 26+ if useWakefulService=true and app is NOT whitelisted. |
| 538 | + // One example is when an FCM high priority message is received the system will |
| 539 | + // temporarily whitelist the app. However it is possible that it does not end up getting |
| 540 | + // whitelisted so we need to catch this and fall back to a job service. |
| 541 | + try { |
| 542 | + we.enqueueWork(work); |
| 543 | + } catch (IllegalStateException e) { |
| 544 | + if (useWakefulService) { |
| 545 | + we = getWorkEnqueuer(context, component, true, jobId, false); |
| 546 | + we.enqueueWork(work); |
| 547 | + } |
| 548 | + else |
| 549 | + throw e; |
| 550 | + } |
535 | 551 | }
|
536 | 552 | }
|
537 | 553 |
|
|
0 commit comments