From 4934a88b618f06600e2d725a43bdd2511715b4ca Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 7 Nov 2014 14:50:28 -0800 Subject: Make LanguageOnSpacebarHelper as utility class Change-Id: I546f3b1943999a41803222bd14f9ddd23aa87a51 --- .../inputmethod/keyboard/KeyboardSwitcher.java | 19 ++-- .../inputmethod/keyboard/MainKeyboardView.java | 12 +-- .../internal/LanguageOnSpacebarHelper.java | 96 -------------------- .../android/inputmethod/latin/SubtypeSwitcher.java | 13 +-- .../latin/utils/LanguageOnSpacebarUtils.java | 100 +++++++++++++++++++++ 5 files changed, 119 insertions(+), 121 deletions(-) delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java create mode 100644 java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java (limited to 'java/src') 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/keyboard/internal/LanguageOnSpacebarHelper.java b/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java deleted file mode 100644 index 8ed80107a..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/LanguageOnSpacebarHelper.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.keyboard.internal; - -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; -import java.util.Locale; - -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 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; - - public 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) { - 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 keyboardLayout = subtype.getKeyboardLayoutSetName(); - int sameLanguageAndLayoutCount = 0; - for (final InputMethodSubtype ims : mEnabledSubtypes) { - final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage(); - if (keyboardLanguage.equals(language) && keyboardLayout.equals( - SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) { - sameLanguageAndLayoutCount++; - } - } - // Display full locale name only when there are multiple subtypes that have the same - // locale and keyboard layout. Otherwise displaying language name is enough. - return sameLanguageAndLayoutCount > 1 ? FORMAT_TYPE_FULL_LOCALE - : FORMAT_TYPE_LANGUAGE_ONLY; - } - - public void onUpdateEnabledSubtypes(@Nonnull final List enabledSubtypes) { - mEnabledSubtypes = enabledSubtypes; - } - - 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 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/latin/utils/LanguageOnSpacebarUtils.java b/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java new file mode 100644 index 000000000..fa1583b7a --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.utils; + +import android.view.inputmethod.InputMethodSubtype; + +import com.android.inputmethod.latin.RichInputMethodSubtype; + +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. + */ +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 static List sEnabledSubtypes = Collections.emptyList(); + private static boolean sIsSystemLanguageSameAsInputLanguage; + + 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 (sEnabledSubtypes.size() < 2 && sIsSystemLanguageSameAsInputLanguage) { + 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 keyboardLayout = subtype.getKeyboardLayoutSetName(); + int sameLanguageAndLayoutCount = 0; + for (final InputMethodSubtype ims : sEnabledSubtypes) { + final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage(); + if (keyboardLanguage.equals(language) && keyboardLayout.equals( + SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) { + sameLanguageAndLayoutCount++; + } + } + // Display full locale name only when there are multiple subtypes that have the same + // locale and keyboard layout. Otherwise displaying language name is enough. + return sameLanguageAndLayoutCount > 1 ? FORMAT_TYPE_FULL_LOCALE + : FORMAT_TYPE_LANGUAGE_ONLY; + } + + public static void setEnabledSubtypes(@Nonnull final List enabledSubtypes) { + sEnabledSubtypes = enabledSubtypes; + } + + 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. + sIsSystemLanguageSameAsInputLanguage = false; + return; + } + final Locale newLocale = newLocales[0]; + if (systemLocale.equals(newLocale)) { + sIsSystemLanguageSameAsInputLanguage = true; + return; + } + if (!systemLocale.getLanguage().equals(newLocale.getLanguage())) { + 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. + sIsSystemLanguageSameAsInputLanguage = implicitlyEnabledSubtype; + } +} -- cgit v1.2.3-83-g751a