step1:调用Activity成员函数dispatchKeyEvent
public boolean dispatchKeyEvent(KeyEvent event) {
// Let action bars open menus in response to the menu key prioritized over
// the window handling it
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU &&
mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
return true;
}
Window win = getWindow();
if (win.superDispatchKeyEvent(event)) {
return true;
}
View decor = mDecor;
if (decor == null) decor = win.getDecorView();
return event.dispatch(this, decor != null
? decor.getKeyDispatcherState() : null, this);
}
step2:Activity成员函数dispatchKeyEvent中调用phoneWindow的成员函数superDispatchKeyEvent
Window win = getWindow();
if (win.superDispatchKeyEvent(event)) {
return true;
}
setp3:在PhoneWindow的superDispatchKeyEvent函数中调用DecorView的成员函数uperDispatchKeyEvent
public boolean superDispatchKeyEvent(KeyEvent event) { return mDecor.superDispatchKeyEvent(event); }
step4:ViewGroup的成员函数DispatchKeyEvent
private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
...
public DecorView(Context context, int featureId) {
super(context);
mFeatureId = featureId;
}
@Override public boolean superDispatchKeyEvent(KeyEvent event) {
if (super.dispatchKeyEvent(event)) { return true; }
...
}
step5:通过mFocused的包含关系实现隧道式分发
public boolean dispatchKeyEvent(KeyEvent event) {
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onKeyEvent(event, 1);
}
if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
== (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
if (super.dispatchKeyEvent(event)) {
return true;
}
} else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
== PFLAG_HAS_BOUNDS) {
if (mFocused.dispatchKeyEvent(event)) {
return true;
}
}
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
}
return false;
}