@NonNull public static JobApi getDefault(Context context) { if (WORK_MANAGER.isSupported(context) && JobConfig.isApiEnabled(WORK_MANAGER)) { return WORK_MANAGER; } else if (V_26.isSupported(context) && JobConfig.isApiEnabled(V_26)) { return V_26; } else if (V_24.isSupported(context) && JobConfig.isApiEnabled(V_24)) { return V_24; } else if (V_21.isSupported(context) && JobConfig.isApiEnabled(V_21)) { return V_21; } else if (GCM.isSupported(context) && JobConfig.isApiEnabled(GCM)) { return GCM; } else if (V_19.isSupported(context) && JobConfig.isApiEnabled(V_19)) { return V_19; } else if (JobConfig.isApiEnabled(V_14)) { return V_14; } else { throw new IllegalStateException("All supported APIs are disabled"); } } }
if (JobApi.WORK_MANAGER.isSupported(this)) { menu.findItem(R.id.action_force_work).setChecked(false); } else { menu.findItem(R.id.action_force_work).setVisible(false); if (JobApi.V_26.isSupported(this)) { menu.findItem(R.id.action_force_26).setChecked(false); } else { menu.findItem(R.id.action_force_26).setVisible(false); if (JobApi.V_24.isSupported(this)) { menu.findItem(R.id.action_force_24).setChecked(false); } else { menu.findItem(R.id.action_force_24).setVisible(false); if (JobApi.V_21.isSupported(this)) { menu.findItem(R.id.action_force_21).setChecked(false); } else { menu.findItem(R.id.action_force_21).setVisible(false); if (JobApi.V_19.isSupported(this)) { menu.findItem(R.id.action_force_19).setChecked(false); } else { menu.findItem(R.id.action_force_19).setVisible(false); if (JobApi.V_14.isSupported(this)) { menu.findItem(R.id.action_force_14).setChecked(false); } else { menu.findItem(R.id.action_force_14).setVisible(false);
private void testAllImpl() { for (JobApi api : JobApi.values()) { if (api.isSupported(this)) { JobConfig.forceApi(api); testSimple(); } else { Log.w("Demo", String.format("%s is not supported", api)); } } JobConfig.reset(); }
@Test public void verifyJobApiNotSupportedWhenServiceIsDisabled() { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { assertThat(JobApi.V_26.isSupported(mContext)).isTrue(); } assertThat(JobApi.V_24.isSupported(mContext)).isTrue(); assertThat(JobApi.V_21.isSupported(mContext)).isTrue(); mPackageManager.setComponentEnabledSetting(mComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); assertThat(JobApi.V_26.isSupported(mContext)).isFalse(); assertThat(JobApi.V_24.isSupported(mContext)).isFalse(); assertThat(JobApi.V_21.isSupported(mContext)).isFalse(); } }
/*package*/ static void cleanUpOrphanedJob(Context context, int jobId) { /* * That's necessary if the database was deleted and jobs (especially the JobScheduler) are still around. * Then if a new job is being scheduled, it's possible that the new job has the ID of the old one. Here * we make sure, that no job is left in the system. */ for (JobApi jobApi : JobApi.values()) { if (jobApi.isSupported(context)) { try { jobApi.getProxy(context).cancel(jobId); } catch (Exception ignored) { // GCM API could crash if it's disabled, ignore crashes at this point and continue } } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mJobManager = JobManager.instance(); if (savedInstanceState != null) { mLastJobId = savedInstanceState.getInt(LAST_JOB_ID, 0); } CompoundButton enableGcm = findViewById(R.id.enable_gcm); mRequiresCharging = findViewById(R.id.check_requires_charging); mRequiresDeviceIdle = findViewById(R.id.check_requires_device_idle); mNetworkTypeSpinner = findViewById(R.id.spinner_network_type); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, getNetworkTypesAsString()); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mNetworkTypeSpinner.setAdapter(adapter); enableGcm.setChecked(JobConfig.isApiEnabled(JobApi.GCM)); enableGcm.setEnabled(JobApi.GCM.isSupported(this)); enableGcm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { JobConfig.setApiEnabled(JobApi.GCM, isChecked); } }); }
if (api == JobApi.V_14 && !api.isSupported(context)) { throw new JobManagerCreateException("All APIs are disabled, cannot schedule any job");
@Test public void verifyNativeImplementationIsUsedWithO() throws Exception { // ignore test if not supported assumeTrue(JobApi.V_26.isSupported(InstrumentationRegistry.getTargetContext())); JobConfig.forceApi(JobApi.V_26); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
throw e; } else { jobApi = JobApi.V_19.isSupported(mContext) ? JobApi.V_19 : JobApi.V_14; // try one last time
@NonNull public static JobApi getDefault(Context context) { if (V_26.isSupported(context) && JobConfig.isApiEnabled(V_26)) { return V_26; } else if (V_24.isSupported(context) && JobConfig.isApiEnabled(V_24)) { return V_24; } else if (V_21.isSupported(context) && JobConfig.isApiEnabled(V_21)) { return V_21; } else if (GCM.isSupported(context) && JobConfig.isApiEnabled(GCM)) { return GCM; } else if (V_19.isSupported(context) && JobConfig.isApiEnabled(V_19)) { return V_19; } else if (JobConfig.isApiEnabled(V_14)) { return V_14; } else { throw new IllegalStateException("All supported APIs are disabled"); } } }
/*package*/ static void cleanUpOrphanedJob(Context context, int jobId) { /* * That's necessary if the database was deleted and jobs (especially the JobScheduler) are still around. * Then if a new job is being scheduled, it's possible that the new job has the ID of the old one. Here * we make sure, that no job is left in the system. */ for (JobApi jobApi : JobApi.values()) { if (jobApi.isSupported(context)) { try { jobApi.getProxy(context).cancel(jobId); } catch (Exception ignored) { // GCM API could crash if it's disabled, ignore crashes at this point and continue } } } }
if (api == JobApi.V_14 && !api.isSupported(context)) { throw new JobManagerCreateException("All APIs are disabled, cannot schedule any job");
throw e; } else { jobApi = JobApi.V_19.isSupported(mContext) ? JobApi.V_19 : JobApi.V_14; // try one last time