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

Commit 5f9f1ca5 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Introduce InputMethodNavButtonFlags

This CL reworks my previous CL [1], which let
InputMethodManagerService report whether the IME switcher icon needs
to be shown or not to the IME process by using IInputMethod IPCs.

It turns out that we need to propagate one more boolean value in order
to address Bug 219820813.  It'd be much clearer if we use bit flags
rather than adding a new boolean parameter to each IPC method.  Thus
this CL rewrites my previous CL by using a bit flag defined in a newly
introduced InputMethodNavButtonFlags.

This is a purely mechanical refactroing.  There should be no behavior
change.

 [1]: I5de9ac0dc8670842edf66306bb4c281c77cea376
      75b935a1

Bug: 215551357
Bug: 219820813
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: I81cb062a08d484ec8ce5d7b2fea64ce19028f82e
parent 612b7a69
Loading
Loading
Loading
Loading
+16 −20
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.view.inputmethod.InputMethodSubtype;

import com.android.internal.inputmethod.CancellationGroup;
import com.android.internal.inputmethod.IInputMethodPrivilegedOperations;
import com.android.internal.inputmethod.InputMethodNavButtonFlags;
import com.android.internal.os.HandlerCaller;
import com.android.internal.os.SomeArgs;
import com.android.internal.view.IInlineSuggestionsRequestCallback;
@@ -70,7 +71,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_ON_NAV_BUTTON_FLAGS_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;
@@ -176,7 +177,7 @@ class IInputMethodWrapper extends IInputMethod.Stub
                try {
                    inputMethod.initializeInternal((IBinder) args.arg1,
                            (IInputMethodPrivilegedOperations) args.arg2, msg.arg1,
                            (boolean) args.arg3, msg.arg2 != 0);
                            (boolean) args.arg3, msg.arg2);
                } finally {
                    args.recycle();
                }
@@ -196,22 +197,20 @@ 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;
                @InputMethodNavButtonFlags
                final int navButtonFlags = args.argi6;
                final InputConnection ic = inputContext != null
                        ? new RemoteInputConnection(mTarget, inputContext, cancellationGroup)
                        : null;
                info.makeCompatible(mTargetSdkVersion);
                inputMethod.dispatchStartInputWithToken(ic, info, restarting, startInputToken,
                        shouldShowImeSwitcherWhenImeIsShown);
                        navButtonFlags);
                args.recycle();
                return;
            }
            case DO_ON_SHOULD_SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN_CHANGED: {
                final boolean shouldShowImeSwitcherWhenImeIsShown = msg.arg1 != 0;
                inputMethod.onShouldShowImeSwitcherWhenImeIsShownChanged(
                        shouldShowImeSwitcherWhenImeIsShown);
            case DO_ON_NAV_BUTTON_FLAGS_CHANGED:
                inputMethod.onNavButtonFlagsChanged(msg.arg1);
                return;
            }
            case DO_CREATE_SESSION: {
                SomeArgs args = (SomeArgs)msg.obj;
                inputMethod.createSession(new InputMethodSessionCallbackWrapper(
@@ -301,10 +300,9 @@ class IInputMethodWrapper extends IInputMethod.Stub
    @Override
    public void initializeInternal(IBinder token, IInputMethodPrivilegedOperations privOps,
            int configChanges, boolean stylusHwSupported,
            boolean shouldShowImeSwitcherWhenImeIsShown) {
            @InputMethodNavButtonFlags int navButtonFlags) {
        mCaller.executeOrSendMessage(mCaller.obtainMessageIIOOO(DO_INITIALIZE_INTERNAL,
                configChanges, shouldShowImeSwitcherWhenImeIsShown ? 1 : 0, token, privOps,
                stylusHwSupported));
                configChanges, navButtonFlags, token, privOps, stylusHwSupported));
    }

    @BinderThread
@@ -344,23 +342,21 @@ class IInputMethodWrapper extends IInputMethod.Stub
    @BinderThread
    @Override
    public void startInput(IBinder startInputToken, IInputContext inputContext,
            EditorInfo attribute, boolean restarting, boolean shouldShowImeSwitcherWhenImeIsShown) {
            EditorInfo attribute, boolean restarting,
            @InputMethodNavButtonFlags int navButtonFlags) {
        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,
                shouldShowImeSwitcherWhenImeIsShown ? 1 : 0));
                inputContext, attribute, mCancellationGroup, restarting ? 1 : 0, navButtonFlags));
    }

    @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));
    public void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
        mCaller.executeOrSendMessage(
                mCaller.obtainMessageI(DO_ON_NAV_BUTTON_FLAGS_CHANGED, navButtonFlags));
    }

    @BinderThread
