diff options
| -rw-r--r-- | services/core/java/com/android/server/inputmethod/InputMethodManagerService.java | 31 | ||||
| -rw-r--r-- | services/core/java/com/android/server/inputmethod/SubtypeUtils.java | 52 |
2 files changed, 54 insertions, 29 deletions
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index 56fbef929872..e0c27be1bd91 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -5528,35 +5528,8 @@ public final class InputMethodManagerService implements IInputMethodManagerImpl. if (imi == null || imi.getSubtypeCount() == 0) { return null; } - final int currentSubtypeHashCode = SecureSettingsWrapper.getInt( - Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID, userId); - if (currentSubtypeHashCode == NOT_A_SUBTYPE_ID || mCurrentSubtype == null - || !SubtypeUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) { - int subtypeId = settings.getSelectedInputMethodSubtypeId(selectedMethodId); - if (subtypeId == NOT_A_SUBTYPE_ID) { - // If there are no selected subtypes, the framework will try to find - // the most applicable subtype from explicitly or implicitly enabled - // subtypes. - List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = - settings.getEnabledInputMethodSubtypeList(imi, true); - // If there is only one explicitly or implicitly enabled subtype, - // just returns it. - if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { - mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); - } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { - final String locale = SystemLocaleWrapper.get(userId).get(0).toString(); - mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtype( - explicitlyOrImplicitlyEnabledSubtypes, - SubtypeUtils.SUBTYPE_MODE_KEYBOARD, locale, true); - if (mCurrentSubtype == null) { - mCurrentSubtype = SubtypeUtils.findLastResortApplicableSubtype( - explicitlyOrImplicitlyEnabledSubtypes, null, locale, true); - } - } - } else { - mCurrentSubtype = SubtypeUtils.getSubtypes(imi).get(subtypeId); - } - } + mCurrentSubtype = SubtypeUtils.getCurrentInputMethodSubtype(imi, settings, + mCurrentSubtype); return mCurrentSubtype; } diff --git a/services/core/java/com/android/server/inputmethod/SubtypeUtils.java b/services/core/java/com/android/server/inputmethod/SubtypeUtils.java index 3d5c867768ac..e0d3b37d7a86 100644 --- a/services/core/java/com/android/server/inputmethod/SubtypeUtils.java +++ b/services/core/java/com/android/server/inputmethod/SubtypeUtils.java @@ -16,9 +16,11 @@ package com.android.server.inputmethod; +import android.annotation.AnyThread; import android.annotation.NonNull; import android.annotation.Nullable; import android.os.LocaleList; +import android.provider.Settings; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Slog; @@ -289,4 +291,54 @@ final class SubtypeUtils { } return applicableSubtype; } + + /** + * Returns a {@link InputMethodSubtype} available in {@code imi} based on + * {@link Settings.Secure#SELECTED_INPUT_METHOD_SUBTYPE}. + * + * @param imi {@link InputMethodInfo} to find out the current + * {@link InputMethodSubtype} + * @param settings {@link InputMethodSettings} to be used to find out the current + * {@link InputMethodSubtype} + * @param currentSubtype the current value that will be used as fallback + * @return {@link InputMethodSubtype} to be used as the current {@link InputMethodSubtype} + */ + @AnyThread + @Nullable + static InputMethodSubtype getCurrentInputMethodSubtype( + @NonNull InputMethodInfo imi, @NonNull InputMethodSettings settings, + @Nullable InputMethodSubtype currentSubtype) { + final int userId = settings.getUserId(); + final int selectedSubtypeHashCode = SecureSettingsWrapper.getInt( + Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID, userId); + if (selectedSubtypeHashCode != NOT_A_SUBTYPE_ID && currentSubtype != null + && isValidSubtypeId(imi, currentSubtype.hashCode())) { + return currentSubtype; + } + + final int subtypeId = settings.getSelectedInputMethodSubtypeId(imi.getId()); + if (subtypeId != NOT_A_SUBTYPE_ID) { + return imi.getSubtypeAt(subtypeId); + } + + // If there are no selected subtypes, the framework will try to find the most applicable + // subtype from explicitly or implicitly enabled subtypes. + final List<InputMethodSubtype> subtypes = settings.getEnabledInputMethodSubtypeList(imi, + true); + if (subtypes.isEmpty()) { + return currentSubtype; + } + // If there is only one explicitly or implicitly enabled subtype, + // just returns it. + if (subtypes.size() == 1) { + return subtypes.get(0); + } + final String locale = SystemLocaleWrapper.get(userId).get(0).toString(); + final var subtype = findLastResortApplicableSubtype(subtypes, SUBTYPE_MODE_KEYBOARD, locale, + true); + if (subtype != null) { + return subtype; + } + return findLastResortApplicableSubtype(subtypes, null, locale, true); + } } |