@Test @SmallTest public void testConvert_ids() { final String expectedWorkSpecId = "026e3422-9cd1-11e7-abc4-cec278b6b50a"; WorkSpec workSpec = new WorkSpec(expectedWorkSpecId, TestWorker.class.getName()); JobInfo jobInfo = mConverter.convert(workSpec, JOB_ID); String actualWorkSpecId = jobInfo.getExtras().getString( SystemJobInfoConverter.EXTRA_WORK_SPEC_ID); assertThat(actualWorkSpecId, is(expectedWorkSpecId)); assertThat(jobInfo.getId(), is(JOB_ID)); }
JobInfo convert(WorkSpec workSpec, int jobId) { Constraints constraints = workSpec.constraints; int jobInfoNetworkType = convertNetworkType(constraints.getRequiredNetworkType()); PersistableBundle extras = new PersistableBundle(); extras.putString(EXTRA_WORK_SPEC_ID, workSpec.id); extras.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic()); JobInfo.Builder builder = new JobInfo.Builder(jobId, mWorkServiceComponent) .setRequiredNetworkType(jobInfoNetworkType) .setRequiresCharging(constraints.requiresCharging()) .setRequiresDeviceIdle(constraints.requiresDeviceIdle()) .setExtras(extras); if (!constraints.requiresDeviceIdle()) { int backoffPolicy = workSpec.backoffPolicy == BackoffPolicy.LINEAR ? JobInfo.BACKOFF_POLICY_LINEAR : JobInfo.BACKOFF_POLICY_EXPONENTIAL; builder.setBackoffCriteria(workSpec.backoffDelayDuration, backoffPolicy); } if (workSpec.isPeriodic()) { if (Build.VERSION.SDK_INT >= 24) { builder.setPeriodic(workSpec.intervalDuration, workSpec.flexDuration); } else { Log.d(TAG, "Flex duration is currently not supported before API 24. Ignoring."); builder.setPeriodic(workSpec.intervalDuration); } } else { builder.setMinimumLatency(workSpec.initialDelay); } if (Build.VERSION.SDK_INT >= 24 && constraints.hasContentUriTriggers()) { for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers()) { builder.addTriggerContentUri(convertContentUriTrigger(trigger)); } } builder.setPersisted(false); if (Build.VERSION.SDK_INT >= 26) { builder.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow()); builder.setRequiresStorageNotLow(constraints.requiresStorageNotLow()); } return builder.build(); }
SystemJobInfoConverter { JobInfo convert(WorkSpec workSpec, int jobId) { Constraints constraints = workSpec.constraints; int jobInfoNetworkType = convertNetworkType(constraints.getRequiredNetworkType()); PersistableBundle extras = new PersistableBundle(); extras.putString(EXTRA_WORK_SPEC_ID, workSpec.id); extras.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic()); JobInfo.Builder builder = new JobInfo.Builder(jobId, mWorkServiceComponent) .setRequiredNetworkType(jobInfoNetworkType) .setRequiresCharging(constraints.requiresCharging()) .setRequiresDeviceIdle(constraints.requiresDeviceIdle()) .setExtras(extras); if (!constraints.requiresDeviceIdle()) { int backoffPolicy = workSpec.backoffPolicy == BackoffPolicy.LINEAR ? JobInfo.BACKOFF_POLICY_LINEAR : JobInfo.BACKOFF_POLICY_EXPONENTIAL; builder.setBackoffCriteria(workSpec.backoffDelayDuration, backoffPolicy); } if (workSpec.isPeriodic()) { if (Build.VERSION.SDK_INT >= 24) { builder.setPeriodic(workSpec.intervalDuration, workSpec.flexDuration); } else { Log.d(TAG, "Flex duration is currently not supported before API 24. Ignoring."); builder.setPeriodic(workSpec.intervalDuration); } } else { builder.setMinimumLatency(workSpec.initialDelay); } if (Build.VERSION.SDK_INT >= 24 && constraints.hasContentUriTriggers()) { for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers()) { builder.addTriggerContentUri(convertContentUriTrigger(trigger)); } } builder.setPersisted(false); if (Build.VERSION.SDK_INT >= 26) { builder.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow()); builder.setRequiresStorageNotLow(constraints.requiresStorageNotLow()); } return builder.build(); } }
SystemJobInfoConverter { JobInfo convert(WorkSpec workSpec, int jobId) { Constraints constraints = workSpec.constraints; int jobInfoNetworkType = convertNetworkType(constraints.getRequiredNetworkType()); PersistableBundle extras = new PersistableBundle(); extras.putString(EXTRA_WORK_SPEC_ID, workSpec.id); extras.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic()); JobInfo.Builder builder = new JobInfo.Builder(jobId, mWorkServiceComponent) .setRequiredNetworkType(jobInfoNetworkType) .setRequiresCharging(constraints.requiresCharging()) .setRequiresDeviceIdle(constraints.requiresDeviceIdle()) .setExtras(extras); if (!constraints.requiresDeviceIdle()) { int backoffPolicy = workSpec.backoffPolicy == BackoffPolicy.LINEAR ? JobInfo.BACKOFF_POLICY_LINEAR : JobInfo.BACKOFF_POLICY_EXPONENTIAL; builder.setBackoffCriteria(workSpec.backoffDelayDuration, backoffPolicy); } if (workSpec.isPeriodic()) { if (Build.VERSION.SDK_INT >= 24) { builder.setPeriodic(workSpec.intervalDuration, workSpec.flexDuration); } else { Log.d(TAG, "Flex duration is currently not supported before API 24. Ignoring."); builder.setPeriodic(workSpec.intervalDuration); } } else { builder.setMinimumLatency(workSpec.initialDelay); } if (Build.VERSION.SDK_INT >= 24 && constraints.hasContentUriTriggers()) { for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers()) { builder.addTriggerContentUri(convertContentUriTrigger(trigger)); } } builder.setPersisted(false); if (Build.VERSION.SDK_INT >= 26) { builder.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow()); builder.setRequiresStorageNotLow(constraints.requiresStorageNotLow()); } return builder.build(); } @VisibleForTesting(otherwise = PACKAGE_PRIVATE)  SystemJobInfoConverter(@NonNull Context context); }
SystemJobInfoConverter { JobInfo convert(WorkSpec workSpec, int jobId) { Constraints constraints = workSpec.constraints; int jobInfoNetworkType = convertNetworkType(constraints.getRequiredNetworkType()); PersistableBundle extras = new PersistableBundle(); extras.putString(EXTRA_WORK_SPEC_ID, workSpec.id); extras.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic()); JobInfo.Builder builder = new JobInfo.Builder(jobId, mWorkServiceComponent) .setRequiredNetworkType(jobInfoNetworkType) .setRequiresCharging(constraints.requiresCharging()) .setRequiresDeviceIdle(constraints.requiresDeviceIdle()) .setExtras(extras); if (!constraints.requiresDeviceIdle()) { int backoffPolicy = workSpec.backoffPolicy == BackoffPolicy.LINEAR ? JobInfo.BACKOFF_POLICY_LINEAR : JobInfo.BACKOFF_POLICY_EXPONENTIAL; builder.setBackoffCriteria(workSpec.backoffDelayDuration, backoffPolicy); } if (workSpec.isPeriodic()) { if (Build.VERSION.SDK_INT >= 24) { builder.setPeriodic(workSpec.intervalDuration, workSpec.flexDuration); } else { Log.d(TAG, "Flex duration is currently not supported before API 24. Ignoring."); builder.setPeriodic(workSpec.intervalDuration); } } else { builder.setMinimumLatency(workSpec.initialDelay); } if (Build.VERSION.SDK_INT >= 24 && constraints.hasContentUriTriggers()) { for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers()) { builder.addTriggerContentUri(convertContentUriTrigger(trigger)); } } builder.setPersisted(false); if (Build.VERSION.SDK_INT >= 26) { builder.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow()); builder.setRequiresStorageNotLow(constraints.requiresStorageNotLow()); } return builder.build(); } @VisibleForTesting(otherwise = PACKAGE_PRIVATE)  SystemJobInfoConverter(@NonNull Context context);  }
SystemJobInfoConverter { JobInfo convert(WorkSpec workSpec, int jobId) { Constraints constraints = workSpec.constraints; int jobInfoNetworkType = convertNetworkType(constraints.getRequiredNetworkType()); PersistableBundle extras = new PersistableBundle(); extras.putString(EXTRA_WORK_SPEC_ID, workSpec.id); extras.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic()); JobInfo.Builder builder = new JobInfo.Builder(jobId, mWorkServiceComponent) .setRequiredNetworkType(jobInfoNetworkType) .setRequiresCharging(constraints.requiresCharging()) .setRequiresDeviceIdle(constraints.requiresDeviceIdle()) .setExtras(extras); if (!constraints.requiresDeviceIdle()) { int backoffPolicy = workSpec.backoffPolicy == BackoffPolicy.LINEAR ? JobInfo.BACKOFF_POLICY_LINEAR : JobInfo.BACKOFF_POLICY_EXPONENTIAL; builder.setBackoffCriteria(workSpec.backoffDelayDuration, backoffPolicy); } if (workSpec.isPeriodic()) { if (Build.VERSION.SDK_INT >= 24) { builder.setPeriodic(workSpec.intervalDuration, workSpec.flexDuration); } else { Log.d(TAG, "Flex duration is currently not supported before API 24. Ignoring."); builder.setPeriodic(workSpec.intervalDuration); } } else { builder.setMinimumLatency(workSpec.initialDelay); } if (Build.VERSION.SDK_INT >= 24 && constraints.hasContentUriTriggers()) { for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers()) { builder.addTriggerContentUri(convertContentUriTrigger(trigger)); } } builder.setPersisted(false); if (Build.VERSION.SDK_INT >= 26) { builder.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow()); builder.setRequiresStorageNotLow(constraints.requiresStorageNotLow()); } return builder.build(); } @VisibleForTesting(otherwise = PACKAGE_PRIVATE)  SystemJobInfoConverter(@NonNull Context context);   }