+7 −10
Original line number Diff line number Diff line
@@ -140,6 +140,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.inputmethod.IInputContentUriToken;
import com.android.internal.inputmethod.IInputMethodPrivilegedOperations;
import com.android.internal.inputmethod.ImeTracing;
import com.android.internal.inputmethod.InputMethodNavButtonFlags;
import com.android.internal.inputmethod.InputMethodPrivilegedOperations;
import com.android.internal.inputmethod.InputMethodPrivilegedOperationsRegistry;
import com.android.internal.view.IInlineSuggestionsRequestCallback;
@@ -660,7 +661,7 @@ public class InputMethodService extends AbstractInputMethodService {
        @Override
        public final void initializeInternal(@NonNull IBinder token,
                IInputMethodPrivilegedOperations privilegedOperations, int configChanges,
                boolean stylusHwSupported, boolean shouldShowImeSwitcherWhenImeIsShown) {
                boolean stylusHwSupported, @InputMethodNavButtonFlags int navButtonFlags) {
            if (mDestroyed) {
                Log.i(TAG, "The InputMethodService has already onDestroyed()."
                    + "Ignore the initialization.");
@@ -673,8 +674,7 @@ public class InputMethodService extends AbstractInputMethodService {
            if (stylusHwSupported) {
                mInkWindow = new InkWindow(mWindow.getContext());
            }
            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
            mNavigationBarController.onNavButtonFlagsChanged(navButtonFlags);
            attachToken(token);
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }
@@ -784,10 +784,9 @@ public class InputMethodService extends AbstractInputMethodService {
        @Override
        public final void dispatchStartInputWithToken(@Nullable InputConnection inputConnection,
                @NonNull EditorInfo editorInfo, boolean restarting,
                @NonNull IBinder startInputToken, boolean shouldShowImeSwitcherWhenImeIsShown) {
                @NonNull IBinder startInputToken, @InputMethodNavButtonFlags int navButtonFlags) {
            mPrivOps.reportStartInputAsync(startInputToken);
            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
            mNavigationBarController.onNavButtonFlagsChanged(navButtonFlags);
            if (restarting) {
                restartInput(inputConnection, editorInfo);
            } else {
@@ -801,10 +800,8 @@ public class InputMethodService extends AbstractInputMethodService {
         */
        @MainThread
        @Override
        public void onShouldShowImeSwitcherWhenImeIsShownChanged(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
                    shouldShowImeSwitcherWhenImeIsShown);
        public void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
            mNavigationBarController.onNavButtonFlagsChanged(navButtonFlags);
        }

        /**
+10 −6
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;

import com.android.internal.inputmethod.InputMethodNavButtonFlags;

import java.util.Objects;

/**
@@ -77,8 +79,7 @@ final class NavigationBarController {
        default void onDestroy() {
        }

        default void setShouldShowImeSwitcherWhenImeIsShown(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
        default void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
        }

        default String toDebugString() {
@@ -117,8 +118,8 @@ final class NavigationBarController {
        mImpl.onDestroy();
    }

    void setShouldShowImeSwitcherWhenImeIsShown(boolean shouldShowImeSwitcherWhenImeIsShown) {
        mImpl.setShouldShowImeSwitcherWhenImeIsShown(shouldShowImeSwitcherWhenImeIsShown);
    void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
        mImpl.onNavButtonFlagsChanged(navButtonFlags);
    }

    String toDebugString() {
@@ -448,11 +449,14 @@ final class NavigationBarController {
        }

        @Override
        public void setShouldShowImeSwitcherWhenImeIsShown(
                boolean shouldShowImeSwitcherWhenImeIsShown) {
        public void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
            if (mDestroyed) {
                return;
            }

            final boolean shouldShowImeSwitcherWhenImeIsShown =
                    (navButtonFlags & InputMethodNavButtonFlags.SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN)
                    != 0;
            if (mShouldShowImeSwitcherWhenImeIsShown == shouldShowImeSwitcherWhenImeIsShown) {
                return;
            }
+8 −11
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.view.MotionEvent;
import android.view.View;

import com.android.internal.inputmethod.IInputMethodPrivilegedOperations;
import com.android.internal.inputmethod.InputMethodNavButtonFlags;
import com.android.internal.view.IInlineSuggestionsRequestCallback;
import com.android.internal.view.InlineSuggestionsRequestInfo;

@@ -105,14 +106,13 @@ 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.
     * @param navButtonFlags The initial state of {@link InputMethodNavButtonFlags}.
     * @hide
     */
    @MainThread
    default void initializeInternal(IBinder token,
            IInputMethodPrivilegedOperations privilegedOperations, int configChanges,
            boolean stylusHwSupported, boolean shouldShowImeSwitcherWhenImeIsShown) {
            boolean stylusHwSupported, @InputMethodNavButtonFlags int navButtonFlags) {
        attachToken(token);
    }

@@ -231,8 +231,7 @@ 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.
     * @param navButtonFlags {@link InputMethodNavButtonFlags} in the initial state of this session.
     * @see #startInput(InputConnection, EditorInfo)
     * @see #restartInput(InputConnection, EditorInfo)
     * @see EditorInfo
@@ -241,7 +240,7 @@ public interface InputMethod {
    @MainThread
    default void dispatchStartInputWithToken(@Nullable InputConnection inputConnection,
            @NonNull EditorInfo editorInfo, boolean restarting,
            @NonNull IBinder startInputToken, boolean shouldShowImeSwitcherWhenImeIsShown) {
            @NonNull IBinder startInputToken, @InputMethodNavButtonFlags int navButtonFlags) {
        if (restarting) {
            restartInput(inputConnection, editorInfo);
        } else {
@@ -250,15 +249,13 @@ public interface InputMethod {
    }

    /**
     * Notifies that whether the IME should show the IME switcher or not is being changed.
     * Notifies that {@link InputMethodNavButtonFlags} have been updated.
     *
     * @param shouldShowImeSwitcherWhenImeIsShown {@code true} If the IME switcher is expected to be
     *                                            shown while the IME is shown.
     * @param navButtonFlags The new {@link InputMethodNavButtonFlags}.
     * @hide
     */
    @MainThread
    default void onShouldShowImeSwitcherWhenImeIsShownChanged(
            boolean shouldShowImeSwitcherWhenImeIsShown) {
    default void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
    }

    /**
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.annotation.IntDef;

import java.lang.annotation.Retention;

/**
 * A set of flags notified from {@link com.android.server.inputmethod.InputMethodManagerService} to
 * {@link android.inputmethodservice.InputMethodService} regarding how
 * {@link android.inputmethodservice.NavigationBarController} should behave.
 *
 * <p>These flags will take effect when and only when
 * {@link android.inputmethodservice.InputMethodService#canImeRenderGesturalNavButtons} returns
 * {@code true}.</p>
 */
@Retention(SOURCE)
@IntDef(flag = true, value = {
        InputMethodNavButtonFlags.SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN,
})
public @interface InputMethodNavButtonFlags {
    /**
     * When set, the IME switcher icon needs to be shown on the navigation bar.
     */
    int SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN = 1;
}
Loading