Android事件分发机制源码解析

你离开我真会死。 提交于 2019-12-11 19:41:57

当手指触摸屏幕的时候,系统就会接收到触摸事件,经过一些列调用以后最终触摸事件会被消费掉。

Motion Event

从触摸屏到应用程序的每个触摸动作都会包装成MotionEventMotionEvent提供了每个触摸事件的信息:触摸动作和相关的元数据(触摸位置,触摸点的手指数以及触摸事件时间)。
MotionEvent根据动作代码和一组坐标轴的值来描述动作。动作代码指定发生的状态更改,比如手指按下或抬起。坐标轴值描述了位置和其他动作属性。

Pointers

多点触控屏幕会为每一个手指发出一个动作跟踪。产生动作的每个手指或其他对象(鼠标,触摸笔,轨迹球)称为指针。MotionEvent包含了当前处于活动状态的所有指针的信息,即使自上一个事件传递以来其中一些指针未移动也是如此。当用手指触摸屏幕的特定点时,生成的信息由X,Y坐标以及其他信息(如索引,ID等)组成。由于Android支持多点触控,因此指针用于标识在同一时间产生动作的所有对象。

ACTIONS

动作类型及其名称表示作用。可用的动作有:

  • ACTION_DOWN:对象(鼠标,触摸笔,轨迹球)或手指与屏幕接触的第一个点。当手指触摸到屏幕时,触发ACTION_DOWN事件
  • ACTION_UP:对象(鼠标,触摸笔,轨迹球)或手指释放屏幕的点。当手指从屏幕上拿开时,触发ACTION_UP事件
  • ACTION_MOVE:指针在屏幕上拖动时产生的事件。可以将其定义为在ACTION_DOWN事件和ACTION_UP事件之间发生的事件
  • ACTION_POINTER_DOWN:类似于ACTION_DOWN,但在支持多点触控的情况下,非主指针之外的其他对象与屏幕接触时会调用它。
  • ACTION_CANCEL:当原来的视图正在处理触摸事件,此时触摸点转移到了另外的视图上的时候,会触发ACTION_CANCEL。当前手势会被中止。不会再收到后续的任何点。

手势定义为以ACTION_DOWN开始,以ACTION_UP结尾。在交互过程中,此循环重复多次。每个指针都有一个唯一的ID,该ID在首次按下(表示为ACTION_DOWN或ACTION_UP)时分配。针ID保持有效,直到指针最后抬起(表示为ACTION_UP或ACTION_POINTER_UP)或取消手势(表示为ACTION_CANCEL)为止。

源码分析

当手指触摸屏幕时,Linux就会收到相应的硬件中断,然后将中断加工成原始的输入事件并进行派发。派发过程中NativeInputEventReceiver的handleEvent会被调用:

int NativeInputEventReceiver::handleEvent(int receiveFd, int events, void* data) {
    if (events & ALOOPER_EVENT_INPUT) {
        JNIEnv* env = AndroidRuntime::getJNIEnv();
        status_t status = consumeEvents(env, false /*consumeBatches*/, -1, NULL);    //consumeEvents consumption events
        return status == OK || status == NO_MEMORY ? 1 : 0;
    }
    ...
}

再来看看NativeInputEventReceiver ::consumeEvents方法:

status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env,
        bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) {
    bool skipCallbacks = false;
    for (;;) {
        ...
         if (inputEventObj) {
            env->CallVoidMethod(receiverObj.get(),
                    gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj,   //gInputEventReceiverClassInfo.dispatchInputEvent
                    displayId);
         ...
        }
    }
}

可以看到通过jni调用了Java对象gInputEventReceiverClassInfo的dispatchInputEvent方法:

private void dispatchInputEvent(int seq, InputEvent event, int displayId) {
    mSeqMap.put(event.getSequenceNumber(), seq);
    onInputEvent(event, displayId);
}

这里的onInputEvent方法就是ViewRootImpl#WindowInputEventReceiver的onInputEvent方法:

