Consolidate KeyboardTextsSet.setLocale and loadStringResoucres

Change-Id: Idaf8f0d80fdeef4f1430f11d4083366e8ab7f6e0
main
Tadashi G. Takaoka 2014-03-20 17:02:12 +09:00
parent 0c268b08b6
commit df39eb6961
6 changed files with 21 additions and 57 deletions

View File

@ -163,7 +163,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mCurrentSettingsValues = settingsValues; mCurrentSettingsValues = settingsValues;
try { try {
mState.onLoadKeyboard(); mState.onLoadKeyboard();
mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocale()); mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocale(), mThemeContext);
} catch (KeyboardLayoutSetException e) { } catch (KeyboardLayoutSetException e) {
Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause()); Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause());
LatinImeLogger.logOnException(e.mKeyboardId.toString(), e.getCause()); LatinImeLogger.logOnException(e.mKeyboardId.toString(), e.getCause());

View File

@ -34,7 +34,6 @@ import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.StringUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils; import com.android.inputmethod.latin.utils.XmlParseUtils;
@ -45,7 +44,6 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
/** /**
* Keyboard Building helper. * Keyboard Building helper.
@ -278,18 +276,7 @@ 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 Locale locale = params.mId.mLocale; params.mTextsSet.setLocale(params.mId.mLocale, mContext);
params.mTextsSet.setLocale(locale);
final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override
protected Void job(final Resources res) {
params.mTextsSet.loadStringResources(mContext);
return null;
}
};
// Null means the current system locale.
job.runInLocale(mResources,
SubtypeLocaleUtils.isNoLanguage(params.mId.mSubtype) ? null : locale);
final int resourceId = keyboardAttr.getResourceId( final int resourceId = keyboardAttr.getResourceId(
R.styleable.Keyboard_touchPositionCorrectionData, 0); R.styleable.Keyboard_touchPositionCorrectionData, 0);

View File

@ -23,6 +23,8 @@ import android.text.TextUtils;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
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.RunInLocale;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
@ -38,17 +40,22 @@ public final class KeyboardTextsSet {
// 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 setLocale(final Locale locale) { public void setLocale(final Locale locale, final Context context) {
final String language = locale.getLanguage(); final String language = locale.getLanguage();
mTextsTable = KeyboardTextsTable.getTextsTable(language); mTextsTable = KeyboardTextsTable.getTextsTable(language);
}
// TODO: Consolidate this method with {@link #setLocale(Locale)}.
public void loadStringResources(final Context context) {
final Resources res = context.getResources(); final Resources res = context.getResources();
final int referenceId = context.getApplicationInfo().labelRes; final int referenceId = context.getApplicationInfo().labelRes;
final String resourcePackageName = res.getResourcePackageName(referenceId); final String resourcePackageName = res.getResourcePackageName(referenceId);
loadStringResourcesInternal(res, RESOURCE_NAMES, resourcePackageName); final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override
protected Void job(final Resources resource) {
loadStringResourcesInternal(res, RESOURCE_NAMES, resourcePackageName);
return null;
}
};
// Null means the current system locale.
job.runInLocale(res,
SubtypeLocaleUtils.NO_LANGUAGE.equals(locale.toString()) ? null : locale);
} }
@UsedForTesting @UsedForTesting
@ -129,7 +136,7 @@ public final class KeyboardTextsSet {
// These texts' name should be aligned with the @string/<name> in // These texts' name should be aligned with the @string/<name> in
// values*/strings-action-keys.xml. // values*/strings-action-keys.xml.
private static final String[] RESOURCE_NAMES = { static final String[] RESOURCE_NAMES = {
// Labels for action. // Labels for action.
"label_go_key", "label_go_key",
"label_send_key", "label_send_key",

View File

@ -22,12 +22,8 @@ import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.PREFIX_
import static com.android.inputmethod.latin.Constants.CODE_OUTPUT_TEXT; import static com.android.inputmethod.latin.Constants.CODE_OUTPUT_TEXT;
import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED;
import android.content.Context;
import android.content.res.Resources;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import com.android.inputmethod.latin.utils.RunInLocale;
import java.util.Locale; import java.util.Locale;
abstract class KeySpecParserTestsBase extends AndroidTestCase { abstract class KeySpecParserTestsBase extends AndroidTestCase {
@ -51,16 +47,7 @@ abstract class KeySpecParserTestsBase extends AndroidTestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
mTextsSet.setLocale(TEST_LOCALE); mTextsSet.setLocale(TEST_LOCALE, getContext());
final Context context = getContext();
new RunInLocale<Void>() {
@Override
protected Void job(final Resources res) {
mTextsSet.loadStringResources(context);
return null;
}
}.runInLocale(context.getResources(), TEST_LOCALE);
mCodeSettings = KeyboardCodesSet.getCode(CODE_SETTINGS_NAME); mCodeSettings = KeyboardCodesSet.getCode(CODE_SETTINGS_NAME);
mCodeActionNext = KeyboardCodesSet.getCode("key_action_next"); mCodeActionNext = KeyboardCodesSet.getCode("key_action_next");
mSettingsIconId = KeyboardIconsSet.getIconId(ICON_SETTINGS_NAME); mSettingsIconId = KeyboardIconsSet.getIconId(ICON_SETTINGS_NAME);

View File

@ -17,7 +17,6 @@
package com.android.inputmethod.keyboard.internal; package com.android.inputmethod.keyboard.internal;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
@ -25,7 +24,6 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.RichInputMethodManager;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -58,10 +56,11 @@ public final class KeyboardTextsSetTests extends AndroidTestCase {
// subtypes. The text is needed to implement Emoji Keyboard, see // subtypes. The text is needed to implement Emoji Keyboard, see
// {@link KeyboardSwitcher#setEmojiKeyboard()}. // {@link KeyboardSwitcher#setEmojiKeyboard()}.
public void testSwitchToAlphaKeyLabel() { public void testSwitchToAlphaKeyLabel() {
final Context context = getContext();
final KeyboardTextsSet textsSet = new KeyboardTextsSet(); final KeyboardTextsSet textsSet = new KeyboardTextsSet();
for (final InputMethodSubtype subtype : mAllSubtypesList) { for (final InputMethodSubtype subtype : mAllSubtypesList) {
final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
textsSet.setLocale(locale); textsSet.setLocale(locale, context);
final String switchToAlphaKeyLabel = textsSet.getText( final String switchToAlphaKeyLabel = textsSet.getText(
KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL); KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL);
assertNotNull("Switch to alpha key label of " + locale, switchToAlphaKeyLabel); assertNotNull("Switch to alpha key label of " + locale, switchToAlphaKeyLabel);
@ -87,15 +86,7 @@ public final class KeyboardTextsSetTests extends AndroidTestCase {
final KeyboardTextsSet textsSet = new KeyboardTextsSet(); final KeyboardTextsSet textsSet = new KeyboardTextsSet();
for (final InputMethodSubtype subtype : mAllSubtypesList) { for (final InputMethodSubtype subtype : mAllSubtypesList) {
final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
textsSet.setLocale(locale); textsSet.setLocale(locale, context);
final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override
protected Void job(final Resources res) {
textsSet.loadStringResources(context);
return null;
}
};
job.runInLocale(context.getResources(), locale);
for (final String name : TEXT_NAMES_FROM_RESOURCE) { for (final String name : TEXT_NAMES_FROM_RESOURCE) {
final String text = textsSet.getText(name); final String text = textsSet.getText(name);
assertNotNull(name + " of " + locale, text); assertNotNull(name + " of " + locale, text);

View File

@ -23,7 +23,6 @@ import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.MediumTest;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.RunInLocale;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,14 +40,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.setLocale(TEST_LOCALE); mTextsSet.setLocale(TEST_LOCALE, targetContext);
new RunInLocale<Void>() {
@Override
protected Void job(final Resources res) {
mTextsSet.loadStringResources(targetContext);
return null;
}
}.runInLocale(targetContext.getResources(), TEST_LOCALE);
final String[] testResourceNames = getAllResourceIdNames( final String[] testResourceNames = getAllResourceIdNames(
com.android.inputmethod.latin.tests.R.string.class); com.android.inputmethod.latin.tests.R.string.class);
final Context testContext = instrumentation.getContext(); final Context testContext = instrumentation.getContext();