Merge "Move logic to determine spacebar text to LanguageOnSpacebarHelper"

main
Tadashi G. Takaoka 2014-11-10 22:59:30 +00:00 committed by Android (Google) Code Review
commit e895f5f914
3 changed files with 34 additions and 29 deletions

View File

@ -25,6 +25,8 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.annotation.Nonnull;
/** /**
* This class determines that the language name on the spacebar should be displayed in what format. * 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<InputMethodSubtype> mEnabledSubtypes = Collections.emptyList(); private List<InputMethodSubtype> mEnabledSubtypes = Collections.emptyList();
private boolean mIsSystemLanguageSameAsInputLanguage; private boolean mIsSystemLanguageSameAsInputLanguage;
public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) { public int getLanguageOnSpacebarFormatType(@Nonnull final RichInputMethodSubtype subtype) {
if (subtype.isNoLanguage()) { if (subtype.isNoLanguage()) {
return FORMAT_TYPE_FULL_LOCALE; return FORMAT_TYPE_FULL_LOCALE;
} }
@ -65,11 +67,30 @@ public final class LanguageOnSpacebarHelper {
: FORMAT_TYPE_LANGUAGE_ONLY; : FORMAT_TYPE_LANGUAGE_ONLY;
} }
public void updateEnabledSubtypes(final List<InputMethodSubtype> enabledSubtypes) { public void onUpdateEnabledSubtypes(@Nonnull final List<InputMethodSubtype> enabledSubtypes) {
mEnabledSubtypes = enabledSubtypes; mEnabledSubtypes = enabledSubtypes;
} }
public void updateIsSystemLanguageSameAsInputLanguage(final boolean isSame) { public void onSubtypeChanged(@Nonnull final RichInputMethodSubtype subtype,
mIsSystemLanguageSameAsInputLanguage = isSame; 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;
} }
} }

View File

@ -24,7 +24,6 @@ import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.List; import java.util.List;
import java.util.Locale;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -69,28 +68,17 @@ public final class SubtypeSwitcher {
public void updateParametersOnStartInputView() { public void updateParametersOnStartInputView() {
final List<InputMethodSubtype> enabledSubtypesOfThisIme = final List<InputMethodSubtype> enabledSubtypesOfThisIme =
mRichImm.getMyEnabledInputMethodSubtypeList(true); mRichImm.getMyEnabledInputMethodSubtypeList(true);
mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypesOfThisIme); mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypesOfThisIme);
mRichImm.updateShortcutIME(); mRichImm.updateShortcutIME();
} }
// Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) { public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) {
final RichInputMethodSubtype richSubtype = mRichImm.onSubtypeChanged(newSubtype); final RichInputMethodSubtype richSubtype = mRichImm.onSubtypeChanged(newSubtype);
final Locale[] newLocales = richSubtype.getLocales(); final boolean implicitlyEnabledSubtype = mRichImm
if (newLocales.length > 1) { .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype);
// In multi-locales mode, the system language is never the same as the input language mLanguageOnSpacebarHelper.onSubtypeChanged(
// because there is no single input language. richSubtype, implicitlyEnabledSubtype, mResources.getConfiguration().locale);
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));
}
mRichImm.updateShortcutIME(); mRichImm.updateShortcutIME();
} }

View File

@ -89,19 +89,15 @@ public class LanguageOnSpacebarHelperTests extends AndroidTestCase {
for (final RichInputMethodSubtype subtype : subtypes) { for (final RichInputMethodSubtype subtype : subtypes) {
enabledSubtypes.add(subtype.getRawSubtype()); enabledSubtypes.add(subtype.getRawSubtype());
} }
mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypes); mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypes);
} }
private void assertFormatType(final RichInputMethodSubtype subtype, private void assertFormatType(final RichInputMethodSubtype subtype,
final boolean implicitlyEnabledSubtype, final Locale systemLocale, final boolean implicitlyEnabledSubtype, final Locale systemLocale,
final int expectedFormat) { final int expectedFormat) {
final Locale newLocale = subtype.getLocales()[0]; mLanguageOnSpacebarHelper.onSubtypeChanged(subtype, implicitlyEnabledSubtype, systemLocale);
final boolean sameLocale = systemLocale.equals(newLocale); assertEquals(subtype.getLocales()[0] + " implicitly=" + implicitlyEnabledSubtype
final boolean sameLanguage = systemLocale.getLanguage().equals(newLocale.getLanguage()); + " in " + systemLocale, expectedFormat,
mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(
sameLocale || (sameLanguage && implicitlyEnabledSubtype));
assertEquals(newLocale + " implicitly=" + implicitlyEnabledSubtype + " in " + systemLocale,
expectedFormat,
mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype)); mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype));
} }