Android触摸事件的传递(六)-ViewRootImpl-->Activity

了解更多,移步Android触摸事件传递机制系列详解
在Android触摸事件的传递(五)--输入系统-InputChannel中事件传递给ViewRootImpl并调用deliverInputEvent处理事件。

1 ViewRootImpl的创建

  • 我们可以猜测,如果外界想要传递点击事件给Activity,那么它就必须持有Activity的引用,这没错,在Activityattach方法中:
    mWindow持有了Activity的引用,它通过setCallback方法来持有Activity.(callback后面说)
    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
        attachBaseContext(context);

        mFragments.attachHost(null );

        mWindow = new PhoneWindow(this);
        mWindow.setCallback(this);
        mWindow.setOnWindowDismissedCallback(this);
        mWindow.getLayoutInflater().setPrivateFactory(this);
    }
  • Activity启动以后,在它的onResume以后,DecorView才开始attachWindowManager从而显示出来。(这句话好厉害)
  • 请看Activity的makeVisible方法,代码如下:
    void makeVisible() {
        if (!mWindowAdded) {
            ViewManager wm = getWindowManager()
            wm.addView(mDecor, getWindow().getAttributes())
            mWindowAdded = true
        }
        mDecor.setVisibility(View.VISIBLE)
    }
  • 系统就会完成添加Window的过程,看WindowManagerGlobaladdView方法
    public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
        ViewRootImpl root;
        View panelParentView = null;
        ...这里省略了一堆代码
        root = new ViewRootImpl(view.getContext(), display);
        view.setLayoutParams(wparams);
        mViews.add(view);
        mRoots.add(root);
        mParams.add(wparams);

        
        try {
            root.setView(view, wparams, panelParentView);
        } catch (RuntimeException e) {
            
            synchronized (mLock) {
                final int index = findViewLocked(view, false);
                if (index >= 0) {
                    removeViewLocked(index, true);
                }
            }
            throw e;
        }
    }
  • 可以看到,ViewRootImpl创建了,在ViewRootImplsetView方法(此方法运行在UI线程)中,会通过跨进程的方式向WMS(WindowManagerService)发起一个调用,从而将DecorView最终添加到Window上。
  • 在这个过程中,ViewRootImplDecorViewWMS会彼此向关联,同时会创建InputChannelInputQueueWindowInputEventReceiver来接受点击事件的消息。
    如何调用到deliverInputEvent 移步Android触摸事件的传递(五)--输入系统-InputChannel

2 DecorView

ViewRootImpldispatchInputEvent方法调用deliverInputEvent方法来处理点击事件的消息

    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 (stage != null) {
            stage.deliver(q);
        } else {
            finishInputEvent(q);
        }
    }
  • ViewRootImpl中,有一系列类似于InputStage(输入事件舞台)的概念,每种InputStage可以处理一定的事件类型,比如AsyncInputStageViewPreImeInputStageViewPostImeInputStage等。
  • 对于点击事件来说,ViewPostImeInputStage可以处理它,ViewPostImeInputStage中,有一个processPointerEvent方法,如下,它会调用mViewdispatchPointerEvent方法,注意,这里的mView其实就是DecorView
  • mView变量是在setview方法中赋值的,对于应用窗口来说,
  • mView变量指向PhoneWindow的内部类DecorView对象,但是调用的是父类ViewdispatchPointerEvent方法,
    注:继承关系如下
private final class DecorView extends FrameLayout implements RootViewSurfaceTaker{...}

public class FrameLayout extends ViewGroup {...}

public abstract class ViewGroup extends View implements ViewParent, ViewManager{..}
        private int processPointerEvent(QueuedInputEvent q) {
            final MotionEvent event = (MotionEvent)q.mEvent;

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

3 activity

View的实现中,dispatchPointerEvent的逻辑如下,这样一来,点击事件就传递给了DecorView的父View的dispatchTouchEvent方法。

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

DecorViewdispatchTouchEvent的实现如下,需要强调的是,DecorViewPhoneWindow的内部类

        public boolean dispatchTouchEvent(MotionEvent ev) {
            final Callback cb = getCallback();
            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
                    : super.dispatchTouchEvent(ev);
        }
  • 这里这个cb对象其实就是Activity,就这样点击事件就传递给了Activity或dialog。
  • activityDialog都是Callback接口的具体实现,主要看activitydispatchTouchEvent方法,
  • Activity实现了一个特殊的接口:Window.Callback。
    致此触摸事件传递给了activity。

4 Window.Callback

Window.Callback

    /**
     * API from a Window back to its caller.  This allows the client to
     * intercept key dispatching, panels and menus, etc.
     */
    public interface Callback {
        /**
         * Called to process key events.  At the very least your
         * implementation must call
         * {@link android.view.Window#superDispatchKeyEvent} to do the
         * standard key processing.
         *
         * @param event The key event.
         *
         * @return boolean Return true if this event was consumed.
         */
        public boolean dispatchKeyEvent(KeyEvent event);

        /**
         * Called to process touch screen events.  At the very least your
         * implementation must call
         * {@link android.view.Window#superDispatchTouchEvent} to do the
         * standard touch screen processing.
         *
         * @param event The touch screen event.
         *
         * @return boolean Return true if this event was consumed.
         */
        public boolean dispatchTouchEvent(MotionEvent event);

        /**
         * Called to process trackball events.  At the very least your
         * implementation must call
         * {@link android.view.Window#superDispatchTrackballEvent} to do the
         * standard trackball processing.
         *
         * @param event The trackball event.
         *
         * @return boolean Return true if this event was consumed.
         */
        public boolean dispatchTrackballEvent(MotionEvent event);
        ...(省略若干代码,下同)

Activity实现了一个特殊的接口:Window.Callback。

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
***
}

InputEvent有2个子类:KeyEventMotionEvent,其中KeyEvent表示键盘事件,而MotionEvent表示点击事件。

参考

Android 中 MotionEvent 的来源和 ViewRootImpl

你可能感兴趣的:(Android触摸事件的传递(六)-ViewRootImpl-->Activity)