From bc60116c5bc49211e926f53d611051c579b69662 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Sun, 17 Jun 2018 21:08:20 -0700 Subject: [PATCH] Fix broken language matching logic in TSMS Checking language similarity with String#startsWith() is fundamentally broken. For instance, "fi" (Finnish) can match "fil" (Filipino). This CL addresses such a broken comparison. Bug: 110275412 Test: Manually verified that spell checker still works Change-Id: I8d71760c534627b7707e16eb0fc648989f7692ae --- .../server/TextServicesManagerService.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/TextServicesManagerService.java b/services/core/java/com/android/server/TextServicesManagerService.java index d82ca0b8c3c11..32d22ec9fe8d6 100644 --- a/services/core/java/com/android/server/TextServicesManagerService.java +++ b/services/core/java/com/android/server/TextServicesManagerService.java @@ -69,6 +69,7 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.function.Predicate; public class TextServicesManagerService extends ITextServicesManager.Stub { @@ -536,25 +537,17 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { && !allowImplicitlySelectedSubtype) { return null; } - String candidateLocale = null; - if (subtypeHashCode == 0) { - // Spell checker language settings == "auto" - // Use System locale if available in the spell checker - candidateLocale = systemLocale.toString(); - } SpellCheckerSubtype candidate = null; for (int i = 0; i < sci.getSubtypeCount(); ++i) { final SpellCheckerSubtype scs = sci.getSubtypeAt(i); if (subtypeHashCode == 0) { - final String scsLocale = scs.getLocale(); - if (candidateLocale.equals(scsLocale)) { + final Locale scsLocale = scs.getLocaleObject(); + if (Objects.equals(scsLocale, systemLocale)) { return scs; - } else if (candidate == null) { - if (candidateLocale.length() >= 2 && scsLocale.length() >= 2 - && candidateLocale.startsWith(scsLocale)) { - // Fall back to the applicable language - candidate = scs; - } + } else if (candidate == null && systemLocale != null && scsLocale != null + && TextUtils.equals(systemLocale.getLanguage(), scsLocale.getLanguage())) { + // Fall back to the applicable language + candidate = scs; } } else if (scs.hashCode() == subtypeHashCode) { if (DBG) {