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