Java Code Examples for com.evernote.android.job.JobRequest#Builder

The following examples show how to use com.evernote.android.job.JobRequest#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnitTestDatabaseCreator.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Override
public void createPeriodic() {
    for (int i = 0; i < 10; i++) {
        JobRequest.Builder builder = new JobRequest.Builder("tag")
                .setPeriodic(TimeUnit.MINUTES.toMillis(1))
                .setRequiresCharging(random())
                .setRequiresDeviceIdle(random())
                .setRequiredNetworkType(random() ? JobRequest.NetworkType.ANY : JobRequest.NetworkType.CONNECTED)
                .setRequirementsEnforced(random());

        if (random()) {
            PersistableBundleCompat extras = new PersistableBundleCompat();
            extras.putString("key", "Hello world");
            builder.setExtras(extras);
        }

        builder.build().schedule();
    }
}
 
Example 2
Source File: UnitTestDatabaseCreator.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Override
public void createPeriodic() {
    for (int i = 0; i < 10; i++) {
        JobRequest.Builder builder = new JobRequest.Builder("tag")
                .setRequiresCharging(random())
                .setRequiresDeviceIdle(random())
                .setRequiredNetworkType(random() ? JobRequest.NetworkType.ANY : JobRequest.NetworkType.CONNECTED)
                .setRequirementsEnforced(random());

        if (random()) {
            PersistableBundleCompat extras = new PersistableBundleCompat();
            extras.putString("key", "Hello world");
            builder.setExtras(extras);
        }
        if (random()) {
            builder.setPeriodic(JobRequest.MIN_INTERVAL);
        } else {
            builder.setPeriodic(JobRequest.MIN_INTERVAL, JobRequest.MIN_FLEX);
        }

        builder.build().schedule();
    }
}
 
Example 3
Source File: UnitTestDatabaseCreator.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Override
public void createPeriodic() {
    for (int i = 0; i < 10; i++) {
        JobRequest.Builder builder = new JobRequest.Builder("tag")
                .setRequiresCharging(random())
                .setRequiresDeviceIdle(random())
                .setRequiresBatteryNotLow(random())
                .setRequiresStorageNotLow(random())
                .setRequiredNetworkType(random() ? JobRequest.NetworkType.ANY : JobRequest.NetworkType.CONNECTED)
                .setRequirementsEnforced(random());

        if (random()) {
            PersistableBundleCompat extras = new PersistableBundleCompat();
            extras.putString("key", "Hello world");
            builder.setExtras(extras);
        }
        if (random()) {
            builder.setPeriodic(JobRequest.MIN_INTERVAL);
        } else {
            builder.setPeriodic(JobRequest.MIN_INTERVAL, JobRequest.MIN_FLEX);
        }

        builder.build().schedule();
    }
}
 
Example 4
Source File: PlatformWorkManagerTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransientExtras() {
    Bundle extras = new Bundle();
    extras.putInt("key", 5);

    JobRequest.Builder builder = new JobRequest.Builder(TAG)
            .setExecutionWindow(TimeUnit.HOURS.toMillis(4), TimeUnit.HOURS.toMillis(5))
            .setTransientExtras(extras);

    int jobId = builder.build().schedule();

    Bundle bundle = TransientBundleHolder.getBundle(jobId);
    assertThat(bundle).isNotNull();
    assertThat(bundle.getInt("key")).isEqualTo(5);

    mWorkManagerRule.getManager().cancel(jobId);
    assertThat(TransientBundleHolder.getBundle(jobId)).isNull();

    jobId = builder.build().schedule();
    mWorkManagerRule.runJob(JobProxyWorkManager.createTag(jobId));

    assertThat(TransientBundleHolder.getBundle(jobId)).isNull();
}
 
Example 5
Source File: PlatformWorkManagerTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
private void testConstraints(JobRequest.Builder builder) {
    int jobId = builder
            .setRequiredNetworkType(JobRequest.NetworkType.METERED)
            .setRequiresBatteryNotLow(true)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(true)
            .setRequiresStorageNotLow(true)
            .build()
            .schedule();

    String tag = JobProxyWorkManager.createTag(jobId);
    List<WorkInfo> statuses = mWorkManagerRule.getWorkStatus(tag);

    assertThat(statuses).isNotNull().hasSize(1);
    assertThat(statuses.get(0).getState()).isEqualTo(WorkInfo.State.ENQUEUED);

    mWorkManagerRule.getManager().cancelAllForTag(TAG);
    assertThat(mWorkManagerRule.getWorkStatus(tag).get(0).getState()).isEqualTo(WorkInfo.State.CANCELLED);
}
 
Example 6
Source File: AndroidJobStrategy.java    From cloudinary_android with MIT License 6 votes vote down vote up
static JobRequest adapt(UploadRequest request) {
    PersistableBundleCompat extras = new PersistableBundleCompat();
    request.populateParamsFromFields(new AndroidJobRequestParams(extras));

    UploadPolicy policy = request.getUploadPolicy();

    JobRequest.Builder builder = new JobRequest.Builder(JOB_TAG)
            .setBackoffCriteria(policy.getBackoffMillis(), adaptPolicy(policy.getBackoffPolicy()))
            .setExtras(extras)
            .setExecutionWindow(request.getTimeWindow().getMinLatencyOffsetMillis(), request.getTimeWindow().getMaxExecutionDelayMillis())
            .setRequiredNetworkType(adaptNetworkType(policy.getNetworkType()))
            .setRequiresCharging(policy.isRequiresCharging())
            .setRequiresDeviceIdle(policy.isRequiresIdle())
            .setRequirementsEnforced(true);

    return builder.build();
}
 
Example 7
Source File: AndroidJobStrategy.java    From cloudinary_android with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void executeRequestsNow(int howMany) {
    int started = 0;
    for (JobRequest jobRequest : JobManager.instance().getAllJobRequests()) {
        if (isSoonButNotImmediate(jobRequest)) {
            JobRequest.Builder builder = jobRequest.cancelAndEdit();
            long endMillis = Math.max(jobRequest.getEndMs(), RUN_NOW_TIME_WINDOW_END);
            builder.setExecutionWindow(RUN_NOW_TIME_WINDOW_START, endMillis).build().schedule();
            started++;
        }

        if (started == howMany) {
            break;
        }
    }

    Logger.d(TAG, String.format("Job scheduled started %d requests.", started));
}
 
Example 8
Source File: DummyJobs.java    From android-job with Apache License 2.0 5 votes vote down vote up
public static JobRequest.Builder createBuilder(Class<? extends Job> jobClass) {
    try {
        String tag = (String) jobClass.getDeclaredField("TAG").get(null);
        return new JobRequest.Builder(tag);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}