40 lines
1.4 KiB
Java
40 lines
1.4 KiB
Java
package androidx.work;
|
|
|
|
import android.content.Context;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import java.util.List;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
public class DelegatingWorkerFactory extends WorkerFactory {
|
|
private static final String TAG = Logger.tagWithPrefix("DelegatingWkrFctry");
|
|
private final List<WorkerFactory> mFactories = new CopyOnWriteArrayList();
|
|
|
|
public final void addFactory(@NonNull WorkerFactory workerFactory) {
|
|
this.mFactories.add(workerFactory);
|
|
}
|
|
|
|
@Override // androidx.work.WorkerFactory
|
|
@Nullable
|
|
public final ListenableWorker createWorker(@NonNull Context context, @NonNull String str, @NonNull WorkerParameters workerParameters) {
|
|
for (WorkerFactory workerFactory : this.mFactories) {
|
|
try {
|
|
ListenableWorker createWorker = workerFactory.createWorker(context, str, workerParameters);
|
|
if (createWorker != null) {
|
|
return createWorker;
|
|
}
|
|
} catch (Throwable th) {
|
|
Logger.get().error(TAG, String.format("Unable to instantiate a ListenableWorker (%s)", str), th);
|
|
throw th;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@NonNull
|
|
@VisibleForTesting
|
|
public List<WorkerFactory> getFactories() {
|
|
return this.mFactories;
|
|
}
|
|
}
|