android.inputmethodservice.Keyboard.Key Java Examples

The following examples show how to use android.inputmethodservice.Keyboard.Key. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Attaches a keyboard to this view. The keyboard can be switched at any time and the
 * view will re-layout itself to accommodate the keyboard.
 * @see Keyboard
 * @see #getKeyboard()
 * @param keyboard the keyboard to display in this view
 */
public void setKeyboard(Keyboard keyboard) {
    if (mKeyboard != null) {
        showPreview(NOT_A_KEY);
    }
    // Remove any pending messages
    removeMessages();
    mKeyboard = keyboard;
    List<Key> keys = mKeyboard.getKeys();
    mKeys = keys.toArray(new Key[keys.size()]);
    requestLayout();
    // Hint to reallocate the buffer if the size changed
    mKeyboardChanged = true;
    invalidateAllKeys();
    computeProximityThreshold(keyboard);
    mMiniKeyboardCache.clear(); // Not really necessary to do every time, but will free up views
    // Switching to a different keyboard should abort any pending keys so that the key up
    // doesn't get delivered to the old or new keyboard
    mAbortKey = true; // Until the next ACTION_DOWN
}
 
Example #2
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
private void checkMultiTap(long eventTime, int keyIndex) {
    if (keyIndex == NOT_A_KEY) return;
    Key key = mKeys[keyIndex];
    if (key.codes.length > 1) {
        mInMultiTap = true;
        if (eventTime < mLastTapTime + MULTITAP_INTERVAL
                && keyIndex == mLastSentIndex) {
            mTapCount = (mTapCount + 1) % key.codes.length;
            return;
        } else {
            mTapCount = -1;
            return;
        }

    } else if (key.codes[0] == CustomKeyboard.KEYCODE_SHIFT) {
        if (eventTime < mLastTapTime + MULTITAP_INTERVAL
                && keyIndex == mLastSentIndex) {
            mInMultiTap = true;
        }
    }

    if (eventTime > mLastTapTime + MULTITAP_INTERVAL || keyIndex != mLastSentIndex) {
        resetMultiTap();
    }
}
 
Example #3
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
private boolean openPopupIfRequired(MotionEvent me) {
    // Check if we have a popup layout specified first.
    if (mPopupLayout == 0) {
        return false;
    }
    if (mCurrentKey < 0 || mCurrentKey >= mKeys.length) {
        return false;
    }

    Key popupKey = mKeys[mCurrentKey];
    boolean result = onLongPress(popupKey);
    if (result) {
        mAbortKey = true;
        showPreview(NOT_A_KEY);
    }
    return result;
}
 
Example #4
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private boolean openPopupIfRequired(MotionEvent me) {
    // Check if we have a popup layout specified first.
    if (mPopupLayout == 0) {
        return false;
    }
    if (mCurrentKey < 0 || mCurrentKey >= mKeys.length) {
        return false;
    }

    Key popupKey = mKeys[mCurrentKey];
    boolean result = onLongPress(popupKey);
    if (result) {
        mAbortKey = true;
        showPreview(NOT_A_KEY);
    }
    return result;
}
 
Example #5
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void checkMultiTap(long eventTime, int keyIndex) {
    if (keyIndex == NOT_A_KEY) return;
    Key key = mKeys[keyIndex];
    if (key.codes.length > 1) {
        mInMultiTap = true;
        if (eventTime < mLastTapTime + MULTITAP_INTERVAL
                && keyIndex == mLastSentIndex) {
            mTapCount = (mTapCount + 1) % key.codes.length;
            return;
        } else {
            mTapCount = -1;
            return;
        }
    }
    if (eventTime > mLastTapTime + MULTITAP_INTERVAL || keyIndex != mLastSentIndex) {
        resetMultiTap();
    }
}
 
Example #6
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Attaches a keyboard to this view. The keyboard can be switched at any time and the
 * view will re-layout itself to accommodate the keyboard.
 *
 * @param keyboard the keyboard to display in this view
 * @see Keyboard
 * @see #getKeyboard()
 */
