Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 17 | package android.app.job; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 18 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Christopher Tate | fa380e9 | 2014-05-19 13:46:29 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 22 | import android.net.Uri; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 25 | import android.os.PersistableBundle; |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 26 | import android.util.Log; |
Shreyas Basarge | d8bf6b9 | 2016-02-02 23:45:14 +0000 | [diff] [blame] | 27 | import static android.util.TimeUtils.formatForLogging; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 28 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 31 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | * Container of data passed to the {@link android.app.job.JobScheduler} fully encapsulating the |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 33 | * parameters required to schedule work against the calling application. These are constructed |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 34 | * using the {@link JobInfo.Builder}. |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 35 | * You must specify at least one sort of constraint on the JobInfo object that you are creating. |
| 36 | * The goal here is to provide the scheduler with high-level semantics about the work you want to |
| 37 | * accomplish. Doing otherwise with throw an exception in your app. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 38 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 39 | public class JobInfo implements Parcelable { |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 40 | private static String TAG = "JobInfo"; |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 41 | /** Default. */ |
| 42 | public static final int NETWORK_TYPE_NONE = 0; |
| 43 | /** This job requires network connectivity. */ |
| 44 | public static final int NETWORK_TYPE_ANY = 1; |
| 45 | /** This job requires network connectivity that is unmetered. */ |
| 46 | public static final int NETWORK_TYPE_UNMETERED = 2; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 49 | * Amount of backoff a job has initially by default, in milliseconds. |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 50 | */ |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 51 | public static final long DEFAULT_INITIAL_BACKOFF_MILLIS = 30000L; // 30 seconds. |
| 52 | |
| 53 | /** |
| 54 | * Maximum backoff we allow for a job, in milliseconds. |
| 55 | */ |
| 56 | public static final long MAX_BACKOFF_DELAY_MILLIS = 5 * 60 * 60 * 1000; // 5 hours. |
| 57 | |
| 58 | /** |
| 59 | * Linearly back-off a failed job. See |
| 60 | * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)} |
| 61 | * retry_time(current_time, num_failures) = |
| 62 | * current_time + initial_backoff_millis * num_failures, num_failures >= 1 |
| 63 | */ |
| 64 | public static final int BACKOFF_POLICY_LINEAR = 0; |
| 65 | |
| 66 | /** |
| 67 | * Exponentially back-off a failed job. See |
| 68 | * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)} |
| 69 | * |
| 70 | * retry_time(current_time, num_failures) = |
| 71 | * current_time + initial_backoff_millis * 2 ^ (num_failures - 1), num_failures >= 1 |
| 72 | */ |
| 73 | public static final int BACKOFF_POLICY_EXPONENTIAL = 1; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 74 | |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 75 | /* Minimum interval for a periodic job, in milliseconds. */ |
| 76 | public static final long MIN_PERIOD_MILLIS = 60 * 60 * 1000L; // 60 minutes |
| 77 | /* Minimum flex for a periodic job, in milliseconds. */ |
| 78 | public static final long MIN_FLEX_MILLIS = 5 * 60 * 1000L; // 5 minutes |
| 79 | |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Default type of backoff. |
| 82 | * @hide |
| 83 | */ |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 84 | public static final int DEFAULT_BACKOFF_POLICY = BACKOFF_POLICY_EXPONENTIAL; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 85 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Default of {@link #getPriority}. |
| 88 | * @hide |
| 89 | */ |
| 90 | public static final int PRIORITY_DEFAULT = 0; |
| 91 | |
| 92 | /** |
| 93 | * Value of {@link #getPriority} for expedited syncs. |
| 94 | * @hide |
| 95 | */ |
| 96 | public static final int PRIORITY_SYNC_EXPEDITED = 10; |
| 97 | |
| 98 | /** |
| 99 | * Value of {@link #getPriority} for first time initialization syncs. |
| 100 | * @hide |
| 101 | */ |
| 102 | public static final int PRIORITY_SYNC_INITIALIZATION = 20; |
| 103 | |
| 104 | /** |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 105 | * Value of {@link #getPriority} for a foreground app (overrides the supplied |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 106 | * JobInfo priority if it is smaller). |
| 107 | * @hide |
| 108 | */ |
| 109 | public static final int PRIORITY_FOREGROUND_APP = 30; |
| 110 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Value of {@link #getPriority} for the current top app (overrides the supplied |
| 113 | * JobInfo priority if it is smaller). |
| 114 | * @hide |
| 115 | */ |
| 116 | public static final int PRIORITY_TOP_APP = 40; |
| 117 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 118 | private final int jobId; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 119 | private final PersistableBundle extras; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 120 | private final ComponentName service; |
| 121 | private final boolean requireCharging; |
| 122 | private final boolean requireDeviceIdle; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 123 | private final TriggerContentUri[] triggerContentUris; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 124 | private final boolean hasEarlyConstraint; |
| 125 | private final boolean hasLateConstraint; |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 126 | private final int networkType; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 127 | private final long minLatencyMillis; |
| 128 | private final long maxExecutionDelayMillis; |
| 129 | private final boolean isPeriodic; |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 130 | private final boolean isPersisted; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 131 | private final long intervalMillis; |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 132 | private final long flexMillis; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 133 | private final long initialBackoffMillis; |
| 134 | private final int backoffPolicy; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 135 | private final int priority; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 136 | |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 137 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 138 | * Unique job id associated with this class. This is assigned to your job by the scheduler. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 139 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 140 | public int getId() { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 141 | return jobId; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Bundle of extras which are returned to your application at execution time. |
| 146 | */ |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 147 | public PersistableBundle getExtras() { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 148 | return extras; |
| 149 | } |
| 150 | |
| 151 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 152 | * Name of the service endpoint that will be called back into by the JobScheduler. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 153 | */ |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 154 | public ComponentName getService() { |
| 155 | return service; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 158 | /** @hide */ |
| 159 | public int getPriority() { |
| 160 | return priority; |
| 161 | } |
| 162 | |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 163 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 164 | * Whether this job needs the device to be plugged in. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 165 | */ |
| 166 | public boolean isRequireCharging() { |
| 167 | return requireCharging; |
| 168 | } |
| 169 | |
| 170 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 171 | * Whether this job needs the device to be in an Idle maintenance window. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 172 | */ |
| 173 | public boolean isRequireDeviceIdle() { |
| 174 | return requireDeviceIdle; |
| 175 | } |
| 176 | |
| 177 | /** |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 178 | * Which content: URIs must change for the job to be scheduled. Returns null |
| 179 | * if there are none required. |
| 180 | */ |
| 181 | @Nullable |
| 182 | public TriggerContentUri[] getTriggerContentUris() { |
| 183 | return triggerContentUris; |
| 184 | } |
| 185 | |
| 186 | /** |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 187 | * One of {@link android.app.job.JobInfo#NETWORK_TYPE_ANY}, |
| 188 | * {@link android.app.job.JobInfo#NETWORK_TYPE_NONE}, or |
| 189 | * {@link android.app.job.JobInfo#NETWORK_TYPE_UNMETERED}. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 190 | */ |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 191 | public int getNetworkType() { |
| 192 | return networkType; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 196 | * Set for a job that does not recur periodically, to specify a delay after which the job |
| 197 | * will be eligible for execution. This value is not set if the job recurs periodically. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 198 | */ |
| 199 | public long getMinLatencyMillis() { |
| 200 | return minLatencyMillis; |
| 201 | } |
| 202 | |
| 203 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 204 | * See {@link Builder#setOverrideDeadline(long)}. This value is not set if the job recurs |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 205 | * periodically. |
| 206 | */ |
| 207 | public long getMaxExecutionDelayMillis() { |
| 208 | return maxExecutionDelayMillis; |
| 209 | } |
| 210 | |
| 211 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 212 | * Track whether this job will repeat with a given period. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 213 | */ |
| 214 | public boolean isPeriodic() { |
| 215 | return isPeriodic; |
| 216 | } |
| 217 | |
| 218 | /** |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 219 | * @return Whether or not this job should be persisted across device reboots. |
| 220 | */ |
| 221 | public boolean isPersisted() { |
| 222 | return isPersisted; |
| 223 | } |
| 224 | |
| 225 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 226 | * Set to the interval between occurrences of this job. This value is <b>not</b> set if the |
| 227 | * job does not recur periodically. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 228 | */ |
| 229 | public long getIntervalMillis() { |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 230 | return intervalMillis >= MIN_PERIOD_MILLIS ? intervalMillis : MIN_PERIOD_MILLIS; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Flex time for this job. Only valid if this is a periodic job. |
| 235 | */ |
| 236 | public long getFlexMillis() { |
| 237 | long interval = getIntervalMillis(); |
| 238 | long percentClamp = 5 * interval / 100; |
| 239 | long clampedFlex = Math.max(flexMillis, Math.max(percentClamp, MIN_FLEX_MILLIS)); |
| 240 | return clampedFlex <= interval ? clampedFlex : interval; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 244 | * The amount of time the JobScheduler will wait before rescheduling a failed job. This value |
| 245 | * will be increased depending on the backoff policy specified at job creation time. Defaults |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 246 | * to 5 seconds. |
| 247 | */ |
| 248 | public long getInitialBackoffMillis() { |
| 249 | return initialBackoffMillis; |
| 250 | } |
| 251 | |
| 252 | /** |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 253 | * One of either {@link android.app.job.JobInfo#BACKOFF_POLICY_EXPONENTIAL}, or |
| 254 | * {@link android.app.job.JobInfo#BACKOFF_POLICY_LINEAR}, depending on which criteria you set |
| 255 | * when creating this job. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 256 | */ |
| 257 | public int getBackoffPolicy() { |
| 258 | return backoffPolicy; |
| 259 | } |
| 260 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 261 | /** |
| 262 | * User can specify an early constraint of 0L, which is valid, so we keep track of whether the |
| 263 | * function was called at all. |
| 264 | * @hide |
| 265 | */ |
| 266 | public boolean hasEarlyConstraint() { |
| 267 | return hasEarlyConstraint; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * User can specify a late constraint of 0L, which is valid, so we keep track of whether the |
| 272 | * function was called at all. |
| 273 | * @hide |
| 274 | */ |
| 275 | public boolean hasLateConstraint() { |
| 276 | return hasLateConstraint; |
| 277 | } |
| 278 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 279 | private JobInfo(Parcel in) { |
| 280 | jobId = in.readInt(); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 281 | extras = in.readPersistableBundle(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 282 | service = in.readParcelable(null); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 283 | requireCharging = in.readInt() == 1; |
| 284 | requireDeviceIdle = in.readInt() == 1; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 285 | triggerContentUris = in.createTypedArray(TriggerContentUri.CREATOR); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 286 | networkType = in.readInt(); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 287 | minLatencyMillis = in.readLong(); |
| 288 | maxExecutionDelayMillis = in.readLong(); |
| 289 | isPeriodic = in.readInt() == 1; |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 290 | isPersisted = in.readInt() == 1; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 291 | intervalMillis = in.readLong(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 292 | flexMillis = in.readLong(); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 293 | initialBackoffMillis = in.readLong(); |
| 294 | backoffPolicy = in.readInt(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 295 | hasEarlyConstraint = in.readInt() == 1; |
| 296 | hasLateConstraint = in.readInt() == 1; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 297 | priority = in.readInt(); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 300 | private JobInfo(JobInfo.Builder b) { |
| 301 | jobId = b.mJobId; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 302 | extras = b.mExtras; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 303 | service = b.mJobService; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 304 | requireCharging = b.mRequiresCharging; |
| 305 | requireDeviceIdle = b.mRequiresDeviceIdle; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 306 | triggerContentUris = b.mTriggerContentUris != null |
| 307 | ? b.mTriggerContentUris.toArray(new TriggerContentUri[b.mTriggerContentUris.size()]) |
| 308 | : null; |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 309 | networkType = b.mNetworkType; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 310 | minLatencyMillis = b.mMinLatencyMillis; |
| 311 | maxExecutionDelayMillis = b.mMaxExecutionDelayMillis; |
| 312 | isPeriodic = b.mIsPeriodic; |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 313 | isPersisted = b.mIsPersisted; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 314 | intervalMillis = b.mIntervalMillis; |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 315 | flexMillis = b.mFlexMillis; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 316 | initialBackoffMillis = b.mInitialBackoffMillis; |
| 317 | backoffPolicy = b.mBackoffPolicy; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 318 | hasEarlyConstraint = b.mHasEarlyConstraint; |
| 319 | hasLateConstraint = b.mHasLateConstraint; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 320 | priority = b.mPriority; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public int describeContents() { |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void writeToParcel(Parcel out, int flags) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 330 | out.writeInt(jobId); |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 331 | out.writePersistableBundle(extras); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 332 | out.writeParcelable(service, flags); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 333 | out.writeInt(requireCharging ? 1 : 0); |
| 334 | out.writeInt(requireDeviceIdle ? 1 : 0); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 335 | out.writeTypedArray(triggerContentUris, flags); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 336 | out.writeInt(networkType); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 337 | out.writeLong(minLatencyMillis); |
| 338 | out.writeLong(maxExecutionDelayMillis); |
| 339 | out.writeInt(isPeriodic ? 1 : 0); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 340 | out.writeInt(isPersisted ? 1 : 0); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 341 | out.writeLong(intervalMillis); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 342 | out.writeLong(flexMillis); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 343 | out.writeLong(initialBackoffMillis); |
| 344 | out.writeInt(backoffPolicy); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 345 | out.writeInt(hasEarlyConstraint ? 1 : 0); |
| 346 | out.writeInt(hasLateConstraint ? 1 : 0); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 347 | out.writeInt(priority); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 350 | public static final Creator<JobInfo> CREATOR = new Creator<JobInfo>() { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 351 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 352 | public JobInfo createFromParcel(Parcel in) { |
| 353 | return new JobInfo(in); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 357 | public JobInfo[] newArray(int size) { |
| 358 | return new JobInfo[size]; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 359 | } |
| 360 | }; |
| 361 | |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 362 | @Override |
| 363 | public String toString() { |
| 364 | return "(job:" + jobId + "/" + service.flattenToShortString() + ")"; |
| 365 | } |
| 366 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 367 | /** |
| 368 | * Information about a content URI modification that a job would like to |
| 369 | * trigger on. |
| 370 | */ |
| 371 | public static final class TriggerContentUri implements Parcelable { |
| 372 | private final Uri mUri; |
| 373 | private final int mFlags; |
| 374 | |
| 375 | /** |
| 376 | * Flag for trigger: also trigger if any descendants of the given URI change. |
| 377 | * Corresponds to the <var>notifyForDescendants</var> of |
| 378 | * {@link android.content.ContentResolver#registerContentObserver}. |
| 379 | */ |
| 380 | public static final int FLAG_NOTIFY_FOR_DESCENDANTS = 1<<0; |
| 381 | |
| 382 | /** |
| 383 | * Create a new trigger description. |
| 384 | * @param uri The URI to observe. Must be non-null. |
| 385 | * @param flags Optional flags for the observer, either 0 or |
| 386 | * {@link #FLAG_NOTIFY_FOR_DESCENDANTS}. |
| 387 | */ |
| 388 | public TriggerContentUri(@NonNull Uri uri, int flags) { |
| 389 | mUri = uri; |
| 390 | mFlags = flags; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Return the Uri this trigger was created for. |
| 395 | */ |
| 396 | public Uri getUri() { |
| 397 | return mUri; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Return the flags supplied for the trigger. |
| 402 | */ |
| 403 | public int getFlags() { |
| 404 | return mFlags; |
| 405 | } |
| 406 | |
| 407 | private TriggerContentUri(Parcel in) { |
| 408 | mUri = Uri.CREATOR.createFromParcel(in); |
| 409 | mFlags = in.readInt(); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public int describeContents() { |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void writeToParcel(Parcel out, int flags) { |
| 419 | mUri.writeToParcel(out, flags); |
| 420 | out.writeInt(mFlags); |
| 421 | } |
| 422 | |
| 423 | public static final Creator<TriggerContentUri> CREATOR = new Creator<TriggerContentUri>() { |
| 424 | @Override |
| 425 | public TriggerContentUri createFromParcel(Parcel in) { |
| 426 | return new TriggerContentUri(in); |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public TriggerContentUri[] newArray(int size) { |
| 431 | return new TriggerContentUri[size]; |
| 432 | } |
| 433 | }; |
| 434 | } |
| 435 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 436 | /** Builder class for constructing {@link JobInfo} objects. */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 437 | public static final class Builder { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 438 | private int mJobId; |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 439 | private PersistableBundle mExtras = PersistableBundle.EMPTY; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 440 | private ComponentName mJobService; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 441 | private int mPriority = PRIORITY_DEFAULT; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 442 | // Requirements. |
| 443 | private boolean mRequiresCharging; |
| 444 | private boolean mRequiresDeviceIdle; |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 445 | private int mNetworkType; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 446 | private ArrayList<TriggerContentUri> mTriggerContentUris; |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 447 | private boolean mIsPersisted; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 448 | // One-off parameters. |
| 449 | private long mMinLatencyMillis; |
| 450 | private long mMaxExecutionDelayMillis; |
| 451 | // Periodic parameters. |
| 452 | private boolean mIsPeriodic; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 453 | private boolean mHasEarlyConstraint; |
| 454 | private boolean mHasLateConstraint; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 455 | private long mIntervalMillis; |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 456 | private long mFlexMillis; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 457 | // Back-off parameters. |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 458 | private long mInitialBackoffMillis = DEFAULT_INITIAL_BACKOFF_MILLIS; |
| 459 | private int mBackoffPolicy = DEFAULT_BACKOFF_POLICY; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 460 | /** Easy way to track whether the client has tried to set a back-off policy. */ |
| 461 | private boolean mBackoffPolicySet = false; |
| 462 | |
| 463 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 464 | * @param jobId Application-provided id for this job. Subsequent calls to cancel, or |
| 465 | * jobs created with the same jobId, will update the pre-existing job with |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 466 | * the same id. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 467 | * @param jobService The endpoint that you implement that will receive the callback from the |
| 468 | * JobScheduler. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 469 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 470 | public Builder(int jobId, ComponentName jobService) { |
| 471 | mJobService = jobService; |
| 472 | mJobId = jobId; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 476 | * @hide |
| 477 | */ |
| 478 | public Builder setPriority(int priority) { |
| 479 | mPriority = priority; |
| 480 | return this; |
| 481 | } |
| 482 | |
| 483 | /** |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 484 | * Set optional extras. This is persisted, so we only allow primitive types. |
| 485 | * @param extras Bundle containing extras you want the scheduler to hold on to for you. |
| 486 | */ |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 487 | public Builder setExtras(PersistableBundle extras) { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 488 | mExtras = extras; |
| 489 | return this; |
| 490 | } |
| 491 | |
| 492 | /** |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 493 | * Set some description of the kind of network type your job needs to have. |
| 494 | * Not calling this function means the network is not necessary, as the default is |
| 495 | * {@link #NETWORK_TYPE_NONE}. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 496 | * Bear in mind that calling this function defines network as a strict requirement for your |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 497 | * job. If the network requested is not available your job will never run. See |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 498 | * {@link #setOverrideDeadline(long)} to change this behaviour. |
| 499 | */ |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 500 | public Builder setRequiredNetworkType(int networkType) { |
| 501 | mNetworkType = networkType; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 502 | return this; |
| 503 | } |
| 504 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 505 | /** |
| 506 | * Specify that to run this job, the device needs to be plugged in. This defaults to |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 507 | * false. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 508 | * @param requiresCharging Whether or not the device is plugged in. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 509 | */ |
| 510 | public Builder setRequiresCharging(boolean requiresCharging) { |
| 511 | mRequiresCharging = requiresCharging; |
| 512 | return this; |
| 513 | } |
| 514 | |
| 515 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 516 | * Specify that to run, the job needs the device to be in idle mode. This defaults to |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 517 | * false. |
| 518 | * <p>Idle mode is a loose definition provided by the system, which means that the device |
| 519 | * is not in use, and has not been in use for some time. As such, it is a good time to |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 520 | * perform resource heavy jobs. Bear in mind that battery usage will still be attributed |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 521 | * to your application, and surfaced to the user in battery stats.</p> |
| 522 | * @param requiresDeviceIdle Whether or not the device need be within an idle maintenance |
| 523 | * window. |
| 524 | */ |
| 525 | public Builder setRequiresDeviceIdle(boolean requiresDeviceIdle) { |
| 526 | mRequiresDeviceIdle = requiresDeviceIdle; |
| 527 | return this; |
| 528 | } |
| 529 | |
| 530 | /** |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 531 | * Add a new content: URI that will be monitored with a |
| 532 | * {@link android.database.ContentObserver}, and will cause the job to execute if changed. |
| 533 | * If you have any trigger content URIs associated with a job, it will not execute until |
| 534 | * there has been a change report for one or more of them. |
| 535 | * <p>Note that trigger URIs can not be used in combination with |
| 536 | * {@link #setPeriodic(long)} or {@link #setPersisted(boolean)}. To continually monitor |
| 537 | * for content changes, you need to schedule a new JobInfo observing the same URIs |
| 538 | * before you finish execution of the JobService handling the most recent changes.</p> |
| 539 | * @param uri The content: URI to monitor. |
| 540 | */ |
| 541 | public Builder addTriggerContentUri(@NonNull TriggerContentUri uri) { |
| 542 | if (mTriggerContentUris == null) { |
| 543 | mTriggerContentUris = new ArrayList<>(); |
| 544 | } |
| 545 | mTriggerContentUris.add(uri); |
| 546 | return this; |
| 547 | } |
| 548 | |
| 549 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 550 | * Specify that this job should recur with the provided interval, not more than once per |
| 551 | * period. You have no control over when within this interval this job will be executed, |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 552 | * only the guarantee that it will be executed at most once within this interval. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 553 | * Setting this function on the builder with {@link #setMinimumLatency(long)} or |
| 554 | * {@link #setOverrideDeadline(long)} will result in an error. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 555 | * @param intervalMillis Millisecond interval for which this job will repeat. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 556 | */ |
| 557 | public Builder setPeriodic(long intervalMillis) { |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 558 | return setPeriodic(intervalMillis, intervalMillis); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Specify that this job should recur with the provided interval and flex. The job can |
| 563 | * execute at any time in a window of flex length at the end of the period. |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 564 | * @param intervalMillis Millisecond interval for which this job will repeat. A minimum |
| 565 | * value of {@link #MIN_PERIOD_MILLIS} is enforced. |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 566 | * @param flexMillis Millisecond flex for this job. Flex is clamped to be at least |
| 567 | * {@link #MIN_FLEX_MILLIS} or 5 percent of the period, whichever is |
| 568 | * higher. |
| 569 | */ |
| 570 | public Builder setPeriodic(long intervalMillis, long flexMillis) { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 571 | mIsPeriodic = true; |
| 572 | mIntervalMillis = intervalMillis; |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 573 | mFlexMillis = flexMillis; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 574 | mHasEarlyConstraint = mHasLateConstraint = true; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 575 | return this; |
| 576 | } |
| 577 | |
| 578 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 579 | * Specify that this job should be delayed by the provided amount of time. |
| 580 | * Because it doesn't make sense setting this property on a periodic job, doing so will |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 581 | * throw an {@link java.lang.IllegalArgumentException} when |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 582 | * {@link android.app.job.JobInfo.Builder#build()} is called. |
| 583 | * @param minLatencyMillis Milliseconds before which this job will not be considered for |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 584 | * execution. |
| 585 | */ |
| 586 | public Builder setMinimumLatency(long minLatencyMillis) { |
| 587 | mMinLatencyMillis = minLatencyMillis; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 588 | mHasEarlyConstraint = true; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 589 | return this; |
| 590 | } |
| 591 | |
| 592 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 593 | * Set deadline which is the maximum scheduling latency. The job will be run by this |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 594 | * deadline even if other requirements are not met. Because it doesn't make sense setting |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 595 | * this property on a periodic job, doing so will throw an |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 596 | * {@link java.lang.IllegalArgumentException} when |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 597 | * {@link android.app.job.JobInfo.Builder#build()} is called. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 598 | */ |
| 599 | public Builder setOverrideDeadline(long maxExecutionDelayMillis) { |
| 600 | mMaxExecutionDelayMillis = maxExecutionDelayMillis; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 601 | mHasLateConstraint = true; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 602 | return this; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Set up the back-off/retry policy. |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 607 | * This defaults to some respectable values: {30 seconds, Exponential}. We cap back-off at |
| 608 | * 5hrs. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 609 | * Note that trying to set a backoff criteria for a job with |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 610 | * {@link #setRequiresDeviceIdle(boolean)} will throw an exception when you call build(). |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 611 | * This is because back-off typically does not make sense for these types of jobs. See |
| 612 | * {@link android.app.job.JobService#jobFinished(android.app.job.JobParameters, boolean)} |
| 613 | * for more description of the return value for the case of a job executing while in idle |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 614 | * mode. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 615 | * @param initialBackoffMillis Millisecond time interval to wait initially when job has |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 616 | * failed. |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 617 | * @param backoffPolicy is one of {@link #BACKOFF_POLICY_LINEAR} or |
| 618 | * {@link #BACKOFF_POLICY_EXPONENTIAL} |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 619 | */ |
| 620 | public Builder setBackoffCriteria(long initialBackoffMillis, int backoffPolicy) { |
| 621 | mBackoffPolicySet = true; |
| 622 | mInitialBackoffMillis = initialBackoffMillis; |
| 623 | mBackoffPolicy = backoffPolicy; |
| 624 | return this; |
| 625 | } |
| 626 | |
| 627 | /** |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 628 | * Set whether or not to persist this job across device reboots. This will only have an |
| 629 | * effect if your application holds the permission |
| 630 | * {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED}. Otherwise an exception will |
| 631 | * be thrown. |
| 632 | * @param isPersisted True to indicate that the job will be written to disk and loaded at |
| 633 | * boot. |
| 634 | */ |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 635 | public Builder setPersisted(boolean isPersisted) { |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 636 | mIsPersisted = isPersisted; |
| 637 | return this; |
| 638 | } |
| 639 | |
| 640 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 641 | * @return The job object to hand to the JobScheduler. This object is immutable. |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 642 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 643 | public JobInfo build() { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 644 | // Allow jobs with no constraints - What am I, a database? |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 645 | if (!mHasEarlyConstraint && !mHasLateConstraint && !mRequiresCharging && |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 646 | !mRequiresDeviceIdle && mNetworkType == NETWORK_TYPE_NONE && |
| 647 | mTriggerContentUris == null) { |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 648 | throw new IllegalArgumentException("You're trying to build a job with no " + |
| 649 | "constraints, this is not allowed."); |
| 650 | } |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 651 | mExtras = new PersistableBundle(mExtras); // Make our own copy. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 652 | // Check that a deadline was not set on a periodic job. |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 653 | if (mIsPeriodic && (mMaxExecutionDelayMillis != 0L)) { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 654 | throw new IllegalArgumentException("Can't call setOverrideDeadline() on a " + |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 655 | "periodic job."); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 656 | } |
Matthew Williams | 3d86fd2 | 2014-05-16 18:02:17 -0700 | [diff] [blame] | 657 | if (mIsPeriodic && (mMinLatencyMillis != 0L)) { |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 658 | throw new IllegalArgumentException("Can't call setMinimumLatency() on a " + |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 659 | "periodic job"); |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 660 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 661 | if (mIsPeriodic && (mTriggerContentUris != null)) { |
| 662 | throw new IllegalArgumentException("Can't call addTriggerContentUri() on a " + |
| 663 | "periodic job"); |
| 664 | } |
| 665 | if (mIsPersisted && (mTriggerContentUris != null)) { |
| 666 | throw new IllegalArgumentException("Can't call addTriggerContentUri() on a " + |
| 667 | "persisted job"); |
| 668 | } |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 669 | if (mBackoffPolicySet && mRequiresDeviceIdle) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 670 | throw new IllegalArgumentException("An idle mode job will not respect any" + |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 671 | " back-off policy, so calling setBackoffCriteria with" + |
| 672 | " setRequiresDeviceIdle is an error."); |
| 673 | } |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 674 | JobInfo job = new JobInfo(this); |
| 675 | if (job.intervalMillis != job.getIntervalMillis()) { |
Shreyas Basarge | d8bf6b9 | 2016-02-02 23:45:14 +0000 | [diff] [blame] | 676 | Log.w(TAG, "Specified interval for " + mJobService.getPackageName() + " is " |
| 677 | + formatForLogging(mIntervalMillis) + ". Clamped to " + |
| 678 | formatForLogging(job.getIntervalMillis())); |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 679 | } |
| 680 | if (job.flexMillis != job.getFlexMillis()) { |
Shreyas Basarge | d8bf6b9 | 2016-02-02 23:45:14 +0000 | [diff] [blame] | 681 | Log.w(TAG, "Specified interval for " + mJobService.getPackageName() + " is " |
| 682 | + formatForLogging(mFlexMillis) + ". Clamped to " + |
| 683 | formatForLogging(job.getFlexMillis())); |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 684 | } |
| 685 | return job; |
Matthew Williams | 6e31c5c | 2014-04-11 15:49:16 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | } |