public final class

RemoteWorkManagerInfo

extends java.lang.Object

 java.lang.Object

↳androidx.work.multiprocess.RemoteWorkManagerInfo

Gradle dependencies

compile group: 'androidx.work', name: 'work-multiprocess', version: '2.8.0-alpha02'

  • groupId: androidx.work
  • artifactId: work-multiprocess
  • version: 2.8.0-alpha02

Artifact androidx.work:work-multiprocess:2.8.0-alpha02 it located at Google repository (https://maven.google.com/)

Overview

Can keep track of WorkManager configuration and schedulers without having to fully initialize WorkManager in a remote process.

Summary

Methods
public static voidclearInstance()

Clears the instance of RemoteWorkManagerInfo.

public ConfigurationgetConfiguration()

public ForegroundUpdatergetForegroundUpdater()

public static RemoteWorkManagerInfogetInstance(Context context)

Returns an instance of RemoteWorkManagerInfo.

public ProgressUpdatergetProgressUpdater()

public TaskExecutorgetTaskExecutor()

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

Methods

public static RemoteWorkManagerInfo getInstance(Context context)

Returns an instance of RemoteWorkManagerInfo.

Parameters:

context: The application .

Returns:

an instance of RemoteWorkManagerInfo which tracks WorkManager configuration without having to initialize WorkManager.

public static void clearInstance()

Clears the instance of RemoteWorkManagerInfo.

public Configuration getConfiguration()

Returns:

The Configuration instance which can be used without having to initialize WorkManager.

public TaskExecutor getTaskExecutor()

Returns:

The TaskExecutor instance that can be used without having to initialize WorkManager.

public ProgressUpdater getProgressUpdater()

Returns:

The ProgressUpdater instance that can be use without having to initialize WorkManager.

public ForegroundUpdater getForegroundUpdater()

Returns:

The ForegroundUpdater instance that can be use without having to initialize WorkManager.

Source

/*
 * Copyright 2021 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.multiprocess;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.work.Configuration;
import androidx.work.ForegroundUpdater;
import androidx.work.ProgressUpdater;
import androidx.work.WorkManager;
import androidx.work.impl.WorkManagerImpl;
import androidx.work.impl.utils.taskexecutor.TaskExecutor;
import androidx.work.impl.utils.taskexecutor.WorkManagerTaskExecutor;

/**
 * Can keep track of WorkManager configuration and schedulers without having to fully
 * initialize {@link androidx.work.WorkManager} in a remote process.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public final class RemoteWorkManagerInfo {

    private static final Object sLock = new Object();
    private static volatile RemoteWorkManagerInfo sInstance;

    private final Configuration mConfiguration;
    private final TaskExecutor mTaskExecutor;
    private final ProgressUpdater mProgressUpdater;
    private final ForegroundUpdater mForegroundUpdater;

    /**
     * Returns an instance of {@link RemoteWorkManagerInfo}.
     *
     * @param context The application {@link Context}.
     * @return an instance of {@link RemoteWorkManagerInfo} which tracks {@link WorkManager}
     * configuration without having to initialize {@link WorkManager}.
     */
    @NonNull
    public static RemoteWorkManagerInfo getInstance(@NonNull Context context) {
        if (sInstance == null) {
            synchronized (sLock) {
                if (sInstance == null) {
                    sInstance = new RemoteWorkManagerInfo(context);
                }
            }
        }
        return sInstance;
    }

    /**
     * Clears the instance of {@link RemoteWorkManagerInfo}.
     */
    @VisibleForTesting
    public static void clearInstance() {
        synchronized (sLock) {
            sInstance = null;
        }
    }

    @SuppressWarnings("deprecation")
    private RemoteWorkManagerInfo(@NonNull Context context) {
        WorkManagerImpl instance = WorkManagerImpl.getInstance();
        if (instance != null) {
            // WorkManager has been initialized in this process.
            mConfiguration = instance.getConfiguration();
            mTaskExecutor = instance.getWorkTaskExecutor();
        } else {
            Context appContext = context.getApplicationContext();
            if (appContext instanceof Configuration.Provider) {
                Configuration.Provider provider = (Configuration.Provider) appContext;
                mConfiguration = provider.getWorkManagerConfiguration();
            } else {
                // Assume that the configuration to be used is the default configuration.
                mConfiguration = new Configuration.Builder()
                        .setDefaultProcessName(appContext.getPackageName())
                        .build();
            }
            mTaskExecutor = new WorkManagerTaskExecutor(mConfiguration.getTaskExecutor());
        }
        mProgressUpdater = new RemoteProgressUpdater();
        mForegroundUpdater = new RemoteForegroundUpdater();
    }

    /**
     * @return The {@link Configuration} instance which can be used without having to initialize
     * {@link WorkManager}.
     */
    @NonNull
    public Configuration getConfiguration() {
        return mConfiguration;
    }

    /**
     * @return The {@link TaskExecutor} instance that can be used without having to initialize
     * {@link WorkManager}.
     */
    @NonNull
    public TaskExecutor getTaskExecutor() {
        return mTaskExecutor;
    }

    /**
     * @return The {@link androidx.work.ProgressUpdater} instance that can be use without
     * having to initialize {@link WorkManager}.
     */
    @NonNull
    public ProgressUpdater getProgressUpdater() {
        return mProgressUpdater;
    }

    /**
     * @return The {@link androidx.work.ForegroundUpdater} instance that can be use without
     * having to initialize {@link WorkManager}.
     */
    @NonNull
    public ForegroundUpdater getForegroundUpdater() {
        return mForegroundUpdater;
    }
}