public void setKeyboard(Keyboard keyboard) {
    if (mKeyboard != null) {
        showPreview(NOT_A_KEY);
    }
    // Remove any pending messages
    removeMessages();
    mKeyboard = keyboard;
    List<Key> keys = mKeyboard.getKeys();
    mKeys = keys.toArray(new Key[keys.size()]);
    requestLayout();
    // Hint to reallocate the buffer if the size changed
    mKeyboardChanged = true;
    invalidateAllKeys();
    computeProximityThreshold(keyboard);
    mMiniKeyboardCache.clear(); // Not really necessary to do every time, but will free up views
    // Switching to a different keyboard should abort any pending keys so that the key up
    // doesn't get delivered to the old or new keyboard
    mAbortKey = true; // Until the next ACTION_DOWN
}
 
Example #7
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Attaches a keyboard to this view. The keyboard can be switched at any time and the
 * view will re-layout itself to accommodate the keyboard.
 * @see Keyboard
 * @see #getKeyboard()
 * @param keyboard the keyboard to display in this view
 */
public void setKeyboard(Keyboard keyboard) {
    if (mKeyboard != null) {
        showPreview(NOT_A_KEY);
    }
    // Remove any pending messages
    removeMessages();
    mKeyboard = keyboard;
    List<Key> keys = mKeyboard.getKeys();
    mKeys = keys.toArray(new Key[keys.size()]);
    requestLayout();
    // Hint to reallocate the buffer if the size changed
    mKeyboardChanged = true;
    invalidateAllKeys();
    computeProximityThreshold(keyboard);
    mMiniKeyboardCache.clear(); // Not really necessary to do every time, but will free up views
    // Switching to a different keyboard should abort any pending keys so that the key up
    // doesn't get delivered to the old or new keyboard
    mAbortKey = true; // Until the next ACTION_DOWN
}
 
Example #8
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private void checkMultiTap(long eventTime, int keyIndex) {
    if (keyIndex == NOT_A_KEY) return;
    Key key = mKeys[keyIndex];
    if (key.codes.length > 1) {
        mInMultiTap = true;
        if (eventTime < mLastTapTime + MULTITAP_INTERVAL
                && keyIndex == mLastSentIndex) {
            mTapCount = (mTapCount + 1) % key.codes.length;
            return;
        } else {
            mTapCount = -1;
            return;
        }
    }
    if (eventTime > mLastTapTime + MULTITAP_INTERVAL || keyIndex != mLastSentIndex) {
        resetMultiTap();
    }
}
 
Example #9
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private boolean openPopupIfRequired(MotionEvent me) {
    // Check if we have a popup layout specified first.
    if (mPopupLayout == 0) {
        return false;
    }
    if (mCurrentKey < 0 || mCurrentKey >= mKeys.length) {
        return false;
    }

    Key popupKey = mKeys[mCurrentKey];
    boolean result = onLongPress(popupKey);
    if (result) {
        mAbortKey = true;
        showPreview(NOT_A_KEY);
    }
    return result;
}
 
Example #10
Source File: ChineseZhuyinKeyboard.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
private String findLabelFromKey(int primaryCode) {
    for (Key key : mKeyboard.getKeys()) {
        if (key.codes[0] == primaryCode) {
            return "" + key.label;
        }
    }

    Log.e(LOGTAG, "Error can't find label from Zhuyin keys: " + primaryCode);
    return null;
}
 
Example #11
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Invalidates a key so that it will be redrawn on the next repaint. Use this method if only
 * one key is changing it's content. Any changes that affect the position or size of the key
 * may not be honored.
 * @param keyIndex the index of the key in the attached {@link Keyboard}.
 * @see #invalidateAllKeys
 */
public void invalidateKey(int keyIndex) {
    if (mKeys == null) return;
    if (keyIndex < 0 || keyIndex >= mKeys.length) {
        return;
    }
    final Key key = mKeys[keyIndex];
    mInvalidatedKey = key;
    mDirtyRect.union(key.x + getPaddingLeft(), key.y + getPaddingTop(),
            key.x + key.width + getPaddingLeft(), key.y + key.height + getPaddingTop());
    onBufferDraw();
    invalidate(key.x + getPaddingLeft(), key.y + getPaddingTop(),
            key.x + key.width + getPaddingLeft(), key.y + key.height + getPaddingTop());
}
 
Example #12
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Handle multi-tap keys by producing the key label for the current multi-tap state.
 */
