Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd2b4aa3 authored by Yohei Yukawa's avatar Yohei Yukawa Committed by Android (Google) Code Review
Browse files

Merge changes I0af0f520,I51be014c

* changes:
  Rewrite a fake language code "tl" in SpellChecker.
  retry: Add @hide SpellCheckerSubtype#getLocaleObject().
parents 65196ee2 eae60ba5
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

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;
@@ -147,22 +150,15 @@ 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
     */
    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;
    @Nullable
    public Locale getLocaleObject() {
        return InputMethodUtils.constructLocaleFromString(mSubtypeLocale);
    }

    /**
@@ -177,7 +173,7 @@ public final class SpellCheckerSubtype implements Parcelable {
     */
    public CharSequence getDisplayName(
            Context context, String packageName, ApplicationInfo appInfo) {
        final Locale locale = constructLocaleFromString(mSubtypeLocale);
        final Locale locale = getLocaleObject();
        final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale;
        if (mSubtypeNameResId == 0) {
            return localeStr;
+1 −1
Original line number Diff line number Diff line
@@ -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 = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale());
            locale = subtype.getLocaleObject();
        } else {
            locale = null;
        }
+29 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.Arrays;
import java.util.Locale;

import static android.test.MoreAsserts.assertNotEqual;

@@ -79,6 +80,34 @@ public class SpellCheckerSubtypeTest extends InstrumentationTestCase {
                clonedSubtype.hashCode());
    }

    @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(