com.evernote.android.job.util.support.PersistableBundleCompat Java Examples

The following examples show how to use com.evernote.android.job.util.support.PersistableBundleCompat. 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: RNJob.java    From react-native-background-task with MIT License 6 votes vote down vote up
@Override
@NonNull
protected Result onRunJob(Params params) {
    Log.d(TAG, "Job is running");
    PersistableBundleCompat requestExtras = params.getExtras();

    // Two different types of bundle so need to re-package
    Bundle headlessExtras = new Bundle();
    headlessExtras.putInt("timeout", requestExtras.getInt("timeout", 30));

    Context context = getContext().getApplicationContext();
    Intent service = new Intent(context, HeadlessTaskService.class);
    service.putExtras(headlessExtras);
    context.startService(service);

    return Result.SUCCESS;
}
 
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")
                .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 #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())
                .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: 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 #5
Source File: JobRequestTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeriodic() {
    long interval = JobRequest.MIN_INTERVAL * 5;
    JobRequest request = getBuilder()
            .setPeriodic(interval)
            .setExtras(new PersistableBundleCompat())
            .build();

    assertThat(request.getJobId()).isGreaterThan(0);
    assertThat(request.getTag()).isEqualTo(DummyJobs.SuccessJob.TAG);
    assertThat(request.getIntervalMs()).isEqualTo(interval);
    assertThat(request.getFlexMs()).isEqualTo(interval);
    assertThat(request.isPeriodic()).isTrue();
    assertThat(request.isFlexSupport()).isFalse();

    assertThat(request.getStartMs()).isNegative();
    assertThat(request.getEndMs()).isNegative();
    assertThat(request.getBackoffMs()).isEqualTo(JobRequest.DEFAULT_BACKOFF_MS);
    assertThat(request.getBackoffPolicy()).isEqualTo(JobRequest.DEFAULT_BACKOFF_POLICY);
    assertThat(request.getExtras()).isNotNull();
    assertThat(request.isExact()).isFalse();
    assertThat(request.requiredNetworkType()).isEqualTo(JobRequest.DEFAULT_NETWORK_TYPE);
    assertThat(request.requirementsEnforced()).isFalse();
    assertThat(request.requiresCharging()).isFalse();
    assertThat(request.requiresDeviceIdle()).isFalse();
}
 
Example #6
Source File: DailyJobTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyExtraValuesAreOverwritten() {
    long start = 0;
    long end = 1L;

    PersistableBundleCompat extras = new PersistableBundleCompat();
    extras.putLong("something", 9L); // make sure this value is not overwritten
    extras.putLong(DailyJob.EXTRA_START_MS, 9L); // make sure they're overwritten
    extras.putLong(DailyJob.EXTRA_END_MS, 9L);

    int jobId = DailyJob.schedule(DummyJobs.createBuilder(DummyJobs.SuccessJob.class).setExtras(extras), start, end);
    JobRequest request = manager().getJobRequest(jobId);

    assertThat(request).isNotNull();
    assertThat(request.getExtras().getLong(DailyJob.EXTRA_START_MS, -1)).isEqualTo(0L);
    assertThat(request.getExtras().getLong(DailyJob.EXTRA_END_MS, -1)).isEqualTo(1L);
    assertThat(request.getExtras().size()).isEqualTo(3);
}
 
Example #7
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 #8
Source File: SendMessageJob.java    From weMessage with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void performJob(String taskIdentifier, String messageId){
    PersistableBundleCompat persistableBundleCompat = new PersistableBundleCompat();
    persistableBundleCompat.putString("taskIdentifier", taskIdentifier);
    persistableBundleCompat.putString("messageId", messageId);

    new JobRequest.Builder(SendMessageJob.TAG)
            .startNow()
            .setExtras(persistableBundleCompat)
            .build()
            .schedule();
}
 
Example #9
Source File: BackgroundTaskModule.java    From react-native-background-task with MIT License 5 votes vote down vote up
/**
 * Main point of interaction from JS users - allows them to specify the scheduling etc for the
 * background task.
 *
 * Default values are specified in JS (more accessible for the average user).
 *
 * @param config the config options passed in from JS:
 *      - period (required): how frequently to carry out the task in seconds
 *      - timeout (required): after how many seconds should the task be auto-killed
 */
@ReactMethod
public void schedule(final ReadableMap config) {
    Log.d(TAG, "@ReactMethod BackgroundTask.schedule");

    // Period can't be below 15m
    int period = config.getInt("period");
    if (period < 900) { period = 900; }

    // Flex must be between 5m and 15m
    int flex = config.getInt("flex");
    if (flex < 300) { flex = 300; }
    if (flex > 900) { flex = 900; }

    // Extra info to store with the JobRequest
    PersistableBundleCompat extras = new PersistableBundleCompat();
    extras.putInt("timeout", config.getInt("timeout"));

    mJobRequest = new JobRequest.Builder(RNJob.JOB_TAG)
            .setPeriodic(TimeUnit.SECONDS.toMillis(period), TimeUnit.SECONDS.toMillis(flex))
            .setPersisted(true)
            .setExtras(extras)
            .build();

    if (!mForeground) {
        commitSchedule();
    }
}
 
Example #10
Source File: JobRequest.java    From android-job with Apache License 2.0 5 votes vote down vote up
/**
 * @return The extras for this job.
 */