private CharSequence getPreviewText(Key key) {
    if (mInMultiTap) {
        // Multi-tap
        mPreviewLabel.setLength(0);
        mPreviewLabel.append((char) key.codes[mTapCount < 0 ? 0 : mTapCount]);
        return adjustCase(mPreviewLabel);
    } else {
        return adjustCase(key.label);
    }
}
 
Example #13
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * We use our own Key.isInside implementation {@link Keyboard#isInside} as that one assumes that the
 * motion event is inside the key if it is an edge key.
 */
public boolean isInside(Key key, int x, int y) {
    if ((x >= key.x) && (x < key.x + key.width) && (y >= key.y) && (y < key.y + key.height)) {
        return true;
    } else {
        return false;
    }
}
 
Example #14
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Compute the average distance between adjacent keys (horizontally and vertically)
 * and square it to get the proximity threshold. We use a square here and in computing
 * the touch distance from a key's center to avoid taking a square root.
 * @param keyboard
 */
private void computeProximityThreshold(Keyboard keyboard) {
    if (keyboard == null) return;
    final Key[] keys = mKeys;
    if (keys == null) return;
    int length = keys.length;
    int dimensionSum = 0;
    for (int i = 0; i < length; i++) {
        Key key = keys[i];
        dimensionSum += Math.min(key.width, key.height) + key.gap;
    }
    if (dimensionSum < 0 || length == 0) return;
    mProximityThreshold = (int) (dimensionSum * 1.4f / length);
    mProximityThreshold *= mProximityThreshold; // Square it
}
 
Example #15
Source File: LatinKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private FPoint getTextPosition(Key zKey, boolean zLeft) {
    FPoint pp = new FPoint();

    //Left or Right side
    if (!zLeft) {
        pp.x = zKey.width - (zKey.width / 2.75f) + zKey.x + getPaddingLeft();
        pp.y = zKey.height / 3.0f + zKey.y + getPaddingTop();
    } else {
        pp.x = (zKey.width / 8.0f) + zKey.x + getPaddingLeft();
        pp.y = zKey.height / 3.0f + zKey.y + getPaddingTop();
    }

    return pp;
}
 
Example #16
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Invalidates a key so that it will be redrawn on the next repaint. Use this method if only
 * one key is changing it's content. Any changes that affect the position or size of the key
 * may not be honored.
 *
 * @param keyIndex the index of the key in the attached {@link Keyboard}.
 * @see #invalidateAllKeys
 */
public void invalidateKey(int keyIndex) {
    if (mKeys == null) return;
    if (keyIndex < 0 || keyIndex >= mKeys.length) {
        return;
    }
    final Key key = mKeys[keyIndex];
    mInvalidatedKey = key;
    mDirtyRect.union(key.x + mPaddingLeft, key.y + mPaddingTop,
            key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop);
    onBufferDraw();
    invalidate(key.x + mPaddingLeft, key.y + mPaddingTop,
            key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop);
}
 
Example #17
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the average distance between adjacent keys (horizontally and vertically)
 * and square it to get the proximity threshold. We use a square here and in computing
 * the touch distance from a key's center to avoid taking a square root.
 * @param keyboard
 */
private void computeProximityThreshold(Keyboard keyboard) {
    if (keyboard == null) return;
    final Key[] keys = mKeys;
    if (keys == null) return;
    int length = keys.length;
    int dimensionSum = 0;
    for (int i = 0; i < length; i++) {
        Key key = keys[i];
        dimensionSum += Math.min(key.width, key.height) + key.gap;
    }
    if (dimensionSum < 0 || length == 0) return;
    mProximityThreshold = (int) (dimensionSum * 1.4f / length);
    mProximityThreshold *= mProximityThreshold; // Square it
}
 
Example #18
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Handle multi-tap keys by producing the key label for the current multi-tap state.
 */
private CharSequence getPreviewText(Key key) {
    if (mInMultiTap) {
        // Multi-tap
        mPreviewLabel.setLength(0);
        mPreviewLabel.append((char) key.codes[mTapCount < 0 ? 0 : mTapCount]);
        return adjustCase(mPreviewLabel);
    } else {
        return adjustCase(key.label);
    }
}
 
Example #19
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Handle multi-tap keys by producing the key label for the current multi-tap state.
 */
private CharSequence getPreviewText(Key key) {
    if (mInMultiTap) {
        // Multi-tap
        mPreviewLabel.setLength(0);
        mPreviewLabel.append((char) key.codes[mTapCount < 0 ? 0 : mTapCount]);
        return adjustCase(mPreviewLabel);
    } else {
        return adjustCase(key.label);
    }
}
 
Example #20
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the average distance between adjacent keys (horizontally and vertically)
 * and square it to get the proximity threshold. We use a square here and in computing
 * the touch distance from a key's center to avoid taking a square root.
 *
 * @param keyboard
 */
private void computeProximityThreshold(Keyboard keyboard) {
    if (keyboard == null) return;
    final Key[] keys = mKeys;
    if (keys == null) return;
    int length = keys.length;
    int dimensionSum = 0;
    for (int i = 0; i < length; i++) {
        Key key = keys[i];
        dimensionSum += Math.min(key.width, key.height) + key.gap;
    }
    if (dimensionSum < 0 || length == 0) return;
    mProximityThreshold = (int) (dimensionSum * 1.4f / length);
    mProximityThreshold *= mProximityThreshold; // Square it
}
 
Example #21
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Invalidates a key so that it will be redrawn on the next repaint. Use this method if only
 * one key is changing it's content. Any changes that affect the position or size of the key
 * may not be honored.
 * @param keyIndex the index of the key in the attached {@link Keyboard}.
 * @see #invalidateAllKeys
 */
public void invalidateKey(int keyIndex) {
    if (mKeys == null) return;
    if (keyIndex < 0 || keyIndex >= mKeys.length) {
        return;
    }
    final Key key = mKeys[keyIndex];
    mInvalidatedKey = key;
    mDirtyRect.union(key.x + mPaddingLeft, key.y + mPaddingTop,
            key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop);
    onBufferDraw();
    invalidate(key.x + mPaddingLeft, key.y + mPaddingTop,
            key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop);
}
 
Example #22
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private boolean repeatKey() {
    Key key = mKeys[mRepeatKeyIndex];
    detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime);
    return true;
}
 