final class WindowInputEventReceiver extends InputEventReceiver {
    public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
        super(inputChannel, looper);
    }

    @Override
    public void onInputEvent(InputEvent event, int displayId) {
        enqueueInputEvent(event, this, 0, true);
    }

    @Override
    public void onBatchedInputEventPending() {
        if (mUnbufferedInputDispatch) {
            super.onBatchedInputEventPending();
        } else {
            scheduleConsumeBatchedInput();
        }
    }

    @Override
    public void dispose() {
        unscheduleConsumeBatchedInput();
        super.dispose();
    }
}

而WindowInputEventReceiver#onInputEvent方法中又调用了enqueueInputEvent方法:

void enqueueInputEvent(InputEvent event, InputEventReceiver receiver, int flags, boolean processImmediately) {
	...
	
    if (processImmediately) {
        doProcessInputEvents();
    } else {
        scheduleProcessInputEvents();
    }
}

接着会调用doProcessInputEvents方法:

void doProcessInputEvents() {
	// Deliver all pending input events in the queue.
    while (mPendingInputEventHead != null) {
        QueuedInputEvent q = mPendingInputEventHead;
        mPendingInputEventHead = q.mNext;
        if (mPendingInputEventHead == null) {
            mPendingInputEventTail = null;
        }
        q.mNext = null;

        ...

        deliverInputEvent(q);
    }
	
	...
}

遍历并传递所有待处理的输入事件:

private void deliverInputEvent(QueuedInputEvent q) {
    Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
            q.mEvent.getSequenceNumber());
    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
    }

    InputStage stage;
    if (q.shouldSendToSynthesizer()) {
        stage = mSyntheticInputStage;
    } else {
        stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
    }

    if (q.mEvent instanceof KeyEvent) {
        mUnhandledKeyManager.preDispatch((KeyEvent) q.mEvent);
    }

    if (stage != null) {
        handleWindowFocusChanged();
        stage.deliver(q);
    } else {
        finishInputEvent(q);
    }
}

然后将QueuedInputEvent传递给InputStage链表进行处理,中间会传递给ViewPostImeInputStage类:

final class ViewPostImeInputStage extends InputStage {
    public ViewPostImeInputStage(InputStage next) {
        super(next);
    }

    @Override
    protected int onProcess(QueuedInputEvent q) {
        if (q.mEvent instanceof KeyEvent) {
            return processKeyEvent(q);
        } else {
            final int source = q.mEvent.getSource();
            if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
                return processPointerEvent(q);
            } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
                return processTrackballEvent(q);
            } else {
                return processGenericMotionEvent(q);
            }
        }
    }
    ...
}

再来看看processPointerEvent方法的实现:

private int processPointerEvent(QueuedInputEvent q) {
        final MotionEvent event = (MotionEvent)q.mEvent;

        mAttachInfo.mUnbufferedDispatchRequested = false;
        mAttachInfo.mHandlingPointerEvent = true;
        boolean handled = mView.dispatchPointerEvent(event);
        maybeUpdatePointerIcon(event);
        maybeUpdateTooltip(event);
        mAttachInfo.mHandlingPointerEvent = false;
        if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
            mUnbufferedInputDispatch = true;
            if (mConsumeBatchedInputScheduled) {
                scheduleConsumeBatchedInputImmediately();
            }
        }
        return handled ? FINISH_HANDLED : FORWARD;
}

最关键的代码就是mView.dispatchPointerEvent(event)这里的mView其实是ViewRootImplWindowManagerGlobaladdView方法中调用setView方法传进来的,而这个传进来的View就是DecorView,所以就来看看DecorView的dispatchPointerEvent方法,其实就是View的dispatchPointerEvent方法:

public final boolean dispatchPointerEvent(MotionEvent event) {
    if (event.isTouchEvent()) {
        return dispatchTouchEvent(event);
    } else {
        return dispatchGenericMotionEvent(event);
    }
}

可以看到最终还是调用了DecorView的dispatchTouchEvent方法:

public boolean dispatchTouchEvent(MotionEvent ev) {
    final Window.Callback cb = mWindow.getCallback();
    return cb != null && !mWindow.isDestroyed() && mFeatureId < 0
            ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev);
}

