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

Commit 9dea2c45 authored by Diego Vela's avatar Diego Vela
Browse files

Ensure listener is not triggered when reregistered

Ensure a listener is not reregistered a second time.
Ensure the listener does not get a repeat value.
Update the javadocs for addGlobalWindowViewsListener to clear up
confusion. Fix some typos.

Flag: android.view.flags.root_view_changed_listener
Bug: 401234449
Test: atest CtsViewTestCases
Change-Id: Icaaee0db756884f90409e07c0f088b04afba4ce3
parent e0150b49
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -329,13 +329,17 @@ public final class WindowManagerGlobal {

    /**
     * Adds a listener that will be notified whenever {@link #getWindowViews()} changes. The
     * current value is provided immediately. If it was registered previously then this is ano op.
     * current value is provided immediately using the provided {@link Executor}. If this
     * {@link Consumer} was registered previously, then this is a no op.
     */
    public void addWindowViewsListener(@NonNull Executor executor,
            @NonNull Consumer<List<View>> consumer) {
        synchronized (mLock) {
            if (mWindowViewsListenerGroup.isConsumerPresent(consumer)) {
                return;
            }
            mWindowViewsListenerGroup.addListener(executor, consumer);
            mWindowViewsListenerGroup.accept(getWindowViews());
            executor.execute(() -> consumer.accept(getWindowViews()));
        }
    }

+3 −2
Original line number Diff line number Diff line
@@ -42,8 +42,9 @@ public final class WindowInspector {
    }

    /**
     * Adds a listener that is notified whenever the list of global window views changes. If a
     * {@link Consumer} is already registered this method is a no op.
     * Adds a listener that is notified whenever the value of {@link #getGlobalWindowViews()}
     * changes. The current value is provided immediately using the provided {@link Executor}.
     * If this {@link Consumer} is already registered, then this method is a no op.
     * @see #getGlobalWindowViews()
     */
    @FlaggedApi(android.view.flags.Flags.FLAG_ROOT_VIEW_CHANGED_LISTENER)
+2 −2
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ public class ListenerGroup<T> {
     * is a no op.
     */
    public void addListener(@NonNull Executor executor, @NonNull Consumer<T> consumer) {
        if (isContained(consumer)) {
        if (isConsumerPresent(consumer)) {
            return;
        }
        mListeners.add(new ListenerWrapper<>(executor, consumer));
@@ -69,7 +69,7 @@ public class ListenerGroup<T> {
     * Returns {@code true} if the {@link Consumer} is present in the list, {@code false}
     * otherwise.
     */
    private boolean isContained(Consumer<T> consumer) {
    public boolean isConsumerPresent(Consumer<T> consumer) {
        return computeIndex(consumer) > -1;
    }