Use correct binding flags when reconnecting to IME

Since ICS MR-1 [1], InputMethodManagerService (IMMS) establishes two
connections to an IME: one is for general connection and the other to
allow the system to adjust the IME's performance characteristics much
more like a foreground activity. The tricky point is that the former
connection can be re-connected not only IMMS#startInputInnerLocked() in
but also in IMMS#showCurrentInputLocked().

It turns out that some previous attempts to adjust BIND_ flags for the
former connection [2][3] were not applied to
IMMS#showCurrentInputLocked().  This CL addresses that inconsistency by
make it clear that IMMS establishes up to connections to the IME.

 [1]: Id1f73de66dc93d63212183958a72119ad174318b
      2c84cfc001fb92a71811bf7384b7f865ff31ff9d
 [2]: I1724113f42abe7862e8aecb6faae5a7620245e89
      c8230519728b14065effd3b7d4eca273ff86160c
 [3]: Ie5793fd9b40d980fa18f80246326511ed6ae0597
      f0f94d129b6eb3c48624e915898d86d4f2de59ff

Test: Manuall tested as follows.
      1. Build and flash an OS image.
      2. Complete the setup wizard (if any).
      3. tapas LatinIME && make -j
      4. adb install -r out/target/product/generic/system/app/LatinIME/LatinIME.apk
      5. Run a test program that keeps calling IMM#showSoftInput()
      6. adb shell ime enable com.android.inputmethod.latin/.LatinIME
      7. adb shell ime set com.android.inputmethod.latin/.LatinIME
      8. adb shell dumpsys activity processes android
         Make sure that there is a ConnectionRecord to
         com.android.inputmethod.latin that has "CR !FG UI !VIS" bits.
      9. adb install -r out/target/product/generic/system/app/LatinIME/LatinIME.apk
     10. adb shell dumpsys activity processes android
         Make sure that there is a ConnectionRecord to
         com.android.inputmethod.latin that has "CR !FG UI !VIS" bits.
Fixes: 36727763
Change-Id: Ib7fa34b21e1fb8bb5b19a9b08e36d0565f3dd266
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 69e481f..0440845 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -202,6 +202,23 @@
     private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
     private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
 
+    /**
+     * Binding flags for establishing connection to the {@link InputMethodService}.
+     */
+    private static final int IME_CONNECTION_BIND_FLAGS =
+            Context.BIND_AUTO_CREATE
+            | Context.BIND_NOT_VISIBLE
+            | Context.BIND_NOT_FOREGROUND
+            | Context.BIND_SHOWING_UI;
+
+    /**
+     * Binding flags used only while the {@link InputMethodService} is showing window.
+     */
+    private static final int IME_VISIBLE_BIND_FLAGS =
+            Context.BIND_AUTO_CREATE
+            | Context.BIND_TREAT_LIKE_ACTIVITY
+            | Context.BIND_FOREGROUND_SERVICE;
+
     @Retention(SOURCE)
     @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
     private @interface  HardKeyboardBehavior {
@@ -1816,9 +1833,7 @@
                 com.android.internal.R.string.input_method_binding_label);
         mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
                 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
-        if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
-                | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
-                | Context.BIND_SHOWING_UI)) {
+        if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
             mLastBindTime = SystemClock.uptimeMillis();
             mHaveConnection = true;
             mCurId = info.getId();
@@ -2469,9 +2484,7 @@
             mInputShown = true;
             if (mHaveConnection && !mVisibleBound) {
                 bindCurrentInputMethodService(
-                        mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
-                                | Context.BIND_TREAT_LIKE_ACTIVITY
-                                | Context.BIND_FOREGROUND_SERVICE);
+                        mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
                 mVisibleBound = true;
             }
             res = true;
@@ -2485,8 +2498,7 @@
                     SystemClock.uptimeMillis()-mLastBindTime,1);
             Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
             mContext.unbindService(this);
-            bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
-                    | Context.BIND_NOT_VISIBLE);
+            bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
         } else {
             if (DEBUG) {
                 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "