1 /* 2 * Copyright 2018 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 17 package androidx.work; 18 19 import android.net.Network; 20 import android.net.Uri; 21 22 import androidx.annotation.IntRange; 23 import androidx.annotation.RequiresApi; 24 import androidx.annotation.RestrictTo; 25 import androidx.work.impl.utils.taskexecutor.TaskExecutor; 26 27 import kotlin.coroutines.CoroutineContext; 28 29 import org.jspecify.annotations.NonNull; 30 import org.jspecify.annotations.Nullable; 31 32 import java.util.Collection; 33 import java.util.Collections; 34 import java.util.HashSet; 35 import java.util.List; 36 import java.util.Set; 37 import java.util.UUID; 38 import java.util.concurrent.Executor; 39 40 /** 41 * Setup parameters for a {@link ListenableWorker}. 42 */ 43 44 public final class WorkerParameters { 45 46 private @NonNull UUID mId; 47 private @NonNull Data mInputData; 48 private @NonNull Set<String> mTags; 49 private @NonNull RuntimeExtras mRuntimeExtras; 50 private int mRunAttemptCount; 51 private @NonNull Executor mBackgroundExecutor; 52 private @NonNull CoroutineContext mWorkerContext; 53 private @NonNull TaskExecutor mWorkTaskExecutor; 54 private @NonNull WorkerFactory mWorkerFactory; 55 private @NonNull ProgressUpdater mProgressUpdater; 56 private @NonNull ForegroundUpdater mForegroundUpdater; 57 private int mGeneration; 58 59 /** 60 */ 61 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) WorkerParameters( @onNull UUID id, @NonNull Data inputData, @NonNull Collection<String> tags, @NonNull RuntimeExtras runtimeExtras, @IntRange(from = 0) int runAttemptCount, @IntRange(from = 0) int generation, @NonNull Executor backgroundExecutor, @NonNull CoroutineContext workerContext, @NonNull TaskExecutor workTaskExecutor, @NonNull WorkerFactory workerFactory, @NonNull ProgressUpdater progressUpdater, @NonNull ForegroundUpdater foregroundUpdater)62 public WorkerParameters( 63 @NonNull UUID id, 64 @NonNull Data inputData, 65 @NonNull Collection<String> tags, 66 @NonNull RuntimeExtras runtimeExtras, 67 @IntRange(from = 0) int runAttemptCount, 68 @IntRange(from = 0) int generation, 69 @NonNull Executor backgroundExecutor, 70 @NonNull CoroutineContext workerContext, 71 @NonNull TaskExecutor workTaskExecutor, 72 @NonNull WorkerFactory workerFactory, 73 @NonNull ProgressUpdater progressUpdater, 74 @NonNull ForegroundUpdater foregroundUpdater) { 75 mId = id; 76 mInputData = inputData; 77 mTags = new HashSet<>(tags); 78 mRuntimeExtras = runtimeExtras; 79 mRunAttemptCount = runAttemptCount; 80 mGeneration = generation; 81 mBackgroundExecutor = backgroundExecutor; 82 mWorkerContext = workerContext; 83 mWorkTaskExecutor = workTaskExecutor; 84 mWorkerFactory = workerFactory; 85 mProgressUpdater = progressUpdater; 86 mForegroundUpdater = foregroundUpdater; 87 } 88 89 /** 90 * Gets the ID of the {@link WorkRequest} that created this {@link ListenableWorker}. 91 * 92 * @return The ID of the creating {@link WorkRequest} 93 */ getId()94 public @NonNull UUID getId() { 95 return mId; 96 } 97 98 /** 99 * Gets the input data. Note that in the case that there are multiple prerequisites for this 100 * {@link ListenableWorker}, the input data has been run through an {@link InputMerger}. 101 * 102 * @return The input data for this work 103 * @see OneTimeWorkRequest.Builder#setInputMerger(Class) 104 */ getInputData()105 public @NonNull Data getInputData() { 106 return mInputData; 107 } 108 109 /** 110 * Gets a {@link java.util.Set} of tags associated with this Worker's {@link WorkRequest}. 111 * 112 * @return The {@link java.util.Set} of tags associated with this Worker's {@link WorkRequest} 113 * @see WorkRequest.Builder#addTag(String) 114 */ getTags()115 public @NonNull Set<String> getTags() { 116 return mTags; 117 } 118 119 /** 120 * Gets the list of content {@link android.net.Uri}s that caused this Worker to execute. See 121 * @code JobParameters#getTriggeredContentUris()} for relevant {@code JobScheduler} code. 122 * 123 * @return The list of content {@link android.net.Uri}s that caused this Worker to execute 124 * @see Constraints.Builder#addContentUriTrigger(android.net.Uri, boolean) 125 */ 126 @RequiresApi(24) getTriggeredContentUris()127 public @NonNull List<Uri> getTriggeredContentUris() { 128 return mRuntimeExtras.triggeredContentUris; 129 } 130 131 /** 132 * Gets the list of content authorities that caused this Worker to execute. See 133 * {@code JobParameters#getTriggeredContentAuthorities()} for relevant {@code JobScheduler} 134 * code. 135 * 136 * @return The list of content authorities that caused this Worker to execute 137 */ 138 @RequiresApi(24) getTriggeredContentAuthorities()139 public @NonNull List<String> getTriggeredContentAuthorities() { 140 return mRuntimeExtras.triggeredContentAuthorities; 141 } 142 143 /** 144 * Gets the {@link android.net.Network} to use for this Worker. This method returns 145 * {@code null} if there is no network needed for this work request. 146 * 147 * @return The {@link android.net.Network} specified by the OS to be used with this Worker 148 */ 149 @RequiresApi(28) getNetwork()150 public @Nullable Network getNetwork() { 151 return mRuntimeExtras.network; 152 } 153 154 /** 155 * Gets the current run attempt count for this work. Note that for periodic work, this value 156 * gets reset between periods. 157 * 158 * @return The current run attempt count for this work. 159 */ 160 @IntRange(from = 0) getRunAttemptCount()161 public int getRunAttemptCount() { 162 return mRunAttemptCount; 163 } 164 165 /** 166 * Gets the generation of this Worker. 167 * <p> 168 * A work has multiple generations, if it was updated via 169 * {@link WorkManager#updateWork(WorkRequest)} or 170 * {@link WorkManager#enqueueUniquePeriodicWork(String, 171 * ExistingPeriodicWorkPolicy, PeriodicWorkRequest)} using 172 * {@link ExistingPeriodicWorkPolicy#UPDATE}. 173 * This worker can possibly be of an older generation rather than latest known, 174 * if an update has happened while this worker is running. 175 * 176 * @return a generation of this work. 177 */ 178 @IntRange(from = 0) getGeneration()179 public int getGeneration() { 180 return mGeneration; 181 } 182 183 /** 184 */ 185 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getBackgroundExecutor()186 public @NonNull Executor getBackgroundExecutor() { 187 return mBackgroundExecutor; 188 } 189 190 /** 191 */ 192 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getWorkerContext()193 public @NonNull CoroutineContext getWorkerContext() { 194 return mWorkerContext; 195 } 196 197 /** 198 */ 199 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getTaskExecutor()200 public @NonNull TaskExecutor getTaskExecutor() { 201 return mWorkTaskExecutor; 202 } 203 204 /** 205 */ 206 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getWorkerFactory()207 public @NonNull WorkerFactory getWorkerFactory() { 208 return mWorkerFactory; 209 } 210 211 /** 212 */ 213 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getProgressUpdater()214 public @NonNull ProgressUpdater getProgressUpdater() { 215 return mProgressUpdater; 216 } 217 218 /** 219 */ 220 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getForegroundUpdater()221 public @NonNull ForegroundUpdater getForegroundUpdater() { 222 return mForegroundUpdater; 223 } 224 225 /** 226 */ 227 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) getRuntimeExtras()228 public @NonNull RuntimeExtras getRuntimeExtras() { 229 return mRuntimeExtras; 230 } 231 232 /** 233 * Extra runtime information for Workers. 234 * 235 */ 236 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 237 public static class RuntimeExtras { 238 public @NonNull List<String> triggeredContentAuthorities = Collections.emptyList(); 239 public @NonNull List<Uri> triggeredContentUris = Collections.emptyList(); 240 241 @RequiresApi(28) 242 public @Nullable Network network; 243 } 244 } 245