Skip to content

Commit b3ca0fe

Browse files
Gilles DebunneAndroid (Google) Code Review
authored andcommitted
Merge "Touch slop added to double tap detection"
2 parents bdf7f96 + c171402 commit b3ca0fe

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

core/java/android/widget/TextView.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ static class InputMethodState {
357357
private float mLastDownPositionX, mLastDownPositionY;
358358
private Callback mCustomSelectionActionModeCallback;
359359

360-
private final int mSquaredTouchSlopDistance;
361360
// Set when this TextView gained focus with some text selected. Will start selection mode.
362361
private boolean mCreatedWithASelection = false;
363362

@@ -443,15 +442,12 @@ public TextView(Context context) {
443442
this(context, null);
444443
}
445444

446-
public TextView(Context context,
447-
AttributeSet attrs) {
445+
public TextView(Context context, AttributeSet attrs) {
448446
this(context, attrs, com.android.internal.R.attr.textViewStyle);
449447
}
450448

451449
@SuppressWarnings("deprecation")
452-
public TextView(Context context,
453-
AttributeSet attrs,
454-
int defStyle) {
450+
public TextView(Context context, AttributeSet attrs, int defStyle) {
455451
super(context, attrs, defStyle);
456452
mText = "";
457453

@@ -1134,10 +1130,6 @@ public TextView(Context context,
11341130
setLongClickable(longClickable);
11351131

11361132
prepareCursorControllers();
1137-
1138-
final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
1139-
final int touchSlop = viewConfiguration.getScaledTouchSlop();
1140-
mSquaredTouchSlopDistance = touchSlop * touchSlop;
11411133
}
11421134

11431135
private void setTypefaceByIndex(int typefaceIndex, int styleIndex) {
@@ -3202,8 +3194,7 @@ private void setText(CharSequence text, BufferType type,
32023194

32033195
int n = mFilters.length;
32043196
for (int i = 0; i < n; i++) {
3205-
CharSequence out = mFilters[i].filter(text, 0, text.length(),
3206-
EMPTY_SPANNED, 0, 0);
3197+
CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
32073198
if (out != null) {
32083199
text = out;
32093200
}
@@ -5619,11 +5610,13 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
56195610
return super.onKeyUp(keyCode, event);
56205611
}
56215612

5622-
@Override public boolean onCheckIsTextEditor() {
5613+
@Override
5614+
public boolean onCheckIsTextEditor() {
56235615
return mInputType != EditorInfo.TYPE_NULL;
56245616
}
56255617

5626-
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5618+
@Override
5619+
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
56275620
if (onCheckIsTextEditor() && isEnabled()) {
56285621
if (mInputMethodState == null) {
56295622
mInputMethodState = new InputMethodState();
@@ -9246,7 +9239,6 @@ public boolean performLongClick() {
92469239
boolean vibrate = true;
92479240

92489241
if (super.performLongClick()) {
9249-
mDiscardNextActionUp = true;
92509242
handled = true;
92519243
}
92529244

@@ -10799,7 +10791,12 @@ public boolean onTouchEvent(MotionEvent ev) {
1079910791
final float deltaX = mDownPositionX - ev.getRawX();
1080010792
final float deltaY = mDownPositionY - ev.getRawY();
1080110793
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
10802-
if (distanceSquared < mSquaredTouchSlopDistance) {
10794+
10795+
final ViewConfiguration viewConfiguration = ViewConfiguration.get(
10796+
TextView.this.getContext());
10797+
final int touchSlop = viewConfiguration.getScaledTouchSlop();
10798+
10799+
if (distanceSquared < touchSlop * touchSlop) {
1080310800
if (mActionPopupWindow != null && mActionPopupWindow.isShowing()) {
1080410801
// Tapping on the handle dismisses the displayed action popup
1080510802
mActionPopupWindow.hide();
@@ -11013,7 +11010,8 @@ private class SelectionModifierCursorController implements CursorController {
1101311010

1101411011
// Double tap detection
1101511012
private long mPreviousTapUpTime = 0;
11016-
private float mPreviousTapPositionX, mPreviousTapPositionY;
11013+
private float mDownPositionX, mDownPositionY;
11014+
private boolean mGestureStayedInTapRegion;
1101711015

1101811016
SelectionModifierCursorController() {
1101911017
resetTouchOffsets();
@@ -11076,20 +11074,28 @@ public void onTouchEvent(MotionEvent event) {
1107611074
mMinTouchOffset = mMaxTouchOffset = getOffsetForPosition(x, y);
1107711075

1107811076
// Double tap detection
11079-
long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime;
11080-
if (duration <= ViewConfiguration.getDoubleTapTimeout() &&
11081-
isPositionOnText(x, y)) {
11082-
final float deltaX = x - mPreviousTapPositionX;
11083-
final float deltaY = y - mPreviousTapPositionY;
11084-
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
11085-
if (distanceSquared < mSquaredTouchSlopDistance) {
11086-
startSelectionActionMode();
11087-
mDiscardNextActionUp = true;
11077+
if (mGestureStayedInTapRegion) {
11078+
long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime;
11079+
if (duration <= ViewConfiguration.getDoubleTapTimeout()) {
11080+
final float deltaX = x - mDownPositionX;
11081+
final float deltaY = y - mDownPositionY;
11082+
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
11083+
11084+
ViewConfiguration viewConfiguration = ViewConfiguration.get(
11085+
TextView.this.getContext());
11086+
int doubleTapSlop = viewConfiguration.getScaledDoubleTapSlop();
11087+
boolean stayedInArea = distanceSquared < doubleTapSlop * doubleTapSlop;
11088+
11089+
if (stayedInArea && isPositionOnText(x, y)) {
11090+
startSelectionActionMode();
11091+
mDiscardNextActionUp = true;
11092+
}
1108811093
}
1108911094
}
1109011095

11091-
mPreviousTapPositionX = x;
11092-
mPreviousTapPositionY = y;
11096+
mDownPositionX = x;
11097+
mDownPositionY = y;
11098+
mGestureStayedInTapRegion = true;
1109311099
break;
1109411100

1109511101
case MotionEvent.ACTION_POINTER_DOWN:
@@ -11102,6 +11108,22 @@ public void onTouchEvent(MotionEvent event) {
1110211108
}
1110311109
break;
1110411110

11111+
case MotionEvent.ACTION_MOVE:
11112+
if (mGestureStayedInTapRegion) {
11113+
final float deltaX = event.getX() - mDownPositionX;
11114+
final float deltaY = event.getY() - mDownPositionY;
11115+
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
11116+
11117+
final ViewConfiguration viewConfiguration = ViewConfiguration.get(
11118+
TextView.this.getContext());
11119+
int doubleTapTouchSlop = viewConfiguration.getScaledDoubleTapTouchSlop();
11120+
11121+
if (distanceSquared > doubleTapTouchSlop * doubleTapTouchSlop) {
11122+
mGestureStayedInTapRegion = false;
11123+
}
11124+
}
11125+
break;
11126+
1110511127
case MotionEvent.ACTION_UP:
1110611128
mPreviousTapUpTime = SystemClock.uptimeMillis();
1110711129
break;

0 commit comments

Comments
 (0)