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

Commit 2978481c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor listener to centralize value logic." into main

parents e7e530ba 7f3f9971
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -14,10 +14,9 @@
 * limitations under the License.
 */

package android.view.translation;
package android.view;

import android.annotation.NonNull;
import android.view.ListenerWrapper;

import java.util.ArrayList;
import java.util.List;
@@ -32,26 +31,39 @@ import java.util.function.Consumer;
 */
public class ListenerGroup<T> {
    private final List<ListenerWrapper<T>> mListeners = new ArrayList<>();
    @NonNull
    private T mLastValue;

    /**
     * Constructs a {@link ListenerGroup} that will replay the last reported value whenever a new
     * listener is registered.
     * @param value the initial value
     */
    public ListenerGroup(@NonNull T value) {
        mLastValue = value;
    }

    /**
     * Relays the value to all the registered {@link java.util.function.Consumer}
     */
    public void accept(@NonNull T value) {
        Objects.requireNonNull(value);
        mLastValue = Objects.requireNonNull(value);
        for (int i = 0; i < mListeners.size(); i++) {
            mListeners.get(i).accept(value);
        }
    }

    /**
     * Adds a {@link Consumer} to the group. If the {@link Consumer} is already present then this
     * is a no op.
     * Adds a {@link Consumer} to the group and replays the last reported value. If the
     * {@link Consumer} is already present then this is a no op.
     */
    public void addListener(@NonNull Executor executor, @NonNull Consumer<T> consumer) {
        if (isConsumerPresent(consumer)) {
            return;
        }
        mListeners.add(new ListenerWrapper<>(executor, consumer));
        final ListenerWrapper<T> listenerWrapper = new ListenerWrapper<>(executor, consumer);
        mListeners.add(listenerWrapper);
        listenerWrapper.accept(mLastValue);
    }

    /**
+2 −3
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.view.inputmethod.InputMethodManager;
import android.view.translation.ListenerGroup;
import android.window.ITrustedPresentationListener;
import android.window.InputTransferToken;
import android.window.TrustedPresentationThresholds;
@@ -154,7 +153,8 @@ public final class WindowManagerGlobal {
     * @hide
     */
    @GuardedBy("mLock")
    private final ListenerGroup<List<View>> mWindowViewsListenerGroup = new ListenerGroup<>();
    private final ListenerGroup<List<View>> mWindowViewsListenerGroup =
            new ListenerGroup<>(new ArrayList<>());
    @UnsupportedAppUsage
    private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
    @UnsupportedAppUsage
@@ -339,7 +339,6 @@ public final class WindowManagerGlobal {
                return;
            }
            mWindowViewsListenerGroup.addListener(executor, consumer);
            executor.execute(() -> consumer.accept(getWindowViews()));
        }
    }