From 4934a88b618f06600e2d725a43bdd2511715b4ca Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 7 Nov 2014 14:50:28 -0800 Subject: [PATCH] Make LanguageOnSpacebarHelper as utility class Change-Id: I546f3b1943999a41803222bd14f9ddd23aa87a51 --- .../keyboard/KeyboardSwitcher.java | 19 ++++++----- .../keyboard/MainKeyboardView.java | 12 +++---- .../inputmethod/latin/SubtypeSwitcher.java | 13 ++----- .../utils/LanguageOnSpacebarUtils.java} | 34 +++++++++++-------- .../utils/LanguageOnSpacebarUtilsTests.java} | 24 ++++++------- 5 files changed, 49 insertions(+), 53 deletions(-) rename java/src/com/android/inputmethod/{keyboard/internal/LanguageOnSpacebarHelper.java => latin/utils/LanguageOnSpacebarUtils.java} (74%) rename tests/src/com/android/inputmethod/{keyboard/internal/LanguageOnSpacebarHelperTests.java => latin/utils/LanguageOnSpacebarUtilsTests.java} (91%) diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 800fd33b4..4c2498893 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -40,6 +40,7 @@ import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.utils.CapsModeUtils; +import com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils; import com.android.inputmethod.latin.utils.NetworkConnectivityUtils; import com.android.inputmethod.latin.utils.RecapitalizeStatus; import com.android.inputmethod.latin.utils.ResourceUtils; @@ -56,6 +57,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, private MainKeyboardView mKeyboardView; private EmojiPalettesView mEmojiPalettesView; private LatinIME mLatinIME; + private RichInputMethodManager mRichImm; private boolean mIsHardwareAcceleratedDrawingEnabled; private KeyboardState mState; @@ -83,6 +85,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, private void initInternal(final LatinIME latinIme) { mLatinIME = latinIme; + mRichImm = RichInputMethodManager.getInstance(); mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mState = new KeyboardState(this); mIsHardwareAcceleratedDrawingEnabled = @@ -116,7 +119,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res); final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues); builder.setKeyboardGeometry(keyboardWidth, keyboardHeight); - builder.setSubtype(RichInputMethodManager.getInstance().getCurrentSubtype()); + builder.setSubtype(mRichImm.getCurrentSubtype()); builder.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey); builder.setLanguageSwitchKeyEnabled(mLatinIME.shouldShowLanguageSwitchKey()); builder.setSplitLayoutEnabledByUser(ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED @@ -125,9 +128,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, try { mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState); // TODO: revisit this for multi-lingual input - mKeyboardTextsSet.setLocale( - RichInputMethodManager.getInstance().getCurrentSubtypeLocales()[0], - mThemeContext); + mKeyboardTextsSet.setLocale(mRichImm.getCurrentSubtypeLocales()[0], mThemeContext); } catch (KeyboardLayoutSetException e) { Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause()); return; @@ -166,12 +167,12 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, currentSettingsValues.mKeyPreviewDismissEndXScale, currentSettingsValues.mKeyPreviewDismissEndYScale, currentSettingsValues.mKeyPreviewDismissDuration); - keyboardView.updateShortcutKey(RichInputMethodManager.getInstance().isShortcutImeReady()); + keyboardView.updateShortcutKey(mRichImm.isShortcutImeReady()); final boolean subtypeChanged = (oldKeyboard == null) || !keyboard.mId.mSubtype.equals(oldKeyboard.mId.mSubtype); - final int languageOnSpacebarFormatType = mSubtypeSwitcher.getLanguageOnSpacebarFormatType( - keyboard.mId.mSubtype); - final boolean hasMultipleEnabledIMEsOrSubtypes = RichInputMethodManager.getInstance() + final int languageOnSpacebarFormatType = LanguageOnSpacebarUtils + .getLanguageOnSpacebarFormatType(keyboard.mId.mSubtype); + final boolean hasMultipleEnabledIMEsOrSubtypes = mRichImm .hasMultipleEnabledIMEsOrSubtypes(true /* shouldIncludeAuxiliarySubtypes */); keyboardView.startDisplayLanguageOnSpacebar(subtypeChanged, languageOnSpacebarFormatType, hasMultipleEnabledIMEsOrSubtypes); @@ -422,7 +423,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions, if (mKeyboardView == null) { return; } - mKeyboardView.updateShortcutKey(RichInputMethodManager.getInstance().isShortcutImeReady()); + mKeyboardView.updateShortcutKey(mRichImm.isShortcutImeReady()); } public int getKeyboardShiftMode() { diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index eeac4755d..ab8b7515d 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -47,7 +47,6 @@ import com.android.inputmethod.keyboard.internal.KeyDrawParams; import com.android.inputmethod.keyboard.internal.KeyPreviewChoreographer; import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams; import com.android.inputmethod.keyboard.internal.KeyPreviewView; -import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.keyboard.internal.MoreKeySpec; import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper; import com.android.inputmethod.keyboard.internal.SlidingKeyInputDrawingPreview; @@ -58,6 +57,7 @@ import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.common.Constants; import com.android.inputmethod.latin.common.CoordinateUtils; import com.android.inputmethod.latin.settings.DebugSettings; +import com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils; import com.android.inputmethod.latin.utils.TypefaceUtils; import java.util.Locale; @@ -783,10 +783,10 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy mHasMultipleEnabledIMEsOrSubtypes = hasMultipleEnabledIMEsOrSubtypes; final ObjectAnimator animator = mLanguageOnSpacebarFadeoutAnimator; if (animator == null) { - mLanguageOnSpacebarFormatType = LanguageOnSpacebarHelper.FORMAT_TYPE_NONE; + mLanguageOnSpacebarFormatType = LanguageOnSpacebarUtils.FORMAT_TYPE_NONE; } else { if (subtypeChanged - && languageOnSpacebarFormatType != LanguageOnSpacebarHelper.FORMAT_TYPE_NONE) { + && languageOnSpacebarFormatType != LanguageOnSpacebarUtils.FORMAT_TYPE_NONE) { setLanguageOnSpacebarAnimAlpha(Constants.Color.ALPHA_OPAQUE); if (animator.isStarted()) { animator.cancel(); @@ -811,7 +811,7 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy final int code = key.getCode(); if (code == Constants.CODE_SPACE) { // If input language are explicitly selected. - if (mLanguageOnSpacebarFormatType != LanguageOnSpacebarHelper.FORMAT_TYPE_NONE) { + if (mLanguageOnSpacebarFormatType != LanguageOnSpacebarUtils.FORMAT_TYPE_NONE) { drawLanguageOnSpacebar(key, canvas, paint); } // Whether space key needs to show the "..." popup hint for special purposes @@ -843,7 +843,7 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy // Layout language name on spacebar. private String layoutLanguageOnSpacebar(final Paint paint, final RichInputMethodSubtype subtype, final int width) { - if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarHelper.FORMAT_TYPE_MULTIPLE) { + if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarUtils.FORMAT_TYPE_MULTIPLE) { final Locale[] locales = subtype.getLocales(); final String[] languages = new String[locales.length]; for (int i = 0; i < locales.length; ++i) { @@ -853,7 +853,7 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy } // Choose appropriate language name to fit into the width. - if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarHelper.FORMAT_TYPE_FULL_LOCALE) { + if (mLanguageOnSpacebarFormatType == LanguageOnSpacebarUtils.FORMAT_TYPE_FULL_LOCALE) { final String fullText = subtype.getFullDisplayName(); if (fitsTextIntoWidth(width, fullText, paint)) { return fullText; diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 92ba6c2d9..d7a03d40b 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -20,7 +20,7 @@ import android.content.Context; import android.content.res.Resources; import android.view.inputmethod.InputMethodSubtype; -import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; +import com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.List; @@ -33,9 +33,6 @@ public final class SubtypeSwitcher { private /* final */ RichInputMethodManager mRichImm; private /* final */ Resources mResources; - private final LanguageOnSpacebarHelper mLanguageOnSpacebarHelper = - new LanguageOnSpacebarHelper(); - public static SubtypeSwitcher getInstance() { return sInstance; } @@ -68,18 +65,14 @@ public final class SubtypeSwitcher { public void updateParametersOnStartInputView() { final List enabledSubtypesOfThisIme = mRichImm.getMyEnabledInputMethodSubtypeList(true); - mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypesOfThisIme); + LanguageOnSpacebarUtils.setEnabledSubtypes(enabledSubtypesOfThisIme); } // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. public void onSubtypeChanged(@Nonnull final RichInputMethodSubtype richSubtype) { final boolean implicitlyEnabledSubtype = mRichImm .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(richSubtype.getRawSubtype()); - mLanguageOnSpacebarHelper.onSubtypeChanged( + LanguageOnSpacebarUtils.onSubtypeChanged( richSubtype, implicitlyEnabledSubtype, mResources.getConfiguration().locale); } - - public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) { - return mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype); - } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java b/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java similarity index 74% rename from java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java rename to java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java index 8ed80107a..fa1583b7a 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java +++ b/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java @@ -14,12 +14,11 @@ * limitations under the License. */ -package com.android.inputmethod.keyboard.internal; +package com.android.inputmethod.latin.utils; import android.view.inputmethod.InputMethodSubtype; import com.android.inputmethod.latin.RichInputMethodSubtype; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.Collections; import java.util.List; @@ -30,21 +29,26 @@ import javax.annotation.Nonnull; /** * This class determines that the language name on the spacebar should be displayed in what format. */ -public final class LanguageOnSpacebarHelper { +public final class LanguageOnSpacebarUtils { 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; + private static List sEnabledSubtypes = Collections.emptyList(); + private static boolean sIsSystemLanguageSameAsInputLanguage; - public int getLanguageOnSpacebarFormatType(@Nonnull final RichInputMethodSubtype subtype) { + private LanguageOnSpacebarUtils() { + // This utility class is not publicly instantiable. + } + + public static int getLanguageOnSpacebarFormatType( + @Nonnull final RichInputMethodSubtype subtype) { if (subtype.isNoLanguage()) { return FORMAT_TYPE_FULL_LOCALE; } // Only this subtype is enabled and equals to the system locale. - if (mEnabledSubtypes.size() < 2 && mIsSystemLanguageSameAsInputLanguage) { + if (sEnabledSubtypes.size() < 2 && sIsSystemLanguageSameAsInputLanguage) { return FORMAT_TYPE_NONE; } final Locale[] locales = subtype.getLocales(); @@ -54,7 +58,7 @@ public final class LanguageOnSpacebarHelper { final String keyboardLanguage = locales[0].getLanguage(); final String keyboardLayout = subtype.getKeyboardLayoutSetName(); int sameLanguageAndLayoutCount = 0; - for (final InputMethodSubtype ims : mEnabledSubtypes) { + for (final InputMethodSubtype ims : sEnabledSubtypes) { final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage(); if (keyboardLanguage.equals(language) && keyboardLayout.equals( SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) { @@ -67,30 +71,30 @@ public final class LanguageOnSpacebarHelper { : FORMAT_TYPE_LANGUAGE_ONLY; } - public void onUpdateEnabledSubtypes(@Nonnull final List enabledSubtypes) { - mEnabledSubtypes = enabledSubtypes; + public static void setEnabledSubtypes(@Nonnull final List enabledSubtypes) { + sEnabledSubtypes = enabledSubtypes; } - public void onSubtypeChanged(@Nonnull final RichInputMethodSubtype subtype, + public static 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; + sIsSystemLanguageSameAsInputLanguage = false; return; } final Locale newLocale = newLocales[0]; if (systemLocale.equals(newLocale)) { - mIsSystemLanguageSameAsInputLanguage = true; + sIsSystemLanguageSameAsInputLanguage = true; return; } if (!systemLocale.getLanguage().equals(newLocale.getLanguage())) { - mIsSystemLanguageSameAsInputLanguage = false; + sIsSystemLanguageSameAsInputLanguage = 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; + sIsSystemLanguageSameAsInputLanguage = implicitlyEnabledSubtype; } } diff --git a/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java b/tests/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtilsTests.java similarity index 91% rename from tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java rename to tests/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtilsTests.java index 6bb255b01..7f7f493b1 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelperTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtilsTests.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.android.inputmethod.keyboard.internal; +package com.android.inputmethod.latin.utils; -import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_FULL_LOCALE; -import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_LANGUAGE_ONLY; -import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_NONE; +import static com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils.FORMAT_TYPE_FULL_LOCALE; +import static com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils.FORMAT_TYPE_LANGUAGE_ONLY; +import static com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils.FORMAT_TYPE_NONE; import android.content.Context; import android.test.AndroidTestCase; @@ -28,6 +28,7 @@ import android.view.inputmethod.InputMethodSubtype; import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.RichInputMethodSubtype; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; +import com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.ArrayList; @@ -36,10 +37,7 @@ import java.util.Locale; import javax.annotation.Nonnull; @SmallTest -public class LanguageOnSpacebarHelperTests extends AndroidTestCase { - private final LanguageOnSpacebarHelper mLanguageOnSpacebarHelper = - new LanguageOnSpacebarHelper(); - +public class LanguageOnSpacebarUtilsTests extends AndroidTestCase { private RichInputMethodManager mRichImm; RichInputMethodSubtype EN_US_QWERTY; @@ -84,21 +82,21 @@ public class LanguageOnSpacebarHelperTests extends AndroidTestCase { return new RichInputMethodSubtype(subtype); } - private void enableSubtypes(final RichInputMethodSubtype ... subtypes) { + private static void enableSubtypes(final RichInputMethodSubtype ... subtypes) { final ArrayList enabledSubtypes = new ArrayList<>(); for (final RichInputMethodSubtype subtype : subtypes) { enabledSubtypes.add(subtype.getRawSubtype()); } - mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypes); + LanguageOnSpacebarUtils.setEnabledSubtypes(enabledSubtypes); } - private void assertFormatType(final RichInputMethodSubtype subtype, + private static void assertFormatType(final RichInputMethodSubtype subtype, final boolean implicitlyEnabledSubtype, final Locale systemLocale, final int expectedFormat) { - mLanguageOnSpacebarHelper.onSubtypeChanged(subtype, implicitlyEnabledSubtype, systemLocale); + LanguageOnSpacebarUtils.onSubtypeChanged(subtype, implicitlyEnabledSubtype, systemLocale); assertEquals(subtype.getLocales()[0] + " implicitly=" + implicitlyEnabledSubtype + " in " + systemLocale, expectedFormat, - mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype)); + LanguageOnSpacebarUtils.getLanguageOnSpacebarFormatType(subtype)); } public void testOneSubtypeImplicitlyEnabled() {