OSDN Git Service

Add flag to JobParameters for job expired
authorMatthew Williams <mjwilliams@google.com>
Thu, 11 Sep 2014 00:32:18 +0000 (17:32 -0700)
committerMatthew Williams <mjwilliams@google.com>
Thu, 11 Sep 2014 00:32:18 +0000 (17:32 -0700)
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
core/java/android/app/job/JobParameters.java
services/core/java/com/android/server/job/JobServiceContext.java
services/core/java/com/android/server/job/controllers/JobStatus.java
tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java

index 42eaef0..361226a 100644 (file)
@@ -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;
   }
index 724856a..62734f2 100644 (file)
@@ -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<JobParameters> CREATOR = new Creator<JobParameters>() {
index 344c57b..d0447bc 100644 (file)
@@ -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;
index f562721..e3c55b6 100644 (file)
@@ -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 <strong>or</strong> 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) {
index a68e04e..9df11fe 100644 (file)
@@ -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;
         }
     }