16e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams/*
26e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Copyright (C) 2014 The Android Open Source Project
36e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
46e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Licensed under the Apache License, Version 2.0 (the "License");
56e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * you may not use this file except in compliance with the License.
66e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * You may obtain a copy of the License at
76e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
86e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *      http://www.apache.org/licenses/LICENSE-2.0
96e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Unless required by applicable law or agreed to in writing, software
116e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * distributed under the License is distributed on an "AS IS" BASIS,
126e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * See the License for the specific language governing permissions and
146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * limitations under the License
156e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */
166e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
177060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepackage android.app.job;
186e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
19fa380e982e41b0dcbbcf2201803abf26808016b5Christopher Tateimport android.content.ComponentName;
206e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcel;
216e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcelable;
223d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williamsimport android.os.PersistableBundle;
236e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
246e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams/**
257060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * Container of data passed to the {@link android.app.job.JobScheduler} fully encapsulating the
266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * parameters required to schedule work against the calling application. These are constructed
277060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * using the {@link JobInfo.Builder}.
289ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams * You must specify at least one sort of constraint on the JobInfo object that you are creating.
299ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams * The goal here is to provide the scheduler with high-level semantics about the work you want to
309ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams * accomplish. Doing otherwise with throw an exception in your app.
316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */
327060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepublic class JobInfo implements Parcelable {
33d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    /** Default. */
34d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int NETWORK_TYPE_NONE = 0;
35d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    /** This job requires network connectivity. */
36d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int NETWORK_TYPE_ANY = 1;
37d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    /** This job requires network connectivity that is unmetered. */
38d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int NETWORK_TYPE_UNMETERED = 2;
396e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
406e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
417060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Amount of backoff a job has initially by default, in milliseconds.
423d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams     */
43d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final long DEFAULT_INITIAL_BACKOFF_MILLIS = 30000L;  // 30 seconds.
443d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams
453d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    /**
46d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * Maximum backoff we allow for a job, in milliseconds.
473d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams     */
48d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final long MAX_BACKOFF_DELAY_MILLIS = 5 * 60 * 60 * 1000;  // 5 hours.
49d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams
50effacfa75bd9c2ebc889a7bc4f002c07f82f4c31Matthew Williams    /**
51d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * Linearly back-off a failed job. See
52d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)}
53d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * retry_time(current_time, num_failures) =
54d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     *     current_time + initial_backoff_millis * num_failures, num_failures >= 1
55effacfa75bd9c2ebc889a7bc4f002c07f82f4c31Matthew Williams     */
56d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int BACKOFF_POLICY_LINEAR = 0;
573d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams
583d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    /**
59d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * Exponentially back-off a failed job. See
60d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)}
61d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     *
62d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * retry_time(current_time, num_failures) =
63d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     *     current_time + initial_backoff_millis * 2 ^ (num_failures - 1), num_failures >= 1
646e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
65d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int BACKOFF_POLICY_EXPONENTIAL = 1;
66d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams
67d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    /**
68d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * Default type of backoff.
69d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * @hide
70d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     */
71d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public static final int DEFAULT_BACKOFF_POLICY = BACKOFF_POLICY_EXPONENTIAL;
726e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
737060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private final int jobId;
743d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    private final PersistableBundle extras;
756de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final ComponentName service;
766de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final boolean requireCharging;
776de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final boolean requireDeviceIdle;
789b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    private final boolean hasEarlyConstraint;
799b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    private final boolean hasLateConstraint;
80d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    private final int networkType;
816de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final long minLatencyMillis;
826de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final long maxExecutionDelayMillis;
836de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final boolean isPeriodic;
84900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams    private final boolean isPersisted;
856de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final long intervalMillis;
866de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final long initialBackoffMillis;
876de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final int backoffPolicy;
886de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams
896e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
907060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Unique job id associated with this class. This is assigned to your job by the scheduler.
916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
929b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    public int getId() {
937060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        return jobId;
946e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
956e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     * Bundle of extras which are returned to your application at execution time.
986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
993d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    public PersistableBundle getExtras() {
1006e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return extras;
1016e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1026e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1036e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1047060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Name of the service endpoint that will be called back into by the JobScheduler.
1056e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1066de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    public ComponentName getService() {
1076de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        return service;
1086e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1096e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1117060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Whether this job needs the device to be plugged in.
1126e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1136e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public boolean isRequireCharging() {
1146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return requireCharging;
1156e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1166e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1176e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1187060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Whether this job needs the device to be in an Idle maintenance window.
1196e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1206e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public boolean isRequireDeviceIdle() {
1216e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return requireDeviceIdle;
1226e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1236e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1246e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
125d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * One of {@link android.app.job.JobInfo#NETWORK_TYPE_ANY},
126d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * {@link android.app.job.JobInfo#NETWORK_TYPE_NONE}, or
127d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * {@link android.app.job.JobInfo#NETWORK_TYPE_UNMETERED}.
1286e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
129d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams    public int getNetworkType() {
130d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        return networkType;
1316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1326e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1336e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1347060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Set for a job that does not recur periodically, to specify a delay after which the job
1357060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * will be eligible for execution. This value is not set if the job recurs periodically.
1366e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1376e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public long getMinLatencyMillis() {
1386e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return minLatencyMillis;
1396e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1406e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1416e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1427060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * See {@link Builder#setOverrideDeadline(long)}. This value is not set if the job recurs
1436e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     * periodically.
1446e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1456e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public long getMaxExecutionDelayMillis() {
1466e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return maxExecutionDelayMillis;
1476e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1486e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1496e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1507060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Track whether this job will repeat with a given period.
1516e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1526e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public boolean isPeriodic() {
1536e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return isPeriodic;
1546e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1556e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1566e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
157900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams     * @return Whether or not this job should be persisted across device reboots.
158900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams     */
159900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams    public boolean isPersisted() {
160900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        return isPersisted;
161900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams    }
162900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams
163900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams    /**
1647060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * Set to the interval between occurrences of this job. This value is <b>not</b> set if the
1657060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * job does not recur periodically.
1666e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1676e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public long getIntervalMillis() {
1686e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return intervalMillis;
1696e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1706e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1716e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
1727060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * The amount of time the JobScheduler will wait before rescheduling a failed job. This value
1737060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * will be increased depending on the backoff policy specified at job creation time. Defaults
1746e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     * to 5 seconds.
1756e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1766e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public long getInitialBackoffMillis() {
1776e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return initialBackoffMillis;
1786e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1796e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1806e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
181d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * One of either {@link android.app.job.JobInfo#BACKOFF_POLICY_EXPONENTIAL}, or
182d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * {@link android.app.job.JobInfo#BACKOFF_POLICY_LINEAR}, depending on which criteria you set
183d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams     * when creating this job.
1846e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
1856e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public int getBackoffPolicy() {
1866e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return backoffPolicy;
1876e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1886e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
1899b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    /**
1909b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * User can specify an early constraint of 0L, which is valid, so we keep track of whether the
1919b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * function was called at all.
1929b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * @hide
1939b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     */
1949b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    public boolean hasEarlyConstraint() {
1959b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        return hasEarlyConstraint;
1969b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    }
1979b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams
1989b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    /**
1999b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * User can specify a late constraint of 0L, which is valid, so we keep track of whether the
2009b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * function was called at all.
2019b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     * @hide
2029b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams     */
2039b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    public boolean hasLateConstraint() {
2049b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        return hasLateConstraint;
2059b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    }
2069b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams
2077060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private JobInfo(Parcel in) {
2087060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        jobId = in.readInt();
2093d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        extras = in.readPersistableBundle();
210effacfa75bd9c2ebc889a7bc4f002c07f82f4c31Matthew Williams        service = in.readParcelable(null);
2116e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        requireCharging = in.readInt() == 1;
2126e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        requireDeviceIdle = in.readInt() == 1;
213d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        networkType = in.readInt();
2146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        minLatencyMillis = in.readLong();
2156e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        maxExecutionDelayMillis = in.readLong();
2166e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        isPeriodic = in.readInt() == 1;
217900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        isPersisted = in.readInt() == 1;
2186e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        intervalMillis = in.readLong();
2196e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        initialBackoffMillis = in.readLong();
2206e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        backoffPolicy = in.readInt();
2219b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        hasEarlyConstraint = in.readInt() == 1;
2229b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        hasLateConstraint = in.readInt() == 1;
2236e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2246e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2257060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private JobInfo(JobInfo.Builder b) {
2267060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        jobId = b.mJobId;
227effacfa75bd9c2ebc889a7bc4f002c07f82f4c31Matthew Williams        extras = b.mExtras;
2287060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        service = b.mJobService;
2296e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        requireCharging = b.mRequiresCharging;
2306e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        requireDeviceIdle = b.mRequiresDeviceIdle;
231d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        networkType = b.mNetworkType;
2326e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        minLatencyMillis = b.mMinLatencyMillis;
2336e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        maxExecutionDelayMillis = b.mMaxExecutionDelayMillis;
2346e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        isPeriodic = b.mIsPeriodic;
235900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        isPersisted = b.mIsPersisted;
2366e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        intervalMillis = b.mIntervalMillis;
2376e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        initialBackoffMillis = b.mInitialBackoffMillis;
2386e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        backoffPolicy = b.mBackoffPolicy;
2399b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        hasEarlyConstraint = b.mHasEarlyConstraint;
2409b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        hasLateConstraint = b.mHasLateConstraint;
2416e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2426e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2436e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2446e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public int describeContents() {
2456e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return 0;
2466e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2476e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2486e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2496e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public void writeToParcel(Parcel out, int flags) {
2507060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        out.writeInt(jobId);
2513d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        out.writePersistableBundle(extras);
252effacfa75bd9c2ebc889a7bc4f002c07f82f4c31Matthew Williams        out.writeParcelable(service, flags);
2536e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeInt(requireCharging ? 1 : 0);
2546e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeInt(requireDeviceIdle ? 1 : 0);
255d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        out.writeInt(networkType);
2566e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeLong(minLatencyMillis);
2576e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeLong(maxExecutionDelayMillis);
2586e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeInt(isPeriodic ? 1 : 0);
259900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        out.writeInt(isPersisted ? 1 : 0);
2606e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeLong(intervalMillis);
2616e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeLong(initialBackoffMillis);
2626e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        out.writeInt(backoffPolicy);
2639b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        out.writeInt(hasEarlyConstraint ? 1 : 0);
2649b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        out.writeInt(hasLateConstraint ? 1 : 0);
2656e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2666e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2677060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public static final Creator<JobInfo> CREATOR = new Creator<JobInfo>() {
2686e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2697060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobInfo createFromParcel(Parcel in) {
2707060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobInfo(in);
2716e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
2726e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2736e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2747060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobInfo[] newArray(int size) {
2757060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobInfo[size];
2766e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
2776e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    };
2786e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
279ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams    @Override
280ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams    public String toString() {
281ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams        return "(job:" + jobId + "/" + service.flattenToShortString() + ")";
282ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams    }
283ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams
2847060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    /** Builder class for constructing {@link JobInfo} objects. */
2859b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams    public static final class Builder {
2867060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        private int mJobId;
2873d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        private PersistableBundle mExtras = PersistableBundle.EMPTY;
2887060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        private ComponentName mJobService;
2896e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        // Requirements.
2906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private boolean mRequiresCharging;
2916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private boolean mRequiresDeviceIdle;
292d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        private int mNetworkType;
293900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        private boolean mIsPersisted;
2946e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        // One-off parameters.
2956e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private long mMinLatencyMillis;
2966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private long mMaxExecutionDelayMillis;
2976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        // Periodic parameters.
2986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private boolean mIsPeriodic;
2999b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        private boolean mHasEarlyConstraint;
3009b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams        private boolean mHasLateConstraint;
3016e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private long mIntervalMillis;
3026e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        // Back-off parameters.
3033d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        private long mInitialBackoffMillis = DEFAULT_INITIAL_BACKOFF_MILLIS;
3043d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        private int mBackoffPolicy = DEFAULT_BACKOFF_POLICY;
3056e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /** Easy way to track whether the client has tried to set a back-off policy. */
3066e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        private boolean mBackoffPolicySet = false;
3076e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3086e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3097060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param jobId Application-provided id for this job. Subsequent calls to cancel, or
3107060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         *               jobs created with the same jobId, will update the pre-existing job with
3116e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         *               the same id.
3127060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param jobService The endpoint that you implement that will receive the callback from the
3137060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         *            JobScheduler.
3146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3157060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public Builder(int jobId, ComponentName jobService) {
3167060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            mJobService = jobService;
3177060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            mJobId = jobId;
3186e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3196e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3206e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3216e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * Set optional extras. This is persisted, so we only allow primitive types.
3226e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * @param extras Bundle containing extras you want the scheduler to hold on to for you.
3236e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3243d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        public Builder setExtras(PersistableBundle extras) {
3256e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mExtras = extras;
3266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3276e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3286e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3296e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
330d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * Set some description of the kind of network type your job needs to have.
331d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * Not calling this function means the network is not necessary, as the default is
332d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * {@link #NETWORK_TYPE_NONE}.
3336e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * Bear in mind that calling this function defines network as a strict requirement for your
334d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * job. If the network requested is not available your job will never run. See
3356e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * {@link #setOverrideDeadline(long)} to change this behaviour.
3366e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
337d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        public Builder setRequiredNetworkType(int networkType) {
338d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams            mNetworkType = networkType;
3396e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3406e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3416e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3427060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        /**
3437060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Specify that to run this job, the device needs to be plugged in. This defaults to
3446e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * false.
3457060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param requiresCharging Whether or not the device is plugged in.
3466e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3476e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setRequiresCharging(boolean requiresCharging) {
3486e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mRequiresCharging = requiresCharging;
3496e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3506e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3516e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3526e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3537060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Specify that to run, the job needs the device to be in idle mode. This defaults to
3546e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * false.
3556e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * <p>Idle mode is a loose definition provided by the system, which means that the device
3566e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * is not in use, and has not been in use for some time. As such, it is a good time to
3577060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * perform resource heavy jobs. Bear in mind that battery usage will still be attributed
3586e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * to your application, and surfaced to the user in battery stats.</p>
3596e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * @param requiresDeviceIdle Whether or not the device need be within an idle maintenance
3606e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         *                           window.
3616e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3626e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setRequiresDeviceIdle(boolean requiresDeviceIdle) {
3636e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mRequiresDeviceIdle = requiresDeviceIdle;
3646e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3656e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3666e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3676e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3687060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Specify that this job should recur with the provided interval, not more than once per
3697060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * period. You have no control over when within this interval this job will be executed,
3706e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * only the guarantee that it will be executed at most once within this interval.
3716e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * Setting this function on the builder with {@link #setMinimumLatency(long)} or
3726e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * {@link #setOverrideDeadline(long)} will result in an error.
3737060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param intervalMillis Millisecond interval for which this job will repeat.
3746e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3756e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setPeriodic(long intervalMillis) {
3766e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mIsPeriodic = true;
3776e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mIntervalMillis = intervalMillis;
3789b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams            mHasEarlyConstraint = mHasLateConstraint = true;
3796e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3806e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3816e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3826e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3837060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Specify that this job should be delayed by the provided amount of time.
3847060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Because it doesn't make sense setting this property on a periodic job, doing so will
3856e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * throw an {@link java.lang.IllegalArgumentException} when
3867060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * {@link android.app.job.JobInfo.Builder#build()} is called.
3877060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param minLatencyMillis Milliseconds before which this job will not be considered for
3886e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         *                         execution.
3896e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
3906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setMinimumLatency(long minLatencyMillis) {
3916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mMinLatencyMillis = minLatencyMillis;
3929b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams            mHasEarlyConstraint = true;
3936e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
3946e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3956e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
3966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
3977060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Set deadline which is the maximum scheduling latency. The job will be run by this
3986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * deadline even if other requirements are not met. Because it doesn't make sense setting
3997060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * this property on a periodic job, doing so will throw an
4006e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * {@link java.lang.IllegalArgumentException} when
4017060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * {@link android.app.job.JobInfo.Builder#build()} is called.
4026e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
4036e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setOverrideDeadline(long maxExecutionDelayMillis) {
4046e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mMaxExecutionDelayMillis = maxExecutionDelayMillis;
4059b9244b6941110ea2d940d9fc8eed0cdff96a016Matthew Williams            mHasLateConstraint = true;
4066e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
4076e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
4086e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
4096e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
4106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * Set up the back-off/retry policy.
411d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * This defaults to some respectable values: {30 seconds, Exponential}. We cap back-off at
412d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * 5hrs.
4137060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * Note that trying to set a backoff criteria for a job with
4146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * {@link #setRequiresDeviceIdle(boolean)} will throw an exception when you call build().
4157060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * This is because back-off typically does not make sense for these types of jobs. See
4167060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * {@link android.app.job.JobService#jobFinished(android.app.job.JobParameters, boolean)}
4177060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * for more description of the return value for the case of a job executing while in idle
4186e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         * mode.
4197060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @param initialBackoffMillis Millisecond time interval to wait initially when job has
4206e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         *                             failed.
421d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * @param backoffPolicy is one of {@link #BACKOFF_POLICY_LINEAR} or
422d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams         * {@link #BACKOFF_POLICY_EXPONENTIAL}
4236e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
4246e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        public Builder setBackoffCriteria(long initialBackoffMillis, int backoffPolicy) {
4256e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mBackoffPolicySet = true;
4266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mInitialBackoffMillis = initialBackoffMillis;
4276e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            mBackoffPolicy = backoffPolicy;
4286e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            return this;
4296e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
4306e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
4316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        /**
432900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         * Set whether or not to persist this job across device reboots. This will only have an
433900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         * effect if your application holds the permission
434900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         * {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED}. Otherwise an exception will
435900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         * be thrown.
436900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         * @param isPersisted True to indicate that the job will be written to disk and loaded at
437900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         *                    boot.
438900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams         */
439d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        public Builder setPersisted(boolean isPersisted) {
440900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams            mIsPersisted = isPersisted;
441900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams            return this;
442900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        }
443900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams
444900c67fc51fc2672458dd1c9641250f2ecc01a31Matthew Williams        /**
4457060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate         * @return The job object to hand to the JobScheduler. This object is immutable.
4466e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams         */
4477060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobInfo build() {
4489ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams            // Allow jobs with no constraints - What am I, a database?
449bafeeb98135a7580cbcdd657818cd78f7bda35d8Matthew Williams            if (!mHasEarlyConstraint && !mHasLateConstraint && !mRequiresCharging &&
450d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams                    !mRequiresDeviceIdle && mNetworkType == NETWORK_TYPE_NONE) {
451bafeeb98135a7580cbcdd657818cd78f7bda35d8Matthew Williams                throw new IllegalArgumentException("You're trying to build a job with no " +
452bafeeb98135a7580cbcdd657818cd78f7bda35d8Matthew Williams                        "constraints, this is not allowed.");
453bafeeb98135a7580cbcdd657818cd78f7bda35d8Matthew Williams            }
4543d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams            mExtras = new PersistableBundle(mExtras);  // Make our own copy.
4557060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            // Check that a deadline was not set on a periodic job.
4563d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams            if (mIsPeriodic && (mMaxExecutionDelayMillis != 0L)) {
4576e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams                throw new IllegalArgumentException("Can't call setOverrideDeadline() on a " +
4587060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate                        "periodic job.");
4596e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            }
4603d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams            if (mIsPeriodic && (mMinLatencyMillis != 0L)) {
4616e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams                throw new IllegalArgumentException("Can't call setMinimumLatency() on a " +
4627060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate                        "periodic job");
4636e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            }
4646e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            if (mBackoffPolicySet && mRequiresDeviceIdle) {
4657060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate                throw new IllegalArgumentException("An idle mode job will not respect any" +
4666e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams                        " back-off policy, so calling setBackoffCriteria with" +
4676e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams                        " setRequiresDeviceIdle is an error.");
4686e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams            }
4697060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobInfo(this);
4706e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
4716e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
4726e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
4736e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams}
474