Move isRtlLanguage method to LocaleUtils

Change-Id: I485a076b81927fed0da593216fea2c740449ef4c
This commit is contained in:
Tadashi G. Takaoka 2014-11-12 16:14:34 -08:00
parent 63e076764b
commit d6e367ff5b
5 changed files with 45 additions and 38 deletions

View File

@ -17,8 +17,12 @@
package com.android.inputmethod.latin.common; package com.android.inputmethod.latin.common;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* A class to help with handling Locales in string form. * 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. * 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) { @Nullable
if (localeStr == null) public static Locale constructLocaleFromString(@Nullable final String localeString) {
if (localeString == null) {
return null; return null;
}
synchronized (sLocaleCache) { synchronized (sLocaleCache) {
if (sLocaleCache.containsKey(localeStr)) if (sLocaleCache.containsKey(localeString)) {
return sLocaleCache.get(localeStr); return sLocaleCache.get(localeString);
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 (retval != null) { final String[] elements = localeString.split("_", 3);
sLocaleCache.put(localeStr, retval); 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 */);
} }
return retval; sLocaleCache.put(localeString, locale);
return locale;
} }
} }
// 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());
}
} }

View File

@ -36,7 +36,6 @@ import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.common.Constants; import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.common.StringUtils;
import com.android.inputmethod.latin.utils.ResourceUtils; 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;
import com.android.inputmethod.latin.utils.XmlParseUtils.ParseException; import com.android.inputmethod.latin.utils.XmlParseUtils.ParseException;

View File

@ -135,7 +135,7 @@ public final class RichInputMethodSubtype {
public boolean isRtlSubtype() { public boolean isRtlSubtype() {
// The subtype is considered RTL if the language of the main subtype is RTL. // 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 // TODO: remove this method

View File

@ -50,10 +50,10 @@ import com.android.inputmethod.latin.PunctuationSuggestions;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 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.Settings;
import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.ViewLayoutUtils; import com.android.inputmethod.latin.utils.ViewLayoutUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -570,8 +570,7 @@ final class SuggestionStripLayoutHelper {
final boolean isRtlLanguage = (ViewCompat.getLayoutDirection(addToDictionaryStrip) final boolean isRtlLanguage = (ViewCompat.getLayoutDirection(addToDictionaryStrip)
== ViewCompat.LAYOUT_DIRECTION_RTL); == ViewCompat.LAYOUT_DIRECTION_RTL);
final String arrow = isRtlLanguage ? RIGHTWARDS_ARROW : LEFTWARDS_ARROW; final String arrow = isRtlLanguage ? RIGHTWARDS_ARROW : LEFTWARDS_ARROW;
final boolean isRtlSystem = SubtypeLocaleUtils.isRtlLanguage( final boolean isRtlSystem = LocaleUtils.isRtlLanguage(res.getConfiguration().locale);
res.getConfiguration().locale);
final CharSequence hint = res.getText(R.string.hint_add_to_dictionary); final CharSequence hint = res.getText(R.string.hint_add_to_dictionary);
hintText = (isRtlLanguage == isRtlSystem) ? (arrow + hint) : (hint + arrow); hintText = (isRtlLanguage == isRtlSystem) ? (arrow + hint) : (hint + arrow);
hintWidth = width - wordWidth; hintWidth = width - wordWidth;

View File

@ -27,11 +27,9 @@ import android.util.Log;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.R; 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.LocaleUtils;
import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.common.StringUtils;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
@ -347,22 +345,6 @@ public final class SubtypeLocaleUtils {
return keyboardLayoutSet; 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) { public static String getCombiningRulesExtraValue(final InputMethodSubtype subtype) {
return subtype.getExtraValueOf(COMBINING_RULES); return subtype.getExtraValueOf(COMBINING_RULES);
} }