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