private static void writeConstraintsToBundle(JobParameters job, Bundle b) { int c = Constraint.compact(job.getConstraints()); b.putBoolean( REQUEST_PARAM_REQUIRES_CHARGING, (c & Constraint.DEVICE_CHARGING) == Constraint.DEVICE_CHARGING); b.putBoolean( REQUEST_PARAM_REQUIRES_IDLE, (c & Constraint.DEVICE_IDLE) == Constraint.DEVICE_IDLE); b.putInt(REQUEST_PARAM_REQUIRED_NETWORK, convertConstraintsToLegacyNetConstant(c)); }
/** * Returns true iff all the specified job constraints are satisfied. Note: At the moment this * method only checks for network constraints and nothing more. * * @param job the job whose constraints are to be checked. */ public boolean areConstraintsSatisfied(JobInvocation job) { int jobConstraints = Constraint.compact(job.getConstraints()); return areNetworkConstraintsSatisfied(jobConstraints); }
@NonNull Bundle encode(@NonNull JobParameters jobParameters, @NonNull Bundle data) { if (data == null) { throw new IllegalArgumentException("Unexpected null Bundle provided"); } Bundle userExtras = jobParameters.getExtras(); if (userExtras != null) { data.putAll(userExtras); } data.putInt(prefix + BundleProtocol.PACKED_PARAM_LIFETIME, jobParameters.getLifetime()); data.putBoolean(prefix + BundleProtocol.PACKED_PARAM_RECURRING, jobParameters.isRecurring()); data.putBoolean( prefix + BundleProtocol.PACKED_PARAM_REPLACE_CURRENT, jobParameters.shouldReplaceCurrent()); data.putString(prefix + BundleProtocol.PACKED_PARAM_TAG, jobParameters.getTag()); data.putString(prefix + BundleProtocol.PACKED_PARAM_SERVICE, jobParameters.getService()); data.putInt( prefix + BundleProtocol.PACKED_PARAM_CONSTRAINTS, compact(jobParameters.getConstraints())); encodeTrigger(jobParameters.getTrigger(), data); encodeRetryStrategy(jobParameters.getRetryStrategy(), data); return data; }
@Test public void compactNull() { assertEquals(0, Constraint.compact(null)); } }
@Test public void testCompactAndUnCompact() { for (List<Integer> combo : TestUtil.getAllConstraintCombinations()) { int[] input = TestUtil.toIntArray(combo); Arrays.sort(input); int[] output = Constraint.uncompact(Constraint.compact(input)); Arrays.sort(output); for (int i = 0; i < combo.size(); i++) { assertEquals("Combination = " + TextUtils.join(", ", combo), input[i], output[i]); } assertEquals("Expected length of two arrays to be the same", input.length, output.length); } }
@Test public void testAddConstraints() { builder .setConstraints() .addConstraint(Constraint.DEVICE_CHARGING) .addConstraint(Constraint.ON_UNMETERED_NETWORK); int[] expected = {Constraint.DEVICE_CHARGING, Constraint.ON_UNMETERED_NETWORK}; assertEquals(Constraint.compact(expected), Constraint.compact(builder.getConstraints())); }
static void assertJobsEqual(JobParameters input, JobParameters output) { assertNotNull("input", input); assertNotNull("output", output); assertEquals("isRecurring()", input.isRecurring(), output.isRecurring()); assertEquals( "shouldReplaceCurrent()", input.shouldReplaceCurrent(), output.shouldReplaceCurrent()); assertEquals("getLifetime()", input.getLifetime(), output.getLifetime()); assertEquals("getTag()", input.getTag(), output.getTag()); assertEquals("getService()", input.getService(), output.getService()); assertEquals( "getConstraints()", Constraint.compact(input.getConstraints()), Constraint.compact(output.getConstraints())); assertTriggersEqual(input.getTrigger(), output.getTrigger()); assertBundlesEqual(input.getExtras(), output.getExtras()); assertRetryStrategiesEqual(input.getRetryStrategy(), output.getRetryStrategy()); }