diff options
3 files changed, 16 insertions, 41 deletions
diff --git a/core/java/android/view/textservice/SpellCheckerSubtype.java b/core/java/android/view/textservice/SpellCheckerSubtype.java index 77fd00294fc7..c753188bea56 100644 --- a/core/java/android/view/textservice/SpellCheckerSubtype.java +++ b/core/java/android/view/textservice/SpellCheckerSubtype.java @@ -16,9 +16,6 @@ package android.view.textservice; -import com.android.internal.inputmethod.InputMethodUtils; - -import android.annotation.Nullable; import android.content.Context; import android.content.pm.ApplicationInfo; import android.os.Parcel; @@ -150,15 +147,22 @@ public final class SpellCheckerSubtype implements Parcelable { } /** - * @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. - * - * <p>TODO: Consider to make this a public API.</p> * @hide */ - @Nullable - public Locale getLocaleObject() { - return InputMethodUtils.constructLocaleFromString(mSubtypeLocale); + public static Locale constructLocaleFromString(String localeStr) { + if (TextUtils.isEmpty(localeStr)) + return null; + String[] localeParams = localeStr.split("_", 3); + // The length of localeStr is guaranteed to always return a 1 <= value <= 3 + // because localeStr is not empty. + if (localeParams.length == 1) { + return new Locale(localeParams[0]); + } else if (localeParams.length == 2) { + return new Locale(localeParams[0], localeParams[1]); + } else if (localeParams.length == 3) { + return new Locale(localeParams[0], localeParams[1], localeParams[2]); + } + return null; } /** @@ -173,7 +177,7 @@ public final class SpellCheckerSubtype implements Parcelable { */ public CharSequence getDisplayName( Context context, String packageName, ApplicationInfo appInfo) { - final Locale locale = getLocaleObject(); + final Locale locale = constructLocaleFromString(mSubtypeLocale); final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale; if (mSubtypeNameResId == 0) { return localeStr; diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 56513a3d997e..cca84ee707d5 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -8785,7 +8785,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); final Locale locale; if (subtype != null) { - locale = subtype.getLocaleObject(); + locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale()); } else { locale = null; } diff --git a/core/tests/coretests/src/android/view/textservice/SpellCheckerSubtypeTest.java b/core/tests/coretests/src/android/view/textservice/SpellCheckerSubtypeTest.java index 157c815aa64b..84d8dd9eb95a 100644 --- a/core/tests/coretests/src/android/view/textservice/SpellCheckerSubtypeTest.java +++ b/core/tests/coretests/src/android/view/textservice/SpellCheckerSubtypeTest.java @@ -21,7 +21,6 @@ import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.SmallTest; import java.util.Arrays; -import java.util.Locale; import static android.test.MoreAsserts.assertNotEqual; @@ -81,34 +80,6 @@ public class SpellCheckerSubtypeTest extends InstrumentationTestCase { } @SmallTest - public void testGetLocaleObject() throws Exception { - assertEquals(new Locale("en"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "en", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("en", "US"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "en_US", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("en", "US", "POSIX"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "en_US_POSIX", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - - // Special rewrite rule for "tl" for versions of Android earlier than Lollipop that did not - // support three letter language codes, and used "tl" (Tagalog) as the language string for - // "fil" (Filipino). - assertEquals(new Locale("fil"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "tl", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("fil", "PH"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "tl_PH", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("fil", "PH", "POSIX"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "tl_PH_POSIX", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - - // So far rejecting invalid/unexpected locale strings is out of the scope. - assertEquals(new Locale("a"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "a", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("a b c"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "a b c", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - assertEquals(new Locale("en-US"), new SpellCheckerSubtype( - SUBTYPE_NAME_RES_ID_A, "en-US", SUBTYPE_EXTRA_VALUE_A).getLocaleObject()); - } - - @SmallTest public void testEquality() throws Exception { assertEquals( new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, |