Commit 76cb02a0 authored by Lijin Shen's avatar Lijin Shen Committed by Commit Bot

Add distance change on gesture swipe listener

Add distance change between the current motion event and the last motion
event.

Bug: 1142475
Change-Id: I365fb9d37c2138ac42ce46a8cafe6b8adf554bf2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518995
Commit-Queue: Lijin Shen <lazzzis@google.com>
Reviewed-by: default avatarJinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarSinan Sahin <sinansahin@google.com>
Cr-Commit-Position: refs/heads/master@{#824760}
parent 52d33a46
...@@ -52,7 +52,6 @@ android_library("java") { ...@@ -52,7 +52,6 @@ android_library("java") {
"java/src/org/chromium/components/browser_ui/widget/displaystyle/ViewResizer.java", "java/src/org/chromium/components/browser_ui/widget/displaystyle/ViewResizer.java",
"java/src/org/chromium/components/browser_ui/widget/dragreorder/DragReorderableListAdapter.java", "java/src/org/chromium/components/browser_ui/widget/dragreorder/DragReorderableListAdapter.java",
"java/src/org/chromium/components/browser_ui/widget/dragreorder/DragStateDelegate.java", "java/src/org/chromium/components/browser_ui/widget/dragreorder/DragStateDelegate.java",
"java/src/org/chromium/components/browser_ui/widget/gesture/EmptySwipeHandler.java",
"java/src/org/chromium/components/browser_ui/widget/gesture/SwipeGestureListener.java", "java/src/org/chromium/components/browser_ui/widget/gesture/SwipeGestureListener.java",
"java/src/org/chromium/components/browser_ui/widget/highlight/PulseDrawable.java", "java/src/org/chromium/components/browser_ui/widget/highlight/PulseDrawable.java",
"java/src/org/chromium/components/browser_ui/widget/highlight/PulseInterpolator.java", "java/src/org/chromium/components/browser_ui/widget/highlight/PulseInterpolator.java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.browser_ui.widget.gesture;
import android.view.MotionEvent;
import org.chromium.components.browser_ui.widget.gesture.SwipeGestureListener.SwipeHandler;
/**
* An empty implementation of {@link SwipeHandler}.
*/
public class EmptySwipeHandler implements SwipeHandler {
@Override
public void onSwipeStarted(int direction, MotionEvent ev) {}
@Override
public void onSwipeUpdated(MotionEvent start, MotionEvent current) {}
@Override
public void onSwipeFinished(MotionEvent end) {}
@Override
public void onFling(int direction, MotionEvent start, MotionEvent end) {}
@Override
public boolean isSwipeEnabled(int direction) {
return true;
}
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.components.browser_ui.widget.gesture; package org.chromium.components.browser_ui.widget.gesture;
import android.content.Context; import android.content.Context;
import android.graphics.PointF;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener; import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent; import android.view.MotionEvent;
...@@ -57,37 +58,52 @@ public class SwipeGestureListener extends SimpleOnGestureListener { ...@@ -57,37 +58,52 @@ public class SwipeGestureListener extends SimpleOnGestureListener {
* @param direction The {@link ScrollDirection} representing the swipe direction. * @param direction The {@link ScrollDirection} representing the swipe direction.
* @param ev The first down motion event triggering the swipe. * @param ev The first down motion event triggering the swipe.
*/ */
void onSwipeStarted(@ScrollDirection int direction, MotionEvent ev); default void onSwipeStarted(@ScrollDirection int direction, MotionEvent ev) {}
/** /**
* @param start The first down motion event triggering the swipe.
* @param current The move motion event triggering the current swipe. * @param current The move motion event triggering the current swipe.
* @param tx The horizontal difference between the start and the current position in px.
* @param ty The vertical difference between the start and the current position in px.
* @param distanceX The distance along the X axis that has been scrolled since the last call
* to onScroll.
* @param distanceY The distance along the Y axis that has been scrolled since the last call
* to onScroll.
*/ */
void onSwipeUpdated(MotionEvent start, MotionEvent current); default void onSwipeUpdated(
MotionEvent current, float tx, float ty, float distanceX, float distanceY) {}
/** /**
* @param end The last motion event canceling the swipe. * @param end The last motion event canceling the swipe.
*/ */
void onSwipeFinished(MotionEvent end); default void onSwipeFinished(MotionEvent end) {}
/** /**
* @param direction The {@link ScrollDirection} representing the swipe direction. * @param direction The {@link ScrollDirection} representing the swipe direction.
* @param start The first down motion event triggering the swipe. * @param current The first down motion event triggering the swipe.
* @param end The last motion event canceling the swipe. * @param tx The horizontal difference between the start and the current position in px.
* @param ty The vertical difference between the start and the current position in px.
* @param velocityX The velocity of this fling measured in pixels per second along the x
* axis.
* @param velocityY The velocity of this fling measured in pixels per second along the y
* axis.
*/ */
void onFling(@ScrollDirection int direction, MotionEvent start, MotionEvent end); default void onFling(@ScrollDirection int direction, MotionEvent current, float tx,
float ty, float velocityX, float velocityY) {}
/** /**
* @param direction The direction of the on-going swipe. * @param direction The direction of the on-going swipe.
* @return False if this direction should be ignored. * @return False if this direction should be ignored.
*/ */
boolean isSwipeEnabled(@ScrollDirection int direction); default boolean isSwipeEnabled(@ScrollDirection int direction) {
return true;
}
} }
/** /**
* The internal {@link GestureDetector} used to recognize swipe gestures. * The internal {@link GestureDetector} used to recognize swipe gestures.
*/ */
private final GestureDetector mGestureDetector; private final GestureDetector mGestureDetector;
private final PointF mMotionStartPoint = new PointF();
@ScrollDirection @ScrollDirection
private int mDirection = ScrollDirection.UNKNOWN; private int mDirection = ScrollDirection.UNKNOWN;
private final SwipeHandler mHandler; private final SwipeHandler mHandler;
...@@ -165,6 +181,11 @@ public class SwipeGestureListener extends SimpleOnGestureListener { ...@@ -165,6 +181,11 @@ public class SwipeGestureListener extends SimpleOnGestureListener {
// Swipe Recognition Helpers // Swipe Recognition Helpers
// ============================================================================================ // ============================================================================================
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (mHandler == null || e1 == null || e2 == null) return false; if (mHandler == null || e1 == null || e2 == null) return false;
...@@ -188,12 +209,14 @@ public class SwipeGestureListener extends SimpleOnGestureListener { ...@@ -188,12 +209,14 @@ public class SwipeGestureListener extends SimpleOnGestureListener {
if (direction != ScrollDirection.UNKNOWN && mHandler.isSwipeEnabled(direction)) { if (direction != ScrollDirection.UNKNOWN && mHandler.isSwipeEnabled(direction)) {
mDirection = direction; mDirection = direction;
mHandler.onSwipeStarted(direction, e1); mHandler.onSwipeStarted(direction, e2);
mMotionStartPoint.set(e2.getRawX(), e2.getRawY());
} }
} }
if (mDirection != ScrollDirection.UNKNOWN) { if (mDirection != ScrollDirection.UNKNOWN) {
mHandler.onSwipeUpdated(e1, e2); mHandler.onSwipeUpdated(e2, e2.getRawX() - mMotionStartPoint.x,
e2.getRawY() - mMotionStartPoint.y, distanceX, distanceY);
return true; return true;
} }
...@@ -205,7 +228,8 @@ public class SwipeGestureListener extends SimpleOnGestureListener { ...@@ -205,7 +228,8 @@ public class SwipeGestureListener extends SimpleOnGestureListener {
if (mHandler == null) return false; if (mHandler == null) return false;
if (mDirection != ScrollDirection.UNKNOWN) { if (mDirection != ScrollDirection.UNKNOWN) {
mHandler.onFling(mDirection, e1, e2); mHandler.onFling(mDirection, e2, e2.getRawX() - mMotionStartPoint.x,
e2.getRawY() - mMotionStartPoint.y, velocityX, velocityY);
return true; return true;
} }
......
...@@ -22,6 +22,7 @@ import org.robolectric.annotation.Config; ...@@ -22,6 +22,7 @@ import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.components.browser_ui.widget.gesture.SwipeGestureListener.ScrollDirection; import org.chromium.components.browser_ui.widget.gesture.SwipeGestureListener.ScrollDirection;
import org.chromium.components.browser_ui.widget.gesture.SwipeGestureListener.SwipeHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -35,7 +36,7 @@ public class SwipeGestureListenerTest { ...@@ -35,7 +36,7 @@ public class SwipeGestureListenerTest {
private SwipeGestureListener mListener; private SwipeGestureListener mListener;
@Mock @Mock
private EmptySwipeHandler mHandler; private SwipeHandler mHandler;
@Before @Before
public void setUp() { public void setUp() {
...@@ -79,7 +80,14 @@ public class SwipeGestureListenerTest { ...@@ -79,7 +80,14 @@ public class SwipeGestureListenerTest {
ArgumentCaptor<MotionEvent> argumentCaptor = ArgumentCaptor.forClass(MotionEvent.class); ArgumentCaptor<MotionEvent> argumentCaptor = ArgumentCaptor.forClass(MotionEvent.class);
Mockito.verify(mHandler).onSwipeStarted( Mockito.verify(mHandler).onSwipeStarted(
Mockito.eq(expectedDirection), argumentCaptor.capture()); Mockito.eq(expectedDirection), argumentCaptor.capture());
Assert.assertEquals(argumentCaptor.getValue().getRawX(), eventStream.get(0).getRawX(), 0.1); boolean found = false;
for (MotionEvent event : eventStream) {
if (Math.abs(event.getRawX() - argumentCaptor.getValue().getRawX()) < 0.1) {
found = true;
break;
}
}
Assert.assertTrue("Can not found the expected first move event", found);
Mockito.verify(mHandler).onSwipeFinished(argumentCaptor.capture()); Mockito.verify(mHandler).onSwipeFinished(argumentCaptor.capture());
Assert.assertEquals(argumentCaptor.getValue().getRawX(), Assert.assertEquals(argumentCaptor.getValue().getRawX(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment