Journey of an event, the android touch - Marco Cova, Facebook

44
Journey of an event The Android touch

Transcript of Journey of an event, the android touch - Marco Cova, Facebook

Page 1: Journey of an event, the android touch - Marco Cova, Facebook

Journey of an eventThe Android touch

Page 2: Journey of an event, the android touch - Marco Cova, Facebook

Marco Cova

Year in Review Riff

Page 3: Journey of an event, the android touch - Marco Cova, Facebook

User eXperience

Page 4: Journey of an event, the android touch - Marco Cova, Facebook
Page 5: Journey of an event, the android touch - Marco Cova, Facebook

What is it?

• InputEvent extended by

• Mouse, pen, finger, trackball.

MotionEventKeyEvent

Page 6: Journey of an event, the android touch - Marco Cova, Facebook

What is it?

• ACTION_DOWN

• ACTION_MOVE

• ACTION_UP - ACTION_CANCEL

Page 7: Journey of an event, the android touch - Marco Cova, Facebook

Journey of a touch event

Page 8: Journey of an event, the android touch - Marco Cova, Facebook

Journey of a touch eventActivity

ViewGroup

ViewGroup (DecorView)

View2View1

Activity

ViewGroup

View2

View1

Page 9: Journey of an event, the android touch - Marco Cova, Facebook

Journey of a touch eventActivity

ViewGroup

ViewGroup (DecorView)

View2View1

Activity

ViewGroup

View2

View1

Page 10: Journey of an event, the android touch - Marco Cova, Facebook

Journey of a touch eventActivity

ViewGroup

View2

View1

Activity

ViewGroup

ViewGroup (DecorView)

View2View1

Page 11: Journey of an event, the android touch - Marco Cova, Facebook

Activity

ViewGroup

View2

View1

ViewActivity

ViewGroup

ViewGroup (DecorView)

View2View1

Page 12: Journey of an event, the android touch - Marco Cova, Facebook

public boolean dispatchTouchEvent(MotionEvent event) { ...

if (..mOnTouchListener.onTouch(this, event)) { result = true; }

if (!result && onTouchEvent(event)) { result = true; } ... return result;}

View.java

Page 13: Journey of an event, the android touch - Marco Cova, Facebook

public boolean onTouchEvent(MotionEvent event)

• OnClickListener

• OnLongClickListener

View

public boolean onTouchEvent(MotionEvent event) {

Page 14: Journey of an event, the android touch - Marco Cova, Facebook

Activity

ViewGroup

View2

View1

ViewGroupActivity

ViewGroup (DecorView)

ViewGroup

View2View1

Page 15: Journey of an event, the android touch - Marco Cova, Facebook

@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (action == MotionEvent.ACTION_DOWN ...) { ArrayList<View> preorderedList = buildOrderedChildList(); for (int i = childrenCount -1; i >=0; i--) {

} } }

ViewGroup.java

Page 16: Journey of an event, the android touch - Marco Cova, Facebook

. . .

if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { continue;}

. . .

ViewGroup.java

Page 17: Journey of an event, the android touch - Marco Cova, Facebook

private boolean dispatchTransformedTouchEvent(…) { . . .

if (child == null) { handled = super.dispatchTouchEvent(transformedEvent); } else { handled = child.dispatchTouchEvent(transformedEvent); } . . .

return handled; }

ViewGroup.java

Page 18: Journey of an event, the android touch - Marco Cova, Facebook

Activity

ViewGroup

View2

View1

Transformed EventActivity

ViewGroup

View2

View1

Page 19: Journey of an event, the android touch - Marco Cova, Facebook
Page 20: Journey of an event, the android touch - Marco Cova, Facebook

@Override public boolean dispatchTouchEvent(MotionEvent ev) { . . .

if (!disallowIntercept) { intercepted = onInterceptTouchEvent(ev); }

. . . }

ViewGroup.java

Page 21: Journey of an event, the android touch - Marco Cova, Facebook

ViewGroup

• ACTION_CANCEL

• requestDisallowInterceptTouchEvent(boolean)

• State reset on ACTION_UP / CANCEL

Page 22: Journey of an event, the android touch - Marco Cova, Facebook
Page 23: Journey of an event, the android touch - Marco Cova, Facebook

What to do?!?