Example #23
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    TypedArray a = context.obtainStyledAttributes(
            attrs, android.R.styleable.KeyboardView, defStyleAttr, defStyleRes);

    LayoutInflater inflate =
            (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int previewLayout = 0;
    int keyTextSize = 0;

    int n = a.getIndexCount();

    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);

        switch (attr) {
        case com.android.internal.R.styleable.KeyboardView_keyBackground:
            mKeyBackground = a.getDrawable(attr);
            break;
        case com.android.internal.R.styleable.KeyboardView_verticalCorrection:
            mVerticalCorrection = a.getDimensionPixelOffset(attr, 0);
            break;
        case com.android.internal.R.styleable.KeyboardView_keyPreviewLayout:
            previewLayout = a.getResourceId(attr, 0);
            break;
        case com.android.internal.R.styleable.KeyboardView_keyPreviewOffset:
            mPreviewOffset = a.getDimensionPixelOffset(attr, 0);
            break;
        case com.android.internal.R.styleable.KeyboardView_keyPreviewHeight:
            mPreviewHeight = a.getDimensionPixelSize(attr, 80);
            break;
        case com.android.internal.R.styleable.KeyboardView_keyTextSize:
            mKeyTextSize = a.getDimensionPixelSize(attr, 18);
            break;
        case com.android.internal.R.styleable.KeyboardView_keyTextColor:
            mKeyTextColor = a.getColor(attr, 0xFF000000);
            break;
        case com.android.internal.R.styleable.KeyboardView_labelTextSize:
            mLabelTextSize = a.getDimensionPixelSize(attr, 14);
            break;
        case com.android.internal.R.styleable.KeyboardView_popupLayout:
            mPopupLayout = a.getResourceId(attr, 0);
            break;
        case com.android.internal.R.styleable.KeyboardView_shadowColor:
            mShadowColor = a.getColor(attr, 0);
            break;
        case com.android.internal.R.styleable.KeyboardView_shadowRadius:
            mShadowRadius = a.getFloat(attr, 0f);
            break;
        }
    }

    a = mContext.obtainStyledAttributes(
            com.android.internal.R.styleable.Theme);
    mBackgroundDimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);

    mPreviewPopup = new PopupWindow(context);
    if (previewLayout != 0) {
        mPreviewText = (TextView) inflate.inflate(previewLayout, null);
        mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
        mPreviewPopup.setContentView(mPreviewText);
        mPreviewPopup.setBackgroundDrawable(null);
    } else {
        mShowPreview = false;
    }

    mPreviewPopup.setTouchable(false);

    mPopupKeyboard = new PopupWindow(context);
    mPopupKeyboard.setBackgroundDrawable(null);
    //mPopupKeyboard.setClippingEnabled(false);

    mPopupParent = this;
    //mPredicting = true;

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setTextSize(keyTextSize);
    mPaint.setTextAlign(Align.CENTER);
    mPaint.setAlpha(255);

    mPadding = new Rect(0, 0, 0, 0);
    mMiniKeyboardCache = new HashMap<Key,View>();
    mKeyBackground.getPadding(mPadding);

    mSwipeThreshold = (int) (500 * getResources().getDisplayMetrics().density);
    mDisambiguateSwipe = getResources().getBoolean(
            com.android.internal.R.bool.config_swipeDisambiguation);

    mAccessibilityManager = AccessibilityManager.getInstance(context);
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    resetMultiTap();
}
 