public PersistableBundleCompat getExtras() {
    if (mBuilder.mExtras == null && !TextUtils.isEmpty(mBuilder.mExtrasXml)) {
        mBuilder.mExtras = PersistableBundleCompat.fromXml(mBuilder.mExtrasXml);
    }
    return mBuilder.mExtras;
}
 
Example #11
Source File: JobRequest.java    From android-job with Apache License 2.0 5 votes vote down vote up
/**
 * Adds optional extras. This is persisted, so only primitive types are allowed. This method overrides
 * values with the same keys inside of {@code extras}, which were set before.
 *
 * @param extras Bundle containing extras which you can retrieve with {@link Job.Params#getExtras()}.
 */
public Builder addExtras(@NonNull PersistableBundleCompat extras) {
    if (mExtras == null) {
        mExtras = extras;
    } else {
        mExtras.putAll(extras);
    }
    mExtrasXml = null;
    return this;
}
 
Example #12
Source File: DailyJob.java    From android-job with Apache License 2.0 5 votes vote down vote up
/**
 * Schedules the daily job only once and runs it immediately. This is helpful if you want to reuse your job
 * and want to trigger the execution immediately. It's possible to schedule a daily job normally with
 * {@link #schedule(JobRequest.Builder, long, long)} and this method at the same time to trigger the
 * execution immediately.
 *
 * @param baseBuilder The builder of your daily job.
 * @return The unique ID for this job.
 */
public static int startNowOnce(@NonNull JobRequest.Builder baseBuilder) {
    PersistableBundleCompat extras = new PersistableBundleCompat();
    extras.putBoolean(EXTRA_ONCE, true);

    return baseBuilder
            .startNow()
            .addExtras(extras)
            .build()
            .schedule();
}
 
Example #13
Source File: DatabaseManualUpgradeTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
protected ContentValues createBaseContentValues(int id) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(JobStorage.COLUMN_ID, id);
    contentValues.put(JobStorage.COLUMN_TAG, "Tag");

    contentValues.put(JobStorage.COLUMN_START_MS, -1L);
    contentValues.put(JobStorage.COLUMN_END_MS, -1L);

    contentValues.put(JobStorage.COLUMN_BACKOFF_MS, JobRequest.DEFAULT_BACKOFF_MS);
    contentValues.put(JobStorage.COLUMN_BACKOFF_POLICY, JobRequest.DEFAULT_BACKOFF_POLICY.toString());

    contentValues.put(JobStorage.COLUMN_INTERVAL_MS, 0L);

    contentValues.put(JobStorage.COLUMN_REQUIREMENTS_ENFORCED, false);
    contentValues.put(JobStorage.COLUMN_REQUIRES_CHARGING, false);
    contentValues.put(JobStorage.COLUMN_REQUIRES_DEVICE_IDLE, false);
    contentValues.put(JobStorage.COLUMN_EXACT, false);
    contentValues.put(JobStorage.COLUMN_NETWORK_TYPE, JobRequest.DEFAULT_NETWORK_TYPE.toString());

    contentValues.put(JobStorage.COLUMN_EXTRAS, new PersistableBundleCompat().saveToXml());
    contentValues.put("persisted", false);

    contentValues.put(JobStorage.COLUMN_NUM_FAILURES, 0);
    contentValues.put(JobStorage.COLUMN_SCHEDULED_AT, System.currentTimeMillis());

    return contentValues;
}
 
Example #14
Source File: DailyJob.java    From android-job with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
protected final Result onRunJob(@NonNull Params params) {
    PersistableBundleCompat extras = params.getExtras();
    boolean runOnce = extras.getBoolean(EXTRA_ONCE, false);

    if (!runOnce && (!extras.containsKey(EXTRA_START_MS) || !extras.containsKey(EXTRA_END_MS))) {
        CAT.e("Daily job doesn't contain start and end time");
        return Result.FAILURE;
    }

    DailyJobResult result = null;

    try {
        if (meetsRequirements(true)) {
            result = onRunDailyJob(params);
        } else {
            result = DailyJobResult.SUCCESS; // reschedule
            CAT.i("Daily job requirements not met, reschedule for the next day");
        }

    } finally {
        if (result == null) {
            // shouldn't happen if the job follows the contract
            result = DailyJobResult.SUCCESS;
            CAT.e("Daily job result was null");
        }

        if (!runOnce) {
            JobRequest request = params.getRequest();
            if (result == DailyJobResult.SUCCESS) {
                CAT.i("Rescheduling daily job %s", request);

                // don't update current, it would cancel this currently running job
                int newJobId = schedule(request.createBuilder(), false,
                        extras.getLong(EXTRA_START_MS, 0) % DAY, extras.getLong(EXTRA_END_MS, 0L) % DAY, true);

                request = JobManager.instance().getJobRequest(newJobId);
                if (request != null) {
                    request.updateStats(false, true);
                }

            } else {
                CAT.i("Cancel daily job %s", request);
            }
        }
    }

    return Result.SUCCESS;
}
 
Example #15
Source File: AndroidJobStrategy.java    From cloudinary_android with MIT License 4 votes vote down vote up
private AndroidJobRequestParams(PersistableBundleCompat bundle) {
    this.bundle = bundle;
}