Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 17 | package androidx.work.impl.model; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 18 | |
Sumir Kataria | 8b3284f | 2018-04-13 09:50:18 -0700 | [diff] [blame] | 19 | import static androidx.work.PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS; |
| 20 | import static androidx.work.PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS; |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 21 | import static androidx.work.WorkInfo.State.ENQUEUED; |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 22 | import static androidx.work.WorkRequest.MAX_BACKOFF_MILLIS; |
| 23 | import static androidx.work.WorkRequest.MIN_BACKOFF_MILLIS; |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 24 | |
Sumir Kataria | 2cdf00b | 2019-05-14 16:08:41 -0700 | [diff] [blame] | 25 | import androidx.annotation.IntRange; |
Rahul Ravikumar | e535666 | 2019-03-04 16:22:14 -0800 | [diff] [blame] | 26 | import androidx.annotation.NonNull; |
| 27 | import androidx.annotation.RestrictTo; |
| 28 | import androidx.arch.core.util.Function; |
| 29 | import androidx.room.ColumnInfo; |
| 30 | import androidx.room.Embedded; |
| 31 | import androidx.room.Entity; |
| 32 | import androidx.room.Index; |
| 33 | import androidx.room.PrimaryKey; |
| 34 | import androidx.room.Relation; |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 35 | import androidx.work.BackoffPolicy; |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 36 | import androidx.work.Constraints; |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 37 | import androidx.work.Data; |
Sumir Kataria | fd60d22 | 2018-06-22 16:23:41 -0700 | [diff] [blame] | 38 | import androidx.work.Logger; |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 39 | import androidx.work.WorkInfo; |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 40 | import androidx.work.WorkRequest; |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 41 | |
Sumir Kataria | 4401f79 | 2018-03-20 12:52:38 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 43 | import java.util.List; |
Sumir Kataria | fa284c9 | 2018-04-23 14:25:53 -0700 | [diff] [blame] | 44 | import java.util.UUID; |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 45 | |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Stores information about a logical unit of work. |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 48 | * |
| 49 | * @hide |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 50 | */ |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 51 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 52 | @Entity( |
| 53 | indices = {@Index(value = {"schedule_requested_at"})} |
| 54 | ) |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 55 | public class WorkSpec { |
Rahul Ravikumar | 38e7c6a | 2018-12-13 16:41:34 -0800 | [diff] [blame] | 56 | private static final String TAG = Logger.tagWithPrefix("WorkSpec"); |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 57 | public static final long SCHEDULE_NOT_REQUESTED_YET = -1; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 58 | |
| 59 | @ColumnInfo(name = "id") |
| 60 | @PrimaryKey |
| 61 | @NonNull |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 62 | public String id; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 63 | |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 64 | @ColumnInfo(name = "state") |
Sumir Kataria | 86894df | 2018-03-06 15:11:16 -0800 | [diff] [blame] | 65 | @NonNull |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 66 | public WorkInfo.State state = ENQUEUED; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 67 | |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 68 | @ColumnInfo(name = "worker_class_name") |
Sumir Kataria | 86894df | 2018-03-06 15:11:16 -0800 | [diff] [blame] | 69 | @NonNull |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 70 | public String workerClassName; |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 71 | |
Sumir Kataria | a48d7ac | 2017-11-28 16:24:15 -0800 | [diff] [blame] | 72 | @ColumnInfo(name = "input_merger_class_name") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 73 | public String inputMergerClassName; |
Sumir Kataria | a48d7ac | 2017-11-28 16:24:15 -0800 | [diff] [blame] | 74 | |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 75 | @ColumnInfo(name = "input") |
Sumir Kataria | 9244d37 | 2017-11-30 13:39:07 -0800 | [diff] [blame] | 76 | @NonNull |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 77 | public Data input = Data.EMPTY; |
Sumir Kataria | 9244d37 | 2017-11-30 13:39:07 -0800 | [diff] [blame] | 78 | |
| 79 | @ColumnInfo(name = "output") |
| 80 | @NonNull |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 81 | public Data output = Data.EMPTY; |
Sumir Kataria | 9244d37 | 2017-11-30 13:39:07 -0800 | [diff] [blame] | 82 | |
Xyan Bhatnagar | a1af78b | 2017-10-04 17:36:36 -0700 | [diff] [blame] | 83 | @ColumnInfo(name = "initial_delay") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 84 | public long initialDelay; |
Xyan Bhatnagar | a1af78b | 2017-10-04 17:36:36 -0700 | [diff] [blame] | 85 | |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 86 | @ColumnInfo(name = "interval_duration") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 87 | public long intervalDuration; |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 88 | |
| 89 | @ColumnInfo(name = "flex_duration") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 90 | public long flexDuration; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 91 | |
| 92 | @Embedded |
Sumir Kataria | 86894df | 2018-03-06 15:11:16 -0800 | [diff] [blame] | 93 | @NonNull |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 94 | public Constraints constraints = Constraints.NONE; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 95 | |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 96 | @ColumnInfo(name = "run_attempt_count") |
Sumir Kataria | 2cdf00b | 2019-05-14 16:08:41 -0700 | [diff] [blame] | 97 | @IntRange(from = 0) |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 98 | public int runAttemptCount; |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 99 | |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 100 | @ColumnInfo(name = "backoff_policy") |
Sumir Kataria | 86894df | 2018-03-06 15:11:16 -0800 | [diff] [blame] | 101 | @NonNull |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 102 | public BackoffPolicy backoffPolicy = BackoffPolicy.EXPONENTIAL; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 103 | |
| 104 | @ColumnInfo(name = "backoff_delay_duration") |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 105 | public long backoffDelayDuration = WorkRequest.DEFAULT_BACKOFF_DELAY_MILLIS; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 106 | |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 107 | /** |
| 108 | * For one-off work, this is the time that the work was unblocked by prerequisites. |
| 109 | * For periodic work, this is the time that the period started. |
| 110 | */ |
Jan Clarin | 0deb5e2 | 2017-12-06 11:26:29 -0800 | [diff] [blame] | 111 | @ColumnInfo(name = "period_start_time") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 112 | public long periodStartTime; |
Jan Clarin | 0deb5e2 | 2017-12-06 11:26:29 -0800 | [diff] [blame] | 113 | |
Sumir Kataria | 3d5949e | 2018-03-06 13:32:20 -0800 | [diff] [blame] | 114 | @ColumnInfo(name = "minimum_retention_duration") |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 115 | public long minimumRetentionDuration; |
Sumir Kataria | 3d5949e | 2018-03-06 13:32:20 -0800 | [diff] [blame] | 116 | |
Rahul Ravikumar | fe793bc | 2018-07-09 12:43:26 -0700 | [diff] [blame] | 117 | /** |
| 118 | * This field tells us if this {@link WorkSpec} instance, is actually currently scheduled and |
| 119 | * being counted against the {@code SCHEDULER_LIMIT}. This bit is reset for PeriodicWorkRequests |
| 120 | * in API < 23, because AlarmManager does not know of PeriodicWorkRequests. So for the next |
| 121 | * request to be rescheduled this field has to be reset to {@code SCHEDULE_NOT_REQUESTED_AT}. |
| 122 | * For the JobScheduler implementation, we don't reset this field because JobScheduler natively |
| 123 | * supports PeriodicWorkRequests. |
| 124 | */ |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 125 | @ColumnInfo(name = "schedule_requested_at") |
| 126 | public long scheduleRequestedAt = SCHEDULE_NOT_REQUESTED_YET; |
| 127 | |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 128 | public WorkSpec(@NonNull String id, @NonNull String workerClassName) { |
| 129 | this.id = id; |
| 130 | this.workerClassName = workerClassName; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Sumir Kataria | a31df031 | 2018-07-27 14:28:01 -0700 | [diff] [blame] | 133 | public WorkSpec(@NonNull WorkSpec other) { |
| 134 | id = other.id; |
| 135 | workerClassName = other.workerClassName; |
| 136 | state = other.state; |
| 137 | inputMergerClassName = other.inputMergerClassName; |
| 138 | input = new Data(other.input); |
| 139 | output = new Data(other.output); |
| 140 | initialDelay = other.initialDelay; |
| 141 | intervalDuration = other.intervalDuration; |
| 142 | flexDuration = other.flexDuration; |
| 143 | constraints = new Constraints(other.constraints); |
| 144 | runAttemptCount = other.runAttemptCount; |
| 145 | backoffPolicy = other.backoffPolicy; |
| 146 | backoffDelayDuration = other.backoffDelayDuration; |
| 147 | periodStartTime = other.periodStartTime; |
| 148 | minimumRetentionDuration = other.minimumRetentionDuration; |
| 149 | scheduleRequestedAt = other.scheduleRequestedAt; |
| 150 | } |
| 151 | |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 152 | /** |
| 153 | * @param backoffDelayDuration The backoff delay duration in milliseconds |
| 154 | */ |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 155 | public void setBackoffDelayDuration(long backoffDelayDuration) { |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 156 | if (backoffDelayDuration > MAX_BACKOFF_MILLIS) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 157 | Logger.get().warning(TAG, "Backoff delay duration exceeds maximum value"); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 158 | backoffDelayDuration = MAX_BACKOFF_MILLIS; |
| 159 | } |
| 160 | if (backoffDelayDuration < MIN_BACKOFF_MILLIS) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 161 | Logger.get().warning(TAG, "Backoff delay duration less than minimum value"); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 162 | backoffDelayDuration = MIN_BACKOFF_MILLIS; |
| 163 | } |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 164 | this.backoffDelayDuration = backoffDelayDuration; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 165 | } |
Xyan Bhatnagar | a1af78b | 2017-10-04 17:36:36 -0700 | [diff] [blame] | 166 | |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 167 | |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 168 | public boolean isPeriodic() { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 169 | return intervalDuration != 0L; |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 172 | public boolean isBackedOff() { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 173 | return state == ENQUEUED && runAttemptCount > 0; |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Sumir Kataria | 6d55d8c | 2017-12-07 13:37:42 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Sets the periodic interval for this unit of work. |
| 178 | * |
| 179 | * @param intervalDuration The interval in milliseconds |
| 180 | */ |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 181 | public void setPeriodic(long intervalDuration) { |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 182 | if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 183 | Logger.get().warning(TAG, String.format( |
Rahul Ravikumar | 697d6a4 | 2018-04-16 15:44:09 -0700 | [diff] [blame] | 184 | "Interval duration lesser than minimum allowed value; Changed to %s", |
| 185 | MIN_PERIODIC_INTERVAL_MILLIS)); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 186 | intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS; |
| 187 | } |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 188 | setPeriodic(intervalDuration, intervalDuration); |
| 189 | } |
| 190 | |
Sumir Kataria | 6d55d8c | 2017-12-07 13:37:42 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Sets the periodic interval for this unit of work. |
| 193 | * |
| 194 | * @param intervalDuration The interval in milliseconds |
| 195 | * @param flexDuration The flex duration in milliseconds |
| 196 | */ |
Jan Clarin | ee302e1 | 2017-10-09 11:16:19 -0700 | [diff] [blame] | 197 | public void setPeriodic(long intervalDuration, long flexDuration) { |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 198 | if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 199 | Logger.get().warning(TAG, String.format( |
Rahul Ravikumar | 697d6a4 | 2018-04-16 15:44:09 -0700 | [diff] [blame] | 200 | "Interval duration lesser than minimum allowed value; Changed to %s", |
| 201 | MIN_PERIODIC_INTERVAL_MILLIS)); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 202 | intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS; |
| 203 | } |
| 204 | if (flexDuration < MIN_PERIODIC_FLEX_MILLIS) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 205 | Logger.get().warning(TAG, |
Rahul Ravikumar | 697d6a4 | 2018-04-16 15:44:09 -0700 | [diff] [blame] | 206 | String.format("Flex duration lesser than minimum allowed value; Changed to %s", |
| 207 | MIN_PERIODIC_FLEX_MILLIS)); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 208 | flexDuration = MIN_PERIODIC_FLEX_MILLIS; |
| 209 | } |
| 210 | if (flexDuration > intervalDuration) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 211 | Logger.get().warning(TAG, |
Sumir Kataria | fd60d22 | 2018-06-22 16:23:41 -0700 | [diff] [blame] | 212 | String.format("Flex duration greater than interval duration; Changed to %s", |
Rahul Ravikumar | 697d6a4 | 2018-04-16 15:44:09 -0700 | [diff] [blame] | 213 | intervalDuration)); |
Sumir Kataria | 1cd0e4e | 2017-12-12 10:53:46 -0800 | [diff] [blame] | 214 | flexDuration = intervalDuration; |
| 215 | } |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 216 | this.intervalDuration = intervalDuration; |
| 217 | this.flexDuration = flexDuration; |
Sumir Kataria | 3d5949e | 2018-03-06 13:32:20 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Jan Clarin | 0deb5e2 | 2017-12-06 11:26:29 -0800 | [diff] [blame] | 220 | /** |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 221 | * Calculates the UTC time at which this {@link WorkSpec} should be allowed to run. |
| 222 | * This method accounts for work that is backed off or periodic. |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 223 | * |
Sumir Kataria | b6430f2 | 2018-02-05 13:45:22 -0800 | [diff] [blame] | 224 | * If Backoff Policy is set to {@link BackoffPolicy#EXPONENTIAL}, then delay |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 225 | * increases at an exponential rate with respect to the run attempt count and is capped at |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 226 | * {@link WorkRequest#MAX_BACKOFF_MILLIS}. |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 227 | * |
Sumir Kataria | b6430f2 | 2018-02-05 13:45:22 -0800 | [diff] [blame] | 228 | * If Backoff Policy is set to {@link BackoffPolicy#LINEAR}, then delay |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 229 | * increases at an linear rate with respect to the run attempt count and is capped at |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 230 | * {@link WorkRequest#MAX_BACKOFF_MILLIS}. |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 231 | * |
| 232 | * Based on {@see https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/job/JobSchedulerService.java#1125} |
| 233 | * |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 234 | * Note that this runtime is for WorkManager internal use and may not match what the OS |
| 235 | * considers to be the next runtime. |
Sumir Kataria | 58e4d6f | 2017-11-14 14:31:15 -0800 | [diff] [blame] | 236 | * |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 237 | * For jobs with constraints, this represents the earliest time at which constraints |
| 238 | * should be monitored for this work. |
| 239 | * |
| 240 | * For jobs without constraints, this represents the earliest time at which this work is |
| 241 | * allowed to run. |
| 242 | * |
| 243 | * @return UTC time at which this {@link WorkSpec} should be allowed to run. |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 244 | */ |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 245 | public long calculateNextRunTime() { |
| 246 | if (isBackedOff()) { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 247 | boolean isLinearBackoff = (backoffPolicy == BackoffPolicy.LINEAR); |
| 248 | long delay = isLinearBackoff ? (backoffDelayDuration * runAttemptCount) |
| 249 | : (long) Math.scalb(backoffDelayDuration, runAttemptCount - 1); |
Rahul Ravikumar | 7031a0f | 2018-04-19 14:24:30 -0700 | [diff] [blame] | 250 | return periodStartTime + Math.min(WorkRequest.MAX_BACKOFF_MILLIS, delay); |
Xyan Bhatnagar | 1c78bb2 | 2017-12-08 15:48:19 -0800 | [diff] [blame] | 251 | } else if (isPeriodic()) { |
Rahul Ravikumar | 25dc219 | 2019-04-29 10:27:18 -0700 | [diff] [blame] | 252 | long now = System.currentTimeMillis(); |
| 253 | long start = periodStartTime == 0 ? (now + initialDelay) : periodStartTime; |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 254 | boolean isFlexApplicable = flexDuration != intervalDuration; |
| 255 | if (isFlexApplicable) { |
Rahul Ravikumar | 25dc219 | 2019-04-29 10:27:18 -0700 | [diff] [blame] | 256 | // To correctly emulate flex, we need to set it |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 257 | // to now, so the PeriodicWorkRequest has an initial delay of |
| 258 | // initialDelay + (interval - flex). |
Rahul Ravikumar | 38beac9b | 2019-02-13 11:16:38 -0800 | [diff] [blame] | 259 | |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 260 | // The subsequent runs will only add the interval duration and no flex. |
| 261 | // This gives us the following behavior: |
| 262 | // 1 => now + (interval - flex) + initialDelay = firstRunTime |
| 263 | // 2 => firstRunTime + 2 * interval - flex |
| 264 | // 3 => firstRunTime + 3 * interval - flex |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 265 | long offset = periodStartTime == 0 ? (-1 * flexDuration) : 0; |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 266 | return start + intervalDuration + offset; |
Rahul Ravikumar | 67a9b19 | 2019-01-28 15:00:20 -0800 | [diff] [blame] | 267 | } else { |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 268 | // Don't use flexDuration for determining next run time for PeriodicWork |
| 269 | // This is because intervalDuration could equal flexDuration. |
Rahul Ravikumar | 25dc219 | 2019-04-29 10:27:18 -0700 | [diff] [blame] | 270 | |
| 271 | // The first run of a periodic work request is immediate in JobScheduler, and we |
| 272 | // need to emulate this behavior. |
| 273 | long offset = periodStartTime == 0 ? 0 : intervalDuration; |
| 274 | return start + offset; |
Rahul Ravikumar | 67a9b19 | 2019-01-28 15:00:20 -0800 | [diff] [blame] | 275 | } |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 276 | } else { |
Rahul Ravikumar | 4d5c5ce | 2019-04-25 16:09:07 -0700 | [diff] [blame] | 277 | // We are checking for (periodStartTime == 0) to support our testing use case. |
| 278 | // For newly created WorkSpecs periodStartTime will always be 0. |
| 279 | long start = (periodStartTime == 0) ? System.currentTimeMillis() : periodStartTime; |
| 280 | return start + initialDelay; |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 281 | } |
Xyan Bhatnagar | 367c649 | 2017-10-06 11:20:02 -0700 | [diff] [blame] | 282 | } |
Jan Clarin | 91bccff | 2017-10-20 17:51:10 -0700 | [diff] [blame] | 283 | |
Rahul Ravikumar | e92262f | 2018-02-07 18:48:15 -0800 | [diff] [blame] | 284 | /** |
| 285 | * @return <code>true</code> if the {@link WorkSpec} has constraints. |
| 286 | */ |
| 287 | public boolean hasConstraints() { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 288 | return !Constraints.NONE.equals(constraints); |
Rahul Ravikumar | e92262f | 2018-02-07 18:48:15 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Jan Clarin | 91bccff | 2017-10-20 17:51:10 -0700 | [diff] [blame] | 291 | @Override |
| 292 | public boolean equals(Object o) { |
Sumir Kataria | 86894df | 2018-03-06 15:11:16 -0800 | [diff] [blame] | 293 | if (this == o) return true; |
| 294 | if (o == null || getClass() != o.getClass()) return false; |
| 295 | |
| 296 | WorkSpec workSpec = (WorkSpec) o; |
| 297 | |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 298 | if (initialDelay != workSpec.initialDelay) return false; |
| 299 | if (intervalDuration != workSpec.intervalDuration) return false; |
| 300 | if (flexDuration != workSpec.flexDuration) return false; |
| 301 | if (runAttemptCount != workSpec.runAttemptCount) return false; |
| 302 | if (backoffDelayDuration != workSpec.backoffDelayDuration) return false; |
| 303 | if (periodStartTime != workSpec.periodStartTime) return false; |
| 304 | if (minimumRetentionDuration != workSpec.minimumRetentionDuration) return false; |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 305 | if (scheduleRequestedAt != workSpec.scheduleRequestedAt) return false; |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 306 | if (!id.equals(workSpec.id)) return false; |
| 307 | if (state != workSpec.state) return false; |
| 308 | if (!workerClassName.equals(workSpec.workerClassName)) return false; |
| 309 | if (inputMergerClassName != null ? !inputMergerClassName.equals( |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 310 | workSpec.inputMergerClassName) |
| 311 | : workSpec.inputMergerClassName != null) { |
Jan Clarin | 91bccff | 2017-10-20 17:51:10 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 314 | if (!input.equals(workSpec.input)) return false; |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 315 | if (!output.equals(workSpec.output)) return false; |
| 316 | if (!constraints.equals(workSpec.constraints)) return false; |
| 317 | return backoffPolicy == workSpec.backoffPolicy; |
Jan Clarin | 91bccff | 2017-10-20 17:51:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public int hashCode() { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 322 | int result = id.hashCode(); |
| 323 | result = 31 * result + state.hashCode(); |
| 324 | result = 31 * result + workerClassName.hashCode(); |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 325 | result = 31 * result + (inputMergerClassName != null ? inputMergerClassName.hashCode() : 0); |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 326 | result = 31 * result + input.hashCode(); |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 327 | result = 31 * result + output.hashCode(); |
| 328 | result = 31 * result + (int) (initialDelay ^ (initialDelay >>> 32)); |
| 329 | result = 31 * result + (int) (intervalDuration ^ (intervalDuration >>> 32)); |
| 330 | result = 31 * result + (int) (flexDuration ^ (flexDuration >>> 32)); |
| 331 | result = 31 * result + constraints.hashCode(); |
| 332 | result = 31 * result + runAttemptCount; |
| 333 | result = 31 * result + backoffPolicy.hashCode(); |
| 334 | result = 31 * result + (int) (backoffDelayDuration ^ (backoffDelayDuration >>> 32)); |
| 335 | result = 31 * result + (int) (periodStartTime ^ (periodStartTime >>> 32)); |
Rahul Ravikumar | 9f91ee8 | 2018-03-20 17:33:38 -0700 | [diff] [blame] | 336 | result = 31 * result + (int) (minimumRetentionDuration ^ (minimumRetentionDuration >>> 32)); |
| 337 | result = 31 * result + (int) (scheduleRequestedAt ^ (scheduleRequestedAt >>> 32)); |
Jan Clarin | 91bccff | 2017-10-20 17:51:10 -0700 | [diff] [blame] | 338 | return result; |
| 339 | } |
Xyan Bhatnagar | bbe9f31 | 2017-11-16 10:31:14 -0800 | [diff] [blame] | 340 | |
Aurimas Liutikas | 2932b7d | 2019-06-13 17:56:26 -0700 | [diff] [blame] | 341 | @NonNull |
Xyan Bhatnagar | bbe9f31 | 2017-11-16 10:31:14 -0800 | [diff] [blame] | 342 | @Override |
| 343 | public String toString() { |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 344 | return "{WorkSpec: " + id + "}"; |
Xyan Bhatnagar | bbe9f31 | 2017-11-16 10:31:14 -0800 | [diff] [blame] | 345 | } |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 346 | |
| 347 | /** |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 348 | * A POJO containing the ID and state of a WorkSpec. |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 349 | */ |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 350 | public static class IdAndState { |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 351 | |
| 352 | @ColumnInfo(name = "id") |
| 353 | public String id; |
| 354 | |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 355 | @ColumnInfo(name = "state") |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 356 | public WorkInfo.State state; |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 357 | |
| 358 | @Override |
| 359 | public boolean equals(Object o) { |
| 360 | if (this == o) return true; |
| 361 | if (o == null || getClass() != o.getClass()) return false; |
| 362 | |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 363 | IdAndState that = (IdAndState) o; |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 364 | |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 365 | if (state != that.state) return false; |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 366 | return id.equals(that.id); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public int hashCode() { |
| 371 | int result = id.hashCode(); |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 372 | result = 31 * result + state.hashCode(); |
| 373 | return result; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
Sumir Kataria | c1d1518 | 2019-04-04 16:37:07 -0700 | [diff] [blame] | 378 | * A POJO containing the ID, state, output, tags, and run attempt count of a WorkSpec. |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 379 | */ |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 380 | public static class WorkInfoPojo { |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 381 | |
| 382 | @ColumnInfo(name = "id") |
| 383 | public String id; |
| 384 | |
| 385 | @ColumnInfo(name = "state") |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 386 | public WorkInfo.State state; |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 387 | |
| 388 | @ColumnInfo(name = "output") |
Sumir Kataria | 64e6bd8 | 2018-03-28 17:14:22 -0700 | [diff] [blame] | 389 | public Data output; |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 390 | |
Sumir Kataria | c1d1518 | 2019-04-04 16:37:07 -0700 | [diff] [blame] | 391 | @ColumnInfo(name = "run_attempt_count") |
| 392 | public int runAttemptCount; |
| 393 | |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 394 | @Relation( |
| 395 | parentColumn = "id", |
| 396 | entityColumn = "work_spec_id", |
| 397 | entity = WorkTag.class, |
| 398 | projection = {"tag"}) |
| 399 | public List<String> tags; |
| 400 | |
Rahul Ravikumar | f9c2cfb | 2019-07-19 13:27:39 -0700 | [diff] [blame^] | 401 | // This is actually a 1-1 relationship. However Room 2.1 models the type as a List. |
| 402 | // This will change in Room 2.2 |
| 403 | @Relation( |
| 404 | parentColumn = "id", |
| 405 | entityColumn = "work_spec_id", |
| 406 | entity = WorkProgress.class, |
| 407 | projection = {"progress"}) |
| 408 | public List<Data> progress; |
| 409 | |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 410 | /** |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 411 | * Converts this POJO to a {@link WorkInfo}. |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 412 | * |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 413 | * @return The {@link WorkInfo} represented by this POJO |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 414 | */ |
Rahul Ravikumar | f9c2cfb | 2019-07-19 13:27:39 -0700 | [diff] [blame^] | 415 | @NonNull |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 416 | public WorkInfo toWorkInfo() { |
Rahul Ravikumar | f9c2cfb | 2019-07-19 13:27:39 -0700 | [diff] [blame^] | 417 | Data progress = this.progress != null && !this.progress.isEmpty() |
| 418 | ? this.progress.get(0) |
| 419 | : Data.EMPTY; |
| 420 | |
| 421 | return new WorkInfo( |
| 422 | UUID.fromString(id), |
| 423 | state, |
| 424 | output, |
| 425 | tags, |
| 426 | progress, |
| 427 | runAttemptCount); |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 430 | @Override |
| 431 | public boolean equals(Object o) { |
| 432 | if (this == o) return true; |
| 433 | if (o == null || getClass() != o.getClass()) return false; |
| 434 | |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 435 | WorkInfoPojo that = (WorkInfoPojo) o; |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 436 | |
Sumir Kataria | c1d1518 | 2019-04-04 16:37:07 -0700 | [diff] [blame] | 437 | if (runAttemptCount != that.runAttemptCount) return false; |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 438 | if (id != null ? !id.equals(that.id) : that.id != null) return false; |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 439 | if (state != that.state) return false; |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 440 | if (output != null ? !output.equals(that.output) : that.output != null) return false; |
Rahul Ravikumar | f9c2cfb | 2019-07-19 13:27:39 -0700 | [diff] [blame^] | 441 | if (tags != null ? !tags.equals(that.tags) : that.tags != null) return false; |
| 442 | return progress != null ? progress.equals(that.progress) : that.progress == null; |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public int hashCode() { |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 447 | int result = id != null ? id.hashCode() : 0; |
| 448 | result = 31 * result + (state != null ? state.hashCode() : 0); |
Sumir Kataria | 9cf4f3d | 2018-02-06 09:57:51 -0800 | [diff] [blame] | 449 | result = 31 * result + (output != null ? output.hashCode() : 0); |
Sumir Kataria | c1d1518 | 2019-04-04 16:37:07 -0700 | [diff] [blame] | 450 | result = 31 * result + runAttemptCount; |
Sumir Kataria | cc5ae8c | 2018-03-12 12:57:46 -0700 | [diff] [blame] | 451 | result = 31 * result + (tags != null ? tags.hashCode() : 0); |
Rahul Ravikumar | f9c2cfb | 2019-07-19 13:27:39 -0700 | [diff] [blame^] | 452 | result = 31 * result + (progress != null ? progress.hashCode() : 0); |
Sumir Kataria | d2db35d | 2017-12-14 14:08:48 -0800 | [diff] [blame] | 453 | return result; |
| 454 | } |
| 455 | } |
Sumir Kataria | 4401f79 | 2018-03-20 12:52:38 -0700 | [diff] [blame] | 456 | |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 457 | public static final Function<List<WorkInfoPojo>, List<WorkInfo>> WORK_INFO_MAPPER = |
| 458 | new Function<List<WorkInfoPojo>, List<WorkInfo>>() { |
Sumir Kataria | 4401f79 | 2018-03-20 12:52:38 -0700 | [diff] [blame] | 459 | @Override |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 460 | public List<WorkInfo> apply(List<WorkInfoPojo> input) { |
Sumir Kataria | 4401f79 | 2018-03-20 12:52:38 -0700 | [diff] [blame] | 461 | if (input == null) { |
| 462 | return null; |
| 463 | } |
Rahul Ravikumar | 7f7ab61 | 2018-11-01 16:22:11 -0700 | [diff] [blame] | 464 | List<WorkInfo> output = new ArrayList<>(input.size()); |
| 465 | for (WorkInfoPojo in : input) { |
| 466 | output.add(in.toWorkInfo()); |
Sumir Kataria | 4401f79 | 2018-03-20 12:52:38 -0700 | [diff] [blame] | 467 | } |
| 468 | return output; |
| 469 | } |
| 470 | }; |
Sumir Kataria | 904ba12 | 2017-09-25 13:05:49 -0700 | [diff] [blame] | 471 | } |