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
diff --git a/core/java/android/inputmethodservice/IInputMethodWrapper.java b/core/java/android/inputmethodservice/IInputMethodWrapper.java
index 41642e7..af57f79 100644
--- a/core/java/android/inputmethodservice/IInputMethodWrapper.java
+++ b/core/java/android/inputmethodservice/IInputMethodWrapper.java
@@ -70,6 +70,7 @@
     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 @@
                 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 @@
                 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 @@
     @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 @@
     @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
diff --git a/core/java/android/inputmethodservice/InputMethodService.java b/core/java/android/inputmethodservice/InputMethodService.java
index 14f92fb..f55c415 100644
--- a/core/java/android/inputmethodservice/InputMethodService.java
+++ b/core/java/android/inputmethodservice/InputMethodService.java
@@ -658,7 +658,7 @@
         @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 @@
             if (stylusHwSupported) {
                 mInkWindow = new InkWindow(mWindow.getContext());
             }
+            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
+                    shouldShowImeSwitcherWhenImeIsShown);
             attachToken(token);
             Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
         }
@@ -780,9 +782,10 @@
         @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 {
@@ -796,6 +799,18 @@
          */
         @MainThread
         @Override
+        public void onShouldShowImeSwitcherWhenImeIsShownChanged(
+                boolean shouldShowImeSwitcherWhenImeIsShown) {
+            mNavigationBarController.setShouldShowImeSwitcherWhenImeIsShown(
+                    shouldShowImeSwitcherWhenImeIsShown);
+        }
+
+        /**
+         * {@inheritDoc}
+         * @hide
+         */
+        @MainThread
+        @Override
         public void hideSoftInputWithToken(int flags, ResultReceiver resultReceiver,
                 IBinder hideInputToken) {
             mSystemCallingHideSoftInput = true;
diff --git a/core/java/android/inputmethodservice/NavigationBarController.java b/core/java/android/inputmethodservice/NavigationBarController.java
index d572f5a..508172d 100644
--- a/core/java/android/inputmethodservice/NavigationBarController.java
+++ b/core/java/android/inputmethodservice/NavigationBarController.java
@@ -74,6 +74,10 @@
         default void onDestroy() {
         }
 
+        default void setShouldShowImeSwitcherWhenImeIsShown(
+                boolean shouldShowImeSwitcherWhenImeIsShown) {
+        }
+
         default void onSystemBarAppearanceChanged(@Appearance int appearance) {
         }
 
@@ -109,6 +113,10 @@
         mImpl.onDestroy();
     }
 
+    void setShouldShowImeSwitcherWhenImeIsShown(boolean shouldShowImeSwitcherWhenImeIsShown) {
+        mImpl.setShouldShowImeSwitcherWhenImeIsShown(shouldShowImeSwitcherWhenImeIsShown);
+    }
+
     void onSystemBarAppearanceChanged(@Appearance int appearance) {
         mImpl.onSystemBarAppearanceChanged(appearance);
     }
@@ -139,6 +147,8 @@
         @Nullable
         private BroadcastReceiver mSystemOverlayChangedReceiver;
 
+        private boolean mShouldShowImeSwitcherWhenImeIsShown;
+
         @Appearance
         private int mAppearance;
 
@@ -205,7 +215,9 @@
                     // 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 {
@@ -423,6 +435,31 @@
         }
 
         @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) {
                 return;
@@ -471,6 +508,7 @@
         public String toDebugString() {
             return "{mRenderGesturalNavButtons=" + mRenderGesturalNavButtons
                     + " mNavigationBarFrame=" + mNavigationBarFrame
+                    + " mShouldShowImeSwitcherWhenImeIsShown" + mShouldShowImeSwitcherWhenImeIsShown
                     + " mAppearance=0x" + Integer.toHexString(mAppearance)
                     + " mDarkIntensity=" + mDarkIntensity
                     + "}";
diff --git a/core/java/android/view/inputmethod/InputMethod.java b/core/java/android/view/inputmethod/InputMethod.java
index ff6903e8..08cc31c 100644
--- a/core/java/android/view/inputmethod/InputMethod.java
+++ b/core/java/android/view/inputmethod/InputMethod.java
@@ -105,12 +105,14 @@
      *                             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 @@
      *                        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 @@
     @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 {
@@ -246,6 +250,18 @@
     }
 
     /**
+     * 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
      * use {@link #revokeSession(InputMethodSession)} to destroy the session
diff --git a/core/java/com/android/internal/view/IInputMethod.aidl b/core/java/com/android/internal/view/IInputMethod.aidl
index da24832..d2bc344 100644
--- a/core/java/com/android/internal/view/IInputMethod.aidl
+++ b/core/java/com/android/internal/view/IInputMethod.aidl
@@ -37,7 +37,8 @@
  */
 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 @@
     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);
 
diff --git a/services/core/java/com/android/server/inputmethod/IInputMethodInvoker.java b/services/core/java/com/android/server/inputmethod/IInputMethodInvoker.java
index c87ca92..6cb3b3b 100644
--- a/services/core/java/com/android/server/inputmethod/IInputMethodInvoker.java
+++ b/services/core/java/com/android/server/inputmethod/IInputMethodInvoker.java
@@ -107,9 +107,11 @@
 
     @AnyThread
     void initializeInternal(IBinder token, IInputMethodPrivilegedOperations privOps,
-            int configChanges, boolean stylusHwSupported) {
+            int configChanges, boolean stylusHwSupported,
+            boolean shouldShowImeSwitcherWhenImeIsShown) {
         try {
-            mTarget.initializeInternal(token, privOps, configChanges, stylusHwSupported);
+            mTarget.initializeInternal(token, privOps, configChanges, stylusHwSupported,
+                    shouldShowImeSwitcherWhenImeIsShown);
         } catch (RemoteException e) {
             logRemoteException(e);
         }
@@ -145,9 +147,20 @@
 
     @AnyThread
     void startInput(IBinder startInputToken, IInputContext inputContext, EditorInfo attribute,
-            boolean restarting) {
+            boolean restarting, boolean shouldShowImeSwitcherWhenImeIsShown) {
         try {
-            mTarget.startInput(startInputToken, inputContext, attribute, restarting);
+            mTarget.startInput(startInputToken, inputContext, attribute, restarting,
+                    shouldShowImeSwitcherWhenImeIsShown);
+        } catch (RemoteException e) {
+            logRemoteException(e);
+        }
+    }
+
+    @AnyThread
+    void onShouldShowImeSwitcherWhenImeIsShownChanged(boolean shouldShowImeSwitcherWhenImeIsShown) {
+        try {
+            mTarget.onShouldShowImeSwitcherWhenImeIsShownChanged(
+                    shouldShowImeSwitcherWhenImeIsShown);
         } catch (RemoteException e) {
             logRemoteException(e);
         }
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index 936b1a2..c207738a 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -2325,10 +2325,12 @@
                     true /* direct */);
         }
 
+        final boolean shouldShowImeSwitcherWhenImeIsShown =
+                shouldShowImeSwitcherWhenImeIsShownLocked();
         final SessionState session = mCurClient.curSession;
         setEnabledSessionLocked(session);
-        session.method.startInput(startInputToken, mCurInputContext, mCurAttribute, restarting);
-
+        session.method.startInput(startInputToken, mCurInputContext, mCurAttribute, restarting,
+                shouldShowImeSwitcherWhenImeIsShown);
         if (mShowRequested) {
             if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
             showCurrentInputLocked(mCurFocusedWindow, getAppShowFlagsLocked(), null,
@@ -2528,7 +2530,7 @@
                     + mCurTokenDisplayId);
         }
         inputMethod.initializeInternal(token, new InputMethodPrivilegedOperationsImpl(this, token),
-                configChanges, supportStylusHw);
+                configChanges, supportStylusHw, shouldShowImeSwitcherWhenImeIsShownLocked());
     }
 
     @AnyThread
