From 03a4da6e8e92b19c1345016c06694cb3aabbfc27 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Wed, 10 Sep 2014 17:32:18 -0700 Subject: Add flag to JobParameters for job expired BUG: 17424511 Introduce an "isOverrideDeadlineExpired" which will allow clients to know when they are being run due to an expiry. Nb that we check deadline expiry by checking that the constraints on the job are not satisfied at execution time. Really this is the same thing, as a job will not be run without its constraints being met, unless the job has expired. Change-Id: I4b91e5b5eadccabd91296d5a5ca66b859dbfaf5c --- api/current.txt | 1 + core/java/android/app/job/JobParameters.java | 17 ++++++++++++++++- .../java/com/android/server/job/JobServiceContext.java | 3 ++- .../com/android/server/job/controllers/JobStatus.java | 15 +++++++++++---- .../demo/jobSchedulerApp/service/TestJobService.java | 11 ++++++++--- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/api/current.txt b/api/current.txt index 42eaef0..361226a 100644 --- a/api/current.txt +++ b/api/current.txt @@ -5679,6 +5679,7 @@ package android.app.job { method public int describeContents(); method public android.os.PersistableBundle getExtras(); method public int getJobId(); + method public boolean isOverrideDeadlineExpired(); method public void writeToParcel(android.os.Parcel, int); field public static final android.os.Parcelable.Creator CREATOR; } diff --git a/core/java/android/app/job/JobParameters.java b/core/java/android/app/job/JobParameters.java index 724856a..62734f2 100644 --- a/core/java/android/app/job/JobParameters.java +++ b/core/java/android/app/job/JobParameters.java @@ -32,12 +32,15 @@ public class JobParameters implements Parcelable { private final int jobId; private final PersistableBundle extras; private final IBinder callback; + private final boolean overrideDeadlineExpired; /** @hide */ - public JobParameters(int jobId, PersistableBundle extras, IBinder callback) { + public JobParameters(IBinder callback, int jobId, PersistableBundle extras, + boolean overrideDeadlineExpired) { this.jobId = jobId; this.extras = extras; this.callback = callback; + this.overrideDeadlineExpired = overrideDeadlineExpired; } /** @@ -56,6 +59,16 @@ public class JobParameters implements Parcelable { return extras; } + /** + * For jobs with {@link android.app.job.JobInfo.Builder#setOverrideDeadline(long)} set, this + * provides an easy way to tell whether the job is being executed due to the deadline + * expiring. Note: If the job is running because its deadline expired, it implies that its + * constraints will not be met. + */ + public boolean isOverrideDeadlineExpired() { + return overrideDeadlineExpired; + } + /** @hide */ public IJobCallback getCallback() { return IJobCallback.Stub.asInterface(callback); @@ -65,6 +78,7 @@ public class JobParameters implements Parcelable { jobId = in.readInt(); extras = in.readPersistableBundle(); callback = in.readStrongBinder(); + overrideDeadlineExpired = in.readInt() == 1; } @Override @@ -77,6 +91,7 @@ public class JobParameters implements Parcelable { dest.writeInt(jobId); dest.writePersistableBundle(extras); dest.writeStrongBinder(callback); + dest.writeInt(overrideDeadlineExpired ? 1 : 0); } public static final Creator CREATOR = new Creator() { diff --git a/services/core/java/com/android/server/job/JobServiceContext.java b/services/core/java/com/android/server/job/JobServiceContext.java index 344c57b..d0447bc 100644 --- a/services/core/java/com/android/server/job/JobServiceContext.java +++ b/services/core/java/com/android/server/job/JobServiceContext.java @@ -153,7 +153,8 @@ public class JobServiceContext extends IJobCallback.Stub implements ServiceConne } mRunningJob = job; - mParams = new JobParameters(job.getJobId(), job.getExtras(), this); + mParams = new JobParameters(this, job.getJobId(), job.getExtras(), + !job.isConstraintsSatisfied()); mExecutionStartTimeElapsed = SystemClock.elapsedRealtime(); mVerb = VERB_BINDING; diff --git a/services/core/java/com/android/server/job/controllers/JobStatus.java b/services/core/java/com/android/server/job/controllers/JobStatus.java index f562721..e3c55b6 100644 --- a/services/core/java/com/android/server/job/controllers/JobStatus.java +++ b/services/core/java/com/android/server/job/controllers/JobStatus.java @@ -195,16 +195,23 @@ public class JobStatus { } /** - * @return Whether or not this job is ready to run, based on its requirements. + * @return Whether or not this job is ready to run, based on its requirements. This is true if + * the constraints are satisfied or the deadline on the job has expired. */ public synchronized boolean isReady() { + return isConstraintsSatisfied() + || (hasDeadlineConstraint() && deadlineConstraintSatisfied.get()); + } + + /** + * @return Whether the constraints set on this job are satisfied. + */ + public synchronized boolean isConstraintsSatisfied() { return (!hasChargingConstraint() || chargingConstraintSatisfied.get()) && (!hasTimingDelayConstraint() || timeDelayConstraintSatisfied.get()) && (!hasConnectivityConstraint() || connectivityConstraintSatisfied.get()) && (!hasUnmeteredConstraint() || unmeteredConstraintSatisfied.get()) - && (!hasIdleConstraint() || idleConstraintSatisfied.get()) - // Also ready if the deadline has expired - special case. - || (hasDeadlineConstraint() && deadlineConstraintSatisfied.get()); + && (!hasIdleConstraint() || idleConstraintSatisfied.get()); } public boolean matches(int uid, int jobId) { diff --git a/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java b/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java index a68e04e..9df11fe 100644 --- a/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java +++ b/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java @@ -29,6 +29,7 @@ import android.os.Messenger; import android.os.RemoteException; import android.util.Log; import android.util.SparseArray; +import android.widget.Toast; import com.android.demo.jobSchedulerApp.MainActivity; @@ -84,12 +85,15 @@ public class TestJobService extends JobService { currentId++; jobParamsMap.put(currentId, params); final int currId = this.currentId; - Log.d(TAG, "putting :" + currId + " for " + params.toString()); - Log.d(TAG, " pulled: " + jobParamsMap.get(currId)); if (mActivity != null) { mActivity.onReceivedStartJob(params); } + Toast.makeText( + this, "On start job: '" + params.getJobId() + "' deadline exceeded: " + + params.isOverrideDeadlineExpired(), + Toast.LENGTH_LONG).show(); + return true; } @@ -100,7 +104,7 @@ public class TestJobService extends JobService { int ind = jobParamsMap.indexOfValue(params); jobParamsMap.remove(ind); mActivity.onReceivedStopJob(); - return true; + return false; // no reschedule } static int currentId = 0; @@ -129,6 +133,7 @@ public class TestJobService extends JobService { return false; } else { jobFinished(params, false); + jobParamsMap.removeAt(0); return true; } } -- cgit v1.1