Search in sources :

Example 1 with JobRequest

use of com.evernote.android.job.JobRequest in project android-job by evernote.

the class PlatformAlarmService method onHandleIntent.

@Override
protected void onHandleIntent(final Intent intent) {
    if (intent == null) {
        Cat.i("Delivered intent is null");
        return;
    }
    int jobId = intent.getIntExtra(PlatformAlarmReceiver.EXTRA_JOB_ID, -1);
    final JobProxy.Common common = new JobProxy.Common(this, jobId);
    // create the JobManager. Seeing sometimes exceptions, that it wasn't created, yet.
    final JobRequest request = common.getPendingRequest(true);
    if (request == null) {
        return;
    }
    // parallel execution
    EXECUTOR_SERVICE.execute(new Runnable() {

        @Override
        public void run() {
            common.executeJobRequest(request);
            // call here, our own wake lock could be acquired too late
            try {
                PlatformAlarmReceiver.completeWakefulIntent(intent);
            } catch (Exception e) {
            // could end in a NPE if the intent has no wake lock
            }
        }
    });
}
Also used : JobProxy(com.evernote.android.job.JobProxy) JobRequest(com.evernote.android.job.JobRequest)

Example 2 with JobRequest

use of com.evernote.android.job.JobRequest in project android by nextcloud.

the class ContactsPreferenceActivity method cancelContactBackupJobForAccount.

public static void cancelContactBackupJobForAccount(Context context, Account account) {
    Log_OC.d(TAG, "disabling contacts backup job for account: " + account.name);
    JobManager jobManager = JobManager.create(context);
    Set<JobRequest> jobs = jobManager.getAllJobRequestsForTag(ContactsBackupJob.TAG);
    for (JobRequest jobRequest : jobs) {
        PersistableBundleCompat extras = jobRequest.getExtras();
        if (extras.getString(ContactsBackupJob.ACCOUNT, "").equalsIgnoreCase(account.name)) {
            jobManager.cancel(jobRequest.getJobId());
        }
    }
}
Also used : JobRequest(com.evernote.android.job.JobRequest) PersistableBundleCompat(com.evernote.android.job.util.support.PersistableBundleCompat) JobManager(com.evernote.android.job.JobManager)

Example 3 with JobRequest

use of com.evernote.android.job.JobRequest in project android-job by evernote.

the class PlatformWorkManagerTest method testCancel.

@Test
@SuppressWarnings("ConstantConditions")
public void testCancel() {
    int jobId = new JobRequest.Builder(TAG).setExecutionWindow(TimeUnit.HOURS.toMillis(4), TimeUnit.HOURS.toMillis(5)).build().schedule();
    JobRequest request = mWorkManagerRule.getManager().getJobRequest(jobId);
    JobProxyWorkManager jobProxyWorkManager = new JobProxyWorkManager(ApplicationProvider.getApplicationContext());
    assertThat(jobProxyWorkManager.isPlatformJobScheduled(request)).isTrue();
    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().cancel(jobId);
    assertThat(mWorkManagerRule.getWorkStatus(tag).get(0).getState()).isEqualTo(WorkInfo.State.CANCELLED);
    assertThat(jobProxyWorkManager.isPlatformJobScheduled(request)).isFalse();
}
Also used : JobRequest(com.evernote.android.job.JobRequest) WorkInfo(androidx.work.WorkInfo) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 4 with JobRequest

use of com.evernote.android.job.JobRequest in project android-job by evernote.

the class PlatformWorker method doWork.

@NonNull
@Override
public Result doWork() {
    final int jobId = getJobId();
    if (jobId < 0) {
        return Result.failure();
    }
    try {
        JobProxy.Common common = new JobProxy.Common(getApplicationContext(), CAT, jobId);
        JobRequest request = common.getPendingRequest(true, true);
        if (request == null) {
            return Result.failure();
        }
        Bundle transientBundle = null;
        if (request.isTransient()) {
            transientBundle = TransientBundleHolder.getBundle(jobId);
            if (transientBundle == null) {
                CAT.d("Transient bundle is gone for request %s", request);
                return Result.failure();
            }
        }
        Job.Result result = common.executeJobRequest(request, transientBundle);
        if (Job.Result.SUCCESS == result) {
            return Result.success();
        } else {
            return Result.failure();
        }
    } finally {
        TransientBundleHolder.cleanUpBundle(jobId);
    }
}
Also used : JobProxy(com.evernote.android.job.JobProxy) JobRequest(com.evernote.android.job.JobRequest) Bundle(android.os.Bundle) Job(com.evernote.android.job.Job) NonNull(androidx.annotation.NonNull)

Example 5 with JobRequest

use of com.evernote.android.job.JobRequest in project android-job by evernote.

the class PlatformJobService method onStartJob.

@Override
public boolean onStartJob(final JobParameters params) {
    final int jobId = params.getJobId();
    final JobProxy.Common common = new JobProxy.Common(this, jobId);
    final JobRequest request = common.getPendingRequest(true);
    if (request == null) {
        return false;
    }
    EXECUTOR_SERVICE.execute(new Runnable() {

        @Override
        public void run() {
            try {
                common.executeJobRequest(request);
            } finally {
                // do not reschedule
                jobFinished(params, false);
            }
        }
    });
    // yes, we have a job running in the background
    return true;
}
Also used : JobProxy(com.evernote.android.job.JobProxy) JobRequest(com.evernote.android.job.JobRequest)

Aggregations

JobRequest (com.evernote.android.job.JobRequest)10 JobProxy (com.evernote.android.job.JobProxy)5 Bundle (android.os.Bundle)3 Job (com.evernote.android.job.Job)2 JobManager (com.evernote.android.job.JobManager)2 PersistableBundleCompat (com.evernote.android.job.util.support.PersistableBundleCompat)2 Test (org.junit.Test)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 NonNull (androidx.annotation.NonNull)1 LargeTest (androidx.test.filters.LargeTest)1 WorkInfo (androidx.work.WorkInfo)1