Use SubtypeLocaleUtils.isRtlLanguage

Change-Id: Ic0208412cd9c02975641cb5c448d092f28786746
main
Tadashi G. Takaoka 2014-02-06 15:43:00 +09:00
parent 48ea52800a
commit 3bbd50c36b
6 changed files with 26 additions and 40 deletions

View File

@ -276,9 +276,9 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0); params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0);
params.mIconsSet.loadIcons(keyboardAttr); params.mIconsSet.loadIcons(keyboardAttr);
final String language = params.mId.mLocale.getLanguage(); final Locale locale = params.mId.mLocale;
params.mCodesSet.setLanguage(language); params.mCodesSet.setLocale(locale);
params.mTextsSet.setLanguage(language); params.mTextsSet.setLocale(locale);
final RunInLocale<Void> job = new RunInLocale<Void>() { final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override @Override
protected Void job(final Resources res) { protected Void job(final Resources res) {
@ -287,9 +287,8 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} }
}; };
// Null means the current system locale. // Null means the current system locale.
final Locale locale = SubtypeLocaleUtils.isNoLanguage(params.mId.mSubtype) job.runInLocale(mResources,
? null : params.mId.mLocale; SubtypeLocaleUtils.isNoLanguage(params.mId.mSubtype) ? null : locale);
job.runInLocale(mResources, locale);
final int resourceId = keyboardAttr.getResourceId( final int resourceId = keyboardAttr.getResourceId(
R.styleable.Keyboard_touchPositionCorrectionData, 0); R.styleable.Keyboard_touchPositionCorrectionData, 0);

View File

