@Test @Config(sdk = Build.VERSION_CODES.LOLLIPOP, shadows = {ShadowGoogleApiAvailability.class}) public void testSchedulerInApi21() { JobInfo api21Job = JobCreator.create(application).setRequiresCharging(true).build(); JobInfo api24Job = JobCreator.create(application).setPeriodic(15 * 60 * 1000L, 5 * 60 * 1000L).build(); JobInfo api26Job = JobCreator.create(application).setRequiresBatteryNotLow(true).build(); ShadowGoogleApiAvailability.setIsGooglePlayServicesAvailable(ConnectionResult.SERVICE_MISSING); assertThat(jobScheduler.getSchedulerForJob(application, api21Job), instanceOf(JobSchedulerSchedulerV21.class)); assertThat(jobScheduler.getSchedulerForJob(application, api24Job), instanceOf(AlarmScheduler.class)); assertThat(jobScheduler.getSchedulerForJob(application, api26Job), instanceOf(AlarmScheduler.class)); ShadowGoogleApiAvailability.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS); assertThat(jobScheduler.getSchedulerForJob(application, api21Job), instanceOf(JobSchedulerSchedulerV21.class)); assertThat(jobScheduler.getSchedulerForJob(application, api24Job), instanceOf(GcmScheduler.class)); assertThat(jobScheduler.getSchedulerForJob(application, api26Job), instanceOf(AlarmScheduler.class)); }
@RestrictTo(RestrictTo.Scope.LIBRARY) Scheduler getSchedulerForJob(Context context, JobInfo job) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return getSchedulerForTag(context, JobSchedulerSchedulerV28.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return getSchedulerForTag(context, JobSchedulerSchedulerV26.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV24.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (!job.isPeriodic() || job.getFlexMillis() >= job.getIntervalMillis()) && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && job.getTriggerContentUris() == null && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV21.TAG); } boolean gcmAvailable; try { gcmAvailable = Class.forName("com.google.android.gms.gcm.GcmNetworkManager") != null && GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; } catch (Throwable ignored) { gcmAvailable = false; } if (gcmAvailable && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, GcmScheduler.TAG); } return getSchedulerForTag(context, AlarmScheduler.TAG); }
JobScheduler { @RestrictTo(RestrictTo.Scope.LIBRARY) Scheduler getSchedulerForJob(Context context, JobInfo job) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return getSchedulerForTag(context, JobSchedulerSchedulerV28.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return getSchedulerForTag(context, JobSchedulerSchedulerV26.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV24.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (!job.isPeriodic() || job.getFlexMillis() >= job.getIntervalMillis()) && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && job.getTriggerContentUris() == null && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV21.TAG); } boolean gcmAvailable; try { gcmAvailable = Class.forName("com.google.android.gms.gcm.GcmNetworkManager") != null && GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; } catch (Throwable ignored) { gcmAvailable = false; } if (gcmAvailable && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, GcmScheduler.TAG); } return getSchedulerForTag(context, AlarmScheduler.TAG); } }
JobScheduler { @RestrictTo(RestrictTo.Scope.LIBRARY) Scheduler getSchedulerForJob(Context context, JobInfo job) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return getSchedulerForTag(context, JobSchedulerSchedulerV28.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return getSchedulerForTag(context, JobSchedulerSchedulerV26.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV24.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (!job.isPeriodic() || job.getFlexMillis() >= job.getIntervalMillis()) && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && job.getTriggerContentUris() == null && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV21.TAG); } boolean gcmAvailable; try { gcmAvailable = Class.forName("com.google.android.gms.gcm.GcmNetworkManager") != null && GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; } catch (Throwable ignored) { gcmAvailable = false; } if (gcmAvailable && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, GcmScheduler.TAG); } return getSchedulerForTag(context, AlarmScheduler.TAG); } private  JobScheduler(Context context); }
JobScheduler { @RestrictTo(RestrictTo.Scope.LIBRARY) Scheduler getSchedulerForJob(Context context, JobInfo job) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return getSchedulerForTag(context, JobSchedulerSchedulerV28.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return getSchedulerForTag(context, JobSchedulerSchedulerV26.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV24.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (!job.isPeriodic() || job.getFlexMillis() >= job.getIntervalMillis()) && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && job.getTriggerContentUris() == null && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV21.TAG); } boolean gcmAvailable; try { gcmAvailable = Class.forName("com.google.android.gms.gcm.GcmNetworkManager") != null && GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; } catch (Throwable ignored) { gcmAvailable = false; } if (gcmAvailable && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, GcmScheduler.TAG); } return getSchedulerForTag(context, AlarmScheduler.TAG); } private  JobScheduler(Context context); static synchronized JobScheduler get(Context context); int schedule(JobInfo job); void cancel(int jobId); void cancelAll(); @NonNull List<JobInfo> getAllPendingJobs(); @Nullable JobInfo getPendingJob(int jobId); @RestrictTo(RestrictTo.Scope.LIBRARY) void onJobCompleted(int jobId, boolean needsReschedule); @RestrictTo(RestrictTo.Scope.LIBRARY) List<JobStatus> getJobsByScheduler(String scheduler); @RestrictTo(RestrictTo.Scope.LIBRARY) JobStatus getJob(int jobId); @RestrictTo(RestrictTo.Scope.LIBRARY) void addJob(JobStatus jobStatus); @RestrictTo(RestrictTo.Scope.LIBRARY) void removeJob(int jobId); }
JobScheduler { @RestrictTo(RestrictTo.Scope.LIBRARY) Scheduler getSchedulerForJob(Context context, JobInfo job) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return getSchedulerForTag(context, JobSchedulerSchedulerV28.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return getSchedulerForTag(context, JobSchedulerSchedulerV26.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV24.TAG); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (!job.isPeriodic() || job.getFlexMillis() >= job.getIntervalMillis()) && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && job.getTriggerContentUris() == null && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, JobSchedulerSchedulerV21.TAG); } boolean gcmAvailable; try { gcmAvailable = Class.forName("com.google.android.gms.gcm.GcmNetworkManager") != null && GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; } catch (Throwable ignored) { gcmAvailable = false; } if (gcmAvailable && job.getNetworkType() != JobInfo.NETWORK_TYPE_NOT_ROAMING && job.getNetworkType() != JobInfo.NETWORK_TYPE_CELLULAR && !job.isRequireBatteryNotLow() && !job.isRequireStorageNotLow()) { return getSchedulerForTag(context, GcmScheduler.TAG); } return getSchedulerForTag(context, AlarmScheduler.TAG); } private  JobScheduler(Context context); static synchronized JobScheduler get(Context context); int schedule(JobInfo job); void cancel(int jobId); void cancelAll(); @NonNull List<JobInfo> getAllPendingJobs(); @Nullable JobInfo getPendingJob(int jobId); @RestrictTo(RestrictTo.Scope.LIBRARY) void onJobCompleted(int jobId, boolean needsReschedule); @RestrictTo(RestrictTo.Scope.LIBRARY) List<JobStatus> getJobsByScheduler(String scheduler); @RestrictTo(RestrictTo.Scope.LIBRARY) JobStatus getJob(int jobId); @RestrictTo(RestrictTo.Scope.LIBRARY) void addJob(JobStatus jobStatus); @RestrictTo(RestrictTo.Scope.LIBRARY) void removeJob(int jobId); static final int RESULT_FAILURE; static final int RESULT_SUCCESS; }