1 /* 2 * Copyright 2020 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.hilt.work; 18 19 import java.lang.annotation.ElementType; 20 import java.lang.annotation.Retention; 21 import java.lang.annotation.RetentionPolicy; 22 import java.lang.annotation.Target; 23 24 import dagger.hilt.GeneratesRootInput; 25 26 /** 27 * A type annotation that identifies a {@link androidx.work.ListenableWorker}'s constructor for 28 * injection. 29 * <p> 30 * The {@code Worker} will be available for creation by the 31 * {@link androidx.hilt.work.HiltWorkerFactory} that should be set in {@code WorkManager}'s 32 * configuration via 33 * {@link androidx.work.Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory)}. 34 * The {@code HiltWorker} containing a constructor annotated with 35 * <a href="https://dagger.dev/api/latest/dagger/assisted/AssistedInject">AssistedInject</a> will 36 * have its dependencies defined in the constructor parameters injected by Dagger's Hilt. 37 * <p> 38 * Example: 39 * <pre> 40 * @HiltWorker 41 * public class UploadWorker extends Worker { 42 * @AssistedInject 43 * public UploadWorker(@Assisted Context context, @Assisted WorkerParameters params, 44 * HttpClient httpClient) { 45 * // ... 46 * } 47 * } 48 * </pre> 49 * <pre> 50 * @HiltAndroidApp 51 * public class MyApplication extends Application implements Configuration.Provider { 52 * @Inject HiltWorkerFactory workerFactory; 53 * 54 * @Override 55 * public Configuration getWorkManagerConfiguration() { 56 * return Configuration.Builder() 57 * .setWorkerFactory(workerFactory) 58 * .build(); 59 * } 60 * } 61 * </pre> 62 * <p> 63 * Only one constructor in the {@code Worker} must be annotated with 64 * <a href="https://dagger.dev/api/latest/dagger/assisted/AssistedInject">AssistedInject</a>. 65 * The constructor must define parameters for a 66 * <a href="https://dagger.dev/api/latest/dagger/assisted/Assisted">Assisted</a>-annotated 67 * {@code Context} and a 68 * <a href="https://dagger.dev/api/latest/dagger/assisted/Assisted">Assisted</a>-annotated 69 * {@code WorkerParameters} along with any other dependencies. Both the {@code Context} and 70 * {@code WorkerParameters} must not be a type param of {@link javax.inject.Provider} nor 71 * <a href="https://dagger.dev/api/latest/dagger/Lazy">Lazy</a> and must not be qualified. 72 * <p> 73 * Only dependencies available in the 74 * <a href="https://dagger.dev/api/latest/dagger/hilt/components/SingletonComponent">SingletonComponent</a> 75 * can be injected into the {@code Worker}. 76 */ 77 @Target(ElementType.TYPE) 78 @Retention(RetentionPolicy.CLASS) 79 @GeneratesRootInput 80 public @interface HiltWorker { 81 } 82