Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03a4da6e authored by Matthew Williams's avatar Matthew Williams
Browse files

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
parent 1b848d48
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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;
  }
+16 −1
Original line number Diff line number Diff line
@@ -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>() {
+2 −1
Original line number Diff line number Diff line
@@ -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;
+11 −4
Original line number Diff line number Diff line
@@ -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) {
+8 −3
Original line number Diff line number Diff line
@@ -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;
        }
    }