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

Commit 23d27fef authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Fix auto correction spacebar LED

Change-Id: I4894d188d5612380cc9f8eec7cb72544e37fb2b0
parent 5a39e527
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@
    <bool name="config_default_bigram_prediction">false</bool>
    <bool name="config_default_sound_enabled">false</bool>
    <bool name="config_default_vibration_enabled">true</bool>
    <bool name="config_auto_correction_spacebar_led_enabled">false</bool>
    <!-- Showing mini keyboard, just above the touched point if true, aligned to the key if false -->
    <bool name="config_show_mini_keyboard_at_touched_point">false</bool>
    <!-- The language is never displayed if == 0, always displayed if < 0 -->
+2 −2
Original line number Diff line number Diff line
@@ -32,8 +32,7 @@
        <item name="maxMoreKeysColumn">@integer/config_max_more_keys_column</item>
    </style>
    <style name="LatinKeyboard">
        <item name="autoCorrectionSpacebarLedEnabled">@bool/config_auto_correction_spacebar_led_enabled
        </item>
        <item name="autoCorrectionSpacebarLedEnabled">true</item>
        <item name="spacebarTextColor">#FFC0C0C0</item>
        <item name="spacebarTextShadowColor">#80000000</item>
    </style>
@@ -242,6 +241,7 @@
        name="LatinKeyboard.IceCreamSandwich"
        parent="LatinKeyboard"
    >
        <item name="autoCorrectionSpacebarLedEnabled">false</item>
        <item name="disabledShortcutIcon">@drawable/sym_keyboard_voice_off_holo</item>
    </style>
    <style
