@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"); } } }
@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); } }); }
@Test public void verifyForceApiDisabledOtherApis() { JobApi forcedApi = JobApi.GCM; for (JobApi api : JobApi.values()) { assertThat(JobConfig.isApiEnabled(api)).isTrue(); } JobConfig.forceApi(forcedApi); for (JobApi api : JobApi.values()) { assertThat(JobConfig.isApiEnabled(api)).isEqualTo(api == forcedApi); } }
@Test @Config(sdk = Build.VERSION_CODES.LOLLIPOP) public void verifyReset() { assertThat(JobConfig.isApiEnabled(JobApi.V_19)).isTrue(); // default JobConfig.setApiEnabled(JobApi.V_19, false); assertThat(JobConfig.isApiEnabled(JobApi.V_19)).isFalse(); // did change assertThat(JobConfig.isAllowSmallerIntervalsForMarshmallow()).isFalse(); // default JobConfig.setAllowSmallerIntervalsForMarshmallow(true); assertThat(JobConfig.isAllowSmallerIntervalsForMarshmallow()).isTrue(); // did change JobConfig.reset(); assertThat(JobConfig.isApiEnabled(JobApi.V_19)).isTrue(); // default assertThat(JobConfig.isAllowSmallerIntervalsForMarshmallow()).isFalse(); // default }
@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"); } } }