Example #24
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
private boolean repeatKey() {
    Key key = mKeys[mRepeatKeyIndex];
    detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime);
    return true;
}
 
Example #25
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int getKeyIndices(int x, int y, int[] allKeys) {
    final Key[] keys = mKeys;
    int primaryIndex = NOT_A_KEY;
    int closestKey = NOT_A_KEY;
    int closestKeyDist = mProximityThreshold + 1;
    java.util.Arrays.fill(mDistances, Integer.MAX_VALUE);
    int [] nearestKeyIndices = mKeyboard.getNearestKeys(x, y);
    final int keyCount = nearestKeyIndices.length;
    for (int i = 0; i < keyCount; i++) {
        final Key key = keys[nearestKeyIndices[i]];
        int dist = 0;
        boolean isInside = key.isInside(x,y);
        if (isInside) {
            primaryIndex = nearestKeyIndices[i];
        }

        if (((mProximityCorrectOn
                && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold)
                || isInside)
                && key.codes[0] > 32) {
            // Find insertion point
            final int nCodes = key.codes.length;
            if (dist < closestKeyDist) {
                closestKeyDist = dist;
                closestKey = nearestKeyIndices[i];
            }

            if (allKeys == null) continue;

            for (int j = 0; j < mDistances.length; j++) {
                if (mDistances[j] > dist) {
                    // Make space for nCodes codes
                    System.arraycopy(mDistances, j, mDistances, j + nCodes,
                            mDistances.length - j - nCodes);
                    System.arraycopy(allKeys, j, allKeys, j + nCodes,
                            allKeys.length - j - nCodes);
                    for (int c = 0; c < nCodes; c++) {
                        allKeys[j + c] = key.codes[c];
                        mDistances[j + c] = dist;
                    }
                    break;
                }
            }
        }
    }
    if (primaryIndex == NOT_A_KEY) {
        primaryIndex = closestKey;
    }
    return primaryIndex;
}
 
Example #26
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft();
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = - mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example #27
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = - mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example #28
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
private int getKeyIndices(int x, int y, int[] allKeys) {
    final Key[] keys = mKeys;
    int primaryIndex = NOT_A_KEY;
    int closestKey = NOT_A_KEY;
    int closestKeyDist = mProximityThreshold + 1;
    java.util.Arrays.fill(mDistances, Integer.MAX_VALUE);
    int [] nearestKeyIndices = mKeyboard.getNearestKeys(x, y);
    final int keyCount = nearestKeyIndices.length;
    for (int i = 0; i < keyCount; i++) {
        final Key key = keys[nearestKeyIndices[i]];
        int dist = 0;
        boolean isInside = isInside(key, x,y);
        if (isInside) {
            primaryIndex = nearestKeyIndices[i];
        }

        if (((mProximityCorrectOn
                && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold)
                || isInside)
                && key.codes[0] > 32) {
            // Find insertion point
            final int nCodes = key.codes.length;
            if (dist < closestKeyDist) {
                closestKeyDist = dist;
                closestKey = nearestKeyIndices[i];
            }

            if (allKeys == null) continue;

            for (int j = 0; j < mDistances.length; j++) {
                if (mDistances[j] > dist) {
                    // Make space for nCodes codes
                    System.arraycopy(mDistances, j, mDistances, j + nCodes,
                            mDistances.length - j - nCodes);
                    System.arraycopy(allKeys, j, allKeys, j + nCodes,
                            allKeys.length - j - nCodes);
                    for (int c = 0; c < nCodes; c++) {
                        allKeys[j + c] = key.codes[c];
                        mDistances[j + c] = dist;
                    }
                    break;
                }
            }
        }
    }
    if (primaryIndex == NOT_A_KEY) {
        primaryIndex = closestKey;
    }
    return primaryIndex;
}
 
