Fix bugs in logic to find a default selectd IME.

With this CL, InputMethodManagerService#resetDefaultImeLocked()
picks up the default selected IME with the same logic to find the
default enabled IMEs [1].  It should make sense because the default
selected IME should be one of the default enabled IMEs.  The previous
code is problematic because it does not check whether the IME is enabled
or not.  There was a chance that unusable IME could be picked up.

This CL also fixes the same problem to Bug 17347871 that only language
part of the locale is taken into account.

  [1] See the following series of CLs.
    - part 1: I831502db502f4073c9c2f50ce7705a4e45e2e1e3
              ed20f8d750ef0b6347448265a14ef2a2c7e1af5c
    - part 2: Ife93d909fb8a24471c425c903e2b7048826e17a3
              745e7bca8a622ffdf0d0a8e8e2485eab98182ede
    - part 3: I6571d464a46453934f0a8f5e79018a67a9a3c845
              d0dbd81fe2cd34c9a83e2f5217374d3e1a79f950
    - part 4: I871ccda787eb0f1099ba3574356c1da4b33681f3
              b21220efae92a56ff7b4b781fa614a6e3a8a3007

Bug: 27197621
Change-Id: Ia0f52c1fb9f5a68230284a1ec4829a2337b60bdd
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 7770d53..507ac22 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -940,35 +940,18 @@
 
     private void resetDefaultImeLocked(Context context) {
         // Do not reset the default (current) IME when it is a 3rd-party IME
-        if (mCurMethodId != null
-                && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
+        if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
             return;
         }
-
-        InputMethodInfo defIm = null;
-        for (InputMethodInfo imi : mMethodList) {
-            if (defIm == null && mSystemReady) {
-                final Locale systemLocale = context.getResources().getConfiguration().locale;
-                if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
-                        true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
-                        InputMethodUtils.SUBTYPE_MODE_ANY)) {
-                    defIm = imi;
-                    Slog.i(TAG, "Selected default: " + imi.getId());
-                }
-            }
+        final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
+                context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
+        if (suitableImes.isEmpty()) {
+            Slog.i(TAG, "No default found");
+            return;
         }
-        if (defIm == null && mMethodList.size() > 0) {
-            defIm = InputMethodUtils.getMostApplicableDefaultIME(
-                    mSettings.getEnabledInputMethodListLocked());
-            if (defIm != null) {
-                Slog.i(TAG, "Default found, using " + defIm.getId());
-            } else {
-                Slog.i(TAG, "No default found");
-            }
-        }
-        if (defIm != null) {
-            setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
-        }
+        final InputMethodInfo defIm = suitableImes.get(0);
+        Slog.i(TAG, "Default found, using " + defIm.getId());
+        setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
     }
 
     private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,