Client 对输入事件处理
输入事件,比如按键事件并不是全部被window的view处理了,比如Back键,如果此时系统输入法是显示的,其实该键首先会去关闭输入法,而window的view是接收不到这个键的,这个就是事件处理器链实现的,这个链上又各种处理器,它们按照处理的优先顺序添加咋链表上
输入事件处理链
- public abstract class InputEventReceiver {
-
- private void dispatchInputEvent(int seq, InputEvent event) {
- mSeqMap.put(event.getSequenceNumber(), seq);
- onInputEvent(event);
- }
- }
-
- final class WindowInputEventReceiver extends InputEventReceiver {
- @Override
- public void onInputEvent(InputEvent event) {
- enqueueInputEvent(event, this, 0, true);
- }
- }
-
- void enqueueInputEvent(InputEvent event) {
- enqueueInputEvent(event, null, 0, false);
- }
-
- void enqueueInputEvent(InputEvent event,
- InputEventReceiver receiver, int flags, boolean processImmediately) {
- QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
- QueuedInputEvent last = mPendingInputEventTail;
- if (last == null) {
- mPendingInputEventHead = q;
- mPendingInputEventTail = q;
- } else {
- last.mNext = q;
- mPendingInputEventTail = q;
- }
- mPendingInputEventCount += 1;
- if (processImmediately) {
-
- doProcessInputEvents();
- } else {
- scheduleProcessInputEvents();
- }
- }
-
- void doProcessInputEvents() {
-
- while (mPendingInputEventHead != null) {
- QueuedInputEvent q = mPendingInputEventHead;
- mPendingInputEventHead = q.mNext;
- if (mPendingInputEventHead == null) {
- mPendingInputEventTail = null;
- }
- q.mNext = null;
-
- mPendingInputEventCount -= 1;
-
- deliverInputEvent(q);
- }
- }
-
- private void deliverInputEvent(QueuedInputEvent q) {
- try {
-
-
- InputStage stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
- if (stage != null) {
- stage.deliver(q);
- } else {
-
- finishInputEvent(q);
- }
- }
- }
-
-
- InputStage syntheticInputStage = new SyntheticInputStage();
- InputStage viewPostImeStage = new ViewPostImeInputStage(syntheticInputStage);
- InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,
- "aq:native-post-ime:" + counterSuffix);
- InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);
- InputStage imeStage = new ImeInputStage(earlyPostImeStage,
- "aq:ime:" + counterSuffix);
- InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);
- InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,
- "aq:native-pre-ime:" + counterSuffix);
- mFirstInputStage = nativePreImeStage;
- mFirstPostImeInputStage = earlyPostImeStage;<div>
- abstract class InputStage {
- public final void deliver(QueuedInputEvent q) {
- if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
-
- forward(q);
- } else if (shouldDropInputEvent(q)) {
- finish(q, false);
- } else {
-
- apply(q, onProcess(q));
- }
- }
-
- protected void apply(QueuedInputEvent q, int result) {
- if (result == FORWARD) {
- forward(q);
- } else if (result == FINISH_HANDLED) {
- finish(q, true);
- }
- }
-
- protected void forward(QueuedInputEvent q) {
- onDeliverToNext(q);
- }
-
- protected void onDeliverToNext(QueuedInputEvent q) {
-
- if (mNext != null) {
- mNext.deliver(q);
- } else {
-
- finishInputEvent(q);
- }
- }
- }
-
-
- final class ViewPostImeInputStage extends InputStage {
- @Override
- protected int onProcess(QueuedInputEvent q) {
- if (q.mEvent instanceof KeyEvent) {
- return processKeyEvent(q);
- }
- }
-
- private int processKeyEvent(QueuedInputEvent q) {
- final KeyEvent event = (KeyEvent)q.mEvent;
-
- if (event.getAction() != KeyEvent.ACTION_UP) {
-
-
- handleDispatchDoneAnimating();
- }
-
-
- if (mView.dispatchKeyEvent(event)) {
- return FINISH_HANDLED;
- }
-
-
- if (mFallbackEventHandler.dispatchKeyEvent(event)) {
- return FINISH_HANDLED;
- }
- return FORWARD;
- }
- }
从上面的逻辑可以看出处理器的处理有限顺序是:
NativePreImeInputStage->ViewPreImeInputStage-> ImeInputStage->
EarlyPostImeInputStage-> NativePostImeInputStage->ViewPostImeInputStage->
SyntheticInputStage
Back按键如何结束Activity
刚刚前面说到,view获得输入事件是由ViewPostImeInputStage传递过来的。ViewPostImeInputStage会将事件传递给activity的根View ---DecorView
- private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
- @Override
- public boolean dispatchKeyEvent(KeyEvent event) {
- if (!isDestroyed()) {
-
- final Callback cb = getCallback() && mFeatureId < 0 ?
- cb.dispatchKeyEvent(event)
- : super.dispatchKeyEvent(event);
- if (handled) {
- return true;
- }
- }
-
- return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event)
- : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event);
- }
- }
-
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (getApplicationInfo().targetSdkVersion
- >= Build.VERSION_CODES.ECLAIR) {
- } else {
-
- onBackPressed();
- }
- return true;
- }
- }
如果非back按键,则会调用super即View.dispatchKeyEvent的接口,view的事件接收及处理就是从这开始的。
View如何获取按键
由于DecorView继承FrameLayout,它自然是一个ViewGroup,所以我们来看下ViewGroup的dispatchKeyEvent。
- @Override
- 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;
- }
- }
- return false;
- }
-
- public boolean dispatchKeyEvent(KeyEvent event) {
- if (event.dispatch(this, mAttachInfo != null
- ? mAttachInfo.mKeyDispatchState : null, this)) {
- return true;
- }
- return false;
- }
-
- public final boolean dispatch(Callback receiver, DispatcherState state,
- Object target) {
- switch (mAction) {
- case ACTION_DOWN: {
- mFlags &= ~FLAG_START_TRACKING;
-
- boolean res = receiver.onKeyDown(mKeyCode, this);
- return res;
- }
- }
- return false;
- }
Camera等快捷键是如何传递处理的
如果view没有处理按键,则最后会给mFallbackEventHandler一个机会处理按键,Camera等快捷键就是由这个handler处理的,下面来看看。
- public ViewRootImpl(Context context, Display display) {
- mFallbackEventHandler= PolicyManager.makeNewFallbackEventHandler(context);
- }
-
- public class Policy implements IPolicy {
- public FallbackEventHandler makeNewFallbackEventHandler(Context context) {
- return new PhoneFallbackEventHandler(context);
- }
- }
-
- public class PhoneFallbackEventHandler implements FallbackEventHandler {
- public boolean dispatchKeyEvent(KeyEvent event) {
-
- final int action = event.getAction();
- final int keyCode = event.getKeyCode();
-
- if (action == KeyEvent.ACTION_DOWN) {
- return onKeyDown(keyCode, event);
- } else {
- return onKeyUp(keyCode, event);
- }
- }
-
- boolean onKeyDown(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_CAMERA: {
- if (event.getRepeatCount() == 0) {
- } else if (event.isLongPress() && dispatcher.isTracking(event)) {
-
- Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
- intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
- mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF,
- null, null, null, 0, null, null);
- }
- return true;
- }
- }
- return false;
- }
输入事件处理完成通知
client将事件处理完了,必须通知server已经完成对该事件的处理,否则server一直在等待事件完成而不能发送后面的事件。
- private void finishInputEvent(QueuedInputEvent q) {
- if (q.mReceiver != null) {
- boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;
-
- q.mReceiver.finishInputEvent(q.mEvent, handled);
- }
- recycleQueuedInputEvent(q);
- }
-
- public abstract class InputEventReceiver {
- public final void finishInputEvent(InputEvent event, boolean handled) {
- {
- int index = mSeqMap.indexOfKey(event.getSequenceNumber());
- if (index < 0) {
- } else {
-
- nativeFinishInputEvent(mReceiverPtr, seq, handled);
- }
- }
- event.recycleIfNeededAfterDispatch();
- }
- }
-
-
- status_t NativeInputEventReceiver::finishInputEvent(uint32_t seq, bool handled) {
-
- status_t status = mInputConsumer.sendFinishedSignal(seq, handled);
- return status;
- }
-
-
- status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
- size_t seqChainCount = mSeqChains.size();
- if (seqChainCount) {
- uint32_t currentSeq = seq;
- uint32_t chainSeqs[seqChainCount];
- size_t chainIndex = 0;
- for (size_t i = seqChainCount; i-- > 0; ) {
- const SeqChain& seqChain = mSeqChains.itemAt(i);
- if (seqChain.seq == currentSeq) {
- currentSeq = seqChain.chain;
- chainSeqs[chainIndex++] = currentSeq;
- mSeqChains.removeAt(i);
- }
- }
- status_t status = OK;
- while (!status && chainIndex-- > 0) {
- status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
- }
- }
-
-
- return sendUnchainedFinishedSignal(seq, handled);
- }
-
- status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
- InputMessage msg;
- msg.header.type = InputMessage::TYPE_FINISHED;
- msg.body.finished.seq = seq;
- msg.body.finished.handled = handled;
- return mChannel->sendMessage(&msg);
- }
-
-
- status_t InputChannel::sendMessage(const InputMessage* msg) {
- size_t msgLength = msg->size();
- ssize_t nWrite;
- do {
- nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
- } while (nWrite == -1 && errno == EINTR);
- return OK;
- }
/********************************
* 本文来自博客 “爱踢门”
* 转载请标明出处:http://blog.csdn.net/itleaks
******************************************/