@Override public void onCreate() { super.onCreate(); JobManager .create(this) .addJobCreator(new DemoUnifiedJobCreator()); } }
/*package*/ void setStarted(boolean started) { mStarted = started; ContentValues contentValues = new ContentValues(); contentValues.put(JobStorage.COLUMN_STARTED, mStarted); JobManager.instance().getJobStorage().update(this, contentValues); }
JobManager manager = JobManager.instance(); Set<JobRequest> requests = new HashSet<>(manager.getAllJobRequestsForTag(builder.mTag)); for (JobRequest request : requests) { if (!request.isExact() || request.getStartMs() != JobRequest.START_NOW) { manager.cancel(request.getJobId());
private static Context context() { return JobManager.instance().getContext(); }
private synchronized int cancelAllInner(@Nullable String tag) { int canceled = 0; Set<JobRequest> requests = getAllJobRequests(tag, true, false); for (JobRequest request : requests) { if (cancelInner(request)) { canceled++; } } //noinspection ConstantConditions Set<Job> jobs = TextUtils.isEmpty(tag) ? getAllJobs() : getAllJobsForTag(tag); for (Job job : jobs) { if (cancelInner(job)) { canceled++; } } return canceled; }
@Override public void onCreate() { super.onCreate(); JobManager.create(this).addJobCreator(new HookFetchJobCreator()); int noOfJobRequests = JobManager.instance().getAllJobRequestsForTag(HookFetchJob.TAG).size(); // No of Job Requests for HookFetchJob would be greater than 1 for older version of // application because of rescheduling of job again and again, causing large no. of wakelocks if (noOfJobRequests > 1) { JobManager.instance().cancelAllForTag(HookFetchJob.TAG); noOfJobRequests = 0; } if (noOfJobRequests == 0) { HookFetchJob.scheduleJob(); } }
@Override public void onReceive(Context context, Intent intent) { /* * Create the job manager. We may need to reschedule jobs and some applications aren't initializing the * manager in Application.onCreate(). It may happen that some jobs can't be created if the JobCreator * wasn't registered, yet. Apps / Libraries need to figure out how to solve this themselves. */ try { JobManager.create(context); } catch (JobManagerCreateException ignored) { } } }
.register(Uri.class, InputStream.class, new FaviconLoader.Factory()); JobManager.create(this) .addJobCreator(new JobCreator() { @Override public Job create(String tag) { JobManager.instance().cancelAll();
/** * Cancel this request if it has been scheduled. Note that if the job isn't periodic, then the * time passed since the job has been scheduled is subtracted from the time frame. For example * a job should run between 4 and 6 seconds from now. You cancel the scheduled job after 2 * seconds, then the job will run between 2 and 4 seconds after it's been scheduled again. * * @return A builder to modify the parameters. */ public Builder cancelAndEdit() { // create a temporary variable, because .cancel() will reset mScheduledAt long scheduledAt = mScheduledAt; JobManager.instance().cancel(getJobId()); Builder builder = new Builder(this.mBuilder); mStarted = false; if (!isPeriodic()) { long offset = JobConfig.getClock().currentTimeMillis() - scheduledAt; long minValue = 1L; // 1ms builder.setExecutionWindow(Math.max(minValue, getStartMs() - offset), Math.max(minValue, getEndMs() - offset)); } return builder; }
@Test public void testScheduleAndCancel() { JobRequest request = DummyJobs.createOneOff(); int jobId = request.schedule(); assertThat(manager().getJobRequest(jobId)).isNotNull(); assertThat(manager().getJob(jobId)).isNull(); assertThat(manager().cancel(jobId)).isTrue(); assertThat(manager().getAllJobRequests()).isEmpty(); assertThat(manager().getAllJobs()).isEmpty(); request.schedule(); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(manager().cancelAll()).isEqualTo(1); assertThat(manager().getAllJobRequests()).isEmpty(); assertThat(manager().getAllJobs()).isEmpty(); }
/** * {@inheritDoc} */ @Override public boolean cancelRequest(String requestId) { boolean cancelled = false; for (Job job : JobManager.instance().getAllJobs()) { String currJobId = ((UploadJob) job).requestId; if (requestId.equals(currJobId)) { job.cancel(); cancelled = true; } } if (!cancelled) { for (JobRequest job : JobManager.instance().getAllJobRequests()) { String id = job.getExtras().getString("requestId", null); if (requestId.equals(id)) { cancelled = JobManager.instance().cancel(job.getJobId()); break; } } } killThread(requestId); Logger.i(TAG, String.format("Cancelling request %s, success: %s", requestId, cancelled)); return cancelled; }
/** * All new Job classes need to be included in the factory method below */ @Override @Nullable public Job create(@NonNull final String tag) { if (D) UserError.Log.ueh("JobCreator", JoH.dateTimeText(JoH.tsl()) + " Passed: " + tag); switch (tag) { /* case CloudSyncJob.TAG: return new CloudSyncJob(); case DailyJob.TAG: return new DailyJob(); */ default: UserError.Log.wtf(TAG, "Failed to match Job: " + tag + " requesting cancellation"); try { JobManager.instance().cancelAllForTag(tag); } catch (Exception e) { // } return null; } } }
@Override public boolean onStopJob(JobParameters params) { Job job = JobManager.create(this).getJob(params.getJobId()); if (job != null) { job.cancel(); CAT.d("Called onStopJob for %s", job); } else { CAT.d("Called onStopJob, job %d not found", params.getJobId()); } // do not reschedule return false; }
@Override protected void before() throws Throwable { JobConfig.setJobReschedulePause(0, TimeUnit.MILLISECONDS); JobConfig.setSkipJobReschedule(true); JobConfig.setApiEnabled(JobApi.WORK_MANAGER, false); mManager = JobManager.create(InstrumentationRegistry.getTargetContext()); mManager.cancelAll(); }
/** * Schedule a request which will be executed in the future. If you want to update an existing * {@link JobRequest}, call {@link JobRequest#cancelAndEdit()}, update your parameters and call * this method again. Calling this method on the same request instance multiple times without * cancelling is idempotent. * * <br> * <br> * * Convenience method. Internally it calls {@link JobManager#schedule(JobRequest)} * and {@link #getJobId()} for this request. * * @return The unique ID for this job. */ public int schedule() { JobManager.instance().schedule(this); return getJobId(); }
@Before public void prepare() { mManager = mJobManagerRule.getManager(); mManager.addJobCreator(new TestJobCreator()); mLatch = new CountDownLatch(1); }
/** * @return A duplicate {@link Set} containing all pending JobRequests or an empty set. * Never returns {@code null}. The set may be modified without direct effects to the actual * backing store. * @see #getJobRequest(int) */ @NonNull public Set<JobRequest> getAllJobRequests() { return getAllJobRequests(null, false, true); }
manager().addJobCreator(new JobCreator() { @Override public Job create(@NonNull String tag) { assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(atomicBoolean.get()).isTrue(); manager().cancelAll(); jobId = DailyJob.schedule(new JobRequest.Builder("any").setRequiresCharging(false).setRequirementsEnforced(true), time, time); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(manager().getAllJobRequests()).hasSize(1); assertThat(atomicBoolean.get()).isFalse();
@Override protected void onHandleWork(@NonNull Intent intent) { /* * Delay this slightly. This avoids a race condition if the app was launched by the * AlarmManager. Then the alarm was already removed, but the JobRequest might still * be available in the storage. We still catch this case, because we never execute * a job with the same ID twice. Nonetheless, add the delay to save resources. */ try { CAT.d("Reschedule service started"); SystemClock.sleep(JobConfig.getJobReschedulePause()); JobManager manager; try { manager = JobManager.create(this); } catch (Exception e) { return; } Set<JobRequest> requests = manager.getAllJobRequests(null, true, true); int rescheduledCount = rescheduleJobs(manager, requests); CAT.d("Reschedule %d jobs of %d jobs", rescheduledCount, requests.size()); } finally { if (latch != null) { // latch can be null, if the service was restarted after a process death latch.countDown(); } } }