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

Commit 0e8f819f authored by Antonio Kantek's avatar Antonio Kantek Committed by Android (Google) Code Review
Browse files

Merge "Remove duplicated code in InputMethodSubtypeSwitchingController" into main

parents 573c9bdc d24bd53f
Loading
Loading
Loading
Loading
+9 −18
Original line number Diff line number Diff line
@@ -69,27 +69,16 @@ final class InputMethodSubtypeSwitchingController {
                    mIsSystemLanguage = true;
                } else {
                    // TODO: Use Locale#getLanguage or Locale#toLanguageTag
                    final String systemLanguage = parseLanguageFromLocaleString(systemLocale);
                    final String subtypeLanguage = parseLanguageFromLocaleString(subtypeLocale);
                    final String systemLanguage = LocaleUtils.getLanguageFromLocaleString(
                            systemLocale);
                    final String subtypeLanguage = LocaleUtils.getLanguageFromLocaleString(
                            subtypeLocale);
                    mIsSystemLanguage = systemLanguage.length() >= 2
                            && systemLanguage.equals(subtypeLanguage);
                }
            }
        }

        /**
         * Returns the language component of a given locale string.
         * TODO: Use {@link Locale#getLanguage()} instead.
         */
        private static String parseLanguageFromLocaleString(final String locale) {
            final int idx = locale.indexOf('_');
            if (idx < 0) {
                return locale;
            } else {
                return locale.substring(0, idx);
            }
        }

        private static int compareNullableCharSequences(@Nullable CharSequence c1,
                @Nullable CharSequence c2) {
            // For historical reasons, an empty text needs to put at the last.
@@ -253,6 +242,7 @@ final class InputMethodSubtypeSwitchingController {

        /**
         * Returns the index of the specified input method and subtype in the given list.
         *
         * @param imi     The {@link InputMethodInfo} to be searched.
         * @param subtype The {@link InputMethodSubtype} to be searched. null if the input method
         *                does not have a subtype.
@@ -327,6 +317,7 @@ final class InputMethodSubtypeSwitchingController {
         * {@link #mUsageHistoryOfSubtypeListItemIndex}.
         * <p>We call the index of {@link #mUsageHistoryOfSubtypeListItemIndex} as "Usage Rank"
         * so as not to be confused with the index in {@link #mImeSubtypeList}.
         *
         * @return -1 when the specified item doesn't belong to {@link #mImeSubtypeList} actually.
         */
        private int getUsageRank(final InputMethodInfo imi, InputMethodSubtype subtype) {
+1 −6
Original line number Diff line number Diff line
@@ -215,12 +215,7 @@ final class LocaleUtils {
     * TODO: Use {@link Locale#toLanguageTag()} and {@link Locale#forLanguageTag(String)}
     */
    static String getLanguageFromLocaleString(String locale) {
        final int idx = locale.indexOf('_');
        if (idx < 0) {
            return locale;
        } else {
            return locale.substring(0, idx);
        }
        return Locale.forLanguageTag(locale).getLanguage();
    }

    static Locale getSystemLocaleFromContext(Context context) {
+8 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.inputmethod;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;

import android.os.LocaleList;
@@ -386,4 +388,10 @@ public class LocaleUtilsTest {
            assertEquals(availableLocales.get(3), dest.get(0));
        }
    }

    @Test
    public void testGetLanguageFromLocaleString() {
        assertThat(LocaleUtils.getLanguageFromLocaleString("en")).isEqualTo("en");
        assertThat(LocaleUtils.getLanguageFromLocaleString("en-US")).isEqualTo("en");
    }
}