Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4934a88b authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Make LanguageOnSpacebarHelper as utility class

Change-Id: I546f3b1943999a41803222bd14f9ddd23aa87a51
parent 2b25f674
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -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() {
+6 −6
Original line number Diff line number Diff line
@@ -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;
+3 −10
Original line number Diff line number Diff line
@@ -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<InputMethodSubtype> 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);
    }
}
+19 −15
Original line number Diff line number Diff line
@@ -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<InputMethodSubtype> mEnabledSubtypes = Collections.emptyList();
    private boolean mIsSystemLanguageSameAsInputLanguage;
    private static List<InputMethodSubtype> 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<InputMethodSubtype> enabledSubtypes) {
        mEnabledSubtypes = enabledSubtypes;
    public static void setEnabledSubtypes(@Nonnull final List<InputMethodSubtype> 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;
    }
}
+11 −13
Original line number Diff line number Diff line
@@ -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<InputMethodSubtype> 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() {