Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Make keyboard keys sticky (#2420)
Browse files Browse the repository at this point in the history
* nits

* Make keys sticky
  • Loading branch information
keianhzo authored and MortimerGoro committed Dec 9, 2019
1 parent 95cc90d commit 3c4b0df
Showing 1 changed file with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.input.CustomKeyboard;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -271,14 +274,45 @@ public interface OnKeyboardActionListener {
/** Whether the requirement of a headset to hear passwords if accessibility is enabled is announced. */
private boolean mHeadsetRequiredToHearPasswordsAnnounced;


// Fork
private Drawable mFeaturedKeyBackground;
private HashSet<Integer> mFeaturedKeyCodes = new HashSet<>();
private int mSelectedForegroundColor;
private int mForegroundColor;

Handler mHandler;
private Handler mHandler;

private static class MessageHandler extends Handler {
private WeakReference<CustomKeyboardView> mView;


public MessageHandler(@NonNull CustomKeyboardView view) {
mView = new WeakReference<>(view);
}

@Override
public void handleMessage(Message msg) {
if (mView.get() != null) {
switch (msg.what) {
case MSG_SHOW_PREVIEW:
mView.get().showKey(msg.arg1);
break;
case MSG_REMOVE_PREVIEW:
mView.get().getPreviewText().setVisibility(INVISIBLE);
break;
case MSG_REPEAT:
if (mView.get().repeatKey()) {
Message repeat = Message.obtain(this, MSG_REPEAT);
sendMessageDelayed(repeat, REPEAT_INTERVAL);
}
break;
case MSG_LONGPRESS:
mView.get().openPopupIfRequired((MotionEvent) msg.obj);
break;
}
}
}
}

public CustomKeyboardView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
Expand Down Expand Up @@ -359,28 +393,7 @@ protected void onAttachedToWindow() {
super.onAttachedToWindow();
initGestureDetector();
if (mHandler == null) {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SHOW_PREVIEW:
showKey(msg.arg1);
break;
case MSG_REMOVE_PREVIEW:
mPreviewText.setVisibility(INVISIBLE);
break;
case MSG_REPEAT:
if (repeatKey()) {
Message repeat = Message.obtain(this, MSG_REPEAT);
sendMessageDelayed(repeat, REPEAT_INTERVAL);
}
break;
case MSG_LONGPRESS:
openPopupIfRequired((MotionEvent) msg.obj);
break;
}
}
};
mHandler = new MessageHandler(this);
}
}

Expand Down Expand Up @@ -823,18 +836,18 @@ private void onBufferDraw() {

// Draw the text
canvas.drawText(label,
(key.width - padding.left - padding.right) / 2
(key.width - padding.left - padding.right) / 2.0f
+ padding.left + statePadding,
(key.height - padding.top - padding.bottom) / 2
(key.height - padding.top - padding.bottom) / 2.0f
+ (paint.getTextSize() / 2) - descent + padding.top + statePadding,
paint);
// Turn off drop shadow
paint.setShadowLayer(0, 0, 0, 0);

} else if (key.icon != null) {
final float drawableX = (key.width - padding.left - padding.right - key.icon.getIntrinsicWidth()) / 2
final float drawableX = (key.width - padding.left - padding.right - key.icon.getIntrinsicWidth()) / 2.0f
+ padding.left + statePadding;
final float drawableY = (key.height - padding.top - padding.bottom - key.icon.getIntrinsicHeight()) / 2
final float drawableY = (key.height - padding.top - padding.bottom - key.icon.getIntrinsicHeight()) / 2.0f
+ padding.top + statePadding;
canvas.translate(drawableX, drawableY);
key.icon.setColorFilter(targetColor, PorterDuff.Mode.MULTIPLY);
Expand Down Expand Up @@ -963,6 +976,10 @@ private void detectAndSendKey(int index, int x, int y, long eventTime) {
}
}

private TextView getPreviewText() {
return mPreviewText;
}

/**
* Handle multi-tap keys by producing the key label for the current multi-tap state.
*/
Expand Down Expand Up @@ -1243,7 +1260,7 @@ public boolean onHoverEvent(MotionEvent event) {
}

public void setFeaturedKeyBackground(int resId, int[] keyCodes) {
mFeaturedKeyBackground = getResources().getDrawable(resId);
mFeaturedKeyBackground = getResources().getDrawable(resId, getContext().getTheme());
mFeaturedKeyCodes.clear();
for (int value: keyCodes) {
mFeaturedKeyCodes.add(value);
Expand All @@ -1270,8 +1287,7 @@ private boolean onModifiedTouchEvent(MotionEvent me, boolean possiblePoly) {
mSwipeTracker.addMovement(me);

// Ignore all motion events until a DOWN.
if (mAbortKey
&& action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
if (mAbortKey && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
return true;
}

Expand Down Expand Up @@ -1336,12 +1352,11 @@ private boolean onModifiedTouchEvent(MotionEvent me, boolean possiblePoly) {
continueLongPress = true;
} else if (mRepeatKeyIndex == NOT_A_KEY) {
resetMultiTap();
mLastKey = mCurrentKey;
mLastKey = mDownKey;
mLastCodeX = mLastX;
mLastCodeY = mLastY;
mLastKeyTime =
mCurrentKeyTime + eventTime - mLastMoveTime;
mCurrentKey = keyIndex;
mLastKeyTime = mCurrentKeyTime + eventTime - mLastMoveTime;
mCurrentKey = mDownKey;
mCurrentKeyTime = 0;
}
}
Expand Down Expand Up @@ -1505,9 +1520,9 @@ private static class SwipeTracker {
static final int NUM_PAST = 4;
static final int LONGEST_PAST_TIME = 200;

final float mPastX[] = new float[NUM_PAST];
final float mPastY[] = new float[NUM_PAST];
final long mPastTime[] = new long[NUM_PAST];
final float[] mPastX = new float[NUM_PAST];
final float[] mPastY = new float[NUM_PAST];
final long[] mPastTime = new long[NUM_PAST];

float mYVelocity;
float mXVelocity;
Expand All @@ -1516,7 +1531,7 @@ public void clear() {
mPastTime[0] = 0;
}

public void addMovement(MotionEvent ev) {
void addMovement(MotionEvent ev) {
long time = ev.getEventTime();
final int N = ev.getHistorySize();
for (int i=0; i<N; i++) {
Expand Down Expand Up @@ -1560,11 +1575,11 @@ private void addPoint(float x, float y, long time) {
}
}

public void computeCurrentVelocity(int units) {
void computeCurrentVelocity(int units) {
computeCurrentVelocity(units, Float.MAX_VALUE);
}

public void computeCurrentVelocity(int units, float maxVelocity) {
void computeCurrentVelocity(int units, float maxVelocity) {
final float[] pastX = mPastX;
final float[] pastY = mPastY;
final long[] pastTime = mPastTime;
Expand Down Expand Up @@ -1601,11 +1616,11 @@ public void computeCurrentVelocity(int units, float maxVelocity) {
: Math.min(accumY, maxVelocity);
}

public float getXVelocity() {
float getXVelocity() {
return mXVelocity;
}

public float getYVelocity() {
float getYVelocity() {
return mYVelocity;
}
}
Expand Down

0 comments on commit 3c4b0df

Please sign in to comment.