Simplify findLastResortApplicableSubtypeLocked

This is a mechanical refactoring CL. There must be no behavior change.

Bug: 309837937
Test: presubit
Change-Id: I100bda0e12d9def3dcd113547862fe916c1d3c00
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index 43305c2..8790406 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -4065,14 +4065,18 @@
                 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
                 if (enabled != null) {
                     final int enabledCount = enabled.size();
-                    final String locale = mCurrentSubtype == null
-                            ? mRes.getConfiguration().locale.toString()
-                            : mCurrentSubtype.getLocale();
+                    final String locale;
+                    if (mCurrentSubtype != null
+                            && !TextUtils.isEmpty(mCurrentSubtype.getLocale())) {
+                        locale = mCurrentSubtype.getLocale();
+                    } else {
+                        locale = mRes.getConfiguration().locale.toString();
+                    }
                     for (int i = 0; i < enabledCount; ++i) {
                         final InputMethodInfo imi = enabled.get(i);
                         if (imi.getSubtypeCount() > 0 && imi.isSystem()) {
                             InputMethodSubtype keyboardSubtype =
-                                    SubtypeUtils.findLastResortApplicableSubtypeLocked(mRes,
+                                    SubtypeUtils.findLastResortApplicableSubtypeLocked(
                                             SubtypeUtils.getSubtypes(imi),
                                             SubtypeUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
                             if (keyboardSubtype != null) {
@@ -5421,12 +5425,13 @@
                 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
                     mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
                 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
+                    final String locale = mRes.getConfiguration().locale.toString();
                     mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtypeLocked(
-                            mRes, explicitlyOrImplicitlyEnabledSubtypes,
-                            SubtypeUtils.SUBTYPE_MODE_KEYBOARD, null, true);
+                            explicitlyOrImplicitlyEnabledSubtypes,
+                            SubtypeUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
                     if (mCurrentSubtype == null) {
                         mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtypeLocked(
-                                mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, true);
+                                explicitlyOrImplicitlyEnabledSubtypes, null, locale, true);
                     }
                 }
             } else {
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
index 984ae1f..94866c1 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
@@ -847,14 +847,15 @@
             if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
                 return explicitlyOrImplicitlyEnabledSubtypes.get(0);
             }
+            final String locale = mRes.getConfiguration().locale.toString();
             final InputMethodSubtype subtype = SubtypeUtils.findLastResortApplicableSubtypeLocked(
-                    mRes, explicitlyOrImplicitlyEnabledSubtypes, SubtypeUtils.SUBTYPE_MODE_KEYBOARD,
-                    null, true);
+                    explicitlyOrImplicitlyEnabledSubtypes, SubtypeUtils.SUBTYPE_MODE_KEYBOARD,
+                    locale, true);
             if (subtype != null) {
                 return subtype;
             }
-            return SubtypeUtils.findLastResortApplicableSubtypeLocked(mRes,
-                    explicitlyOrImplicitlyEnabledSubtypes, null, null, true);
+            return SubtypeUtils.findLastResortApplicableSubtypeLocked(
+                    explicitlyOrImplicitlyEnabledSubtypes, null, locale, true);
         }
 
         boolean setAdditionalInputMethodSubtypes(@NonNull String imeId,
diff --git a/services/core/java/com/android/server/inputmethod/SubtypeUtils.java b/services/core/java/com/android/server/inputmethod/SubtypeUtils.java
index 0185190..f09a001 100644
--- a/services/core/java/com/android/server/inputmethod/SubtypeUtils.java
+++ b/services/core/java/com/android/server/inputmethod/SubtypeUtils.java
@@ -220,7 +220,7 @@
 
         if (applicableSubtypes.isEmpty()) {
             InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
-                    res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
+                    subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
             if (lastResortKeyboardSubtype != null) {
                 applicableSubtypes.add(lastResortKeyboardSubtype);
             }
@@ -249,14 +249,11 @@
      * @return the most applicable subtypeId
      */
     static InputMethodSubtype findLastResortApplicableSubtypeLocked(
-            Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
+            List<InputMethodSubtype> subtypes, String mode, @NonNull String locale,
             boolean canIgnoreLocaleAsLastResort) {
         if (subtypes == null || subtypes.isEmpty()) {
             return null;
         }
-        if (TextUtils.isEmpty(locale)) {
-            locale = res.getConfiguration().locale.toString();
-        }
         final String language = LocaleUtils.getLanguageFromLocaleString(locale);
         boolean partialMatchFound = false;
         InputMethodSubtype applicableSubtype = null;