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

Commit 0e4f0da4 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Move PointerTracker.DrawingProxy to MainKeyboardView (step 1)

Change-Id: I86014de147416453503b6412eb862a2d172426aa
parent d9449ccf
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -57,12 +57,6 @@
        <attr name="state_left_edge" format="boolean" />
        <attr name="state_right_edge" format="boolean" />
        <attr name="state_has_morekeys" format="boolean" />
        <!-- Vertical offset of the key press feedback from the key. -->
        <attr name="keyPreviewOffset" format="dimension" />
        <!-- Height of the key press feedback popup. -->
        <attr name="keyPreviewHeight" format="dimension" />
        <!-- Delay after key releasing and key press feedback dismissing in millisecond -->
        <attr name="keyPreviewLingerTimeout" format="integer" />

        <!-- Amount to offset the touch Y coordinate by, for bias correction. -->
        <attr name="verticalCorrection" format="dimension" />
@@ -123,6 +117,12 @@
        <attr name="longPressShiftKeyTimeout" format="integer" />
        <!-- Ignore special key timeout while typing in millisecond. -->
        <attr name="ignoreAltCodeKeyTimeout" format="integer" />
        <!-- Vertical offset of the key press feedback from the key. -->
        <attr name="keyPreviewOffset" format="dimension" />
        <!-- Height of the key press feedback popup. -->
        <attr name="keyPreviewHeight" format="dimension" />
        <!-- Delay after key releasing and key press feedback dismissing in millisecond -->
        <attr name="keyPreviewLingerTimeout" format="integer" />
        <!-- Layout resource for more keys keyboard -->
        <attr name="moreKeysKeyboardLayout" format="reference" />
        <!-- More keys keyboard will shown at touched point. -->
+3 −3
Original line number Diff line number Diff line
@@ -55,10 +55,7 @@
        <item name="keyShiftedLetterHintPadding">@dimen/key_uppercase_letter_padding</item>
        <item name="keyPreviewLayout">@layout/key_preview</item>
        <item name="keyPreviewTextColor">@color/key_text_color_default</item>
        <item name="keyPreviewOffset">@dimen/key_preview_offset</item>
        <item name="keyPreviewHeight">@dimen/key_preview_height</item>
        <item name="keyPreviewTextRatio">@fraction/key_preview_text_ratio</item>
        <item name="keyPreviewLingerTimeout">@integer/config_key_preview_linger_timeout</item>
        <item name="verticalCorrection">@dimen/keyboard_vertical_correction</item>
        <item name="keyTextShadowColor">@color/key_text_shadow_color_default</item>
        <item name="keyTextShadowRadius">2.75</item>
@@ -88,6 +85,9 @@
        <item name="longPressKeyTimeout">@integer/config_long_press_key_timeout</item>
        <item name="longPressShiftKeyTimeout">@integer/config_long_press_shift_key_timeout</item>
        <item name="ignoreAltCodeKeyTimeout">@integer/config_ignore_alt_code_key_timeout</item>
        <item name="keyPreviewOffset">@dimen/key_preview_offset</item>
        <item name="keyPreviewHeight">@dimen/key_preview_height</item>
        <item name="keyPreviewLingerTimeout">@integer/config_key_preview_linger_timeout</item>
        <item name="moreKeysKeyboardLayout">@layout/more_keys_keyboard</item>
        <item name="showMoreKeysKeyboardAtTouchedPoint">@bool/config_show_more_keys_keyboard_at_touched_point</item>
        <item name="languageOnSpacebarFinalAlpha">@integer/config_language_on_spacebar_final_alpha</item>
+17 −185
Original line number Diff line number Diff line
@@ -33,14 +33,12 @@ import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.inputmethod.keyboard.internal.KeyDrawParams;
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
import com.android.inputmethod.keyboard.internal.KeyVisualAttributes;
import com.android.inputmethod.keyboard.internal.PreviewPlacerView;
import com.android.inputmethod.latin.CollectionUtils;
@@ -49,7 +47,6 @@ import com.android.inputmethod.latin.CoordinateUtils;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.research.ResearchLogger;
@@ -61,9 +58,6 @@ import java.util.HashSet;
 *
 * @attr ref R.styleable#KeyboardView_keyBackground
 * @attr ref R.styleable#KeyboardView_keyPreviewLayout
 * @attr ref R.styleable#KeyboardView_keyPreviewOffset
 * @attr ref R.styleable#KeyboardView_keyPreviewHeight
 * @attr ref R.styleable#KeyboardView_keyPreviewLingerTimeout
 * @attr ref R.styleable#KeyboardView_keyLabelHorizontalPadding
 * @attr ref R.styleable#KeyboardView_keyHintLetterPadding
 * @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding
@@ -102,8 +96,7 @@ import java.util.HashSet;
 * @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintActivatedColor
 * @attr ref R.styleable#Keyboard_Key_keyPreviewTextColor
 */
