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

Commit 658c29e8 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

retry: Add @hide SpellCheckerSubtype#getLocaleObject().

This is the 2nd try of I39dc0c310158ad23ba6c987efce07deaf30ce693.

This is a mechanical refactoring with no behavior change.

With this CL, InputMethodSubtype and SpellCheckerSubtype have the same
getLocaleObject() hidden API, which makes it easy to share the logic in
subsequent CLs.

No behavior change is intended.

Bug: 11736916
Bug: 20696126
Bug: 22858221
Change-Id: I51be014c752b736a808e2b0d56e664941a218a2f
parent 8687b424
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.view.textservice;

import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Parcel;
@@ -147,9 +148,19 @@ 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) {
    @Nullable
    public Locale getLocaleObject() {
        // TODO: Use InputMethodUtils.constructLocaleFromString() instead.
        return constructLocaleFromString(mSubtypeLocale);
    }

    private static Locale constructLocaleFromString(String localeStr) {
        if (TextUtils.isEmpty(localeStr))
            return null;
        String[] localeParams = localeStr.split("_", 3);
+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;
        }
+28 −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,33 @@ 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" was not yet supported in spell checker.
        // TODO: Match the behavior to InputMethodSubtype.
        assertEquals(new Locale("tl"), new SpellCheckerSubtype(
                SUBTYPE_NAME_RES_ID_A, "tl", SUBTYPE_EXTRA_VALUE_A).getLocaleObject());
        assertEquals(new Locale("tl", "PH"), new SpellCheckerSubtype(
                SUBTYPE_NAME_RES_ID_A, "tl_PH", SUBTYPE_EXTRA_VALUE_A).getLocaleObject());
        assertEquals(new Locale("tl", "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(