这里的Window.Callback其实就是Activity,那么就会去调用Activity#dispatchTouchEvent方法:

public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        onUserInteraction();
    }
    if (getWindow().superDispatchTouchEvent(ev)) {
        return true;
    }
    return onTouchEvent(ev);
}

然后会调用PhoneWindow的superDispatchTouchEvent方法:

@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
    return mDecor.superDispatchTouchEvent(event);
}

又回到了DecorView的superDispatchTouchEvent方法中了。

为什么事件要从DecorView中传到Activity,然后又传回DecorView中呢?主要是为了方便在Activity中通过控制dispatchTouchEvent 来控制当前Activity 事件的分发

接下来看看DecorView#superDispatchTouchEvent方法实现:

public boolean superDispatchTouchEvent(MotionEvent event) {
    return super.dispatchTouchEvent(event);
}

而DecorView继承自FrameLayout,FrameLayout又继承自ViewGroup,那么super.dispatchTouchEvent最终调用到了ViewGroup#dispatchTouchEvent方法中:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
        ...
        boolean handled = false;  //是否处理事件
        
        if (onFilterTouchEventForSecurity(ev)) {
            final int action = ev.getAction();
            final int actionMasked = action & MotionEvent.ACTION_MASK;

            // 处理最开始的按下事件
            if (actionMasked == MotionEvent.ACTION_DOWN) {
                //开始新的触摸手势时,丢弃所有先前的状态
				//框架可能由于应用程序切换,ANR或其他一些状态更改而丢弃上一个手势的抬起或取消事件
                cancelAndClearTouchTargets(ev);
                resetTouchState();
            }

            // Check for interception.
            final boolean intercepted; //是否被拦截
            if (actionMasked == MotionEvent.ACTION_DOWN
                    || mFirstTouchTarget != null) {
                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
                if (!disallowIntercept) { //如果允许ViewGroup对事件进行拦截
                    intercepted = onInterceptTouchEvent(ev);  //判断ViewGroup是否需要拦截事件
                    ev.setAction(action); // restore action in case it was changed
                } else {
                	//如果不允许拦截
                    intercepted = false;
                }
            } else {
              	//如果事件最开始就不是按下事件,则直接拦截
                intercepted = true;
            }

            ...

            // 如果设置了PFLAG_CANCEL_NEXT_UP_EVENT标识或者事件就是取消事件,则canceled为true
            final boolean canceled = resetCancelNextUpFlag(this)
                    || actionMasked == MotionEvent.ACTION_CANCEL;

            // Update list of touch targets for pointer down, if needed.
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;
            if (!canceled && !intercepted) {
				//如果不取消或者不拦截,则进行事件分发
                
                ...

                if (actionMasked == MotionEvent.ACTION_DOWN
                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
                    final int actionIndex = ev.getActionIndex(); // always 0 for down
                    final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
                            : TouchTarget.ALL_POINTER_IDS;

                    //清除此指针ID先前的触摸目标,防止它们变得不同步
                    removePointersFromTouchTargets(idBitsToAssign);

                    final int childrenCount = mChildrenCount;  //子视图的个数
                    if (newTouchTarget == null && childrenCount != 0) {
                        final float x = ev.getX(actionIndex);
                        final float y = ev.getY(actionIndex);
                        // Find a child that can receive the event.
                        // Scan children from front to back.
                        final ArrayList<View> preorderedList = buildTouchDispatchChildList();
                        final boolean customOrder = preorderedList == null
                                && isChildrenDrawingOrderEnabled();
                        final View[] children = mChildren;
                        for (int i = childrenCount - 1; i >= 0; i--) {
                            final int childIndex = getAndVerifyPreorderedIndex(
                                    childrenCount, i, customOrder);
                            final View child = getAndVerifyPreorderedView(
                                    preorderedList, children, childIndex);

                            ...
							
							//如果视图不可见并且无动画,或者触摸点不在视图范围内,则直接判断下一个视图
                            if (!canViewReceivePointerEvents(child)
                                    || !isTransformedTouchPointInView(x, y, child, null)) {
                                ev.setTargetAccessibilityFocus(false);
                                continue;
                            }

                            newTouchTarget = getTouchTarget(child);  //判断触摸的视图是否在触摸目标链表中,如果不在则返回null
                            if (newTouchTarget != null) {
                            	//如果不为空,说明在触摸目标链表中存在,即之前已经接受了触摸事件,则给其提供新的指针ID
                                newTouchTarget.pointerIdBits |= idBitsToAssign;
                                //结束事件分发
                                break;
                            }
							
                            resetCancelNextUpFlag(child);  //重置取消下一个抬起事件标志
                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
                                // 如果有子视图处理事件
                                mLastTouchDownTime = ev.getDownTime();
                                if (preorderedList != null) {
                                    // childIndex points into presorted list, find original index
                                    for (int j = 0; j < childrenCount; j++) {
                                        if (children[childIndex] == mChildren[j]) {
                                            mLastTouchDownIndex = j;
                                            break;
                                        }
                                    }
                                } else {
                                    mLastTouchDownIndex = childIndex;
                                }
                                mLastTouchDownX = ev.getX();
                                mLastTouchDownY = ev.getY();
                                newTouchTarget = addTouchTarget(child, idBitsToAssign);  //将子视图添加到触摸目标链表
                                alreadyDispatchedToNewTouchTarget = true;
                                break;
                            }
                        }
                        if (preorderedList != null) preorderedList.clear();
                    }

                    if (newTouchTarget == null && mFirstTouchTarget != null) {
                        //如果没有找到接受事件的视图,则将指针赋值给最开始添加的触摸目标
                        newTouchTarget = mFirstTouchTarget;
                        while (newTouchTarget.next != null) {
                        	//找到触摸目标链表的最后一个节点
                            newTouchTarget = newTouchTarget.next;
                        }
                        newTouchTarget.pointerIdBits |= idBitsToAssign;
                    }
                }
            }

            // 将事件分发到触摸目标
            if (mFirstTouchTarget == null) {
                // 如果没有子视图接受触摸事件
                handled = dispatchTransformedTouchEvent(ev, canceled, null,
                        TouchTarget.ALL_POINTER_IDS);
            } else {
                //如果有子视图接受触摸事件,则分发到触摸目标
                TouchTarget predecessor = null;
                TouchTarget target = mFirstTouchTarget;
                while (target != null) {  //遍历触摸目标链表
                    final TouchTarget next = target.next;
                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                    	//如果在按下的时候有子视图接受处理
                        handled = true;
                    } else {
                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
                                || intercepted;
                        if (dispatchTransformedTouchEvent(ev, cancelChild,
                                target.child, target.pointerIdBits)) {
                            //后续事件直接分发
                            handled = true;
                        }
                        if (cancelChild) {
                            if (predecessor == null) {
                                mFirstTouchTarget = next;
                            } else {
                                predecessor.next = next;
                            }
                            target.recycle();
                            target = next;
                            continue;
                        }
                    }
                    predecessor = target;
                    target = next;
                }
            }

     		...
        }

      	...
        return handled;
}