seekBar.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) {

if (event.getMaskAction() == MotionEvent.ACTION_DOWN) { v.getParent().requestDisallowInterceptTouchEvent(true); } return false; }});

Page 24: Journey of an event, the android touch - Marco Cova, Facebook
Page 25: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup:

ViewGroup:

View1:

View1:

ViewGroup:

ViewGroup

View2

dispatchTouchEvent(..)

onInterceptTouchEvent(..)

dispatchTouchEvent(..)

onTouchEvent(..)

onTouchEvent(..)View1

Page 26: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup:

ViewGroup:

View1:

View1:

ViewGroup:

ViewGroup

View2View1

dispatchTouchEvent(..)

onInterceptTouchEvent(..)

dispatchTouchEvent(..)

onTouchEvent(..)

onTouchEvent(..)

Page 27: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup:

ViewGroup:

View1:

View1:

ViewGroup:

ViewGroup

View2View1

dispatchTouchEvent(..)

onInterceptTouchEvent(..)

dispatchTouchEvent(..)

onTouchEvent(..)

onTouchEvent(..)

Page 28: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup:

ViewGroup:

View1:

View1:

ViewGroup:

ViewGroup

View2View1

dispatchTouchEvent(..)

onInterceptTouchEvent(..)

dispatchTouchEvent(..)

onTouchEvent(..)

onTouchEvent(..)

Page 29: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup:

ViewGroup:

View1:

View1:

ViewGroup:

ViewGroup

View2View1

dispatchTouchEvent(..)

onInterceptTouchEvent(..)

dispatchTouchEvent(..)

onTouchEvent(..)

onTouchEvent(..)

Page 30: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ACTION_DOWN

onTouchEvent - true

onTouchEvent - false

ViewGroup

View2View1

Page 31: Journey of an event, the android touch - Marco Cova, Facebook

Let’s recap

ViewGroup onTouchEvent - true

ACTION_MOVE

View2View1

Page 32: Journey of an event, the android touch - Marco Cova, Facebook

What’s at the top?Activity

ViewGroup

ViewGroup (DecorView)

View2View1

Page 33: Journey of an event, the android touch - Marco Cova, Facebook

public boolean dispatchTouchEvent(MotionEvent ev) {

. . .

if (getWindow().superDispatchTouchEvent(ev)) { return true; }

return onTouchEvent(ev); }

Activity.java

Page 34: Journey of an event, the android touch - Marco Cova, Facebook
Page 35: Journey of an event, the android touch - Marco Cova, Facebook

Need help?

• GestureDetector: onScroll, onFling…

• ScaleGestureDetector: onScale

Page 36: Journey of an event, the android touch - Marco Cova, Facebook

GestureDetectorpublic class MyView extends View { GestureDetector mGestureDetector; OnGestureListener mOnGestureListener; private void init() { mOnGestureListener = new SimpleOnGestureListener() { . . . }; mGestureDetector = new GestureDetector(getContext(), mOnGestureListener); }

@Override public boolean onTouchEvent(MotionEvent event) { boolean detectorHandled = mGestureDetector.onTouch(event); boolean superHandled = super.onTouchEvent(event); return detectorHandled || superHandled; } }

Page 37: Journey of an event, the android touch - Marco Cova, Facebook

ViewConfiguration.java

• getScaleTouchSlop()

• getScaled…FlingVelocity()

• getLongPressTimeout()

Page 38: Journey of an event, the android touch - Marco Cova, Facebook
Page 39: Journey of an event, the android touch - Marco Cova, Facebook

Choreographer.java

void doFrame(long frameTimeNanos, int frame) { . . . doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos); . . . }

Page 40: Journey of an event, the android touch - Marco Cova, Facebook
Page 41: Journey of an event, the android touch - Marco Cova, Facebook

Multi-Touch

• ACTION_POINTER_DOWN / _UP

• ViewGroup MotionEvent splitting on _DOWN

• getActionIndex(), getX(int), getY(int)

Page 42: Journey of an event, the android touch - Marco Cova, Facebook
Page 43: Journey of an event, the android touch - Marco Cova, Facebook

parent ViewGroup

bounds

TouchDelegate

delegateView

• Parent bounds

• Click only checks

Page 44: Journey of an event, the android touch - Marco Cova, Facebook

Questions?

Disclaimer: The information contained in these slides has been prepared from the author and the employer has no responsibility.