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

Commit 835ab94b authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Extract constructLocaleFromString()

The main motivation of this refactoring is to move InputMethodUtils to
com.android.server.inputmethod as a package-private class.  With a lot
of cleanup CLs in Bug 77730201, the only remaining utility method that
is used by multiple Java package is this constructLocaleFromString()
method.

This is purely a mechanical refactoring.  There should be no
observable behavior difference.

Bug: 114660660
Test: atest CtsInputMethodTestCases CtsInputMethodServiceHostTestCases
Test: atest FrameworksCoreTests:com.android.internal.inputmethod
Change-Id: Ic47b680ad9efde104015d34311f49e224d3fb56f
parent 3781ca8c
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Slog;

import com.android.internal.inputmethod.InputMethodUtils;
import com.android.internal.inputmethod.SubtypeLocaleUtils;

import java.util.ArrayList;
import java.util.Arrays;
@@ -384,7 +384,7 @@ public final class InputMethodSubtype implements Parcelable {
            if (!TextUtils.isEmpty(mSubtypeLanguageTag)) {
                mCachedLocaleObj = Locale.forLanguageTag(mSubtypeLanguageTag);
            } else {
                mCachedLocaleObj = InputMethodUtils.constructLocaleFromString(mSubtypeLocale);
                mCachedLocaleObj = SubtypeLocaleUtils.constructLocaleFromString(mSubtypeLocale);
            }
            return mCachedLocaleObj;
        }
+2 −2
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Slog;

import com.android.internal.inputmethod.InputMethodUtils;
import com.android.internal.inputmethod.SubtypeLocaleUtils;

import java.util.ArrayList;
import java.util.Arrays;
@@ -228,7 +228,7 @@ public final class SpellCheckerSubtype implements Parcelable {
        if (!TextUtils.isEmpty(mSubtypeLanguageTag)) {
            return Locale.forLanguageTag(mSubtypeLanguageTag);
        }
        return InputMethodUtils.constructLocaleFromString(mSubtypeLocale);
        return SubtypeLocaleUtils.constructLocaleFromString(mSubtypeLocale);
    }

    /**
+0 −26
Original line number Diff line number Diff line
@@ -334,32 +334,6 @@ public class InputMethodUtils {
        return getDefaultEnabledImes(context, imis, false /* onlyMinimum */);
    }

    public static Locale constructLocaleFromString(String localeStr) {
        if (TextUtils.isEmpty(localeStr)) {
            return null;
        }
        // TODO: Use {@link Locale#toLanguageTag()} and {@link Locale#forLanguageTag(languageTag)}.
        String[] localeParams = localeStr.split("_", 3);
        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";
        }
        // 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;
    }

    public static boolean containsSubtypeOf(final InputMethodInfo imi,
            @Nullable final Locale locale, final boolean checkCountry, final String mode) {
        if (locale == null) {
+72 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import android.annotation.Nullable;
import android.text.TextUtils;

import java.util.Locale;

/**
 * A utility class to handle {@link Locale} related logic for
 * {@link android.view.inputmethod.InputMethodSubtype} and
 * {@link android.view.textservice.SpellCheckerSubtype}.
 */
public class SubtypeLocaleUtils {
    /**
     * Maintains deprecated logic about how subtype locales specified in XML resources have been
     * parsed.
     *
     * <p>This logic is kept basically for compatibility purpose.  Consider relying on languageTag
     * attribute instead.</p>
     *
     * @param localeStr string representation that is specified in the locale attribute
     * @return {@link Locale} object parsed from {@code localeStr}. {@code null} for unexpected
     *         format
     *
     * @attr ref android.R.styleable#InputMethod_Subtype_imeSubtypeLocale
     * @attr ref android.R.styleable#InputMethod_Subtype_languageTag
     * @attr ref android.R.styleable#SpellChecker_Subtype_languageTag
     * @attr ref android.R.styleable#SpellChecker_Subtype_subtypeLocale
     */
    @Nullable
    public static Locale constructLocaleFromString(String localeStr) {
        if (TextUtils.isEmpty(localeStr)) {
            return null;
        }
        // TODO: Use {@link Locale#toLanguageTag()} and {@link Locale#forLanguageTag(languageTag)}.
        String[] localeParams = localeStr.split("_", 3);
        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";
        }
        // 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;
    }
}
+0 −21
Original line number Diff line number Diff line
@@ -1082,27 +1082,6 @@ public class InputMethodUtilsTest {
        return preinstalledImes;
    }

    @Test
    public void testConstructLocaleFromString() throws Exception {
        assertEquals(new Locale("en"), InputMethodUtils.constructLocaleFromString("en"));
        assertEquals(new Locale("en", "US"), InputMethodUtils.constructLocaleFromString("en_US"));
        assertEquals(new Locale("en", "US", "POSIX"),
                InputMethodUtils.constructLocaleFromString("en_US_POSIX"));

        // 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"), InputMethodUtils.constructLocaleFromString("tl"));
        assertEquals(new Locale("fil", "PH"), InputMethodUtils.constructLocaleFromString("tl_PH"));
        assertEquals(new Locale("fil", "PH", "POSIX"),
                InputMethodUtils.constructLocaleFromString("tl_PH_POSIX"));

        // So far rejecting an invalid/unexpected locale string is out of the scope of this method.
        assertEquals(new Locale("a"), InputMethodUtils.constructLocaleFromString("a"));
        assertEquals(new Locale("a b c"), InputMethodUtils.constructLocaleFromString("a b c"));
        assertEquals(new Locale("en-US"), InputMethodUtils.constructLocaleFromString("en-US"));
    }

    @Test
    public void testIsSoftInputModeStateVisibleAllowed() {
        // On pre-P devices, SOFT_INPUT_STATE_VISIBLE/SOFT_INPUT_STATE_ALWAYS_VISIBLE are always
Loading