再来看看dispatchTransformedTouchEvent方法,这个方法负责分发事件:

private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
        View child, int desiredPointerIdBits) {
    final boolean handled;

    // Canceling motions is a special case.  We don't need to perform any transformations
    // or filtering.  The important part is the action, not the contents.
    final int oldAction = event.getAction();
    if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
        event.setAction(MotionEvent.ACTION_CANCEL);
        if (child == null) {
            handled = super.dispatchTouchEvent(event);
        } else {
            handled = child.dispatchTouchEvent(event);
        }
        event.setAction(oldAction);
        return handled;
    }

    // Calculate the number of pointers to deliver.
    final int oldPointerIdBits = event.getPointerIdBits();
    final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;

    // If for some reason we ended up in an inconsistent state where it looks like we
    // might produce a motion event with no pointers in it, then drop the event.
    if (newPointerIdBits == 0) {
        return false;
    }

    //如果指针的数量相同,并且不需要执行任何变换,那么只要还原更改,就可以重用motion事件,否则需要进行复制
    final MotionEvent transformedEvent;
    if (newPointerIdBits == oldPointerIdBits) {  //如果是同一个指针
        if (child == null || child.hasIdentityMatrix()) {
            if (child == null) {
            	//如果没有子视图处理事件,则调用View的dispatchTouchEvent方法
                handled = super.dispatchTouchEvent(event);
            } else {
            	//如果有子视图处理事件
                final float offsetX = mScrollX - child.mLeft;
                final float offsetY = mScrollY - child.mTop;
                event.offsetLocation(offsetX, offsetY);
				//还原坐标并将事件分发给子视图
                handled = child.dispatchTouchEvent(event);

                event.offsetLocation(-offsetX, -offsetY);
            }
            return handled;
        }
        transformedEvent = MotionEvent.obtain(event);
    } else {
        transformedEvent = event.split(newPointerIdBits);
    }

    // Perform any necessary transformations and dispatch.
    if (child == null) {
        handled = super.dispatchTouchEvent(transformedEvent);
    } else {
        final float offsetX = mScrollX - child.mLeft;
        final float offsetY = mScrollY - child.mTop;
        transformedEvent.offsetLocation(offsetX, offsetY);
        if (! child.hasIdentityMatrix()) {
            transformedEvent.transform(child.getInverseMatrix());
        }
		//对坐标进行转换以后将事件分发给子视图
        handled = child.dispatchTouchEvent(transformedEvent);
    }

    // Done.
    transformedEvent.recycle();
    return handled;
}

