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