From 92280cd309b0f5967dd253280962d8581844db89 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 2 Jun 2015 16:50:14 -0700 Subject: [PATCH] Convert subtypes whose locale is "tl" to "fil". On Android, "tl" is a historic hack for what should really be "fil". Now that we properly support 3-letter language codes, we should be using "fil" throughout. Given this historical usage, IMEs that really want to support Tagalog (and not Filipino) should use the ISO-639-3 code for Tagalog, which is "tgl". For backward compatibility reasons, this CL uses the similar approach to I26e3aa0333aa3c76c80a3c1c9090cc2b368c8e10. InputMethodSubtype.getLocale() continues to return the "locale" string parameter passed to the constructor as is, but in the Android framework we do normalizations/conversions whenever we need a valid ISO-639-3 code. In I26e3aa0333aa3c76c80a3c1c9090cc2b368c8e10, we rely on the conversion in the Locale constructor. In this CL, we do replace "tl" with "fil" by ourselves. This CL also adds InputMethodSubtype#getLocaleObject() a hidden API so that we can start relying on the Locale object at least in the framework. This CL is based on the investigation by Narayan Kamath and his patch. Bug: 20696126 Change-Id: I94f203bddceb9c87710cb187cc3cc0ee6d9092a5 --- .../view/inputmethod/InputMethodSubtype.java | 16 +++++++++++++++- .../internal/inputmethod/InputMethodUtils.java | 10 +++++++++- .../src/android/os/InputMethodSubtypeTest.java | 4 ++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodSubtype.java b/core/java/android/view/inputmethod/InputMethodSubtype.java index c2f37779025fc..4ee155c1d4a24 100644 --- a/core/java/android/view/inputmethod/InputMethodSubtype.java +++ b/core/java/android/view/inputmethod/InputMethodSubtype.java @@ -16,6 +16,7 @@ package android.view.inputmethod; +import android.annotation.Nullable; import android.content.Context; import android.content.pm.ApplicationInfo; import android.os.Parcel; @@ -322,6 +323,19 @@ public final class InputMethodSubtype implements Parcelable { return mSubtypeLocale; } + /** + * @return The normalized {@link Locale} object of the subtype. The returned locale may or may + * not equal to "locale" string parameter passed to the constructor. + * + *

TODO: Consider to make this a public API.

+ * @hide + */ + @Nullable + public Locale getLocaleObject() { + // TODO: Move the following method from InputMethodUtils to InputMethodSubtype. + return InputMethodUtils.constructLocaleFromString(mSubtypeLocale); + } + /** * @return The mode of the subtype. */ @@ -381,7 +395,7 @@ public final class InputMethodSubtype implements Parcelable { */ public CharSequence getDisplayName( Context context, String packageName, ApplicationInfo appInfo) { - final Locale locale = InputMethodUtils.constructLocaleFromString(mSubtypeLocale); + final Locale locale = getLocaleObject(); final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale; if (mSubtypeNameResId == 0) { return localeStr; diff --git a/core/java/com/android/internal/inputmethod/InputMethodUtils.java b/core/java/com/android/internal/inputmethod/InputMethodUtils.java index 06bdb24f59593..042db7154bee4 100644 --- a/core/java/com/android/internal/inputmethod/InputMethodUtils.java +++ b/core/java/com/android/internal/inputmethod/InputMethodUtils.java @@ -379,6 +379,14 @@ public class InputMethodUtils { // The length of localeStr is guaranteed to always return a 1 <= value <= 3 // because localeStr is not empty. if (localeParams.length == 1) { + if (localeParams.length >= 1 && "tl".equals(localeParams[0])) { + // Convert a locale whose language is "tl" to one whose language is "fil". + // For example, "tl_PH" will get converted to "fil_PH". + // Versions of Android earlier than Lollipop did not support three letter language + // codes, and used "tl" (Tagalog) as the language string for "fil" (Filipino). + // On Lollipop and above, the current three letter version must be used. + localeParams[0] = "fil"; + } return new Locale(localeParams[0]); } else if (localeParams.length == 2) { return new Locale(localeParams[0], localeParams[1]); @@ -397,7 +405,7 @@ public class InputMethodUtils { for (int i = 0; i < N; ++i) { final InputMethodSubtype subtype = imi.getSubtypeAt(i); if (checkCountry) { - final Locale subtypeLocale = constructLocaleFromString(subtype.getLocale()); + final Locale subtypeLocale = subtype.getLocaleObject(); if (subtypeLocale == null || !TextUtils.equals(subtypeLocale.getLanguage(), locale.getLanguage()) || !TextUtils.equals(subtypeLocale.getCountry(), locale.getCountry())) { diff --git a/core/tests/inputmethodtests/src/android/os/InputMethodSubtypeTest.java b/core/tests/inputmethodtests/src/android/os/InputMethodSubtypeTest.java index 323a360cded36..8feac9be2180d 100644 --- a/core/tests/inputmethodtests/src/android/os/InputMethodSubtypeTest.java +++ b/core/tests/inputmethodtests/src/android/os/InputMethodSubtypeTest.java @@ -56,6 +56,10 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { verifyLocale("zz"); verifyLocale("iw"); verifyLocale("he"); + verifyLocale("tl"); + verifyLocale("tl_PH"); + verifyLocale("fil"); + verifyLocale("fil_PH"); } @SmallTest