From 0d6ce46528c43b3060b14cc3096adb03f55e6f76 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Sat, 8 Nov 2014 18:50:59 -0800 Subject: [PATCH] Move logic to determine spacebar text to LanguageOnSpacebarHelper Change-Id: Ib9dbbb4bd4e35c15fd752c364d3012e8a7da2410 --- .../internal/LanguageOnSpacebarHelper.java | 29 ++++++++++++++++--- .../inputmethod/latin/SubtypeSwitcher.java | 22 ++++---------- .../LanguageOnSpacebarHelperTests.java | 12 +++----- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java b/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java index 21eaed950..2a70ef51a 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java +++ b/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java @@ -25,6 +25,8 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import javax.annotation.Nonnull; + /** * This class determines that the language name on the spacebar should be displayed in what format. */ @@ -37,7 +39,7 @@ public final class LanguageOnSpacebarHelper { private List mEnabledSubtypes = Collections.emptyList(); private boolean mIsSystemLanguageSameAsInputLanguage; - public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) { + public int getLanguageOnSpacebarFormatType(@Nonnull final RichInputMethodSubtype subtype) { if (subtype.isNoLanguage()) { return FORMAT_TYPE_FULL_LOCALE; } @@ -65,11 +67,30 @@ public final class LanguageOnSpacebarHelper { : FORMAT_TYPE_LANGUAGE_ONLY; } - public void updateEnabledSubtypes(final List enabledSubtypes) { + public void onUpdateEnabledSubtypes(@Nonnull final List enabledSubtypes) { mEnabledSubtypes = enabledSubtypes; } - public void updateIsSystemLanguageSameAsInputLanguage(final boolean isSame) { - mIsSystemLanguageSameAsInputLanguage = isSame; + public void onSubtypeChanged(@Nonnull final RichInputMethodSubtype subtype, + final boolean implicitlyEnabledSubtype, @Nonnull final Locale systemLocale) { + final Locale[] newLocales = subtype.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. + mIsSystemLanguageSameAsInputLanguage = false; + return; + } + final Locale newLocale = newLocales[0]; + if (systemLocale.equals(newLocale)) { + mIsSystemLanguageSameAsInputLanguage = true; + return; + } + if (!systemLocale.getLanguage().equals(newLocale.getLanguage())) { + mIsSystemLanguageSameAsInputLanguage = false; + return; + } + // If the subtype is enabled explicitly, the language name should be displayed even when + // the keyboard language and the system language are equal. + mIsSystemLanguageSameAsInputLanguage = implicitlyEnabledSubtype; } } diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index b2766e27a..23e348bff 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -24,7 +24,6 @@ import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.List; -import java.util.Locale; import javax.annotation.Nonnull; @@ -69,28 +68,17 @@ public final class SubtypeSwitcher { public void updateParametersOnStartInputView() { final List enabledSubtypesOfThisIme = mRichImm.getMyEnabledInputMethodSubtypeList(true); - mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypesOfThisIme); + mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypesOfThisIme); mRichImm.updateShortcutIME(); } // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) { final RichInputMethodSubtype richSubtype = mRichImm.onSubtypeChanged(newSubtype); - final Locale[] newLocales = richSubtype.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); - mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage( - sameLocale || (sameLanguage && implicitlyEnabled)); - } + final boolean implicitlyEnabledSubtype = mRichImm + .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype); + mLanguageOnSpacebarHelper.onSubtypeChanged( + richSubtype, implicitlyEnabledSubtype, mResources.getConfiguration().locale); mRichImm.updateShortcutIME(); } diff --git a/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java b/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java index e8788d122..6bb255b01 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java @@ -89,19 +89,15 @@ public class LanguageOnSpacebarHelperTests extends AndroidTestCase { for (final RichInputMethodSubtype subtype : subtypes) { enabledSubtypes.add(subtype.getRawSubtype()); } - mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypes); + mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypes); } private void assertFormatType(final RichInputMethodSubtype subtype, final boolean implicitlyEnabledSubtype, final Locale systemLocale, final int expectedFormat) { - final Locale newLocale = subtype.getLocales()[0]; - final boolean sameLocale = systemLocale.equals(newLocale); - final boolean sameLanguage = systemLocale.getLanguage().equals(newLocale.getLanguage()); - mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage( - sameLocale || (sameLanguage && implicitlyEnabledSubtype)); - assertEquals(newLocale + " implicitly=" + implicitlyEnabledSubtype + " in " + systemLocale, - expectedFormat, + mLanguageOnSpacebarHelper.onSubtypeChanged(subtype, implicitlyEnabledSubtype, systemLocale); + assertEquals(subtype.getLocales()[0] + " implicitly=" + implicitlyEnabledSubtype + + " in " + systemLocale, expectedFormat, mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype)); }