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

Commit c5b1c700 authored by wilsonshih's avatar wilsonshih Committed by Wei Sheng Shih
Browse files

Update receiving dispatcher to ImeOnBackDispatcher after IMM gain focus

The InputMethodManager can lost mCurRootView after lost focus, such as
screen off. So when screen turned on and IME reshow, the
ImeOnBackDispatcher may not able to set OnBackInvokedCallback to
mCurRootView immediately, there should wait for IMM re-gain focus.

Flag: EXEMPT bugfix
Bug: 355006526
Test: atest KeyboardVisibilityControlTest
Change-Id: I947f113d7c7b0e79b41d2ac848407a56db9f1fef
parent a3292406
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -973,8 +973,12 @@ public final class InputMethodManager {

        @GuardedBy("mH")
        private void setCurrentRootViewLocked(ViewRootImpl rootView) {
            final boolean wasEmpty = mCurRootView == null;
            mImeDispatcher.switchRootView(mCurRootView, rootView);
            mCurRootView = rootView;
            if (wasEmpty && mCurRootView != null) {
                mImeDispatcher.updateReceivingDispatcher(mCurRootView.getOnBackInvokedDispatcher());
            }
        }
    }

+15 −1
Original line number Diff line number Diff line
@@ -27,10 +27,12 @@ import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.util.Log;
import android.util.Pair;
import android.view.ViewRootImpl;

import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.function.Consumer;

@@ -58,7 +60,7 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc
    // The handler to run callbacks on. This should be on the same thread
    // the ViewRootImpl holding IME's WindowOnBackInvokedDispatcher is created on.
    private Handler mHandler;

    private final ArrayDeque<Pair<Integer, Bundle>> mQueuedReceive = new ArrayDeque<>();
    public ImeOnBackInvokedDispatcher(Handler handler) {
        mResultReceiver = new ResultReceiver(handler) {
            @Override
@@ -66,11 +68,22 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc
                WindowOnBackInvokedDispatcher dispatcher = getReceivingDispatcher();
                if (dispatcher != null) {
                    receive(resultCode, resultData, dispatcher);
                } else {
                    mQueuedReceive.add(new Pair<>(resultCode, resultData));
                }
            }
        };
    }

    /** Set receiving dispatcher to consume queued receiving events. */
    public void updateReceivingDispatcher(@NonNull WindowOnBackInvokedDispatcher dispatcher) {
        while (!mQueuedReceive.isEmpty()) {
            final Pair<Integer, Bundle> queuedMessage = mQueuedReceive.poll();
            receive(queuedMessage.first, queuedMessage.second, dispatcher);
        }
    }


    void setHandler(@NonNull Handler handler) {
        mHandler = handler;
    }
@@ -198,6 +211,7 @@ public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parc
            }
        }
        mImeCallbacks.clear();
        mQueuedReceive.clear();
    }

    @VisibleForTesting(visibility = PACKAGE)