From df39eb696152954cca5bdac4cdc4ea6650db889f Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 20 Mar 2014 17:02:12 +0900 Subject: [PATCH] Consolidate KeyboardTextsSet.setLocale and loadStringResoucres Change-Id: Idaf8f0d80fdeef4f1430f11d4083366e8ab7f6e0 --- .../keyboard/KeyboardSwitcher.java | 2 +- .../keyboard/internal/KeyboardBuilder.java | 15 +------------ .../keyboard/internal/KeyboardTextsSet.java | 21 ++++++++++++------- .../internal/KeySpecParserTestsBase.java | 15 +------------ .../internal/KeyboardTextsSetTests.java | 15 +++---------- .../internal/MoreKeySpecSplitTests.java | 10 +-------- 6 files changed, 21 insertions(+), 57 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 66cb9e35d..2dfde9434 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -163,7 +163,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { mCurrentSettingsValues = settingsValues; try { mState.onLoadKeyboard(); - mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocale()); + mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocale(), mThemeContext); } catch (KeyboardLayoutSetException e) { Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause()); LatinImeLogger.logOnException(e.mKeyboardId.toString(), e.getCause()); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java index 81a8e7196..dfe0df04c 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java @@ -34,7 +34,6 @@ import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.R; 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.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.XmlParseUtils; @@ -45,7 +44,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.Arrays; -import java.util.Locale; /** * Keyboard Building helper. @@ -278,18 +276,7 @@ public class KeyboardBuilder { params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0); params.mIconsSet.loadIcons(keyboardAttr); - final Locale locale = params.mId.mLocale; - params.mTextsSet.setLocale(locale); - final RunInLocale job = new RunInLocale() { - @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); + params.mTextsSet.setLocale(params.mId.mLocale, mContext); final int resourceId = keyboardAttr.getResourceId( R.styleable.Keyboard_touchPositionCorrectionData, 0); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java index bdc36ed45..044cd119e 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java @@ -23,6 +23,8 @@ import android.text.TextUtils; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.Constants; 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.Locale; @@ -38,17 +40,22 @@ public final class KeyboardTextsSet { // Resource name to text map. private HashMap mResourceNameToTextsMap = CollectionUtils.newHashMap(); - public void setLocale(final Locale locale) { + public void setLocale(final Locale locale, final Context context) { final String language = locale.getLanguage(); mTextsTable = KeyboardTextsTable.getTextsTable(language); - } - - // TODO: Consolidate this method with {@link #setLocale(Locale)}. - public void loadStringResources(final Context context) { final Resources res = context.getResources(); final int referenceId = context.getApplicationInfo().labelRes; final String resourcePackageName = res.getResourcePackageName(referenceId); - loadStringResourcesInternal(res, RESOURCE_NAMES, resourcePackageName); + final RunInLocale job = new RunInLocale() { + @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 @@ -129,7 +136,7 @@ public final class KeyboardTextsSet { // These texts' name should be aligned with the @string/ in // values*/strings-action-keys.xml. - private static final String[] RESOURCE_NAMES = { + static final String[] RESOURCE_NAMES = { // Labels for action. "label_go_key", "label_send_key", diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java index c342533c8..7923afc1a 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java @@ -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_UNSPECIFIED; -import android.content.Context; -import android.content.res.Resources; import android.test.AndroidTestCase; -import com.android.inputmethod.latin.utils.RunInLocale; - import java.util.Locale; abstract class KeySpecParserTestsBase extends AndroidTestCase { @@ -51,16 +47,7 @@ abstract class KeySpecParserTestsBase extends AndroidTestCase { protected void setUp() throws Exception { super.setUp(); - mTextsSet.setLocale(TEST_LOCALE); - final Context context = getContext(); - new RunInLocale() { - @Override - protected Void job(final Resources res) { - mTextsSet.loadStringResources(context); - return null; - } - }.runInLocale(context.getResources(), TEST_LOCALE); - + mTextsSet.setLocale(TEST_LOCALE, getContext()); mCodeSettings = KeyboardCodesSet.getCode(CODE_SETTINGS_NAME); mCodeActionNext = KeyboardCodesSet.getCode("key_action_next"); mSettingsIconId = KeyboardIconsSet.getIconId(ICON_SETTINGS_NAME); diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSetTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSetTests.java index 17d768718..eb906cd9f 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSetTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSetTests.java @@ -17,7 +17,6 @@ package com.android.inputmethod.keyboard.internal; import android.content.Context; -import android.content.res.Resources; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; 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.utils.CollectionUtils; -import com.android.inputmethod.latin.utils.RunInLocale; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.ArrayList; @@ -58,10 +56,11 @@ public final class KeyboardTextsSetTests extends AndroidTestCase { // subtypes. The text is needed to implement Emoji Keyboard, see // {@link KeyboardSwitcher#setEmojiKeyboard()}. public void testSwitchToAlphaKeyLabel() { + final Context context = getContext(); final KeyboardTextsSet textsSet = new KeyboardTextsSet(); for (final InputMethodSubtype subtype : mAllSubtypesList) { final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); - textsSet.setLocale(locale); + textsSet.setLocale(locale, context); final String switchToAlphaKeyLabel = textsSet.getText( KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL); assertNotNull("Switch to alpha key label of " + locale, switchToAlphaKeyLabel); @@ -87,15 +86,7 @@ public final class KeyboardTextsSetTests extends AndroidTestCase { final KeyboardTextsSet textsSet = new KeyboardTextsSet(); for (final InputMethodSubtype subtype : mAllSubtypesList) { final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); - textsSet.setLocale(locale); - final RunInLocale job = new RunInLocale() { - @Override - protected Void job(final Resources res) { - textsSet.loadStringResources(context); - return null; - } - }; - job.runInLocale(context.getResources(), locale); + textsSet.setLocale(locale, context); for (final String name : TEXT_NAMES_FROM_RESOURCE) { final String text = textsSet.getText(name); assertNotNull(name + " of " + locale, text); diff --git a/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecSplitTests.java b/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecSplitTests.java index d01c2ec79..a1933076a 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecSplitTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecSplitTests.java @@ -23,7 +23,6 @@ import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.MediumTest; import com.android.inputmethod.latin.utils.CollectionUtils; -import com.android.inputmethod.latin.utils.RunInLocale; import java.lang.reflect.Field; import java.util.ArrayList; @@ -41,14 +40,7 @@ public class MoreKeySpecSplitTests extends InstrumentationTestCase { final Instrumentation instrumentation = getInstrumentation(); final Context targetContext = instrumentation.getTargetContext(); - mTextsSet.setLocale(TEST_LOCALE); - new RunInLocale() { - @Override - protected Void job(final Resources res) { - mTextsSet.loadStringResources(targetContext); - return null; - } - }.runInLocale(targetContext.getResources(), TEST_LOCALE); + mTextsSet.setLocale(TEST_LOCALE, targetContext); final String[] testResourceNames = getAllResourceIdNames( com.android.inputmethod.latin.tests.R.string.class); final Context testContext = instrumentation.getContext();