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

Commit 75b935a1 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Support IME switcher icon visibility update

With this CL, the IME switcher icon becomes visible only when
necessary, even if InputMethodService renders the back button and the
IME switcher button in the gestural navigation mode.

Implementation idea:

InputMethodManagerService#shouldShowImeSwitcherLocked() is the source
of truth about whether the IME switcher visibility should be shown or
not, and it internally depends on the following conditions:

 A. com.android.internal.R.bool.show_ongoing_ime_switcher
 B. Whether the IME switcher is already shown or not.
 C. Whether the IME is perceptible or not.
 D. Whether one or more hardware keyboards are attached or not.
 E. Keyguard state.
 F. What IMEs and their subtypes are enabled.

Here are what those conditions would mean for this project.

 * A is considered to be a per-device constant value.
 * B, D, and F can happen at any time outside of the IME lifecycle
   events such as startInput().
 * C is no longer relevant if those buttons are rendered by the IME.
 * E is considered to be constant throughout each startInput() cycle.

This CL uses the following 3 IPCs to notify when the IME switcher
visibility is changing.

 1. IInputMethod#initializeInternal()
 2. IInputMethod#startInput()
 3. IInputMethod#onShouldShowImeSwitcherWhenImeIsShownChanged()

1 and 2 will be used to provide the "initial" value to avoid potential
flickers.  3 is still necessary to take care of async changes
triggered by B, D, and F.

Fix: 215551357
Test: Manually verified with for the following scenarios:
 * Enabling/disabling multiple IMEs
 * Attaching/detaching a hardware keyboard
 * Showing/hinding the IME switcher
 * Showing an IME on the lock screen
Change-Id: I5de9ac0dc8670842edf66306bb4c281c77cea376
parent 3578c8f2
Loading
Loading
Loading
Loading
+28 −8
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ class IInputMethodWrapper extends IInputMethod.Stub
    private static final int DO_SET_INPUT_CONTEXT = 20;
    private static final int DO_UNSET_INPUT_CONTEXT = 30;
    private static final int DO_START_INPUT = 32;
    private static final int DO_ON_SHOULD_SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN_CHANGED = 35;
    private static final int DO_CREATE_SESSION = 40;
    private static final int DO_SET_SESSION_ENABLED = 45;
    private static final int DO_SHOW_SOFT_INPUT = 60;
@@ -175,7 +176,7 @@ class IInputMethodWrapper extends IInputMethod.Stub
                try {
                    inputMethod.initializeInternal((IBinder) args.arg1,
                            (IInputMethodPrivilegedOperations) args.arg2, msg.arg1,
                            (boolean) args.arg3);
                            (boolean) args.arg3, msg.arg2 != 0);
                } finally {
                    args.recycle();
                }
