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

Commit d6e367ff authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Move isRtlLanguage method to LocaleUtils

Change-Id: I485a076b81927fed0da593216fea2c740449ef4c
parent 63e07676
Loading
Loading
Loading
Loading
+42 −15
Original line number Diff line number Diff line
@@ -17,8 +17,12 @@
package com.android.inputmethod.latin.common;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * A class to help with handling Locales in string form.
 *
@@ -160,26 +164,49 @@ public final class LocaleUtils {

    /**
     * Creates a locale from a string specification.
     * @param localeString a string specification of a locale, in a format of "ll_cc_variant" where
     * "ll" is a language code, "cc" is a country code.
     */
    public static Locale constructLocaleFromString(final String localeStr) {
        if (localeStr == null)
    @Nullable
    public static Locale constructLocaleFromString(@Nullable final String localeString) {
        if (localeString == null) {
            return null;
        }
        synchronized (sLocaleCache) {
            if (sLocaleCache.containsKey(localeStr))
                return sLocaleCache.get(localeStr);
            Locale retval = null;
            String[] localeParams = localeStr.split("_", 3);
            if (localeParams.length == 1) {
                retval = new Locale(localeParams[0]);
            } else if (localeParams.length == 2) {
                retval = new Locale(localeParams[0], localeParams[1]);
            } else if (localeParams.length == 3) {
                retval = new Locale(localeParams[0], localeParams[1], localeParams[2]);
            if (sLocaleCache.containsKey(localeString)) {
                return sLocaleCache.get(localeString);
            }
            final String[] elements = localeString.split("_", 3);
            final Locale locale;
            if (elements.length == 1) {
                locale = new Locale(elements[0] /* language */);
            } else if (elements.length == 2) {
                locale = new Locale(elements[0] /* language */, elements[1] /* country */);
            } else { // localeParams.length == 3
                locale = new Locale(elements[0] /* language */, elements[1] /* country */,
                        elements[2] /* variant */);
            }
            if (retval != null) {
                sLocaleCache.put(localeStr, retval);
            sLocaleCache.put(localeString, locale);
            return locale;
        }
            return retval;
    }

    // TODO: Get this information from the framework instead of maintaining here by ourselves.
    private static final HashSet<String> sRtlLanguageCodes = new HashSet<>();
    static {
        // List of known Right-To-Left language codes.
        sRtlLanguageCodes.add("ar"); // Arabic
        sRtlLanguageCodes.add("fa"); // Persian
        sRtlLanguageCodes.add("iw"); // Hebrew
        sRtlLanguageCodes.add("ku"); // Kurdish
        sRtlLanguageCodes.add("ps"); // Pashto
        sRtlLanguageCodes.add("sd"); // Sindhi
        sRtlLanguageCodes.add("ug"); // Uyghur
        sRtlLanguageCodes.add("ur"); // Urdu
        sRtlLanguageCodes.add("yi"); // Yiddish
    }

    public static boolean isRtlLanguage(@Nonnull final Locale locale) {
        return sRtlLanguageCodes.contains(locale.getLanguage());
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.common.StringUtils;
import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils.ParseException;

+1 −1
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ public final class RichInputMethodSubtype {

    public boolean isRtlSubtype() {
        // The subtype is considered RTL if the language of the main subtype is RTL.
        return SubtypeLocaleUtils.isRtlLanguage(mLocales[0]);
        return LocaleUtils.isRtlLanguage(mLocales[0]);
    }

    // TODO: remove this method
+2 −3
Original line number Diff line number Diff line
@@ -50,10 +50,10 @@ import com.android.inputmethod.latin.PunctuationSuggestions;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.common.LocaleUtils;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.ViewLayoutUtils;

import java.util.ArrayList;
@@ -570,8 +570,7 @@ final class SuggestionStripLayoutHelper {
            final boolean isRtlLanguage = (ViewCompat.getLayoutDirection(addToDictionaryStrip)
                    == ViewCompat.LAYOUT_DIRECTION_RTL);
            final String arrow = isRtlLanguage ? RIGHTWARDS_ARROW : LEFTWARDS_ARROW;
            final boolean isRtlSystem = SubtypeLocaleUtils.isRtlLanguage(
                    res.getConfiguration().locale);
            final boolean isRtlSystem = LocaleUtils.isRtlLanguage(res.getConfiguration().locale);
            final CharSequence hint = res.getText(R.string.hint_add_to_dictionary);
            hintText = (isRtlLanguage == isRtlSystem) ? (arrow + hint) : (hint + arrow);
            hintWidth = width - wordWidth;
+0 −18
Original line number Diff line number Diff line
@@ -27,11 +27,9 @@ import android.util.Log;
import android.view.inputmethod.InputMethodSubtype;

import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodSubtype;
import com.android.inputmethod.latin.common.LocaleUtils;
import com.android.inputmethod.latin.common.StringUtils;

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

@@ -347,22 +345,6 @@ public final class SubtypeLocaleUtils {
        return keyboardLayoutSet;
    }

    // TODO: Get this information from the framework instead of maintaining here by ourselves.
    // Sorted list of known Right-To-Left language codes.
    private static final String[] SORTED_RTL_LANGUAGES = {
        "ar", // Arabic
        "fa", // Persian
        "iw", // Hebrew
    };
    static {
        Arrays.sort(SORTED_RTL_LANGUAGES);
    }

    public static boolean isRtlLanguage(final Locale locale) {
        final String language = locale.getLanguage();
        return Arrays.binarySearch(SORTED_RTL_LANGUAGES, language) >= 0;
    }

    public static String getCombiningRulesExtraValue(final InputMethodSubtype subtype) {
        return subtype.getExtraValueOf(COMBINING_RULES);
    }