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

Commit 9d370d65 authored by Johannes Gallmann's avatar Johannes Gallmann
Browse files

Prevent duplicate IME callback registrations

This CL prevents the mImeCallbacks list in ImeOnBackInvokedDispatcher
from ever containing two callbacks with the same id.

Bug: 407224281
Test: Manual, i.e. verified IME back navigation works as usual
Flag: EXEMPT bugfix
Change-Id: I93e9ea44089710a7a9a3336dcd199b9fa0c166b0
parent 0fd1e3e3
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -182,12 +182,26 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc
        } else {
            imeCallback = new ImeOnBackInvokedCallback(iCallback, callbackId, priority);
        }
        if (unregisterCallback(callbackId, receivingDispatcher)) {
            Log.w(TAG, "Received IME callback that's already registered. Unregistering and "
                    + "reregistering. (callbackId: " + callbackId
                    + " current callbacks: " + mImeCallbacks.size() + ")");
        }
        mImeCallbacks.add(imeCallback);
        receivingDispatcher.registerOnBackInvokedCallbackUnchecked(imeCallback, priority);
    }

    private void unregisterReceivedCallback(
            int callbackId, OnBackInvokedDispatcher receivingDispatcher) {
            int callbackId, @NonNull OnBackInvokedDispatcher receivingDispatcher) {
        if (!unregisterCallback(callbackId, receivingDispatcher)) {
            Log.e(TAG, "Ime callback not found. Ignoring unregisterReceivedCallback. "
                    + "callbackId: " + callbackId
                    + " remaining callbacks: " + mImeCallbacks.size());
        }
    }

    private boolean unregisterCallback(int callbackId,
            @NonNull OnBackInvokedDispatcher receivingDispatcher) {
        ImeOnBackInvokedCallback callback = null;
        for (ImeOnBackInvokedCallback imeCallback : mImeCallbacks) {
            if (imeCallback.getId() == callbackId) {
@@ -196,13 +210,11 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc
            }
        }
        if (callback == null) {
            Log.e(TAG, "Ime callback not found. Ignoring unregisterReceivedCallback. "
                    + "callbackId: " + callbackId
                    + " remaining callbacks: " + mImeCallbacks.size());
            return;
            return false;
        }
        receivingDispatcher.unregisterOnBackInvokedCallback(callback);
        mImeCallbacks.remove(callback);
        return true;
    }

    /**