了解更多,移步Android触摸事件传递机制系列详解
在Android触摸事件的传递(五)--输入系统-InputChannel中事件传递给ViewRootImpl
并调用deliverInputEvent
处理事件。
1 ViewRootImpl的创建
- 我们可以猜测,如果外界想要传递点击事件给
Activity
,那么它就必须持有Activity
的引用,这没错,在Activity
的attach
方法中:
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
才开始attach
给WindowManager
从而显示出来。(这句话好厉害) - 请看Activity的makeVisible方法,代码如下:
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager()
wm.addView(mDecor, getWindow().getAttributes())
mWindowAdded = true
}
mDecor.setVisibility(View.VISIBLE)
}
- 系统就会完成添加
Window
的过程,看WindowManagerGlobal
的addView
方法
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
创建了,在ViewRootImpl
的setView
方法(此方法运行在UI线程)中,会通过跨进程的方式向WMS(WindowManagerService)
发起一个调用,从而将DecorView
最终添加到Window
上。 - 在这个过程中,
ViewRootImpl
、DecorView
和WMS
会彼此向关联,同时会创建InputChannel
、InputQueue
和WindowInputEventReceiver
来接受点击事件的消息。
如何调用到deliverInputEvent
移步Android触摸事件的传递(五)--输入系统-InputChannel
2 DecorView
ViewRootImpl
的dispatchInputEvent
方法调用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
可以处理一定的事件类型,比如AsyncInputStage
、ViewPreImeInputStage
、ViewPostImeInputStage
等。 - 对于点击事件来说,
ViewPostImeInputStage
可以处理它,ViewPostImeInputStage
中,有一个processPointerEvent
方法,如下,它会调用mView
的dispatchPointerEvent
方法,注意,这里的mView
其实就是DecorView
。 - mView变量是在
setview
方法中赋值的,对于应用窗口来说, - mView变量指向
PhoneWindow
的内部类DecorView
对象,但是调用的是父类View
的dispatchPointerEvent
方法,
注:继承关系如下
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);
}
}
DecorView
的dispatchTouchEvent
的实现如下,需要强调的是,DecorView
是PhoneWindow
的内部类
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。
-
activity
和Dialog
都是Callback
接口的具体实现,主要看activity
的dispatchTouchEvent
方法, - 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个子类:KeyEvent
和MotionEvent
,其中KeyEvent
表示键盘事件,而MotionEvent
表示点击事件。
参考
Android 中 MotionEvent 的来源和 ViewRootImpl