public final class

ContextAwareHelper

extends java.lang.Object

 java.lang.Object

↳androidx.activity.contextaware.ContextAwareHelper

Gradle dependencies

compile group: 'androidx.activity', name: 'activity', version: '1.6.0-alpha04'

  • groupId: androidx.activity
  • artifactId: activity
  • version: 1.6.0-alpha04

Artifact androidx.activity:activity:1.6.0-alpha04 it located at Google repository (https://maven.google.com/)

Overview

Helper class for implementing ContextAware. Classes using this helper should call ContextAwareHelper.addOnContextAvailableListener(OnContextAvailableListener) and ContextAwareHelper.removeOnContextAvailableListener(OnContextAvailableListener) as the respective methods of ContextAware are called.

You must call ContextAwareHelper.dispatchOnContextAvailable(Context) once the is available to dispatch the callbacks to all registered listeners.

Listeners added after the context has been made available via ContextAwareHelper.dispatchOnContextAvailable(Context) will have the Context synchronously delivered to them up until ContextAwareHelper.clearAvailableContext() is called.

Summary

Constructors
publicContextAwareHelper()

Construct a new ContextAwareHelper.

Methods
public voidaddOnContextAvailableListener(OnContextAvailableListener listener)

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a .

public voidclearAvailableContext()

Clear any previously made available via ContextAwareHelper.dispatchOnContextAvailable(Context).

public voiddispatchOnContextAvailable(Context context)

Dispatch the callback of OnContextAvailableListener.onContextAvailable(Context) to all currently added listeners in the order they were added.

public ContextpeekAvailableContext()

Get the if it is currently available.

public voidremoveOnContextAvailableListener(OnContextAvailableListener listener)

Remove a OnContextAvailableListener previously added via ContextAwareHelper.addOnContextAvailableListener(OnContextAvailableListener).

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

Constructors

public ContextAwareHelper()

Construct a new ContextAwareHelper.

Methods

public Context peekAvailableContext()

Get the if it is currently available. If this returns null, you can use ContextAwareHelper.addOnContextAvailableListener(OnContextAvailableListener) to receive a callback for when it available.

Returns:

the Context if it is currently available.

public void addOnContextAvailableListener(OnContextAvailableListener listener)

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a .

Parameters:

listener: The listener that should be added.

See also: ContextAwareHelper.removeOnContextAvailableListener(OnContextAvailableListener)

public void removeOnContextAvailableListener(OnContextAvailableListener listener)

Remove a OnContextAvailableListener previously added via ContextAwareHelper.addOnContextAvailableListener(OnContextAvailableListener).

Parameters:

listener: The listener that should be removed.

See also: ContextAwareHelper.addOnContextAvailableListener(OnContextAvailableListener)

public void dispatchOnContextAvailable(Context context)

Dispatch the callback of OnContextAvailableListener.onContextAvailable(Context) to all currently added listeners in the order they were added.

Parameters:

context: The the ContextAware object is now associated with.

public void clearAvailableContext()

Clear any previously made available via ContextAwareHelper.dispatchOnContextAvailable(Context).

Source

/*
 * Copyright 2020 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.activity.contextaware;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Helper class for implementing {@link ContextAware}. Classes using this helper should
 * call {@link #addOnContextAvailableListener(OnContextAvailableListener)} and
 * {@link #removeOnContextAvailableListener(OnContextAvailableListener)} as the respective
 * methods of {@link ContextAware} are called.
 * <p>
 * You must call {@link #dispatchOnContextAvailable(Context)} once the
 * {@link Context} is available to dispatch the callbacks to all registered listeners.
 * <p>
 * Listeners added after the context has been made available via
 * {@link #dispatchOnContextAvailable(Context)} will have the Context synchronously
 * delivered to them up until {@link #clearAvailableContext()} is called.
 */
public final class ContextAwareHelper {

    private final Set<OnContextAvailableListener> mListeners = new CopyOnWriteArraySet<>();

    private volatile Context mContext;

    /**
     * Construct a new ContextAwareHelper.
     */
    public ContextAwareHelper() {
    }

    /**
     * Get the {@link Context} if it is currently available. If this returns
     * <code>null</code>, you can use
     * {@link #addOnContextAvailableListener(OnContextAvailableListener)} to receive
     * a callback for when it available.
     *
     * @return the Context if it is currently available.
     */
    @Nullable
    public Context peekAvailableContext() {
        return mContext;
    }

    /**
     * Add a new {@link OnContextAvailableListener} for receiving a callback for when
     * this class is associated with a {@link android.content.Context}.
     *
     * @param listener The listener that should be added.
     * @see #removeOnContextAvailableListener(OnContextAvailableListener)
     */
    public void addOnContextAvailableListener(@NonNull OnContextAvailableListener listener) {
        if (mContext != null) {
            listener.onContextAvailable(mContext);
        }
        mListeners.add(listener);
    }

    /**
     * Remove a {@link OnContextAvailableListener} previously added via
     * {@link #addOnContextAvailableListener(OnContextAvailableListener)}.
     *
     * @param listener The listener that should be removed.
     * @see #addOnContextAvailableListener(OnContextAvailableListener)
     */
    public void removeOnContextAvailableListener(@NonNull OnContextAvailableListener listener) {
        mListeners.remove(listener);
    }

    /**
     * Dispatch the callback of {@link OnContextAvailableListener#onContextAvailable} to
     * all currently added listeners in the order they were added.
     *
     * @param context The {@link Context} the {@link ContextAware} object is now associated with.
     */
    public void dispatchOnContextAvailable(@NonNull Context context) {
        mContext = context;
        for (OnContextAvailableListener listener : mListeners) {
            listener.onContextAvailable(context);
        }
    }

    /**
     * Clear any {@link Context} previously made available via
     * {@link #dispatchOnContextAvailable(Context)}.
     */
    public void clearAvailableContext() {
        mContext = null;
    }
}