@@ -2731,6 +2733,12 @@
     }
 
     @GuardedBy("ImfLock.class")
+    boolean shouldShowImeSwitcherWhenImeIsShownLocked() {
+        return shouldShowImeSwitcherLocked(
+                InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE);
+    }
+
+    @GuardedBy("ImfLock.class")
     private boolean shouldShowImeSwitcherLocked(int visibility) {
         if (!mShowOngoingImeSwitcherForPhones) return false;
         if (mMenuController.getSwitchingDialogLocked() != null) return false;
@@ -2990,6 +2998,7 @@
         // the same enabled IMEs list.
         mSwitchingController.resetCircularListLocked(mContext);
 
+        sendShouldShowImeSwitcherWhenImeIsShownLocked();
     }
 
     @GuardedBy("ImfLock.class")
@@ -4308,6 +4317,7 @@
                 updateImeWindowStatus(msg.arg1 == 1);
                 return true;
             }
+
             // ---------------------------------------------------------
 
             case MSG_UNBIND_CLIENT:
@@ -4368,6 +4378,9 @@
             // --------------------------------------------------------------
             case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
                 mMenuController.handleHardKeyboardStatusChange(msg.arg1 == 1);
+                synchronized (ImfLock.class) {
+                    sendShouldShowImeSwitcherWhenImeIsShownLocked();
+                }
                 return true;
             case MSG_SYSTEM_UNLOCK_USER: {
                 final int userId = msg.arg1;
@@ -4638,6 +4651,8 @@
         // the same enabled IMEs list.
         mSwitchingController.resetCircularListLocked(mContext);
 
+        sendShouldShowImeSwitcherWhenImeIsShownLocked();
+
         // Notify InputMethodListListeners of the new installed InputMethods.
         final List<InputMethodInfo> inputMethodList = new ArrayList<>(mMethodList);
         mHandler.obtainMessage(MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED,
@@ -4645,6 +4660,17 @@
     }
 
     @GuardedBy("ImfLock.class")
+    void sendShouldShowImeSwitcherWhenImeIsShownLocked() {
+        final IInputMethodInvoker curMethod = mBindingController.getCurMethod();
+        if (curMethod == null) {
+            // No need to send the data if the IME is not yet bound.
+            return;
+        }
+        curMethod.onShouldShowImeSwitcherWhenImeIsShownChanged(
+                shouldShowImeSwitcherWhenImeIsShownLocked());
+    }
+
+    @GuardedBy("ImfLock.class")
     private void updateDefaultVoiceImeIfNeededLocked() {
         final String systemSpeechRecognizer =
                 mContext.getString(com.android.internal.R.string.config_systemSpeechRecognizer);
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodMenuController.java b/services/core/java/com/android/server/inputmethod/InputMethodMenuController.java
index 348bb2d..98bde11 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodMenuController.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodMenuController.java
@@ -203,6 +203,7 @@
             attrs.setTitle("Select input method");
             w.setAttributes(attrs);
             mService.updateSystemUiLocked();
+            mService.sendShouldShowImeSwitcherWhenImeIsShownLocked();
             mSwitchingDialog.show();
         }
     }
@@ -238,6 +239,7 @@
             mSwitchingDialogTitleView = null;
 
             mService.updateSystemUiLocked();
+            mService.sendShouldShowImeSwitcherWhenImeIsShownLocked();
             mDialogBuilder = null;
             mIms = null;
         }