Fix key press

This commit is contained in:
J-Jamet committed 2022-01-26 14:23:45 +01:00
1 parent 1d16ad764f
commit 910c45a6be
2 files changed
+113 -42

No files matched your search

@@ -55,10 +55,10 @@ import java.util.StringTokenizer;
* </Keyboard>
* </pre>
*
* @attr ref android.R.styleable#Keyboard_keyWidth
* @attr ref android.R.styleable#Keyboard_keyHeight
* @attr ref android.R.styleable#Keyboard_horizontalGap
* @attr ref android.R.styleable#Keyboard_verticalGap
* ref android.R.styleable#Keyboard_keyWidth
* ref android.R.styleable#Keyboard_keyHeight
* ref android.R.styleable#Keyboard_horizontalGap
* ref android.R.styleable#Keyboard_verticalGap
*/
public class Keyboard {
@@ -166,19 +166,19 @@ public class Keyboard {
*/
private static float SEARCH_DISTANCE = 1.8f;
private ArrayList<Row> rows = new ArrayList<Row>();
private ArrayList<Row> rows = new ArrayList<>();
/**
* Container for keys in the keyboard. All keys in a row are at the same Y-coordinate.
* Some of the key size defaults can be overridden per row from what the {@link Keyboard}
* defines.
*
* @attr ref android.R.styleable#Keyboard_keyWidth
* @attr ref android.R.styleable#Keyboard_keyHeight
* @attr ref android.R.styleable#Keyboard_horizontalGap
* @attr ref android.R.styleable#Keyboard_verticalGap
* @attr ref android.R.styleable#Keyboard_Row_rowEdgeFlags
* @attr ref android.R.styleable#Keyboard_Row_keyboardMode
* ref R.styleable#Keyboard_keyWidth
* ref R.styleable#Keyboard_keyHeight
* ref R.styleable#Keyboard_horizontalGap
* ref R.styleable#Keyboard_verticalGap
* ref R.styleable#Keyboard_Row_rowEdgeFlags
* ref R.styleable#Keyboard_Row_keyboardMode
*/
public static class Row {
/**
@@ -229,30 +229,30 @@ public class Keyboard {
parent.mDisplayWidth, parent.mDefaultHorizontalGap);
verticalGap = getDimensionOrFraction(a, R.styleable.Keyboard_verticalGap,
parent.mDisplayHeight, parent.mDefaultVerticalGap);
a.recycle();
a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Row);
rowEdgeFlags = a.getInt(R.styleable.Keyboard_Row_rowEdgeFlags, 0);
mode = a.getResourceId(R.styleable.Keyboard_Row_keyboardMode, 0);
a.recycle();
}
}
/**
* Class for describing the position and characteristics of a single key in the keyboard.
*
* @attr ref R.styleable#Keyboard_keyWidth
* @attr ref R.styleable#Keyboard_keyHeight
* @attr ref R.styleable#Keyboard_horizontalGap
* @attr ref R.styleable#Keyboard_Key_codes
* @attr ref R.styleable#Keyboard_Key_keyIcon
* @attr ref R.styleable#Keyboard_Key_keyLabel
* @attr ref R.styleable#Keyboard_Key_iconPreview
* @attr ref R.styleable#Keyboard_Key_isSticky
* @attr ref R.styleable#Keyboard_Key_isRepeatable
* @attr ref R.styleable#Keyboard_Key_isModifier
* @attr ref R.styleable#Keyboard_Key_popupKeyboard
* @attr ref R.styleable#Keyboard_Key_popupCharacters
* @attr ref R.styleable#Keyboard_Key_keyOutputText
* @attr ref R.styleable#Keyboard_Key_keyEdgeFlags
* ref R.styleable#Keyboard_keyWidth
* ref R.styleable#Keyboard_keyHeight
* ref R.styleable#Keyboard_horizontalGap
* ref R.styleable#Keyboard_Key_codes
* ref R.styleable#Keyboard_Key_keyIcon
* ref R.styleable#Keyboard_Key_keyLabel
* ref R.styleable#Keyboard_Key_iconPreview
* ref R.styleable#Keyboard_Key_isSticky
* ref R.styleable#Keyboard_Key_isRepeatable
* ref R.styleable#Keyboard_Key_isModifier
* ref R.styleable#Keyboard_Key_popupKeyboard
* ref R.styleable#Keyboard_Key_popupCharacters
* ref R.styleable#Keyboard_Key_keyOutputText
* ref R.styleable#Keyboard_Key_keyEdgeFlags
*/
public static class Key {
/**
@@ -512,14 +512,10 @@ public class Keyboard {
boolean rightEdge = (edgeFlags & EDGE_RIGHT) > 0;
boolean topEdge = (edgeFlags & EDGE_TOP) > 0;
boolean bottomEdge = (edgeFlags & EDGE_BOTTOM) > 0;
if ((x >= this.x || (leftEdge && x <= this.x + this.width))
return (x >= this.x || (leftEdge && x <= this.x + this.width))
&& (x < this.x + this.width || (rightEdge && x >= this.x))
&& (y >= this.y || (topEdge && y <= this.y + this.height))
&& (y < this.y + this.height || (bottomEdge && y >= this.y))) {
return true;
} else {
return false;
}
&& (y < this.y + this.height || (bottomEdge && y >= this.y));
}
/**
@@ -59,15 +59,15 @@ import java.util.Map;
* A view that renders a virtual {@link Keyboard}. It handles rendering of keys and
* detecting key presses and touch movements.
*
* @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_labelTextSize
* @attr ref R.styleable#KeyboardView_keyTextSize
* @attr ref R.styleable#KeyboardView_keyTextColor
* @attr ref R.styleable#KeyboardView_verticalCorrection
* @attr ref R.styleable#KeyboardView_popupLayout
* ref R.styleable#KeyboardView_keyBackground
* ref R.styleable#KeyboardView_keyPreviewLayout
* ref R.styleable#KeyboardView_keyPreviewOffset
* ref R.styleable#KeyboardView_keyPreviewHeight
* ref R.styleable#KeyboardView_labelTextSize
* ref R.styleable#KeyboardView_keyTextSize
* ref R.styleable#KeyboardView_keyTextColor
* ref R.styleable#KeyboardView_verticalCorrection
* ref R.styleable#KeyboardView_popupLayout
*/
public class KeyboardView extends View implements View.OnClickListener {
@@ -140,6 +140,7 @@ public class KeyboardView extends View implements View.OnClickListener {
private static final int[] LONG_PRESSABLE_STATE_SET = {R.attr.state_long_pressable};
private Keyboard mKeyboard;
private int mCurrentKeyIndex = NOT_A_KEY;
private int mLabelTextSize;
private int mKeyTextSize;
private int mKeyTextColor;
@@ -183,6 +184,7 @@ public class KeyboardView extends View implements View.OnClickListener {
private int mProximityThreshold;
private boolean mPreviewCentered = false;
private boolean mShowPreview = true;
private boolean mShowTouchPoints = true;
private int mPopupPreviewX;
private int mPopupPreviewY;
@@ -329,6 +331,8 @@ public class KeyboardView extends View implements View.OnClickListener {
mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
} else {
mShowPreview = false;
}
mPreviewPopup.setTouchable(false);
@@ -464,6 +468,9 @@ public class KeyboardView extends View implements View.OnClickListener {
* @see #getKeyboard()
*/
public void setKeyboard(Keyboard keyboard) {
if (mKeyboard != null) {
showPreview(NOT_A_KEY);
}
// Remove any pending messages
removeMessages();
mKeyboard = keyboard;
@@ -522,6 +529,27 @@ public class KeyboardView extends View implements View.OnClickListener {
return false;
}
/**
* 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 popup
* @see #isPreviewEnabled()
*/
public void setPreviewEnabled(boolean previewEnabled) {
mShowPreview = previewEnabled;
}
/**
* Returns the enabled state of the key feedback popup.
*
* @return whether or not the key feedback popup is enabled
* @see #setPreviewEnabled(boolean)
*/
public boolean isPreviewEnabled() {
return mShowPreview;
}
public void setPopupParent(View v) {
mPopupParent = v;
}
@@ -838,6 +866,48 @@ public class KeyboardView extends View implements View.OnClickListener {
}
}
private void showPreview(int keyIndex) {
int oldKeyIndex = mCurrentKeyIndex;
final PopupWindow previewPopup = mPreviewPopup;
mCurrentKeyIndex = keyIndex;
// Release the old key and press the new key
final Key[] keys = mKeys;
if (oldKeyIndex != mCurrentKeyIndex) {
if (oldKeyIndex != NOT_A_KEY && keys.length > oldKeyIndex) {
Key oldKey = keys[oldKeyIndex];
oldKey.onReleased(mCurrentKeyIndex == NOT_A_KEY);
invalidateKey(oldKeyIndex);
}
if (mCurrentKeyIndex != NOT_A_KEY && keys.length > mCurrentKeyIndex) {
Key newKey = keys[mCurrentKeyIndex];
newKey.onPressed();
invalidateKey(mCurrentKeyIndex);
}
}
// If key changed and preview is on ...
if (oldKeyIndex != mCurrentKeyIndex && mShowPreview) {
mHandler.removeMessages(MSG_SHOW_PREVIEW);
if (previewPopup.isShowing()) {
if (keyIndex == NOT_A_KEY) {
mHandler.sendMessageDelayed(mHandler
.obtainMessage(MSG_REMOVE_PREVIEW),
DELAY_AFTER_PREVIEW);
}
}
if (keyIndex != NOT_A_KEY) {
if (previewPopup.isShowing() && mPreviewText.getVisibility() == VISIBLE) {
// Show right away, if it's already visible and finger is moving around
showKey(keyIndex);
} else {
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_SHOW_PREVIEW, keyIndex, 0),
DELAY_BEFORE_PREVIEW);
}
}
}
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Key[] keys = mKeys;
@@ -872,7 +942,6 @@ public class KeyboardView extends View implements View.OnClickListener {
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();
}
@@ -961,6 +1030,7 @@ public class KeyboardView extends View implements View.OnClickListener {
boolean result = onLongPress(popupKey);
if (result) {
mAbortKey = true;
showPreview(NOT_A_KEY);
}
return result;
}
@@ -1120,6 +1190,7 @@ public class KeyboardView extends View implements View.OnClickListener {
}
if (mGestureDetector.onTouchEvent(me)) {
showPreview(NOT_A_KEY);
mHandler.removeMessages(MSG_REPEAT);
mHandler.removeMessages(MSG_LONGPRESS);
return true;
@@ -1163,6 +1234,7 @@ public class KeyboardView extends View implements View.OnClickListener {
Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me);
mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
}
showPreview(keyIndex);
break;
case MotionEvent.ACTION_MOVE:
@@ -1196,6 +1268,7 @@ public class KeyboardView extends View implements View.OnClickListener {
mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
}
}
showPreview(mCurrentKey);
mLastMoveTime = eventTime;
break;
@@ -1216,6 +1289,7 @@ public class KeyboardView extends View implements View.OnClickListener {
touchX = mLastCodeX;
touchY = mLastCodeY;
}
showPreview(NOT_A_KEY);
Arrays.fill(mKeyIndices, NOT_A_KEY);
// If we're not on a repeating key (which sends on a DOWN event)
if (mRepeatKeyIndex == NOT_A_KEY && !mMiniKeyboardOnScreen && !mAbortKey) {
@@ -1228,6 +1302,7 @@ public class KeyboardView extends View implements View.OnClickListener {
removeMessages();
dismissPopupKeyboard();
mAbortKey = true;
showPreview(NOT_A_KEY);
invalidateKey(mCurrentKey);
break;
}