Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 413e7e93 authored by Diego Vela's avatar Diego Vela Committed by Android (Google) Code Review
Browse files

Merge "Delete DataProducer abstraction." into main

parents 5145363d c8cd48a6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import java.util.Optional;
import java.util.function.Consumer;

/**
 * Implementation of {@link androidx.window.util.DataProducer} that produces a
 * Implementation of {@link androidx.window.util.BaseDataProducer} that produces a
 * {@link String} that can be parsed to a {@link CommonFoldingFeature}.
 * {@link RawFoldingFeatureProducer} searches for the value in two places. The first check is in
 * settings where the {@link String} property is saved with the key
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import java.util.function.Consumer;
/**
 * A base class that works with {@link BaseDataProducer} to add/remove a consumer that should
 * only be used once when {@link BaseDataProducer#notifyDataChanged} is called.
 * @param <T> The type of data this producer returns through {@link DataProducer#getData}.
 * @param <T> The type of data this producer returns through {@link BaseDataProducer#getData}.
 */
public class AcceptOnceConsumer<T> implements Consumer<T> {
    private final Consumer<T> mCallback;
+12 −9
Original line number Diff line number Diff line
@@ -26,13 +26,12 @@ import java.util.Set;
import java.util.function.Consumer;

/**
 * Base class that provides the implementation for the callback mechanism of the
 * {@link DataProducer} API.  This class is thread safe for adding, removing, and notifying
 * consumers.
 * Base class that manages listeners when listening to a piece of data that changes.  This class is
 * thread safe for adding, removing, and notifying consumers.
 *
 * @param <T> The type of data this producer returns through {@link DataProducer#getData}.
 * @param <T> The type of data this producer returns through {@link BaseDataProducer#getData}.
 */
public abstract class BaseDataProducer<T> implements DataProducer<T>,
public abstract class BaseDataProducer<T> implements
        AcceptOnceConsumer.AcceptOnceProducerCallback<T> {

    private final Object mLock = new Object();
@@ -41,13 +40,18 @@ public abstract class BaseDataProducer<T> implements DataProducer<T>,
    @GuardedBy("mLock")
    private final Set<Consumer<T>> mCallbacksToRemove = new HashSet<>();

    /**
     * Emits the first available data at that point in time.
     * @param dataConsumer a {@link Consumer} that will receive one value.
     */
    public abstract void getData(@NonNull Consumer<T> dataConsumer);

    /**
     * Adds a callback to the set of callbacks listening for data. Data is delivered through
     * {@link BaseDataProducer#notifyDataChanged(Object)}. This method is thread safe. Callers
     * should ensure that callbacks are thread safe.
     * @param callback that will receive data from the producer.
     */
    @Override
    public final void addDataChangedCallback(@NonNull Consumer<T> callback) {
        synchronized (mLock) {
            mCallbacks.add(callback);
@@ -63,7 +67,6 @@ public abstract class BaseDataProducer<T> implements DataProducer<T>,
     * @param callback that was registered in
     * {@link BaseDataProducer#addDataChangedCallback(Consumer)}.
     */
    @Override
    public final void removeDataChangedCallback(@NonNull Consumer<T> callback) {
        synchronized (mLock) {
            mCallbacks.remove(callback);
@@ -92,8 +95,8 @@ public abstract class BaseDataProducer<T> implements DataProducer<T>,

    /**
     * Called to notify all registered consumers that the data provided
     * by {@link DataProducer#getData} has changed. Calls to this are thread save but callbacks need
     * to ensure thread safety.
     * by {@link BaseDataProducer#getData} has changed. Calls to this are thread save but callbacks
     * need to ensure thread safety.
     */
    protected void notifyDataChanged(T value) {
        synchronized (mLock) {
+0 −44
Original line number Diff line number Diff line
/*
 * Copyright (C) 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.window.util;

import android.annotation.NonNull;

import java.util.function.Consumer;

/**
 * Produces data through {@link DataProducer#getData} and provides a mechanism for receiving
 * a callback when the data managed by the produces has changed.
 *
 * @param <T> The type of data this producer returns through {@link DataProducer#getData}.
 */
public interface DataProducer<T> {
    /**
     * Emits the first available data at that point in time.
     * @param dataConsumer a {@link Consumer} that will receive one value.
     */
    void getData(@NonNull Consumer<T> dataConsumer);

    /**
     * Adds a callback to be notified when the data returned
     * from {@link DataProducer#getData} has changed.
     */
    void addDataChangedCallback(@NonNull Consumer<T> callback);

    /** Removes a callback previously added with {@link #addDataChangedCallback(Consumer)}. */
    void removeDataChangedCallback(@NonNull Consumer<T> callback);
}