// TODO: Move PointerTracker.DrawingProxy to MainKeyboardView
public class KeyboardView extends View implements PointerTracker.DrawingProxy {
public class KeyboardView extends View {
    private static final String TAG = KeyboardView.class.getSimpleName();

    // XML attributes
@@ -139,42 +132,14 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
    private final int[] mOriginCoords = CoordinateUtils.newInstance();

    // Key preview
    private static final int PREVIEW_ALPHA = 240;
    private final int mKeyPreviewLayoutId;
    private final int mKeyPreviewOffset;
    private final int mKeyPreviewHeight;
    private final SparseArray<TextView> mKeyPreviewTexts = CollectionUtils.newSparseArray();
    protected final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams();
    private boolean mShowKeyPreviewPopup = true;
    private int mKeyPreviewLingerTimeout;
    // TODO: Move these variable to MainKeyboardView
    protected final int mKeyPreviewLayoutId;
    protected final SparseArray<TextView> mKeyPreviewTexts = CollectionUtils.newSparseArray();

    // Gesture floating preview text
    // TODO: Make this parameter customizable by user via settings.
    private int mGestureFloatingPreviewTextLingerTimeout;

    // Background state set
    private static final int[][][] KEY_PREVIEW_BACKGROUND_STATE_TABLE = {
        { // STATE_MIDDLE
            EMPTY_STATE_SET,
            { R.attr.state_has_morekeys }
        },
        { // STATE_LEFT
            { R.attr.state_left_edge },
            { R.attr.state_left_edge, R.attr.state_has_morekeys }
        },
        { // STATE_RIGHT
            { R.attr.state_right_edge },
            { R.attr.state_right_edge, R.attr.state_has_morekeys }
        }
    };
    private static final int STATE_MIDDLE = 0;
    private static final int STATE_LEFT = 1;
    private static final int STATE_RIGHT = 2;
    private static final int STATE_NORMAL = 0;
    private static final int STATE_HAS_MOREKEYS = 1;
    private static final int[] KEY_PREVIEW_BACKGROUND_DEFAULT_STATE =
            KEY_PREVIEW_BACKGROUND_STATE_TABLE[STATE_MIDDLE][STATE_NORMAL];

    // Drawing
    /** True if the entire keyboard needs to be dimmed. */
    private boolean mNeedsToDimEntireKeyboard;
@@ -199,7 +164,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
    private static final char[] KEY_LABEL_REFERENCE_CHAR = { 'M' };
    private static final char[] KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR = { '8' };

    private final DrawingHandler mDrawingHandler = new DrawingHandler(this);
    // TODO: Move this to MainKeyboardView
    protected final DrawingHandler mDrawingHandler = new DrawingHandler(this);