+28 −13
Original line number Diff line number Diff line
@@ -166,18 +166,29 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
                SettingsValues.getKeyPreviewPopupDismissDelay(mPrefs, mResources));
        final boolean localeChanged = (oldKeyboard == null)
                || !keyboard.mId.mLocale.equals(oldKeyboard.mId.mLocale);
        mInputMethodService.mHandler.startDisplayLanguageOnSpacebar(localeChanged);
        if (keyboard instanceof LatinKeyboard) {
            final LatinKeyboard latinKeyboard = (LatinKeyboard)keyboard;
            latinKeyboard.updateAutoCorrectionState(mIsAutoCorrectionActive);
            // If the cached keyboard had been switched to another keyboard while the language was
            // displayed on its spacebar, it might have had arbitrary text fade factor. In such
            // case, we should reset the text fade factor. It is also applicable to shortcut key.
            latinKeyboard.updateSpacebarLanguage(0.0f,
                    Utils.hasMultipleEnabledIMEsOrSubtypes(true /* include aux subtypes */),
                    mSubtypeSwitcher.needsToDisplayLanguage(latinKeyboard.mId.mLocale));
            latinKeyboard.updateShortcutKey(mSubtypeSwitcher.isShortcutImeReady());
        }
        updateShiftState();
        mInputMethodService.mHandler.startDisplayLanguageOnSpacebar(localeChanged);
    }

    // TODO: Move this method to KeyboardSet.
    private LatinKeyboard getKeyboard(KeyboardId id) {
    private LatinKeyboard getKeyboard(Context context, KeyboardId id) {
        final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id);
        LatinKeyboard keyboard = (ref == null) ? null : ref.get();
        if (keyboard == null) {
            final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, id.mLocale);
            try {
                final LatinKeyboard.Builder builder = new LatinKeyboard.Builder(mThemeContext);
                final LatinKeyboard.Builder builder = new LatinKeyboard.Builder(context);
                builder.load(id);
                builder.setTouchPositionCorrectionEnabled(
                        mSubtypeSwitcher.currentSubtypeContainsExtraValueKey(
@@ -198,14 +209,8 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
                    + " theme=" + themeName(keyboard.mThemeId));
        }

        keyboard.onAutoCorrectionStateChanged(mIsAutoCorrectionActive);
        keyboard.setShiftLocked(false);
        keyboard.setShifted(false);
        // If the cached keyboard had been switched to another keyboard while the language was
        // displayed on its spacebar, it might have had arbitrary text fade factor. In such case,
        // we should reset the text fade factor. It is also applicable to shortcut key.
        keyboard.setSpacebarTextFadeFactor(0.0f, null);
        keyboard.updateShortcutKey(mSubtypeSwitcher.isShortcutImeReady(), null);
        return keyboard;
    }

@@ -338,19 +343,19 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
    // Implements {@link KeyboardState.SwitchActions}.
    @Override
    public void setSymbolsKeyboard() {
        setKeyboard(getKeyboard(mKeyboardSet.mSymbolsId));
        setKeyboard(getKeyboard(mThemeContext, mKeyboardSet.mSymbolsId));
    }

    // Implements {@link KeyboardState.SwitchActions}.
    @Override
    public void setAlphabetKeyboard() {
        setKeyboard(getKeyboard(mKeyboardSet.mAlphabetId));
        setKeyboard(getKeyboard(mThemeContext, mKeyboardSet.mAlphabetId));
    }

    // Implements {@link KeyboardState.SwitchActions}.
    @Override
    public void setSymbolsShiftedKeyboard() {
        final Keyboard keyboard = getKeyboard(mKeyboardSet.mSymbolsShiftedId);
        final Keyboard keyboard = getKeyboard(mThemeContext, mKeyboardSet.mSymbolsShiftedId);
        setKeyboard(keyboard);
        // TODO: Remove this logic once we introduce initial keyboard shift state attribute.
        // Symbol shift keyboard may have a shift key that has a caps lock style indicator (a.k.a.
@@ -451,12 +456,22 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions,
        }
    }

    public void onNetworkStateChanged() {
        final LatinKeyboard keyboard = getLatinKeyboard();
        if (keyboard == null) return;
        final Key updatedKey = keyboard.updateShortcutKey(
                SubtypeSwitcher.getInstance().isShortcutImeReady());
        if (updatedKey != null && mKeyboardView != null) {
            mKeyboardView.invalidateKey(updatedKey);
        }
    }

    public void onAutoCorrectionStateChanged(boolean isAutoCorrection) {
        if (mIsAutoCorrectionActive != isAutoCorrection) {
            mIsAutoCorrectionActive = isAutoCorrection;
            final LatinKeyboard keyboard = getLatinKeyboard();
            if (keyboard != null && keyboard.needsAutoCorrectionSpacebarLed()) {
                final Key invalidatedKey = keyboard.onAutoCorrectionStateChanged(isAutoCorrection);
                final Key invalidatedKey = keyboard.updateAutoCorrectionState(isAutoCorrection);
                final LatinKeyboardView keyboardView = getKeyboardView();
                if (keyboardView != null)
                    keyboardView.invalidateKey(invalidatedKey);
+26 −27
Original line number Diff line number Diff line
@@ -31,11 +31,9 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.Utils;

import java.util.Arrays;
@@ -48,7 +46,6 @@ public class LatinKeyboard extends Keyboard {

    private final Resources mRes;
    private final Theme mTheme;
    private final SubtypeSwitcher mSubtypeSwitcher = SubtypeSwitcher.getInstance();

    /* Space key and its icons, drawables and colors. */
    private final Key mSpaceKey;
@@ -57,11 +54,15 @@ public class LatinKeyboard extends Keyboard {
    private final Drawable mAutoCorrectionSpacebarLedIcon;
    private final int mSpacebarTextColor;
    private final int mSpacebarTextShadowColor;
    private float mSpacebarTextFadeFactor = 0.0f;
    private final HashMap<Integer, BitmapDrawable> mSpaceDrawableCache =
            new HashMap<Integer, BitmapDrawable>();
    private final boolean mIsSpacebarTriggeringPopupByLongPress;

    private boolean mAutoCorrectionSpacebarLedOn;
    private boolean mMultipleEnabledIMEsOrSubtypes;
    private boolean mNeedsToDisplayLanguage;
    private float mSpacebarTextFadeFactor = 0.0f;

    /* Shortcut key and its icons if available */
    private final Key mShortcutKey;
    private final Drawable mEnabledShortcutIcon;
@@ -140,11 +141,13 @@ public class LatinKeyboard extends Keyboard {
        }
    }

    public void setSpacebarTextFadeFactor(float fadeFactor, KeyboardView view) {
    public Key updateSpacebarLanguage(float fadeFactor, boolean multipleEnabledIMEsOrSubtypes,
            boolean needsToDisplayLanguage) {
        mSpacebarTextFadeFactor = fadeFactor;
        updateSpacebarForLocale(false);
        if (view != null)
            view.invalidateKey(mSpaceKey);
        mMultipleEnabledIMEsOrSubtypes = multipleEnabledIMEsOrSubtypes;
        mNeedsToDisplayLanguage = needsToDisplayLanguage;
        updateSpacebarIcon();
        return mSpaceKey;
    }

    private static int getSpacebarTextColor(int color, float fadeFactor) {
@@ -153,13 +156,12 @@ public class LatinKeyboard extends Keyboard {
        return newColor;
    }

    public void updateShortcutKey(boolean available, KeyboardView view) {
    public Key updateShortcutKey(boolean available) {
        if (mShortcutKey == null)
            return;
            return null;
        mShortcutKey.setEnabled(available);
        mShortcutKey.setIcon(available ? mEnabledShortcutIcon : mDisabledShortcutIcon);
        if (view != null)
            view.invalidateKey(mShortcutKey);
        return mShortcutKey;
    }

    public boolean needsAutoCorrectionSpacebarLed() {
@@ -169,8 +171,9 @@ public class LatinKeyboard extends Keyboard {
    /**
     * @return a key which should be invalidated.
     */
    public Key onAutoCorrectionStateChanged(boolean isAutoCorrection) {
        updateSpacebarForLocale(isAutoCorrection);
    public Key updateAutoCorrectionState(boolean isAutoCorrection) {
        mAutoCorrectionSpacebarLedOn = isAutoCorrection;
        updateSpacebarIcon();
        return mSpaceKey;
    }

@@ -183,19 +186,15 @@ public class LatinKeyboard extends Keyboard {
        return label;
    }

    private void updateSpacebarForLocale(boolean isAutoCorrection) {
    private void updateSpacebarIcon() {
        if (mSpaceKey == null) return;
        final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance();
        if (imm == null) return;
        // The "..." popup hint for triggering something by a long-pressing the spacebar
        final boolean shouldShowInputMethodPicker = mIsSpacebarTriggeringPopupByLongPress
                && Utils.hasMultipleEnabledIMEsOrSubtypes(imm, true /* include aux subtypes */);
                && mMultipleEnabledIMEsOrSubtypes;
        mSpaceKey.setNeedsSpecialPopupHint(shouldShowInputMethodPicker);
        // If application locales are explicitly selected.
        if (mSubtypeSwitcher.needsToDisplayLanguage(mId.mLocale)) {
            mSpaceKey.setIcon(getSpaceDrawable(mId.mLocale, isAutoCorrection));
        } else if (isAutoCorrection) {
            mSpaceKey.setIcon(getSpaceDrawable(null, true));
        if (mNeedsToDisplayLanguage) {
            mSpaceKey.setIcon(getSpaceDrawable(mId.mLocale));
        } else if (mAutoCorrectionSpacebarLedOn) {
            mSpaceKey.setIcon(getSpaceDrawable(null));
        } else {
            mSpaceKey.setIcon(mSpaceIcon);
        }
@@ -245,15 +244,15 @@ public class LatinKeyboard extends Keyboard {
        return language;
    }

    private BitmapDrawable getSpaceDrawable(Locale locale, boolean isAutoCorrection) {
    private BitmapDrawable getSpaceDrawable(Locale locale) {
        final Integer hashCode = Arrays.hashCode(
                new Object[] { locale, isAutoCorrection, mSpacebarTextFadeFactor });
                new Object[] { locale, mAutoCorrectionSpacebarLedOn, mSpacebarTextFadeFactor });
        final BitmapDrawable cached = mSpaceDrawableCache.get(hashCode);
        if (cached != null) {
            return cached;
        }
        final BitmapDrawable drawable = new BitmapDrawable(mRes, drawSpacebar(
                locale, isAutoCorrection, mSpacebarTextFadeFactor));
                locale, mAutoCorrectionSpacebarLedOn, mSpacebarTextFadeFactor));
        mSpaceDrawableCache.put(hashCode, drawable);
        return drawable;
    }
+0 −9
Original line number Diff line number Diff line
@@ -374,15 +374,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
        return miniKeyboardView;
    }

    public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboard oldKeyboard) {
        final Keyboard keyboard = getKeyboard();
        // We should not set text fade factor to the keyboard which does not display the language on
        // its spacebar.
        if (keyboard instanceof LatinKeyboard && keyboard == oldKeyboard) {
            ((LatinKeyboard)keyboard).setSpacebarTextFadeFactor(fadeFactor, this);
        }
    }

    /**
     * Called when a key is long pressed. By default this will open mini keyboard associated
     * with this key.
Loading