JobCoder { @Nullable public JobInvocation.Builder decode(@NonNull Bundle providedBundle) { if (providedBundle == null) { throw new IllegalArgumentException("Unexpected null Bundle provided"); } Bundle data = new Bundle(providedBundle); boolean recur = data.getBoolean(prefix + BundleProtocol.PACKED_PARAM_RECURRING); boolean replaceCur = data.getBoolean(prefix + BundleProtocol.PACKED_PARAM_REPLACE_CURRENT); int lifetime = data.getInt(prefix + BundleProtocol.PACKED_PARAM_LIFETIME); int[] constraints = uncompact(data.getInt(prefix + BundleProtocol.PACKED_PARAM_CONSTRAINTS)); JobTrigger trigger = decodeTrigger(data); RetryStrategy retryStrategy = decodeRetryStrategy(data); String tag = data.getString(prefix + BundleProtocol.PACKED_PARAM_TAG); String service = data.getString(prefix + BundleProtocol.PACKED_PARAM_SERVICE); if (tag == null || service == null || trigger == null || retryStrategy == null) { return null; } JobInvocation.Builder builder = new JobInvocation.Builder(); builder.setTag(tag); builder.setService(service); builder.setTrigger(trigger); builder.setRetryStrategy(retryStrategy); builder.setRecurring(recur); builder.setLifetime(lifetime); builder.setConstraints(constraints); builder.setReplaceCurrent(replaceCur); if (!TextUtils.isEmpty(prefix)) { Iterator<String> keyIterator = data.keySet().iterator(); while (keyIterator.hasNext()) { String key = keyIterator.next(); if (key.startsWith(prefix)) { keyIterator.remove(); } } } builder.addExtras(data); return builder; } JobCoder(String prefix); @Nullable JobInvocation.Builder decode(@NonNull Bundle providedBundle);  }
@Test(expected = IllegalArgumentException.class) public void testDecode_throwsOnNullBundle() { coder.decode(null); }
@Test public void decode_contentUriTrigger() { ContentUriTrigger contentUriTrigger = TestUtil.getContentUriTrigger(); Bundle bundle = TestUtil.encodeContentUriJob(contentUriTrigger, coder); JobInvocation decode = coder.decode(bundle).build(); ContentUriTrigger trigger = (ContentUriTrigger) decode.getTrigger(); assertEquals(contentUriTrigger.getUris(), trigger.getUris()); }
@Test public void decode_addBundleAsExtras() { ContentUriTrigger contentUriTrigger = TestUtil.getContentUriTrigger(); Bundle bundle = TestUtil.encodeContentUriJob(contentUriTrigger, coder); bundle.putString("test_key", "test_value"); JobInvocation decode = coder.decode(bundle).build(); assertEquals("test_value", decode.getExtras().getString("test_key")); }