@NonNull @Override protected Result onRunJob(Params params) { Log.d(getClass().getSimpleName(), "scheduled unified work begins"); if (getParams() .getExtras() .getBoolean(PollReceiver.EXTRA_IS_DOWNLOAD, false)) { new DownloadJob().run(); // do synchronously, as we are on // a background thread already } Log.d(getClass().getSimpleName(), "scheduled unified work ends"); return(Result.SUCCESS); } }
.setContentTitle("ID " + params.getId()) .setContentText("Job ran, exact " + params.isExact() + " , periodic " + params.isPeriodic() + ", transient " + params.isTransient()) .setAutoCancel(true) .setChannelId(TAG)
@SuppressWarnings("deprecation") private void setupDeviceIdle(Job job, boolean requirement, boolean deviceIdle) { PowerManager powerManager = mock(PowerManager.class); when(powerManager.isDeviceIdleMode()).thenReturn(deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(powerManager.isScreenOn()).thenReturn(!deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(job.getParams().getRequest().requiresDeviceIdle()).thenReturn(requirement); when(job.getContext().getSystemService(Context.POWER_SERVICE)).thenReturn(powerManager); }
@NonNull @Override protected final Result onRunJob(@NonNull Params params) { PersistableBundleCompat extras = params.getExtras(); boolean runOnce = extras.getBoolean(EXTRA_ONCE, false); JobRequest request = params.getRequest(); if (result == DailyJobResult.SUCCESS) { CAT.i("Rescheduling daily job %s", request);
/*package*/ boolean meetsRequirements(boolean checkRequirementsEnforced) { if (checkRequirementsEnforced && !getParams().getRequest().requirementsEnforced()) { return true; } if (!isRequirementChargingMet()) { CAT.w("Job requires charging, reschedule"); return false; } if (!isRequirementDeviceIdleMet()) { CAT.w("Job requires device to be idle, reschedule"); return false; } if (!isRequirementNetworkTypeMet()) { CAT.w("Job requires network to be %s, but was %s", getParams().getRequest().requiredNetworkType(), Device.getNetworkType(getContext())); return false; } if (!isRequirementBatteryNotLowMet()) { CAT.w("Job requires battery not be low, reschedule"); return false; } if (!isRequirementStorageNotLowMet()) { CAT.w("Job requires storage not be low, reschedule"); return false; } return true; }
/** * @return {@code false} if the {@link Job} requires the device to be in a specific network state and it * isn't in this state. Otherwise always returns {@code true}. */ protected boolean isRequirementNetworkTypeMet() { JobRequest.NetworkType requirement = getParams().getRequest().requiredNetworkType(); if (requirement == JobRequest.NetworkType.ANY) { return true; } JobRequest.NetworkType current = Device.getNetworkType(getContext()); switch (requirement) { case CONNECTED: return current != JobRequest.NetworkType.ANY; case NOT_ROAMING: return current == JobRequest.NetworkType.NOT_ROAMING || current == JobRequest.NetworkType.UNMETERED || current == JobRequest.NetworkType.METERED; case UNMETERED: return current == JobRequest.NetworkType.UNMETERED; case METERED: return current == JobRequest.NetworkType.CONNECTED || current == JobRequest.NetworkType.NOT_ROAMING; default: throw new IllegalStateException("not implemented"); } }
private void handleResult(Job job, Job.Result result) { JobRequest request = mJob.getParams().getRequest(); boolean incFailureCount = false; boolean updateLastRun = false; if (!request.isPeriodic() && Job.Result.RESCHEDULE.equals(result) && !job.isDeleted()) { request = request.reschedule(true, true); mJob.onReschedule(request.getJobId()); updateLastRun = true; } else if (request.isPeriodic()) { updateLastRun = true; if (!Job.Result.SUCCESS.equals(result)) { incFailureCount = true; } } if (!job.isDeleted()) { // otherwise it would be persisted again if (incFailureCount || updateLastRun) { //noinspection ConstantConditions request.updateStats(incFailureCount, updateLastRun); } } } }
public synchronized Set<Job> getAllJobsForTag(String tag) { Set<Job> result = new HashSet<>(); for (int i = 0; i < mJobs.size(); i++) { Job job = mJobs.valueAt(i); if (tag == null || tag.equals(job.getParams().getTag())) { result.add(job); } } Map<Integer, WeakReference<Job>> snapshot = mFinishedJobsCache.snapshot(); for (WeakReference<Job> reference : snapshot.values()) { Job job = reference.get(); if (job == null) { continue; } if (tag == null || tag.equals(job.getParams().getTag())) { result.add(job); } } return result; }
@NonNull @Override protected Result onRunJob(Params params) { PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CLD_UPLOADER"); this.requestId = params.getExtras().getString("requestId", null); registerThread(); // Acquire wake readWriteLock - evernote android job only takes wakelock for 3 minutes and file uploads can take (much) longer. wl.acquire(); try { // call the generic processor: UploadStatus result = MediaManager.get().processRequest(getContext(), new AndroidJobRequestParams(params.getExtras())); return adaptResult(result); } finally { wl.release(); unregisterThread(); } }
/*package*/ final Result runJob() { try { // daily jobs check the requirements manually if (this instanceof DailyJob || meetsRequirements(true)) { mResult = onRunJob(getParams()); } else { mResult = getParams().isPeriodic() ? Result.FAILURE : Result.RESCHEDULE; } return mResult; } finally { mFinishedTimeStamp = System.currentTimeMillis(); } }
@Override public int hashCode() { return mParams.hashCode(); }
/** * @return Whether the battery not low requirement is met. That's true either if it's not a requirement * or if the battery actually isn't low. The battery is low, if less than 15% are left and the device isn't * charging. */ protected boolean isRequirementBatteryNotLowMet() { return !(getParams().getRequest().requiresBatteryNotLow() && Device.getBatteryStatus(getContext()).isBatteryLow()); }
/** * @return Whether the storage not low requirement is met. That's true either if it's not a requirement * or if the storage actually isn't low. */ protected boolean isRequirementStorageNotLowMet() { return !(getParams().getRequest().requiresStorageNotLow() && Device.isStorageLow()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Job job = (Job) o; return mParams.equals(job.mParams); }
@SuppressWarnings("UnusedReturnValue") /*package*/ final Job setRequest(JobRequest request, @NonNull Bundle transientExtras) { mParams = new Params(request, transientExtras); return this; }
/** * @return {@code false} if the {@link Job} requires the device to be charging and it isn't charging. * Otherwise always returns {@code true}. */ protected boolean isRequirementChargingMet() { return !(getParams().getRequest().requiresCharging() && !Device.getBatteryStatus(getContext()).isCharging()); }
@Override public String toString() { return "job{" + "id=" + mParams.getId() + ", finished=" + isFinished() + ", result=" + mResult + ", canceled=" + mCanceled + ", periodic=" + mParams.isPeriodic() + ", class=" + getClass().getSimpleName() + ", tag=" + mParams.getTag() + '}'; }
@VisibleForTesting /*package*/ synchronized void markJobAsFinished(Job job) { int id = job.getParams().getId(); mJobs.remove(id); cleanUpRoutine(mFinishedJobsCache); mFinishedJobResults.put(id, job.getResult()); mFinishedJobsCache.put(id, new WeakReference<>(job)); }
/** * @return {@code false} if the {@link Job} requires the device to be idle and it isn't idle. Otherwise * always returns {@code true}. */ protected boolean isRequirementDeviceIdleMet() { return !(getParams().getRequest().requiresDeviceIdle() && !Device.isIdle(getContext())); }
@NonNull @Override protected final Result onRunJob(@NonNull Params params) { PersistableBundleCompat extras = params.getExtras(); boolean runOnce = extras.getBoolean(EXTRA_ONCE, false); JobRequest request = params.getRequest(); if (result == DailyJobResult.SUCCESS) { CAT.i("Rescheduling daily job %s", request);