/** * Returns whether this is a transient jobs. <b>WARNING:</b> It's not guaranteed that a transient job * will run at all, e.g. rebooting the device or force closing the app will cancel the * job. * * @return If this is a transient job. */ public boolean isTransient() { return mRequest.isTransient(); }
protected JobInfo.Builder setTransientBundle(JobRequest request, JobInfo.Builder builder) { if (request.isTransient()) { TransientBundleCompat.persistBundle(mContext, request); } return builder; }
@Override public String toString() { return "request{id=" + getJobId() + ", tag=" + getTag() + ", transient=" + isTransient() + '}'; }
@SuppressWarnings("SimplifiableIfStatement") protected boolean isJobInfoScheduled(@Nullable JobInfo info, @NonNull JobRequest request) { boolean correctInfo = info != null && info.getId() == request.getJobId(); if (!correctInfo) { return false; } return !request.isTransient() || TransientBundleCompat.isScheduled(mContext, request.getJobId()); }
/*package*/ Set<JobRequest> getAllJobRequests(@Nullable String tag, boolean includeStarted, boolean cleanUpTransient) { Set<JobRequest> requests = getJobStorage().getAllJobRequests(tag, includeStarted); if (cleanUpTransient) { Iterator<JobRequest> iterator = requests.iterator(); while (iterator.hasNext()) { JobRequest request = iterator.next(); if (request.isTransient() && !request.getJobApi().getProxy(mContext).isPlatformJobScheduled(request)) { getJobStorage().remove(request); iterator.remove(); } } } return requests; }
.build(); if (newJob && (request.isExact() || request.isPeriodic() || request.isTransient())) { throw new IllegalArgumentException("Daily jobs cannot be exact, periodic or transient");
/** * @param jobId The unique ID of the pending {@link JobRequest}. * @return The {@link JobRequest} if it's pending or {@code null} otherwise. */ public JobRequest getJobRequest(int jobId) { JobRequest request = getJobRequest(jobId, false); if (request != null && request.isTransient() && !request.getJobApi().getProxy(mContext).isPlatformJobScheduled(request)) { getJobStorage().remove(request); return null; } else { return request; } }
protected JobInfo.Builder createBaseBuilder(JobRequest request, boolean allowPersisting) { JobInfo.Builder builder = new JobInfo.Builder(request.getJobId(), new ComponentName(mContext, PlatformJobService.class)) .setRequiresCharging(request.requiresCharging()) .setRequiresDeviceIdle(request.requiresDeviceIdle()) .setRequiredNetworkType(convertNetworkType(request.requiredNetworkType())) .setPersisted(allowPersisting && !request.isTransient() && JobUtil.hasBootPermission(mContext)); return setTransientBundle(request, builder); }
if (request.isTransient()) { transientBundle = TransientBundleHolder.getBundle(jobId); if (transientBundle == null) {
@Test public void verifyAlarmNotCanceledForPeriodicAfterStart() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); Bundle extras = new Bundle(); extras.putString("key", "value"); int jobId = new JobRequest.Builder("tag") .setPeriodic(TimeUnit.DAYS.toMillis(1)) .setTransientExtras(extras) .build() .schedule(); assertThat(mJobManagerRule.getAllPendingJobsFromScheduler()).isNotNull().isNotEmpty(); JobRequest request = mJobManagerRule.getManager().getJobRequest(jobId); assertThat(request.isTransient()).isTrue(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); boolean started = TransientBundleCompat.startWithTransientBundle(context(), request); assertThat(started).isTrue(); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); }
@Override public void run() { try { final int jobId = params.getJobId(); final JobProxy.Common common = new JobProxy.Common(PlatformJobService.this, CAT, jobId); // don't mark starting! final JobRequest request = common.getPendingRequest(true, false); if (request == null) { return; } if (request.isTransient()) { if (TransientBundleCompat.startWithTransientBundle(PlatformJobService.this, request)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // should only happen during testing if an API is disabled CAT.d("PendingIntent for transient bundle is not null although running on O, using compat mode, request %s", request); } return; } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { CAT.d("PendingIntent for transient job %s expired", request); return; } } common.markStarting(request); common.executeJobRequest(request, getTransientBundle(params)); } finally { // do not reschedule jobFinished(params, false); } } });
@Override public void plantOneOff(JobRequest request) { if (request.isTransient()) { TransientBundleHolder.putBundle(request.getJobId(), request.getTransientExtras()); } OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(PlatformWorker.class) .setInitialDelay(request.getStartMs(), TimeUnit.MILLISECONDS) // don't use the average here, WorkManager will do the right thing .setConstraints(buildConstraints(request)) .addTag(createTag(request.getJobId())) .build(); // don't set the back-off criteria, android-job is handling this WorkManager workManager = getWorkManager(); if (workManager == null) { throw new JobProxyIllegalStateException("WorkManager is null"); } workManager.enqueue(workRequest); }
/** * Returns whether this is a transient jobs. <b>WARNING:</b> It's not guaranteed that a transient job * will run at all, e.g. rebooting the device or force closing the app will cancel the * job. * * @return If this is a transient job. */ public boolean isTransient() { return mRequest.isTransient(); }
protected JobInfo.Builder setTransientBundle(JobRequest request, JobInfo.Builder builder) { if (request.isTransient()) { TransientBundleCompat.persistBundle(mContext, request); } return builder; }
@Override public String toString() { return "request{id=" + getJobId() + ", tag=" + getTag() + ", transient=" + isTransient() + '}'; }
/*package*/ Set<JobRequest> getAllJobRequests(@Nullable String tag, boolean includeStarted, boolean cleanUpTransient) { Set<JobRequest> requests = mJobStorage.getAllJobRequests(tag, includeStarted); if (cleanUpTransient) { Iterator<JobRequest> iterator = requests.iterator(); while (iterator.hasNext()) { JobRequest request = iterator.next(); if (request.isTransient() && !request.getJobApi().getProxy(mContext).isPlatformJobScheduled(request)) { mJobStorage.remove(request); iterator.remove(); } } } return requests; }
@SuppressWarnings("SimplifiableIfStatement") protected boolean isJobInfoScheduled(@Nullable JobInfo info, @NonNull JobRequest request) { boolean correctInfo = info != null && info.getId() == request.getJobId(); if (!correctInfo) { return false; } return !request.isTransient() || TransientBundleCompat.isScheduled(mContext, request.getJobId()); }
/** * @param jobId The unique ID of the pending {@link JobRequest}. * @return The {@link JobRequest} if it's pending or {@code null} otherwise. */ public JobRequest getJobRequest(int jobId) { JobRequest request = getJobRequest(jobId, false); if (request != null && request.isTransient() && !request.getJobApi().getProxy(mContext).isPlatformJobScheduled(request)) { getJobStorage().remove(request); return null; } else { return request; } }
protected JobInfo.Builder createBaseBuilder(JobRequest request, boolean allowPersisting) { JobInfo.Builder builder = new JobInfo.Builder(request.getJobId(), new ComponentName(mContext, PlatformJobService.class)) .setRequiresCharging(request.requiresCharging()) .setRequiresDeviceIdle(request.requiresDeviceIdle()) .setRequiredNetworkType(convertNetworkType(request.requiredNetworkType())) .setPersisted(allowPersisting && !request.isTransient() && JobUtil.hasBootPermission(mContext)); return setTransientBundle(request, builder); }
@Override public void run() { try { final int jobId = params.getJobId(); final JobProxy.Common common = new JobProxy.Common(PlatformJobService.this, CAT, jobId); // don't mark starting! final JobRequest request = common.getPendingRequest(true, false); if (request == null) { return; } if (request.isTransient()) { if (TransientBundleCompat.startWithTransientBundle(PlatformJobService.this, request)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // should only happen during testing if an API is disabled CAT.d("PendingIntent for transient bundle is not null although running on O, using compat mode, request %s", request); } return; } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { CAT.d("PendingIntent for transient job %s expired", request); return; } } common.markStarting(request); common.executeJobRequest(request, getTransientBundle(params)); } finally { // do not reschedule jobFinished(params, false); } } });