Cancelling previous touch interaction when a new touch starts

Bug: 79220524
Change-Id: I7157dcdc7af92e1e5689da56c87961a82a0e6a05
This commit is contained in:
Sunny Goyal
2018-10-01 15:56:36 -07:00
parent 15dba75e55
commit 6c3f44b663
2 changed files with 56 additions and 12 deletions

View File

@@ -16,6 +16,10 @@
package com.android.launcher3.views;
import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_UP;
import static com.android.launcher3.Utilities.SINGLE_FRAME_MS;
import android.content.Context;
@@ -79,6 +83,9 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
protected TouchController mActiveController;
private TouchCompleteListener mTouchCompleteListener;
// Object controlling the current touch interaction
private Object mCurrentTouchOwner;
public BaseDragLayer(Context context, AttributeSet attrs, int alphaChannelCount) {
super(context, attrs);
mActivity = (T) ActivityContext.lookupContext(context);
@@ -94,7 +101,7 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
if (action == ACTION_UP || action == ACTION_CANCEL) {
if (mTouchCompleteListener != null) {
mTouchCompleteListener.onTouchComplete();
}
@@ -177,7 +184,7 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
if (action == ACTION_UP || action == ACTION_CANCEL) {
if (mTouchCompleteListener != null) {
mTouchCompleteListener.onTouchComplete();
}
@@ -192,6 +199,37 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return verifyTouchDispatch(this, ev) && super.dispatchTouchEvent(ev);
}
/**
* Returns true if the {@param caller} is allowed to dispatch {@param ev} on this view,
* false otherwise.
*/
public boolean verifyTouchDispatch(Object caller, MotionEvent ev) {
int action = ev.getAction();
if (action == ACTION_DOWN) {
if (mCurrentTouchOwner != null) {
// Another touch in progress.
ev.setAction(ACTION_CANCEL);
super.dispatchTouchEvent(ev);
ev.setAction(action);
}
mCurrentTouchOwner = caller;
return true;
}
if (mCurrentTouchOwner != caller) {
// Someone else is controlling the touch
return false;
}
if (action == ACTION_UP || action == ACTION_CANCEL) {
mCurrentTouchOwner = null;
}
return true;
}
/**
* Determine the rect of the descendant in this DragLayer's coordinates
*