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

Commit 2c8068c9 authored by Ming-Shin Lu's avatar Ming-Shin Lu
Browse files

Consolidate InputMethodManager#toggleSoftInput reliability

As previously InputMethodManager#toggleSoftInput is designed to tell
InputMethodService directly through IInputMethodSession to toggle
soft-keyboard visibility, this could be happened some unexpected IME
visibility issues that when the app calling this method in the wrong
state like the app toggling IME visibility when the app is off-screen
but unexpectedly it ends up showing soft-keyboard when the IME is in
invisible state.

To minimize the app compatibility without changing the public API
surface and reducing unexpected IME visibilty been toggled behavior
especially happens when switching the apps, changed the internal IPC
protocols to call IMMS#showSoftInput or IMMS#hideSoftInput directly
according the previous IME consumer requested visibility state,
so that in IMMS side can validate to see if the token user is
still focused and ready to toggle the IME visibility to show or hide.

As the result, we deprecated toggleSoftInput and
toggleSoftInputFromWindow to state the reason as the above, and
recommand to use showSoftInput or hideSoftInputFromWindow instead,
so that framework side no longer has to call {InputMethodSessionWrapper,
InputMethodSessionImpl}#toggleSoftInput.

Bug: 182071625
Test: m checkapi doc-comment-check-docs
Test: atest KeyboardVisibilityControlTest#testToggleSoftInput
Change-Id: I390dc029e7bcc30c200926a9bfbbbd0268a1f714
parent 4255d362
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -19314,7 +19314,7 @@ package android.inputmethodservice {
    method public void appPrivateCommand(String, android.os.Bundle);
    method public void displayCompletions(android.view.inputmethod.CompletionInfo[]);
    method public void finishInput();
    method public void toggleSoftInput(int, int);
    method @Deprecated public void toggleSoftInput(int, int);
    method public void updateCursor(android.graphics.Rect);
    method public void updateCursorAnchorInfo(android.view.inputmethod.CursorAnchorInfo);
    method public void updateExtractedText(int, android.view.inputmethod.ExtractedText);
@@ -51999,8 +51999,8 @@ package android.view.inputmethod {
    method @Deprecated public void showStatusIcon(android.os.IBinder, String, @DrawableRes int);
    method @Deprecated public boolean switchToLastInputMethod(android.os.IBinder);
    method @Deprecated public boolean switchToNextInputMethod(android.os.IBinder, boolean);
    method public void toggleSoftInput(int, int);
    method public void toggleSoftInputFromWindow(android.os.IBinder, int, int);
    method @Deprecated public void toggleSoftInput(int, int);
    method @Deprecated public void toggleSoftInputFromWindow(android.os.IBinder, int, int);
    method @Deprecated public void updateCursor(android.view.View, int, int, int, int);
    method public void updateCursorAnchorInfo(android.view.View, android.view.inputmethod.CursorAnchorInfo);
    method public void updateExtractedText(android.view.View, int, android.view.inputmethod.ExtractedText);
@@ -52023,7 +52023,7 @@ package android.view.inputmethod {
    method public void dispatchTrackballEvent(int, android.view.MotionEvent, android.view.inputmethod.InputMethodSession.EventCallback);
    method public void displayCompletions(android.view.inputmethod.CompletionInfo[]);
    method public void finishInput();
    method public void toggleSoftInput(int, int);
    method @Deprecated public void toggleSoftInput(int, int);
    method public void updateCursor(android.graphics.Rect);
    method public void updateCursorAnchorInfo(android.view.inputmethod.CursorAnchorInfo);
    method public void updateExtractedText(int, android.view.inputmethod.ExtractedText);
+0 −11
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ class IInputMethodSessionWrapper extends IInputMethodSession.Stub
    private static final int DO_UPDATE_CURSOR = 95;
    private static final int DO_UPDATE_CURSOR_ANCHOR_INFO = 99;
    private static final int DO_APP_PRIVATE_COMMAND = 100;
    private static final int DO_TOGGLE_SOFT_INPUT = 105;
    private static final int DO_FINISH_SESSION = 110;
    private static final int DO_VIEW_CLICKED = 115;
    private static final int DO_NOTIFY_IME_HIDDEN = 120;
@@ -123,10 +122,6 @@ class IInputMethodSessionWrapper extends IInputMethodSession.Stub
                args.recycle();
                return;
            }
            case DO_TOGGLE_SOFT_INPUT: {
                mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2);
                return;
            }
            case DO_FINISH_SESSION: {
                doFinishSession();
                return;
@@ -217,12 +212,6 @@ class IInputMethodSessionWrapper extends IInputMethodSession.Stub
                mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data));
    }

    @Override
    public void toggleSoftInput(int showFlags, int hideFlags) {
        mCaller.executeOrSendMessage(
                mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags));
    }

    @Override
    public void finishSession() {
        mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION));
+7 −1
Original line number Diff line number Diff line
@@ -1042,8 +1042,14 @@ public class InputMethodService extends AbstractInputMethodService {
        }
        
        /**
         * Handles a request to toggle the IME visibility.
         *
         * @deprecated Starting in {@link Build.VERSION_CODES#S} the system no longer invokes this
         * method, instead it explicitly shows or hides the IME. An {@code InputMethodService}
         * wishing to toggle its own visibility should instead invoke {@link
         * InputMethodService#requestShowSelf} or {@link InputMethodService#requestHideSelf}
         */
        @Deprecated
        public void toggleSoftInput(int showFlags, int hideFlags) {
            InputMethodService.this.onToggleSoftInput(showFlags, hideFlags);
        }
+0 −19
Original line number Diff line number Diff line
@@ -256,18 +256,6 @@ final class MultiClientInputMethodClientCallbackAdaptor {
            }
        }

        @Override
        public void toggleSoftInput(int showFlags, int hideFlags) {
            synchronized (mSessionLock) {
                if (mCallbackImpl == null || mHandler == null) {
                    return;
                }
                mHandler.sendMessage(PooledLambda.obtainMessage(
                        CallbackImpl::toggleSoftInput, mCallbackImpl, showFlags,
                        hideFlags));
            }
        }

        @Override
        public void finishSession() {
            synchronized (mSessionLock) {
@@ -419,13 +407,6 @@ final class MultiClientInputMethodClientCallbackAdaptor {
            mOriginalCallback.onAppPrivateCommand(action, data);
        }

        void toggleSoftInput(int showFlags, int hideFlags) {
            if (mFinished) {
                return;
            }
            mOriginalCallback.onToggleSoftInput(showFlags, hideFlags);
        }

        void finishSession() {
            if (mFinished) {
                return;
+0 −10
Original line number Diff line number Diff line
@@ -165,16 +165,6 @@ public final class MultiClientInputMethodServiceDelegate {
                @SoftInputModeFlags int softInputMode,
                int targetWindowHandle);

        /**
         * Called when the associated IME client called {@link
         * android.view.inputmethod.InputMethodManager#toggleSoftInput(int, int)}.
         *
         * @param showFlags The flag passed by the client.
         * @param hideFlags The flag passed by the client.
         * @see android.inputmethodservice.InputMethodService#onToggleSoftInput(int, int)
         */
        void onToggleSoftInput(int showFlags, int hideFlags);

        /**
         * Called when the associated IME client called {@link
         * android.view.inputmethod.InputMethodManager#updateCursorAnchorInfo(View,
Loading