From faefad5b0fd5b188d7eefa66dfaf33c47e181c03 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Fri, 3 Oct 2014 23:48:53 +0000 Subject: [PATCH] Revert "[ML14] Forward the locale list to relevant places" This reverts commit 15dbd38283fc9c8fdfd2540f1d94d4dbf2eabcb5. Bug: 11230254 Reverting due to unit test breakage. Change-Id: Icf29ba5c808c754515aef2d1b4162d72f606acfe --- .../com/android/inputmethod/keyboard/Key.java | 18 ++++---- .../inputmethod/keyboard/KeyboardId.java | 9 ++-- .../keyboard/KeyboardSwitcher.java | 6 +-- .../keyboard/MainKeyboardView.java | 5 --- .../keyboard/internal/KeyboardBuilder.java | 31 +++---------- .../internal/LanguageOnSpacebarHelper.java | 8 +--- .../latin/DictionaryFacilitator.java | 43 ++++++++---------- .../android/inputmethod/latin/LatinIME.java | 44 +++++++++---------- .../latin/RichInputMethodSubtype.java | 11 ++--- .../inputmethod/latin/SubtypeSwitcher.java | 30 +++++-------- .../latin/utils/SubtypeLocaleUtils.java | 11 +++++ .../ContextualDictionaryTests.java | 5 +-- .../PersonalizationDictionaryTests.java | 5 +-- .../utils/SpacebarLanguageUtilsTests.java | 24 +++------- .../latin/utils/SubtypeLocaleUtilsTests.java | 23 ++++------ 15 files changed, 110 insertions(+), 163 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 45ce6a85f..bf29b5ffe 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -278,8 +278,8 @@ public class Key implements Comparable { mLabelFlags = style.getFlags(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags) | row.getDefaultKeyLabelFlags(); - final boolean needsToUpcase = needsToUpcase(mLabelFlags, params.mId.mElementId); - final Locale localeForUpcasing = params.mId.getLocales()[0]; + final boolean needsToUpperCase = needsToUpperCase(mLabelFlags, params.mId.mElementId); + final Locale locale = params.mId.mLocale; int actionFlags = style.getFlags(keyAttr, R.styleable.Keyboard_Key_keyActionFlags); String[] moreKeys = style.getStringArray(keyAttr, R.styleable.Keyboard_Key_moreKeys); @@ -321,7 +321,7 @@ public class Key implements Comparable { actionFlags |= ACTION_FLAGS_ENABLE_LONG_PRESS; mMoreKeys = new MoreKeySpec[moreKeys.length]; for (int i = 0; i < moreKeys.length; i++) { - mMoreKeys[i] = new MoreKeySpec(moreKeys[i], needsToUpcase, localeForUpcasing); + mMoreKeys[i] = new MoreKeySpec(moreKeys[i], needsToUpperCase, locale); } } else { mMoreKeys = null; @@ -342,16 +342,16 @@ public class Key implements Comparable { mLabel = new StringBuilder().appendCodePoint(code).toString(); } else { mLabel = StringUtils.toUpperCaseOfStringForLocale( - KeySpecParser.getLabel(keySpec), needsToUpcase, localeForUpcasing); + KeySpecParser.getLabel(keySpec), needsToUpperCase, locale); } if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) { mHintLabel = null; } else { mHintLabel = StringUtils.toUpperCaseOfStringForLocale(style.getString(keyAttr, - R.styleable.Keyboard_Key_keyHintLabel), needsToUpcase, localeForUpcasing); + R.styleable.Keyboard_Key_keyHintLabel), needsToUpperCase, locale); } String outputText = StringUtils.toUpperCaseOfStringForLocale( - KeySpecParser.getOutputText(keySpec), needsToUpcase, localeForUpcasing); + KeySpecParser.getOutputText(keySpec), needsToUpperCase, locale); // Choose the first letter of the label as primary code if not specified. if (code == CODE_UNSPECIFIED && TextUtils.isEmpty(outputText) && !TextUtils.isEmpty(mLabel)) { @@ -377,12 +377,12 @@ public class Key implements Comparable { mCode = CODE_OUTPUT_TEXT; } } else { - mCode = StringUtils.toUpperCaseOfCodeForLocale(code, needsToUpcase, localeForUpcasing); + mCode = StringUtils.toUpperCaseOfCodeForLocale(code, needsToUpperCase, locale); } final int altCodeInAttr = KeySpecParser.parseCode( style.getString(keyAttr, R.styleable.Keyboard_Key_altCode), CODE_UNSPECIFIED); final int altCode = StringUtils.toUpperCaseOfCodeForLocale( - altCodeInAttr, needsToUpcase, localeForUpcasing); + altCodeInAttr, needsToUpperCase, locale); mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode, disabledIconId, visualInsetsLeft, visualInsetsRight); mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr); @@ -432,7 +432,7 @@ public class Key implements Comparable { return (filteredMoreKeys == moreKeys) ? key : new Key(key, filteredMoreKeys); } - private static boolean needsToUpcase(final int labelFlags, final int keyboardElementId) { + private static boolean needsToUpperCase(final int labelFlags, final int keyboardElementId) { if ((labelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0) return false; switch (keyboardElementId) { case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED: diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java index ab0d63306..f9cf3535e 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java @@ -63,6 +63,7 @@ public final class KeyboardId { public static final int ELEMENT_EMOJI_CATEGORY6 = 16; public final RichInputMethodSubtype mSubtype; + public final Locale mLocale; public final int mWidth; public final int mHeight; public final int mMode; @@ -78,6 +79,7 @@ public final class KeyboardId { public KeyboardId(final int elementId, final KeyboardLayoutSet.Params params) { mSubtype = params.mSubtype; + mLocale = SubtypeLocaleUtils.getSubtypeLocale(mSubtype); mWidth = params.mKeyboardWidth; mHeight = params.mKeyboardHeight; mMode = params.mMode; @@ -165,10 +167,6 @@ public final class KeyboardId { return InputTypeUtils.getImeOptionsActionIdFromEditorInfo(mEditorInfo); } - public Locale[] getLocales() { - return mSubtype.getLocales(); - } - @Override public boolean equals(final Object other) { return other instanceof KeyboardId && equals((KeyboardId) other); @@ -183,8 +181,7 @@ public final class KeyboardId { public String toString() { return String.format(Locale.ROOT, "[%s %s:%s %dx%d %s %s%s%s%s%s%s%s%s%s]", elementIdToName(mElementId), - Arrays.deepToString(mSubtype.getLocales()), - mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET), + mLocale, mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET), mWidth, mHeight, modeName(mMode), actionName(imeAction()), diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 93123d1ec..7f2957fff 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -120,9 +120,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { mKeyboardLayoutSet = builder.build(); try { mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState); - // TODO: revisit this for multi-lingual input - mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocales()[0], - mThemeContext); + mKeyboardTextsSet.setLocale(mSubtypeSwitcher.getCurrentSubtypeLocale(), mThemeContext); } catch (KeyboardLayoutSetException e) { Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause()); return; @@ -163,7 +161,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { currentSettingsValues.mKeyPreviewDismissDuration); keyboardView.updateShortcutKey(mSubtypeSwitcher.isShortcutImeReady()); final boolean subtypeChanged = (oldKeyboard == null) - || !keyboard.mId.mSubtype.equals(oldKeyboard.mId.mSubtype); + || !keyboard.mId.mLocale.equals(oldKeyboard.mId.mLocale); final int languageOnSpacebarFormatType = mSubtypeSwitcher.getLanguageOnSpacebarFormatType( keyboard.mId.mSubtype); final boolean hasMultipleEnabledIMEsOrSubtypes = RichInputMethodManager.getInstance() diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index ad30b746e..cded2cb8c 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -875,11 +875,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack // Layout language name on spacebar. private String layoutLanguageOnSpacebar(final Paint paint, final RichInputMethodSubtype subtype, final int width) { - if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarHelper.FORMAT_TYPE_MULTIPLE) { - // TODO: return an appropriate string - return ""; - } - // Choose appropriate language name to fit into the width. if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarHelper.FORMAT_TYPE_FULL_LOCALE) { final String fullText = subtype.getFullDisplayName(); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java index 031cad0a5..50385555c 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java @@ -45,7 +45,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.Arrays; -import java.util.Locale; /** * Keyboard Building helper. @@ -282,8 +281,7 @@ public class KeyboardBuilder { params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0); params.mIconsSet.loadIcons(keyboardAttr); - // TODO: this needs to be revisited for multi-lingual input. - params.mTextsSet.setLocale(params.mId.getLocales()[0], mContext); + params.mTextsSet.setLocale(params.mId.mLocale, mContext); final int resourceId = keyboardAttr.getResourceId( R.styleable.Keyboard_touchPositionCorrectionData, 0); @@ -674,10 +672,12 @@ public class KeyboardBuilder { R.styleable.Keyboard_Case_imeAction, id.imeAction()); final boolean isIconDefinedMatched = isIconDefined(caseAttr, R.styleable.Keyboard_Case_isIconDefined, mParams.mIconsSet); - final Locale[] locales = id.getLocales(); - final boolean localeCodeMatched = matchLocaleCodes(caseAttr, locales); - final boolean languageCodeMatched = matchLanguageCodes(caseAttr, locales); - final boolean countryCodeMatched = matchCountryCodes(caseAttr, locales); + final boolean localeCodeMatched = matchString(caseAttr, + R.styleable.Keyboard_Case_localeCode, id.mLocale.toString()); + final boolean languageCodeMatched = matchString(caseAttr, + R.styleable.Keyboard_Case_languageCode, id.mLocale.getLanguage()); + final boolean countryCodeMatched = matchString(caseAttr, + R.styleable.Keyboard_Case_countryCode, id.mLocale.getCountry()); final boolean splitLayoutMatched = matchBoolean(caseAttr, R.styleable.Keyboard_Case_isSplitLayout, id.mIsSplitLayout); final boolean selected = keyboardLayoutSetMatched && keyboardLayoutSetElementMatched @@ -733,23 +733,6 @@ public class KeyboardBuilder { } } - private boolean matchLocaleCodes(TypedArray caseAttr, final Locale[] locales) { - // TODO: adujst this for multilingual input - return matchString(caseAttr, R.styleable.Keyboard_Case_languageCode, locales[0].toString()); - } - - private boolean matchLanguageCodes(TypedArray caseAttr, Locale[] locales) { - // TODO: adujst this for multilingual input - return matchString(caseAttr, R.styleable.Keyboard_Case_languageCode, - locales[0].getLanguage()); - } - - private boolean matchCountryCodes(TypedArray caseAttr, Locale[] locales) { - // TODO: adujst this for multilingual input - return matchString(caseAttr, R.styleable.Keyboard_Case_countryCode, - locales[0].getCountry()); - } - private static boolean matchInteger(final TypedArray a, final int index, final int value) { // If does not have "index" attribute, that means this is wild-card for // the attribute. diff --git a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java b/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java index 21eaed950..72ad2bd97 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java +++ b/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java @@ -23,7 +23,6 @@ import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.Collections; import java.util.List; -import java.util.Locale; /** * This class determines that the language name on the spacebar should be displayed in what format. @@ -32,7 +31,6 @@ public final class LanguageOnSpacebarHelper { public static final int FORMAT_TYPE_NONE = 0; public static final int FORMAT_TYPE_LANGUAGE_ONLY = 1; public static final int FORMAT_TYPE_FULL_LOCALE = 2; - public static final int FORMAT_TYPE_MULTIPLE = 3; private List mEnabledSubtypes = Collections.emptyList(); private boolean mIsSystemLanguageSameAsInputLanguage; @@ -45,11 +43,7 @@ public final class LanguageOnSpacebarHelper { if (mEnabledSubtypes.size() < 2 && mIsSystemLanguageSameAsInputLanguage) { return FORMAT_TYPE_NONE; } - final Locale[] locales = subtype.getLocales(); - if (1 < locales.length) { - return FORMAT_TYPE_MULTIPLE; - } - final String keyboardLanguage = locales[0].getLanguage(); + final String keyboardLanguage = SubtypeLocaleUtils.getSubtypeLocale(subtype).getLanguage(); final String keyboardLayout = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); int sameLanguageAndLayoutCount = 0; for (final InputMethodSubtype ims : mEnabledSubtypes) { diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java index 4a218d550..1f0317288 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java @@ -416,38 +416,33 @@ public class DictionaryFacilitator { } @UsedForTesting - public void resetDictionariesForTesting(final Context context, final Locale[] locales, + public void resetDictionariesForTesting(final Context context, final Locale locale, final ArrayList dictionaryTypes, final HashMap dictionaryFiles, final Map> additionalDictAttributes) { Dictionary mainDictionary = null; final Map subDicts = new HashMap<>(); - final DictionaryGroup[] dictionaryGroups = new DictionaryGroup[locales.length]; - for (int i = 0; i < locales.length; ++i) { - final Locale locale = locales[i]; - for (final String dictType : dictionaryTypes) { - if (dictType.equals(Dictionary.TYPE_MAIN)) { - mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, - locale); - } else { - final File dictFile = dictionaryFiles.get(dictType); - final ExpandableBinaryDictionary dict = getSubDict( - dictType, context, locale, dictFile, "" /* dictNamePrefix */); - if (additionalDictAttributes.containsKey(dictType)) { - dict.clearAndFlushDictionaryWithAdditionalAttributes( - additionalDictAttributes.get(dictType)); - } - if (dict == null) { - throw new RuntimeException("Unknown dictionary type: " + dictType); - } - dict.reloadDictionaryIfRequired(); - dict.waitAllTasksForTests(); - subDicts.put(dictType, dict); + for (final String dictType : dictionaryTypes) { + if (dictType.equals(Dictionary.TYPE_MAIN)) { + mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, locale); + } else { + final File dictFile = dictionaryFiles.get(dictType); + final ExpandableBinaryDictionary dict = getSubDict( + dictType, context, locale, dictFile, "" /* dictNamePrefix */); + if (additionalDictAttributes.containsKey(dictType)) { + dict.clearAndFlushDictionaryWithAdditionalAttributes( + additionalDictAttributes.get(dictType)); } + if (dict == null) { + throw new RuntimeException("Unknown dictionary type: " + dictType); + } + dict.reloadDictionaryIfRequired(); + dict.waitAllTasksForTests(); + subDicts.put(dictType, dict); } - dictionaryGroups[i] = new DictionaryGroup(locale, mainDictionary, subDicts); } - mDictionaryGroups = dictionaryGroups; + mDictionaryGroups = new DictionaryGroup[] { + new DictionaryGroup(locale, mainDictionary, subDicts) }; } public void closeDictionaries() { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index a6be75943..be2efb2dc 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -102,7 +102,7 @@ import com.android.inputmethod.latin.utils.ViewLayoutUtils; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.concurrent.TimeUnit; @@ -575,19 +575,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Has to be package-visible for unit tests @UsedForTesting void loadSettings() { - final Locale[] locales = mSubtypeSwitcher.getCurrentSubtypeLocales(); + final Locale locale = mSubtypeSwitcher.getCurrentSubtypeLocale(); final EditorInfo editorInfo = getCurrentInputEditorInfo(); final InputAttributes inputAttributes = new InputAttributes( editorInfo, isFullscreenMode(), getPackageName()); - // TODO: pass the array instead - mSettings.loadSettings(this, locales[0], inputAttributes); + mSettings.loadSettings(this, locale, inputAttributes); final SettingsValues currentSettingsValues = mSettings.getCurrent(); AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(currentSettingsValues); // This method is called on startup and language switch, before the new layout has // been displayed. Opening dictionaries never affects responsivity as dictionaries are // asynchronously loaded. if (!mHandler.hasPendingReopenDictionaries()) { - resetDictionaryFacilitatorForLocale(locales); + resetDictionaryFacilitatorForLocale(locale); } mDictionaryFacilitator.updateEnabledSubtypes(mRichImm.getMyEnabledInputMethodSubtypeList( true /* allowsImplicitlySelectedSubtypes */)); @@ -629,34 +628,35 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void resetDictionaryFacilitatorIfNecessary() { - final Locale[] subtypeSwitcherLocales = mSubtypeSwitcher.getCurrentSubtypeLocales(); - if (mDictionaryFacilitator.isForLocales(subtypeSwitcherLocales)) { + final Locale switcherSubtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale(); + if (mDictionaryFacilitator.isForLocales(new Locale[] { switcherSubtypeLocale })) { return; } - final Locale[] subtypeLocales; - if (0 == subtypeSwitcherLocales.length) { + final String switcherLocaleStr = switcherSubtypeLocale.toString(); + final Locale subtypeLocale; + if (TextUtils.isEmpty(switcherLocaleStr)) { // This happens in very rare corner cases - for example, immediately after a switch // to LatinIME has been requested, about a frame later another switch happens. In this // case, we are about to go down but we still don't know it, however the system tells - // us there is no current subtype. + // us there is no current subtype so the locale is the empty string. Take the best + // possible guess instead -- it's bound to have no consequences, and we have no way + // of knowing anyway. Log.e(TAG, "System is reporting no current subtype."); - subtypeLocales = new Locale[] { getResources().getConfiguration().locale }; + subtypeLocale = getResources().getConfiguration().locale; } else { - subtypeLocales = subtypeSwitcherLocales; + subtypeLocale = switcherSubtypeLocale; } - resetDictionaryFacilitatorForLocale(subtypeLocales); + resetDictionaryFacilitatorForLocale(subtypeLocale); } /** - * Reset the facilitator by loading dictionaries for the locales and the current settings values + * Reset the facilitator by loading dictionaries for the locale and the current settings values. * - * @param locales the locales + * @param locale the locale */ - // TODO: make sure the current settings always have the right locales, and read from them - private void resetDictionaryFacilitatorForLocale(final Locale[] locales) { + // TODO: make sure the current settings always have the right locale, and read from them + private void resetDictionaryFacilitatorForLocale(final Locale locale) { final SettingsValues settingsValues = mSettings.getCurrent(); - // TODO: pass the array instead - final Locale locale = locales[0]; mDictionaryFacilitator.resetDictionaries(this /* context */, locale, settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, false /* forceReloadMainDictionary */, this); @@ -881,7 +881,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Update to a gesture consumer with the current editor and IME state. mGestureConsumer = GestureConsumer.newInstance(editorInfo, mInputLogic.getPrivateCommandPerformer(), - Arrays.asList(mSubtypeSwitcher.getCurrentSubtypeLocales()), + Collections.singletonList(mSubtypeSwitcher.getCurrentSubtypeLocale()), switcher.getKeyboard()); // Forward this event to the accessibility utilities, if enabled. @@ -1434,7 +1434,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen public void onStartBatchInput() { mInputLogic.onStartBatchInput(mSettings.getCurrent(), mKeyboardSwitcher, mHandler); mGestureConsumer.onGestureStarted( - Arrays.asList(mSubtypeSwitcher.getCurrentSubtypeLocales()), + Collections.singletonList(mSubtypeSwitcher.getCurrentSubtypeLocale()), mKeyboardSwitcher.getKeyboard()); } @@ -1558,7 +1558,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // We should clear the contextual strip if there is no suggestion from dictionaries. || noSuggestionsFromDictionaries) { mSuggestionStripView.setSuggestions(suggestedWords, - mSubtypeSwitcher.getCurrentSubtype().isRtlSubtype()); + SubtypeLocaleUtils.isRtlLanguage(mSubtypeSwitcher.getCurrentSubtype())); } } diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java b/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java index 8d055531c..0b08c48e5 100644 --- a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java +++ b/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java @@ -114,13 +114,10 @@ public final class RichInputMethodSubtype { return "Multi-lingual subtype: " + mSubtype.toString() + ", " + Arrays.toString(mLocales); } - public Locale[] getLocales() { - return mLocales; - } - - public boolean isRtlSubtype() { - // The subtype is considered RTL if the language of the main subtype is RTL. - return SubtypeLocaleUtils.isRtlLanguage(mLocales[0]); + // TODO: remove this method! We can always have several locales. Multi-lingual input will only + // be done when this method is gone. + public String getLocale() { + return mSubtype.getLocale(); } // TODO: remove this method diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index c339e96fb..45d67ff88 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -170,21 +170,15 @@ public final class SubtypeSwitcher { Log.w(TAG, "onSubtypeChanged: " + newSubtype.getNameForLogging()); } - final Locale[] newLocales = newSubtype.getLocales(); - if (newLocales.length > 1) { - // In multi-locales mode, the system language is never the same as the input language - // because there is no single input language. - mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(false); - } else { - final Locale newLocale = newLocales[0]; - final Locale systemLocale = mResources.getConfiguration().locale; - final boolean sameLocale = systemLocale.equals(newLocale); - final boolean sameLanguage = systemLocale.getLanguage().equals(newLocale.getLanguage()); - final boolean implicitlyEnabled = mRichImm - .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype.getRawSubtype()); - mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage( - sameLocale || (sameLanguage && implicitlyEnabled)); - } + final Locale newLocale = SubtypeLocaleUtils.getSubtypeLocale(newSubtype); + final Locale systemLocale = mResources.getConfiguration().locale; + final boolean sameLocale = systemLocale.equals(newLocale); + final boolean sameLanguage = systemLocale.getLanguage().equals(newLocale.getLanguage()); + final boolean implicitlyEnabled = mRichImm + .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype.getRawSubtype()); + mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage( + sameLocale || (sameLanguage && implicitlyEnabled)); + updateShortcutIME(); } @@ -290,11 +284,11 @@ public final class SubtypeSwitcher { sForcedSubtypeForTesting = new RichInputMethodSubtype(subtype); } - public Locale[] getCurrentSubtypeLocales() { + public Locale getCurrentSubtypeLocale() { if (null != sForcedSubtypeForTesting) { - return sForcedSubtypeForTesting.getLocales(); + return LocaleUtils.constructLocaleFromString(sForcedSubtypeForTesting.getLocale()); } - return getCurrentSubtype().getLocales(); + return SubtypeLocaleUtils.getSubtypeLocale(getCurrentSubtype()); } public RichInputMethodSubtype getCurrentSubtype() { diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java index 61661cd52..5a7f7662c 100644 --- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java @@ -293,6 +293,13 @@ public final class SubtypeLocaleUtils { return LocaleUtils.constructLocaleFromString(localeString); } + // TODO: remove this. When RichInputMethodSubtype#getLocale is removed we can do away with this + // method at the same time. + public static Locale getSubtypeLocale(final RichInputMethodSubtype subtype) { + final String localeString = subtype.getLocale(); + return LocaleUtils.constructLocaleFromString(localeString); + } + public static String getKeyboardLayoutSetDisplayName(final InputMethodSubtype subtype) { final String layoutName = getKeyboardLayoutSetName(subtype); return getKeyboardLayoutSetDisplayName(layoutName); @@ -341,6 +348,10 @@ public final class SubtypeLocaleUtils { return Arrays.binarySearch(SORTED_RTL_LANGUAGES, language) >= 0; } + public static boolean isRtlLanguage(final RichInputMethodSubtype subtype) { + return isRtlLanguage(getSubtypeLocale(subtype)); + } + public static String getCombiningRulesExtraValue(final InputMethodSubtype subtype) { return subtype.getExtraValueOf(Constants.Subtype.ExtraValue.COMBINING_RULES); } diff --git a/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java index 011309942..565fadb2a 100644 --- a/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java @@ -42,9 +42,8 @@ public class ContextualDictionaryTests extends AndroidTestCase { final ArrayList dictTypes = new ArrayList<>(); dictTypes.add(Dictionary.TYPE_CONTEXTUAL); final DictionaryFacilitator dictionaryFacilitator = new DictionaryFacilitator(); - dictionaryFacilitator.resetDictionariesForTesting(getContext(), - new Locale[] { LOCALE_EN_US }, dictTypes, new HashMap(), - new HashMap>()); + dictionaryFacilitator.resetDictionariesForTesting(getContext(), LOCALE_EN_US, dictTypes, + new HashMap(), new HashMap>()); return dictionaryFacilitator; } diff --git a/tests/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryTests.java index afabbbd38..4e7e8140a 100644 --- a/tests/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryTests.java @@ -55,9 +55,8 @@ public class PersonalizationDictionaryTests extends AndroidTestCase { dictTypes.add(Dictionary.TYPE_MAIN); dictTypes.add(Dictionary.TYPE_PERSONALIZATION); final DictionaryFacilitator dictionaryFacilitator = new DictionaryFacilitator(getContext()); - dictionaryFacilitator.resetDictionariesForTesting(getContext(), - new Locale[] { LOCALE_EN_US }, dictTypes, new HashMap(), - new HashMap>()); + dictionaryFacilitator.resetDictionariesForTesting(getContext(), LOCALE_EN_US, dictTypes, + new HashMap(), new HashMap>()); // Set subtypes. RichInputMethodManager.init(getContext()); final RichInputMethodManager richImm = RichInputMethodManager.getInstance(); diff --git a/tests/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtilsTests.java index 8810eaf37..b766ab2e7 100644 --- a/tests/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtilsTests.java @@ -119,17 +119,12 @@ public class SpacebarLanguageUtilsTests extends AndroidTestCase { final String subtypeName = SubtypeLocaleUtils .getSubtypeDisplayNameInSystemLocale(subtype.getRawSubtype()); final String spacebarText = subtype.getFullDisplayName(); - final Locale[] locales = subtype.getLocales(); - if (1 == locales.length) { - final String languageName = SubtypeLocaleUtils - .getSubtypeLocaleDisplayName(locales[0].toString()); - if (subtype.isNoLanguage()) { - assertFalse(subtypeName, spacebarText.contains(languageName)); - } else { - assertTrue(subtypeName, spacebarText.contains(languageName)); - } + final String languageName = SubtypeLocaleUtils + .getSubtypeLocaleDisplayName(subtype.getLocale()); + if (subtype.isNoLanguage()) { + assertFalse(subtypeName, spacebarText.contains(languageName)); } else { - // TODO: test multi-lingual subtype spacebar display + assertTrue(subtypeName, spacebarText.contains(languageName)); } } } @@ -138,14 +133,8 @@ public class SpacebarLanguageUtilsTests extends AndroidTestCase { for (final RichInputMethodSubtype subtype : mSubtypesList) { final String subtypeName = SubtypeLocaleUtils .getSubtypeDisplayNameInSystemLocale(subtype.getRawSubtype()); - final Locale[] locales = subtype.getLocales(); - if (locales.length > 1) { - // TODO: test multi-lingual subtype spacebar display - continue; - } - final Locale locale = locales[0]; if (SubtypeLocaleUtils.sExceptionalLocaleDisplayedInRootLocale.contains( - locale.toString())) { + subtype.getLocale())) { // Skip test because the language part of this locale string doesn't represent // the locale to be displayed on the spacebar (for example hi_ZZ and Hinglish). continue; @@ -155,6 +144,7 @@ public class SpacebarLanguageUtilsTests extends AndroidTestCase { assertEquals(subtypeName, SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName( subtype.getRawSubtype()), spacebarText); } else { + final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); assertEquals(subtypeName, SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale.getLanguage()), spacebarText); diff --git a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java index 02be5138f..c095e6831 100644 --- a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java @@ -111,19 +111,14 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase { for (final RichInputMethodSubtype subtype : mSubtypesList) { final String subtypeName = SubtypeLocaleUtils .getSubtypeDisplayNameInSystemLocale(subtype.getRawSubtype()); - final Locale[] locales = subtype.getLocales(); - if (1 == locales.length) { - if (subtype.isNoLanguage()) { - final String layoutName = SubtypeLocaleUtils - .getKeyboardLayoutSetDisplayName(subtype.getRawSubtype()); - assertTrue(subtypeName, subtypeName.contains(layoutName)); - } else { - final String languageName = SubtypeLocaleUtils - .getSubtypeLocaleDisplayNameInSystemLocale(locales[0].toString()); - assertTrue(subtypeName, subtypeName.contains(languageName)); - } + if (subtype.isNoLanguage()) { + final String layoutName = SubtypeLocaleUtils + .getKeyboardLayoutSetDisplayName(subtype.getRawSubtype()); + assertTrue(subtypeName, subtypeName.contains(layoutName)); } else { - // TODO: test multi-lingual subtype spacebar display + final String languageName = SubtypeLocaleUtils + .getSubtypeLocaleDisplayNameInSystemLocale(subtype.getLocale()); + assertTrue(subtypeName, subtypeName.contains(languageName)); } } } @@ -320,9 +315,9 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase { .getSubtypeDisplayNameInSystemLocale(rawSubtype); if (rawSubtype.equals(ARABIC) || rawSubtype.equals(FARSI) || rawSubtype.equals(HEBREW)) { - assertTrue(subtypeName, subtype.isRtlSubtype()); + assertTrue(subtypeName, SubtypeLocaleUtils.isRtlLanguage(subtype)); } else { - assertFalse(subtypeName, subtype.isRtlSubtype()); + assertFalse(subtypeName, SubtypeLocaleUtils.isRtlLanguage(subtype)); } } }