    public static class DrawingHandler extends StaticInnerHandlerWrapper<KeyboardView> {
        private static final int MSG_DISMISS_KEY_PREVIEW = 0;
@@ -259,12 +225,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
                R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
        mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground);
        mKeyBackground.getPadding(mKeyBackgroundPadding);
        mKeyPreviewOffset = keyboardViewAttr.getDimensionPixelOffset(
                R.styleable.KeyboardView_keyPreviewOffset, 0);
        mKeyPreviewHeight = keyboardViewAttr.getDimensionPixelSize(
                R.styleable.KeyboardView_keyPreviewHeight, 80);
        mKeyPreviewLingerTimeout = keyboardViewAttr.getInt(
                R.styleable.KeyboardView_keyPreviewLingerTimeout, 0);
        mKeyLabelHorizontalPadding = keyboardViewAttr.getDimensionPixelOffset(
                R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
        mKeyHintLetterPadding = keyboardViewAttr.getDimension(
@@ -277,9 +237,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
                R.styleable.KeyboardView_keyTextShadowRadius, 0.0f);
        mKeyPreviewLayoutId = keyboardViewAttr.getResourceId(
                R.styleable.KeyboardView_keyPreviewLayout, 0);
        if (mKeyPreviewLayoutId == 0) {
            mShowKeyPreviewPopup = false;
        }
        mVerticalCorrection = keyboardViewAttr.getDimension(
                R.styleable.KeyboardView_verticalCorrection, 0);
        mBackgroundDimAlpha = keyboardViewAttr.getInt(
@@ -329,27 +286,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        return mKeyboard;
    }

    /**
     * Enables or disables the key feedback popup. This is a popup that shows a magnified
     * version of the depressed key. By default the preview is enabled.
     * @param previewEnabled whether or not to enable the key feedback preview
     * @param delay the delay after which the preview is dismissed
     * @see #isKeyPreviewPopupEnabled()
     */
    public void setKeyPreviewPopupEnabled(final boolean previewEnabled, final int delay) {
        mShowKeyPreviewPopup = previewEnabled;
        mKeyPreviewLingerTimeout = delay;
    }

    /**
     * Returns the enabled state of the key feedback preview
     * @return whether or not the key feedback preview is enabled
     * @see #setKeyPreviewPopupEnabled(boolean, int)
     */
    public boolean isKeyPreviewPopupEnabled() {
        return mShowKeyPreviewPopup;
    }

    // TODO: Move this method to MainKeyboardView
    public void setGesturePreviewMode(final boolean drawsGesturePreviewTrail,
            final boolean drawsGestureFloatingPreviewText) {
        mPreviewPlacerView.setGesturePreviewMode(
@@ -458,6 +395,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
            }
        }

        // TODO: Move this code block to MainKeyboardView.onDraw
        // Overlay a dark rectangle to dim.
        if (mNeedsToDimEntireKeyboard) {
            paint.setColor(Color.BLACK);
@@ -477,6 +415,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        mInvalidateAllKeys = false;
    }

    // TODO: Move this method to MainKeyboardView.
    public void dimEntireKeyboard(final boolean dimmed) {
        final boolean needsRedrawing = mNeedsToDimEntireKeyboard != dimmed;
        mNeedsToDimEntireKeyboard = dimmed;
@@ -798,7 +737,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        mDrawingHandler.cancelAllMessages();
    }

    private TextView getKeyPreviewText(final int pointerId) {
    // TODO: Move this method to MainKeyboardView.
    protected TextView getKeyPreviewText(final int pointerId) {
        TextView previewText = mKeyPreviewTexts.get(pointerId);
        if (previewText != null) {
            return previewText;
@@ -813,6 +753,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        return previewText;
    }

    // TODO: Move this method to MainKeyboardView.
    private void dismissAllKeyPreviews() {
        final int pointerCount = mKeyPreviewTexts.size();
        for (int id = 0; id < pointerCount; id++) {
@@ -824,18 +765,15 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        PointerTracker.setReleasedKeyGraphicsToAllKeys();
    }

    @Override
    public void dismissKeyPreview(final PointerTracker tracker) {
        mDrawingHandler.dismissKeyPreview(mKeyPreviewLingerTimeout, tracker);
    }

    private void addKeyPreview(final TextView keyPreview) {
    // TODO: Move this to MainKeyboardView
    protected void addKeyPreview(final TextView keyPreview) {
        locatePreviewPlacerView();
        mPreviewPlacerView.addView(
                keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacerView, 0, 0));
    }

    private void locatePreviewPlacerView() {
    // TODO: Move this to MainKeyboardView
    protected void locatePreviewPlacerView() {
        if (mPreviewPlacerView.getParent() != null) {
            return;
        }
@@ -866,17 +804,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        }
    }

    @Override
    public void showSlidingKeyInputPreview(final PointerTracker tracker) {
        locatePreviewPlacerView();
        mPreviewPlacerView.showSlidingKeyInputPreview(tracker);
    }

    @Override
    public void dismissSlidingKeyInputPreview() {
        mPreviewPlacerView.dismissSlidingKeyInputPreview();
    }

    public void showGestureFloatingPreviewText(final SuggestedWords suggestedWords) {
        locatePreviewPlacerView();
        mPreviewPlacerView.setGestureFloatingPreviewText(suggestedWords);
@@ -887,107 +814,13 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
        mDrawingHandler.dismissGestureFloatingPreviewText(mGestureFloatingPreviewTextLingerTimeout);
    }

    @Override
    // TODO: Move This to MainKeyboardView
    public void showGesturePreviewTrail(final PointerTracker tracker,
            final boolean isOldestTracker) {
        locatePreviewPlacerView();
        mPreviewPlacerView.invalidatePointer(tracker, isOldestTracker);
    }

    @Override
    public void showKeyPreview(final PointerTracker tracker) {
        final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams;
        if (!mShowKeyPreviewPopup) {
            previewParams.mPreviewVisibleOffset = -mKeyboard.mVerticalGap;
            return;
        }

        final TextView previewText = getKeyPreviewText(tracker.mPointerId);
        // If the key preview has no parent view yet, add it to the ViewGroup which can place
        // key preview absolutely in SoftInputWindow.
        if (previewText.getParent() == null) {
            addKeyPreview(previewText);
        }

        mDrawingHandler.cancelDismissKeyPreview(tracker);
        final Key key = tracker.getKey();
        // If key is invalid or IME is already closed, we must not show key preview.
        // Trying to show key preview while root window is closed causes
        // WindowManager.BadTokenException.
        if (key == null) {
            return;
        }

        final KeyDrawParams drawParams = mKeyDrawParams;
        previewText.setTextColor(drawParams.mPreviewTextColor);
        final Drawable background = previewText.getBackground();
        if (background != null) {
            background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE);
            background.setAlpha(PREVIEW_ALPHA);
        }
        final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel;
        // What we show as preview should match what we show on a key top in onDraw().
        if (label != null) {
            // TODO Should take care of temporaryShiftLabel here.
            previewText.setCompoundDrawables(null, null, null, null);
            if (StringUtils.codePointCount(label) > 1) {
                previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, drawParams.mLetterSize);
                previewText.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, drawParams.mPreviewTextSize);
                previewText.setTypeface(key.selectTypeface(drawParams));
            }
            previewText.setText(label);
        } else {
            previewText.setCompoundDrawables(null, null, null,
                    key.getPreviewIcon(mKeyboard.mIconsSet));
            previewText.setText(null);
        }

        previewText.measure(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int keyDrawWidth = key.getDrawWidth();
        final int previewWidth = previewText.getMeasuredWidth();
        final int previewHeight = mKeyPreviewHeight;
        // The width and height of visible part of the key preview background. The content marker
        // of the background 9-patch have to cover the visible part of the background.
        previewParams.mPreviewVisibleWidth = previewWidth - previewText.getPaddingLeft()
                - previewText.getPaddingRight();
        previewParams.mPreviewVisibleHeight = previewHeight - previewText.getPaddingTop()
                - previewText.getPaddingBottom();
        // The distance between the top edge of the parent key and the bottom of the visible part
        // of the key preview background.
        previewParams.mPreviewVisibleOffset = mKeyPreviewOffset - previewText.getPaddingBottom();
        getLocationInWindow(mOriginCoords);
        // The key preview is horizontally aligned with the center of the visible part of the
        // parent key. If it doesn't fit in this {@link KeyboardView}, it is moved inward to fit and
        // the left/right background is used if such background is specified.
        final int statePosition;
        int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2
                + CoordinateUtils.x(mOriginCoords);
        if (previewX < 0) {
            previewX = 0;
            statePosition = STATE_LEFT;
        } else if (previewX > getWidth() - previewWidth) {
            previewX = getWidth() - previewWidth;
            statePosition = STATE_RIGHT;
        } else {
            statePosition = STATE_MIDDLE;
        }
        // The key preview is placed vertically above the top edge of the parent key with an
        // arbitrary offset.
        final int previewY = key.mY - previewHeight + mKeyPreviewOffset
                + CoordinateUtils.y(mOriginCoords);

        if (background != null) {
            final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL;
            background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]);
        }
        ViewLayoutUtils.placeViewAt(
                previewText, previewX, previewY, previewWidth, previewHeight);
        previewText.setVisibility(VISIBLE);
    }