@@ -195,14 +196,22 @@ class IInputMethodWrapper extends IInputMethod.Stub
                final EditorInfo info = (EditorInfo) args.arg3;
                final CancellationGroup cancellationGroup = (CancellationGroup) args.arg4;
                final boolean restarting = args.argi5 == 1;
                final boolean shouldShowImeSwitcherWhenImeIsShown = args.argi6 != 0;
                final InputConnection ic = inputContext != null
                        ? new RemoteInputConnection(mTarget, inputContext, cancellationGroup)
                        : null;
                info.makeCompatible(mTargetSdkVersion);
                inputMethod.dispatchStartInputWithToken(ic, info, restarting, startInputToken);
                inputMethod.dispatchStartInputWithToken(ic, info, restarting, startInputToken,
                        shouldShowImeSwitcherWhenImeIsShown);
                args.recycle();
                return;
            }
            case DO_ON_SHOULD_SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN_CHANGED: {
                final boolean shouldShowImeSwitcherWhenImeIsShown = msg.arg1 != 0;
                inputMethod.onShouldShowImeSwitcherWhenImeIsShownChanged(
                        shouldShowImeSwitcherWhenImeIsShown);
                return;
            }
            case DO_CREATE_SESSION: {
                SomeArgs args = (SomeArgs)msg.obj;
                inputMethod.createSession(new InputMethodSessionCallbackWrapper(
@@ -291,10 +300,11 @@ class IInputMethodWrapper extends IInputMethod.Stub
    @BinderThread
    @Override
    public void initializeInternal(IBinder token, IInputMethodPrivilegedOperations privOps,
            int configChanges, boolean stylusHwSupported) {
        mCaller.executeOrSendMessage(
                mCaller.obtainMessageIOOO(
                        DO_INITIALIZE_INTERNAL, configChanges, token, privOps, stylusHwSupported));
            int configChanges, boolean stylusHwSupported,
            boolean shouldShowImeSwitcherWhenImeIsShown) {
        mCaller.executeOrSendMessage(mCaller.obtainMessageIIOOO(DO_INITIALIZE_INTERNAL,
                configChanges, shouldShowImeSwitcherWhenImeIsShown ? 1 : 0, token, privOps,
                stylusHwSupported));
    }

    @BinderThread
@@ -334,13 +344,23 @@ class IInputMethodWrapper extends IInputMethod.Stub
    @BinderThread
    @Override
    public void startInput(IBinder startInputToken, IInputContext inputContext,
            EditorInfo attribute, boolean restarting) {
            EditorInfo attribute, boolean restarting, boolean shouldShowImeSwitcherWhenImeIsShown) {
        if (mCancellationGroup == null) {
            Log.e(TAG, "startInput must be called after bindInput.");
            mCancellationGroup = new CancellationGroup();
        }
        mCaller.executeOrSendMessage(mCaller.obtainMessageOOOOII(DO_START_INPUT, startInputToken,
                inputContext, attribute, mCancellationGroup, restarting ? 1 : 0, 0 /* unused */));
                inputContext, attribute, mCancellationGroup, restarting ? 1 : 0,
                shouldShowImeSwitcherWhenImeIsShown ? 1 : 0));
    }

    @BinderThread
    @Override
    public void onShouldShowImeSwitcherWhenImeIsShownChanged(
            boolean shouldShowImeSwitcherWhenImeIsShown) {
        mCaller.executeOrSendMessage(mCaller.obtainMessageI(
                DO_ON_SHOULD_SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN_CHANGED,
                shouldShowImeSwitcherWhenImeIsShown ? 1 : 0));
    }

    @BinderThread
+18 −3
Original line number Diff line number Diff line
@@ -658,7 +658,7 @@ public class InputMethodService extends AbstractInputMethodService {
        @Override
        public final void initializeInternal(@NonNull IBinder token,
                IInputMethodPrivilegedOperations privilegedOperations, int configChanges,
                boolean stylusHwSupported) {
                boolean stylusHwSupported, boolean shouldShowImeSwitcherWhenImeIsShown) {
            if (mDestroyed) {
                Log.i(TAG, "The InputMethodService has already onDestroyed()."
                    + "Ignore the initialization.");
@@ -671,6 +671,8 @@ public class InputMethodService extends AbstractInputMethodService {
            if (stylusHwSupported) {
                mInkWindow = new InkWindow(mWindow.getContext());
            }
            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
            attachToken(token);
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }
@@ -780,9 +782,10 @@ public class InputMethodService extends AbstractInputMethodService {
        @Override
        public final void dispatchStartInputWithToken(@Nullable InputConnection inputConnection,
                @NonNull EditorInfo editorInfo, boolean restarting,
                @NonNull IBinder startInputToken) {
                @NonNull IBinder startInputToken, boolean shouldShowImeSwitcherWhenImeIsShown) {
            mPrivOps.reportStartInputAsync(startInputToken);

            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
            if (restarting) {
                restartInput(inputConnection, editorInfo);
            } else {
@@ -790,6 +793,18 @@ public class InputMethodService extends AbstractInputMethodService {
            }
        }

        /**
         * {@inheritDoc}
         * @hide
         */
        @MainThread
        @Override
        public void onShouldShowImeSwitcherWhenImeIsShownChanged(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
        }

        /**
         * {@inheritDoc}
         * @hide
+39 −1
Original line number Diff line number Diff line
@@ -74,6 +74,10 @@ final class NavigationBarController {
        default void onDestroy() {
        }

        default void setShouldShowImeSwitcherWhenImeIsShown(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
        }

        default void onSystemBarAppearanceChanged(@Appearance int appearance) {
        }

@@ -109,6 +113,10 @@ final class NavigationBarController {
        mImpl.onDestroy();
    }

    void setShouldShowImeSwitcherWhenImeIsShown(boolean shouldShowImeSwitcherWhenImeIsShown) {
        mImpl.setShouldShowImeSwitcherWhenImeIsShown(shouldShowImeSwitcherWhenImeIsShown);
    }

    void onSystemBarAppearanceChanged(@Appearance int appearance) {
        mImpl.onSystemBarAppearanceChanged(appearance);
    }
@@ -139,6 +147,8 @@ final class NavigationBarController {
        @Nullable
        private BroadcastReceiver mSystemOverlayChangedReceiver;

        private boolean mShouldShowImeSwitcherWhenImeIsShown;

        @Appearance
        private int mAppearance;

@@ -205,7 +215,9 @@ final class NavigationBarController {
                    // TODO(b/213337792): Support InputMethodService#setBackDisposition().
                    // TODO(b/213337792): Set NAVIGATION_HINT_IME_SHOWN only when necessary.
                    final int hints = StatusBarManager.NAVIGATION_HINT_BACK_ALT
                            | StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
                            | (mShouldShowImeSwitcherWhenImeIsShown
                                    ? StatusBarManager.NAVIGATION_HINT_IME_SHOWN
                                    : 0);
                    navigationBarView.setNavigationIconHints(hints);
                }
            } else {
@@ -422,6 +434,31 @@ final class NavigationBarController {
            }
        }

        @Override
        public void setShouldShowImeSwitcherWhenImeIsShown(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
            if (mDestroyed) {
                return;
            }
            if (mShouldShowImeSwitcherWhenImeIsShown == shouldShowImeSwitcherWhenImeIsShown) {
                return;
            }
            mShouldShowImeSwitcherWhenImeIsShown = shouldShowImeSwitcherWhenImeIsShown;

            if (mNavigationBarFrame == null) {
                return;
            }
            final NavigationBarView navigationBarView =
                    mNavigationBarFrame.findViewByPredicate(NavigationBarView.class::isInstance);
            if (navigationBarView == null) {
                return;
            }
            final int hints = StatusBarManager.NAVIGATION_HINT_BACK_ALT
                    | (shouldShowImeSwitcherWhenImeIsShown
                    ? StatusBarManager.NAVIGATION_HINT_IME_SHOWN : 0);
            navigationBarView.setNavigationIconHints(hints);
        }

        @Override
        public void onSystemBarAppearanceChanged(@Appearance int appearance) {
            if (mDestroyed) {
@@ -471,6 +508,7 @@ final class NavigationBarController {
        public String toDebugString() {
            return "{mRenderGesturalNavButtons=" + mRenderGesturalNavButtons
                    + " mNavigationBarFrame=" + mNavigationBarFrame
                    + " mShouldShowImeSwitcherWhenImeIsShown" + mShouldShowImeSwitcherWhenImeIsShown
                    + " mAppearance=0x" + Integer.toHexString(mAppearance)
                    + " mDarkIntensity=" + mDarkIntensity
                    + "}";
+18 −2
Original line number Diff line number Diff line
@@ -105,12 +105,14 @@ public interface InputMethod {
     *                             current IME.
     * @param configChanges {@link InputMethodInfo#getConfigChanges()} declared by IME.
     * @param stylusHwSupported {@link InputMethodInfo#supportsStylusHandwriting()} declared by IME.
     * @param shouldShowImeSwitcherWhenImeIsShown {@code true} If the IME switcher is expected to be
     *                                            shown while the IME is shown.
     * @hide
     */
    @MainThread
    default void initializeInternal(IBinder token,
            IInputMethodPrivilegedOperations privilegedOperations, int configChanges,
            boolean stylusHwSupported) {
            boolean stylusHwSupported, boolean shouldShowImeSwitcherWhenImeIsShown) {
        attachToken(token);
    }

@@ -229,6 +231,8 @@ public interface InputMethod {
     *                        the next {@link #startInput(InputConnection, EditorInfo, IBinder)} as
     *                        long as your implementation of {@link InputMethod} relies on such
     *                        IPCs
     * @param shouldShowImeSwitcherWhenImeIsShown {@code true} If the IME switcher is expected to be
     *                                            shown while the IME is shown.
     * @see #startInput(InputConnection, EditorInfo)
     * @see #restartInput(InputConnection, EditorInfo)
     * @see EditorInfo
@@ -237,7 +241,7 @@ public interface InputMethod {
    @MainThread
    default void dispatchStartInputWithToken(@Nullable InputConnection inputConnection,
            @NonNull EditorInfo editorInfo, boolean restarting,
            @NonNull IBinder startInputToken) {
            @NonNull IBinder startInputToken, boolean shouldShowImeSwitcherWhenImeIsShown) {
        if (restarting) {
            restartInput(inputConnection, editorInfo);
        } else {
@@ -245,6 +249,18 @@ public interface InputMethod {
        }
    }

    /**
     * Notifies that whether the IME should show the IME switcher or not is being changed.
     *
     * @param shouldShowImeSwitcherWhenImeIsShown {@code true} If the IME switcher is expected to be
     *                                            shown while the IME is shown.
     * @hide
     */
    @MainThread
    default void onShouldShowImeSwitcherWhenImeIsShownChanged(
            boolean shouldShowImeSwitcherWhenImeIsShown) {
    }

    /**
     * Create a new {@link InputMethodSession} that can be handed to client
     * applications for interacting with the input method.  You can later
+6 −2
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ import com.android.internal.view.InlineSuggestionsRequestInfo;
 */
oneway interface IInputMethod {
    void initializeInternal(IBinder token, IInputMethodPrivilegedOperations privOps,
             int configChanges, boolean stylusHwSupported);
             int configChanges, boolean stylusHwSupported,
             boolean shouldShowImeSwitcherWhenImeIsShown);

    void onCreateInlineSuggestionsRequest(in InlineSuggestionsRequestInfo requestInfo,
            in IInlineSuggestionsRequestCallback cb);
@@ -47,7 +48,10 @@ oneway interface IInputMethod {
    void unbindInput();

    void startInput(in IBinder startInputToken, in IInputContext inputContext,
            in EditorInfo attribute, boolean restarting);
            in EditorInfo attribute, boolean restarting,
             boolean shouldShowImeSwitcherWhenImeIsShown);

    void onShouldShowImeSwitcherWhenImeIsShownChanged(boolean shouldShowImeSwitcherWhenImeIsShown);

    void createSession(in InputChannel channel, IInputSessionCallback callback);

Loading