Tweak the conditions to hide IME switcher icon.

With this CL, the visibility of IME switcher is basically determined by
the number of enabled subtypes except for the following veto rules:

  Rule A:  Hide IME switcher if
    A.1 There is a physical keyboard attached, and
    A.2 The current IME is not receiving physical key events.

  Rule B:  Hide IME switcher if
    B.1 There is no physical keyboard attached, and
    B.2 The current IME does not have any visible UI element.

Bug: 25432652
Change-Id: I6d818f5a698b74708253be7a5350621f99a8f703
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 1c90524..cd9086d 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -1691,8 +1691,14 @@
         if (!mShowOngoingImeSwitcherForPhones) return false;
         if (mSwitchingDialog != null) return false;
         if (isScreenLocked()) return false;
-        if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
-        if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
+        final boolean isImeActive = ((visibility & InputMethodService.IME_ACTIVE) != 0);
+        final boolean isHardkeyboardAvailable = mWindowManagerService.isHardKeyboardAvailable();
+        final boolean isImeVisible = ((visibility & InputMethodService.IME_VISIBLE) != 0);
+        if (isHardkeyboardAvailable) {
+            if (!isImeActive) return false;
+        } else {
+            if (!isImeVisible) return false;
+        }
 
         List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
         final int N = imis.size();