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