Example #29
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = -mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example #30
Source File: LatinKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    //Set the key positions correctly - ANDROID broken ?
    setKeyPositions();

    super.onDraw(canvas);

    //get The Keyboard..
    Keyboard keyb = getKeyboard();

    if (keyb == null) return;

    //Get the keys..
    List<Key> keys = keyb.getKeys();
    Key[] allkeys = keys.toArray(new Key[keys.size()]);

    int kh = allkeys[0].height;

    //Set the Text Height
    mWhitePaint.setTextSize(kh / 5.0f);
    mYellowPaint.setTextSize(kh / 5.0f);
    mGreenPaint.setTextSize(kh / 6.0f);
    mBluePaint.setTextSize(kh / 6.0f);
    mRedPaint.setTextSize(kh / 5.5f);

    //Now cycle and final int keyCount = keys.length;
    int keyCount = allkeys.length;
    for (int i = 0; i < keyCount; i++) {
        Key key = allkeys[i];

        FPoint fpleft = getTextPosition(key, true);
        FPoint fpright = getTextPosition(key, false);

        String label = key.label == null ? null : key.label.toString();
        if (label != null && label.length() >= 1) {
            //Get the Twin key pair
            String val = mKeyTwinPair.get(label);
            if (val == null) {
                if (mMode == MODE_SMALL) {
                    val = mModeNormKeyTwinPair.get(label);
                } else {
                    val = mModeLargeKeyTwinPair.get(label);
                }
            }
            if (val != null) {
                canvas.drawText(val, fpright.x, fpright.y, mWhitePaint);
            }

            //Check Symbol Pair
            val = mSymbolPair.get(label);
            if (val == null) {
                if (mMode == MODE_SMALL) {
                    val = mModeNormSymbolPair.get(label);
                } else {
                    val = mModeLargeSymbolPair.get(label);
                }
            }

            if (val != null) {
                if (val.startsWith("F")) {
                    if (mFunction) {
                        canvas.drawText(val, fpleft.x, fpleft.y, mGreenPaint);
                    } else {
                        canvas.drawText(val, fpleft.x, fpleft.y, mRedPaint);
                    }
                } else {
                    if (mMode == MODE_SMALL) {
                        canvas.drawText(val, fpleft.x, fpleft.y, mYellowPaint);
                    }
                }
            }

        } else if ((key.codes[0] <= 22 && key.codes[0] >= 19) || (key.codes[0] <= -150 && key.codes[0] >= -153)) {

            Paint kcol = mBluePaint;
            if (mFunction) {
                kcol = mGreenPaint;
            }

            //Arrow Keys
            float xstep = key.width / 6;
            float ystep = kh / 3.5f;

            if (key.codes[0] == 19 || key.codes[0] == -150) {
                canvas.drawText("PgUp", xstep + key.x + getPaddingLeft(), ystep + key.y + getPaddingTop(), kcol);
            } else if (key.codes[0] == 21 || key.codes[0] == -152) {
                canvas.drawText("Home", xstep + key.x + getPaddingLeft(), ystep + key.y + getPaddingTop(), kcol);
            } else if (key.codes[0] == 20 || key.codes[0] == -151) {
                canvas.drawText("PgDn", xstep + key.x + getPaddingLeft(), ystep + key.y + getPaddingTop(), kcol);
            } else if (key.codes[0] == 22 || key.codes[0] == -153) {
                canvas.drawText("End", xstep + key.x + getPaddingLeft(), ystep + key.y + getPaddingTop(), kcol);
            }
        }
    }
}