如果没有子视图对事件进行处理,那么负责分发的视图就会调用View#dispatchTouchEvent方法判断是否消费事件:

public boolean dispatchTouchEvent(MotionEvent event) {
    ...
    boolean result = false;

    ...

    if (onFilterTouchEventForSecurity(event)) {
        if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
            result = true;
        }
        //noinspection SimplifiableIfStatement
        ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnTouchListener != null
                && (mViewFlags & ENABLED_MASK) == ENABLED
                && li.mOnTouchListener.onTouch(this, event)) {
            //如果设置了OnTouchListener并且onTouch方法返回true则接受处理事件
            result = true;
        }
        
		//如果没有设置OnTouchListener或者onTouch方法返回false则调用onTouchEvent方法
        if (!result && onTouchEvent(event)) {
            result = true;
        }
    }

    ...

    return result;
}

总结下来就是:当触摸屏幕触发按下事件的时候,或者之前就已经有子视图接受处理触摸事件的时候,首先会判断ViewGroup是否不允许拦截(通过ViewGroup#requestDisallowInterceptTouchEvent方法设置)。如果ViewGroup不允许拦截,则ViewGroup的方法onInterceptTouchEvent不会执行。如果ViewGroup允许拦截,则会调用ViewGroup的方法onInterceptTouchEvent判断是否拦截事件,如果onInterceptTouchEvent返回true,则直接会调用View#dispatchTouchEvent方法,不会进行分发;如果onInterceptTouchEvent返回false,则会遍历所有子视图,如果触摸点在子视图内,则会调用子视图的dispatchTouchEvent方法判断是否接受处理触摸事件;如果有子视图接受处理事件,则直接跳出遍历,并且将接受事件的子视图添加到TouchTarget链表头部。如果没有子视图接受处理事件,则直接调用View#dispatchTouchEvent方法。如果是后续滑动/抬起事件并且有子视图接受处理事件,则后续事件直接交给接受处理事件的子视图处理。如果没有子视图接受处理事件,则将事件交还给父视图判断是否接受处理事件,如果所有视图都不处理,则最终Activity会在onTouchEvent中消费掉。

用一张图片来概括流程就是:
事件分发

事件冲突

之所以会产生事件冲突,是因为本来应该被视图A处理的事件结果被视图B处理了。一般有两种方式解决事件冲突:

  • 外部拦截法:重写onInterceptTouchEvent对事件进行拦截
  • 内部拦截法:调用requestDisallowInterceptTouchEvent不允许拦截事件

感谢大家的支持,如有错误请指正,如需转载请标明原文出处!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!