public class

DelegatingWorkerFactory

extends WorkerFactory

 java.lang.Object

androidx.work.WorkerFactory

↳androidx.work.DelegatingWorkerFactory

Overview

A WorkerFactory which delegates to other factories. Factories can register themselves as delegates, and they will be invoked in order until a delegated factory returns a non-null ListenableWorker instance.

Summary

Constructors
publicDelegatingWorkerFactory()

Creates a new instance of the DelegatingWorkerFactory.

Methods
public final voidaddFactory(WorkerFactory workerFactory)

Adds a WorkerFactory to the list of delegates.

public abstract ListenableWorkercreateWorker(Context appContext, java.lang.String workerClassName, WorkerParameters workerParameters)

Override this method to implement your custom worker-creation logic.

from WorkerFactorycreateWorkerWithDefaultFallback, getDefaultWorkerFactory
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public DelegatingWorkerFactory()

Creates a new instance of the DelegatingWorkerFactory.

Methods

public final void addFactory(WorkerFactory workerFactory)

Adds a WorkerFactory to the list of delegates.

Parameters:

workerFactory: The WorkerFactory instance.

public abstract ListenableWorker createWorker(Context appContext, java.lang.String workerClassName, WorkerParameters workerParameters)

Override this method to implement your custom worker-creation logic. Use Configuration.Builder.setWorkerFactory(WorkerFactory) to use your custom class.

Throwing an java.lang.Exception here will crash the application. If a WorkerFactory is unable to create an instance of the ListenableWorker, it should return null so it can delegate to the default WorkerFactory.

Returns a new instance of the specified workerClassName given the arguments. The returned worker must be a newly-created instance and must not have been previously returned or invoked by WorkManager. Otherwise, WorkManager will throw an java.lang.IllegalStateException.

Parameters:

appContext: The application context
workerClassName: The class name of the worker to create
workerParameters: Parameters for worker initialization

Returns:

A new ListenableWorker instance of type workerClassName, or null if the worker could not be created

Source

/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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;

/**
 * A {@link WorkerFactory} which delegates to other factories. Factories can register themselves
 * as delegates, and they will be invoked in order until a delegated factory returns a
 * non-null {@link ListenableWorker} instance.
 */
public class DelegatingWorkerFactory extends WorkerFactory {

    private static final String TAG = Logger.tagWithPrefix("DelegatingWkrFctry");

    private final List<WorkerFactory> mFactories;

    /**
     * Creates a new instance of the {@link DelegatingWorkerFactory}.
     */
    @SuppressWarnings("JdkObsolete") // TODO(b/141962522): Suppressed during upgrade to AGP 3.6.
    public DelegatingWorkerFactory() {
        // Use a CopyOnWriteArrayList here to allow modifying a list of factories during
        // iteration. This allows createWorker() to call addFactory().
        mFactories = new CopyOnWriteArrayList<>();
    }

    @VisibleForTesting
    @NonNull
    List<WorkerFactory> getFactories() {
        return mFactories;
    }

    /**
     * Adds a {@link WorkerFactory} to the list of delegates.
     *
     * @param workerFactory The {@link WorkerFactory} instance.
     */
    public final void addFactory(@NonNull WorkerFactory workerFactory) {
        mFactories.add(workerFactory);
    }

    @Override
    @Nullable
    public final ListenableWorker createWorker(@NonNull Context appContext,
            @NonNull String workerClassName, @NonNull WorkerParameters workerParameters) {

        for (WorkerFactory factory : mFactories) {
            try {
                ListenableWorker worker = factory.createWorker(
                        appContext, workerClassName, workerParameters);
                if (worker != null) {
                    return worker;
                }
            } catch (Throwable throwable) {
                String message =
                        "Unable to instantiate a ListenableWorker (" + workerClassName + ")";
                Logger.get().error(TAG, message, throwable);
                throw throwable;
            }
        }
        // If none of the delegates can instantiate a ListenableWorker return null
        // so we can fallback to the default factory which is based on reflection.
        return null;
    }
}