    /**
     * Requests a redraw of the entire keyboard. Calling {@link #invalidate} is not sufficient
     * because the keyboard renders the keys to an off-screen buffer and an invalidate() only
@@ -1007,7 +840,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     * @param key key in the attached {@link Keyboard}.
     * @see #invalidateAllKeys
     */
    @Override
    public void invalidateKey(final Key key) {
        if (mInvalidateAllKeys) return;
        if (key == null) return;
+183 −2

File changed.

Preview size limit exceeded, changes collapsed.

+6 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.graphics.drawable.Drawable;
import android.view.View;

import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
@@ -262,9 +263,11 @@ public final class MoreKeysKeyboard extends Keyboard {
         * @param containerView the container of {@link MoreKeysKeyboardView}.
         * @param parentKey the {@link Key} that invokes more keys keyboard.
         * @param parentKeyboardView the {@link KeyboardView} that contains the parentKey.
         * @param keyPreviewDrawParams the parameter to place key preview.
         */
        public Builder(final View containerView, final Key parentKey,
                final KeyboardView parentKeyboardView) {
                final MainKeyboardView parentKeyboardView,
                final KeyPreviewDrawParams keyPreviewDrawParams) {
            super(containerView.getContext(), new MoreKeysKeyboardParams());
            final Keyboard parentKeyboard = parentKeyboardView.getKeyboard();
            load(parentKeyboard.mMoreKeysTemplate, parentKeyboard.mId);
@@ -285,8 +288,8 @@ public final class MoreKeysKeyboard extends Keyboard {
                // left/right/top paddings. The bottom paddings of both backgrounds don't need to
                // be considered because the vertical positions of both backgrounds were already
                // adjusted with their bottom paddings deducted.
                width = parentKeyboardView.mKeyPreviewDrawParams.mPreviewVisibleWidth;
                height = parentKeyboardView.mKeyPreviewDrawParams.mPreviewVisibleHeight
                width = keyPreviewDrawParams.mPreviewVisibleWidth;
                height = keyPreviewDrawParams.mPreviewVisibleHeight
                        + mParams.mVerticalGap;
            } else {
                width = getMaxKeyWidth(parentKeyboardView, parentKey, mParams.mDefaultKeyWidth);
Loading