@ -18,20 +18,20 @@ package com.android.inputmethod.keyboard.internal;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
public final class KeyboardCodesSet { public final class KeyboardCodesSet {
public static final String PREFIX_CODE = "!code/"; public static final String PREFIX_CODE = "!code/";
private static final HashMap<String, int[]> sLanguageToCodesMap = CollectionUtils.newHashMap();
private static final HashMap<String, Integer> sNameToIdMap = CollectionUtils.newHashMap(); private static final HashMap<String, Integer> sNameToIdMap = CollectionUtils.newHashMap();
private int[] mCodes = DEFAULT; private int[] mCodes = DEFAULT;
public void setLanguage(final String language) { public void setLocale(final Locale locale) {
final int[] codes = sLanguageToCodesMap.get(language); mCodes = SubtypeLocaleUtils.isRtlLanguage(locale) ? RTL : DEFAULT;
mCodes = (codes != null) ? codes : DEFAULT;
} }
public int getCode(final String name) { public int getCode(final String name) {
@ -134,18 +134,6 @@ public final class KeyboardCodesSet {
CODE_LEFT_CURLY_BRACKET, CODE_LEFT_CURLY_BRACKET,
}; };
private static final String LANGUAGE_DEFAULT = "DEFAULT";
private static final String LANGUAGE_ARABIC = "ar";
private static final String LANGUAGE_PERSIAN = "fa";
private static final String LANGUAGE_HEBREW = "iw";
private static final Object[] LANGUAGE_AND_CODES = {
LANGUAGE_DEFAULT, DEFAULT,
LANGUAGE_ARABIC, RTL,
LANGUAGE_PERSIAN, RTL,
LANGUAGE_HEBREW, RTL,
};
static { static {
if (DEFAULT.length != RTL.length || DEFAULT.length != ID_TO_NAME.length) { if (DEFAULT.length != RTL.length || DEFAULT.length != ID_TO_NAME.length) {
throw new RuntimeException("Internal inconsistency"); throw new RuntimeException("Internal inconsistency");
@ -153,11 +141,5 @@ public final class KeyboardCodesSet {
for (int i = 0; i < ID_TO_NAME.length; i++) { for (int i = 0; i < ID_TO_NAME.length; i++) {
sNameToIdMap.put(ID_TO_NAME[i], i); sNameToIdMap.put(ID_TO_NAME[i], i);
} }
for (int i = 0; i < LANGUAGE_AND_CODES.length; i += 2) {
final String language = (String)LANGUAGE_AND_CODES[i];
final int[] codes = (int[])LANGUAGE_AND_CODES[i + 1];
sLanguageToCodesMap.put(language, codes);
}
} }
} }

View File

@ -25,6 +25,7 @@ import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
/** /**
* !!!!! DO NOT EDIT THIS FILE !!!!! * !!!!! DO NOT EDIT THIS FILE !!!!!
@ -52,15 +53,17 @@ public final class KeyboardTextsSet {
private static final int MAX_STRING_REFERENCE_INDIRECTION = 10; private static final int MAX_STRING_REFERENCE_INDIRECTION = 10;
// Language to texts map. // Language to texts map.
private static final HashMap<String, String[]> sLocaleToTextsMap = CollectionUtils.newHashMap(); private static final HashMap<String, String[]> sLanguageToTextsMap =
CollectionUtils.newHashMap();
private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap(); private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
private String[] mTexts; private String[] mTexts;
// Resource name to text map. // Resource name to text map.
private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap(); private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap();
public void setLanguage(final String language) { public void setLocale(final Locale locale) {
mTexts = sLocaleToTextsMap.get(language); final String language = locale.getLanguage();
mTexts = sLanguageToTextsMap.get(language);
if (mTexts == null) { if (mTexts == null) {
mTexts = LANGUAGE_DEFAULT; mTexts = LANGUAGE_DEFAULT;
} }
@ -3647,7 +3650,7 @@ public final class KeyboardTextsSet {
for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
final String language = (String)LANGUAGES_AND_TEXTS[i]; final String language = (String)LANGUAGES_AND_TEXTS[i];
final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1];
sLocaleToTextsMap.put(language, texts); sLanguageToTextsMap.put(language, texts);
} }
} }
} }

View File

@ -52,9 +52,8 @@ abstract class KeySpecParserTestsBase extends AndroidTestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
final String language = TEST_LOCALE.getLanguage(); mCodesSet.setLocale(TEST_LOCALE);
mCodesSet.setLanguage(language); mTextsSet.setLocale(TEST_LOCALE);
mTextsSet.setLanguage(language);
final Context context = getContext(); final Context context = getContext();
new RunInLocale<Void>() { new RunInLocale<Void>() {
@Override @Override

View File

@ -41,7 +41,7 @@ public class MoreKeySpecSplitTests extends InstrumentationTestCase {
final Instrumentation instrumentation = getInstrumentation(); final Instrumentation instrumentation = getInstrumentation();
final Context targetContext = instrumentation.getTargetContext(); final Context targetContext = instrumentation.getTargetContext();
mTextsSet.setLanguage(TEST_LOCALE.getLanguage()); mTextsSet.setLocale(TEST_LOCALE);
new RunInLocale<Void>() { new RunInLocale<Void>() {
@Override @Override
protected Void job(final Resources res) { protected Void job(final Resources res) {

View File

@ -25,6 +25,7 @@ import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
/** /**
* !!!!! DO NOT EDIT THIS FILE !!!!! * !!!!! DO NOT EDIT THIS FILE !!!!!
@ -52,15 +53,17 @@ public final class KeyboardTextsSet {
private static final int MAX_STRING_REFERENCE_INDIRECTION = 10; private static final int MAX_STRING_REFERENCE_INDIRECTION = 10;
// Language to texts map. // Language to texts map.
private static final HashMap<String, String[]> sLocaleToTextsMap = CollectionUtils.newHashMap(); private static final HashMap<String, String[]> sLanguageToTextsMap =
CollectionUtils.newHashMap();
private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap(); private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
private String[] mTexts; private String[] mTexts;
// Resource name to text map. // Resource name to text map.
private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap(); private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap();
public void setLanguage(final String language) { public void setLocale(final Locale locale) {
mTexts = sLocaleToTextsMap.get(language); final String language = locale.getLanguage();
mTexts = sLanguageToTextsMap.get(language);
if (mTexts == null) { if (mTexts == null) {
mTexts = LANGUAGE_DEFAULT; mTexts = LANGUAGE_DEFAULT;
} }
@ -195,7 +198,7 @@ public final class KeyboardTextsSet {
for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
final String language = (String)LANGUAGES_AND_TEXTS[i]; final String language = (String)LANGUAGES_AND_TEXTS[i];
final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1];
sLocaleToTextsMap.put(language, texts); sLanguageToTextsMap.put(language, texts);
} }
} }
} }