void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() { status_t status; const uint32_t seq = 15; const int32_t deviceId = 1; const int32_t source = AINPUT_SOURCE_KEYBOARD; const int32_t action = AKEY_EVENT_ACTION_DOWN; const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM; const int32_t keyCode = AKEYCODE_ENTER; const int32_t scanCode = 13; const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; const int32_t repeatCount = 1; const nsecs_t downTime = 3; const nsecs_t eventTime = 4; status = mPublisher->publishKeyEvent(seq, deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, downTime, eventTime); ASSERT_EQ(OK, status) << "publisher publishKeyEvent should return OK"; uint32_t consumeSeq; InputEvent* event; status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event); ASSERT_EQ(OK, status) << "consumer consume should return OK"; ASSERT_TRUE(event != NULL) << "consumer should have returned non-NULL event"; ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType()) << "consumer should have returned a key event"; KeyEvent* keyEvent = static_cast<KeyEvent*>(event); EXPECT_EQ(seq, consumeSeq); EXPECT_EQ(deviceId, keyEvent->getDeviceId()); EXPECT_EQ(source, keyEvent->getSource()); EXPECT_EQ(action, keyEvent->getAction()); EXPECT_EQ(flags, keyEvent->getFlags()); EXPECT_EQ(keyCode, keyEvent->getKeyCode()); EXPECT_EQ(scanCode, keyEvent->getScanCode()); EXPECT_EQ(metaState, keyEvent->getMetaState()); EXPECT_EQ(repeatCount, keyEvent->getRepeatCount()); EXPECT_EQ(downTime, keyEvent->getDownTime()); EXPECT_EQ(eventTime, keyEvent->getEventTime()); status = mConsumer->sendFinishedSignal(seq, true); ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; uint32_t finishedSeq = 0; bool handled = false; status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled); ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK"; ASSERT_EQ(seq, finishedSeq) << "publisher receiveFinishedSignal should have returned the original sequence number"; ASSERT_TRUE(handled) << "publisher receiveFinishedSignal should have set handled to consumer's reply"; }
status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Consuming input events, consumeBatches=%s, frameTime=%lld.", getInputChannelName(), consumeBatches ? "true" : "false", (long long)frameTime); } if (consumeBatches) { mBatchedInputEventPending = false; } if (outConsumedBatch) { *outConsumedBatch = false; } ScopedLocalRef<jobject> receiverObj(env, NULL); bool skipCallbacks = false; for (;;) { uint32_t seq; InputEvent* inputEvent; status_t status = mInputConsumer.consume(&mInputEventFactory, consumeBatches, frameTime, &seq, &inputEvent); if (status) { if (status == WOULD_BLOCK) { if (!skipCallbacks && !mBatchedInputEventPending && mInputConsumer.hasPendingBatch()) { // There is a pending batch. Come back later. if (!receiverObj.get()) { receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal)); if (!receiverObj.get()) { ALOGW("channel '%s' ~ Receiver object was finalized " "without being disposed.", getInputChannelName()); return DEAD_OBJECT; } } mBatchedInputEventPending = true; if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Dispatching batched input event pending notification.", getInputChannelName()); } env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.dispatchBatchedInputEventPending); if (env->ExceptionCheck()) { ALOGE("Exception dispatching batched input events."); mBatchedInputEventPending = false; // try again later } } return OK; } ALOGE("channel '%s' ~ Failed to consume input event. status=%d", getInputChannelName(), status); return status; } assert(inputEvent); if (!skipCallbacks) { if (!receiverObj.get()) { receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal)); if (!receiverObj.get()) { ALOGW("channel '%s' ~ Receiver object was finalized " "without being disposed.", getInputChannelName()); return DEAD_OBJECT; } } jobject inputEventObj; switch (inputEvent->getType()) { case AINPUT_EVENT_TYPE_KEY: if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Received key event.", getInputChannelName()); } inputEventObj = android_view_KeyEvent_fromNative(env, static_cast<KeyEvent*>(inputEvent)); break; case AINPUT_EVENT_TYPE_MOTION: { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Received motion event.", getInputChannelName()); } MotionEvent* motionEvent = static_cast<MotionEvent*>(inputEvent); if ((motionEvent->getAction() & AMOTION_EVENT_ACTION_MOVE) && outConsumedBatch) { *outConsumedBatch = true; } inputEventObj = android_view_MotionEvent_obtainAsCopy(env, motionEvent); break; } default: assert(false); // InputConsumer should prevent this from ever happening inputEventObj = NULL; } if (inputEventObj) { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Dispatching input event.", getInputChannelName()); } env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj); if (env->ExceptionCheck()) { ALOGE("Exception dispatching input event."); skipCallbacks = true; } env->DeleteLocalRef(inputEventObj); } else { ALOGW("channel '%s' ~ Failed to obtain event object.", getInputChannelName()); skipCallbacks = true; } } if (skipCallbacks) { mInputConsumer.sendFinishedSignal(seq, false); } } }
void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() { status_t status; const int32_t deviceId = 1; const int32_t source = AINPUT_SOURCE_KEYBOARD; const int32_t action = AKEY_EVENT_ACTION_DOWN; const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM; const int32_t keyCode = AKEYCODE_ENTER; const int32_t scanCode = 13; const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; const int32_t repeatCount = 1; const nsecs_t downTime = 3; const nsecs_t eventTime = 4; status = mPublisher->publishKeyEvent(deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, downTime, eventTime); ASSERT_EQ(OK, status) << "publisher publishKeyEvent should return OK"; status = mPublisher->sendDispatchSignal(); ASSERT_EQ(OK, status) << "publisher sendDispatchSignal should return OK"; status = mConsumer->receiveDispatchSignal(); ASSERT_EQ(OK, status) << "consumer receiveDispatchSignal should return OK"; InputEvent* event; status = mConsumer->consume(& mEventFactory, & event); ASSERT_EQ(OK, status) << "consumer consume should return OK"; ASSERT_TRUE(event != NULL) << "consumer should have returned non-NULL event"; ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType()) << "consumer should have returned a key event"; KeyEvent* keyEvent = static_cast<KeyEvent*>(event); EXPECT_EQ(deviceId, keyEvent->getDeviceId()); EXPECT_EQ(source, keyEvent->getSource()); EXPECT_EQ(action, keyEvent->getAction()); EXPECT_EQ(flags, keyEvent->getFlags()); EXPECT_EQ(keyCode, keyEvent->getKeyCode()); EXPECT_EQ(scanCode, keyEvent->getScanCode()); EXPECT_EQ(metaState, keyEvent->getMetaState()); EXPECT_EQ(repeatCount, keyEvent->getRepeatCount()); EXPECT_EQ(downTime, keyEvent->getDownTime()); EXPECT_EQ(eventTime, keyEvent->getEventTime()); status = mConsumer->sendFinishedSignal(); ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; status = mPublisher->receiveFinishedSignal(); ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK"; status = mPublisher->reset(); ASSERT_EQ(OK, status) << "publisher reset should return OK"; }
void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent( size_t samplesToAppendBeforeDispatch, size_t samplesToAppendAfterDispatch) { status_t status; const int32_t deviceId = 1; const int32_t source = AINPUT_SOURCE_TOUCHSCREEN; const int32_t action = AMOTION_EVENT_ACTION_MOVE; const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP; const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; const float xOffset = -10; const float yOffset = -20; const float xPrecision = 0.25; const float yPrecision = 0.5; const nsecs_t downTime = 3; const size_t pointerCount = 3; const int32_t pointerIds[pointerCount] = { 2, 0, 1 }; Vector<nsecs_t> sampleEventTimes; Vector<PointerCoords> samplePointerCoords; for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) { sampleEventTimes.push(i + 10); for (size_t j = 0; j < pointerCount; j++) { samplePointerCoords.push(); samplePointerCoords.editTop().x = 100 * i + j; samplePointerCoords.editTop().y = 200 * i + j; samplePointerCoords.editTop().pressure = 0.5 * i + j; samplePointerCoords.editTop().size = 0.7 * i + j; samplePointerCoords.editTop().touchMajor = 1.5 * i + j; samplePointerCoords.editTop().touchMinor = 1.7 * i + j; samplePointerCoords.editTop().toolMajor = 2.5 * i + j; samplePointerCoords.editTop().toolMinor = 2.7 * i + j; samplePointerCoords.editTop().orientation = 3.5 * i + j; } } status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags, metaState, xOffset, yOffset, xPrecision, yPrecision, downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array()); ASSERT_EQ(OK, status) << "publisher publishMotionEvent should return OK"; for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) { size_t sampleIndex = i + 1; status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex], samplePointerCoords.array() + sampleIndex * pointerCount); ASSERT_EQ(OK, status) << "publisher appendMotionEvent should return OK"; } status = mPublisher->sendDispatchSignal(); ASSERT_EQ(OK, status) << "publisher sendDispatchSignal should return OK"; for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) { size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch; status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex], samplePointerCoords.array() + sampleIndex * pointerCount); ASSERT_EQ(OK, status) << "publisher appendMotionEvent should return OK"; } status = mConsumer->receiveDispatchSignal(); ASSERT_EQ(OK, status) << "consumer receiveDispatchSignal should return OK"; InputEvent* event; status = mConsumer->consume(& mEventFactory, & event); ASSERT_EQ(OK, status) << "consumer consume should return OK"; ASSERT_TRUE(event != NULL) << "consumer should have returned non-NULL event"; ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) << "consumer should have returned a motion event"; size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch; MotionEvent* motionEvent = static_cast<MotionEvent*>(event); EXPECT_EQ(deviceId, motionEvent->getDeviceId()); EXPECT_EQ(source, motionEvent->getSource()); EXPECT_EQ(action, motionEvent->getAction()); EXPECT_EQ(flags, motionEvent->getFlags()); EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags()); EXPECT_EQ(metaState, motionEvent->getMetaState()); EXPECT_EQ(xPrecision, motionEvent->getXPrecision()); EXPECT_EQ(yPrecision, motionEvent->getYPrecision()); EXPECT_EQ(downTime, motionEvent->getDownTime()); EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime()); EXPECT_EQ(pointerCount, motionEvent->getPointerCount()); EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize()); for (size_t i = 0; i < pointerCount; i++) { SCOPED_TRACE(i); EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i)); } for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) { SCOPED_TRACE(sampleIndex); EXPECT_EQ(sampleEventTimes[sampleIndex], motionEvent->getHistoricalEventTime(sampleIndex)); for (size_t i = 0; i < pointerCount; i++) { SCOPED_TRACE(i); size_t offset = sampleIndex * pointerCount + i; EXPECT_EQ(samplePointerCoords[offset].x, motionEvent->getHistoricalRawX(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].y, motionEvent->getHistoricalRawY(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].x + xOffset, motionEvent->getHistoricalX(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].y + yOffset, motionEvent->getHistoricalY(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].pressure, motionEvent->getHistoricalPressure(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].size, motionEvent->getHistoricalSize(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].touchMajor, motionEvent->getHistoricalTouchMajor(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].touchMinor, motionEvent->getHistoricalTouchMinor(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].toolMajor, motionEvent->getHistoricalToolMajor(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].toolMinor, motionEvent->getHistoricalToolMinor(i, sampleIndex)); EXPECT_EQ(samplePointerCoords[offset].orientation, motionEvent->getHistoricalOrientation(i, sampleIndex)); } } SCOPED_TRACE(lastSampleIndex); EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime()); for (size_t i = 0; i < pointerCount; i++) { SCOPED_TRACE(i); size_t offset = lastSampleIndex * pointerCount + i; EXPECT_EQ(samplePointerCoords[offset].x, motionEvent->getRawX(i)); EXPECT_EQ(samplePointerCoords[offset].y, motionEvent->getRawY(i)); EXPECT_EQ(samplePointerCoords[offset].x + xOffset, motionEvent->getX(i)); EXPECT_EQ(samplePointerCoords[offset].y + yOffset, motionEvent->getY(i)); EXPECT_EQ(samplePointerCoords[offset].pressure, motionEvent->getPressure(i)); EXPECT_EQ(samplePointerCoords[offset].size, motionEvent->getSize(i)); EXPECT_EQ(samplePointerCoords[offset].touchMajor, motionEvent->getTouchMajor(i)); EXPECT_EQ(samplePointerCoords[offset].touchMinor, motionEvent->getTouchMinor(i)); EXPECT_EQ(samplePointerCoords[offset].toolMajor, motionEvent->getToolMajor(i)); EXPECT_EQ(samplePointerCoords[offset].toolMinor, motionEvent->getToolMinor(i)); EXPECT_EQ(samplePointerCoords[offset].orientation, motionEvent->getOrientation(i)); } status = mConsumer->sendFinishedSignal(); ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; status = mPublisher->receiveFinishedSignal(); ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK"; status = mPublisher->reset(); ASSERT_EQ(OK, status) << "publisher reset should return OK"; }
Dialog::DialogOutput MessageDialog::handleEvent(const InputEvent &event) { if (event.getType() == InputEvent::KEY_PRESSED || event.getType() == InputEvent::CLICK_SINGLE) return Dialog::CLOSE; return Dialog::NONE; }
void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() { status_t status; const uint32_t seq = 15; const int32_t deviceId = 1; const int32_t source = AINPUT_SOURCE_TOUCHSCREEN; const int32_t action = AMOTION_EVENT_ACTION_MOVE; const int32_t actionButton = 0; const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP; const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; const int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY; const float xOffset = -10; const float yOffset = -20; const float xPrecision = 0.25; const float yPrecision = 0.5; const nsecs_t downTime = 3; const size_t pointerCount = 3; const nsecs_t eventTime = 4; PointerProperties pointerProperties[pointerCount]; PointerCoords pointerCoords[pointerCount]; for (size_t i = 0; i < pointerCount; i++) { pointerProperties[i].clear(); pointerProperties[i].id = (i + 2) % pointerCount; pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; pointerCoords[i].clear(); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i); pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i); } status = mPublisher->publishMotionEvent(seq, deviceId, source, action, actionButton, flags, edgeFlags, metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount, pointerProperties, pointerCoords); ASSERT_EQ(OK, status) << "publisher publishMotionEvent should return OK"; uint32_t consumeSeq; InputEvent* event; status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event); ASSERT_EQ(OK, status) << "consumer consume should return OK"; ASSERT_TRUE(event != NULL) << "consumer should have returned non-NULL event"; ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) << "consumer should have returned a motion event"; MotionEvent* motionEvent = static_cast<MotionEvent*>(event); EXPECT_EQ(seq, consumeSeq); EXPECT_EQ(deviceId, motionEvent->getDeviceId()); EXPECT_EQ(source, motionEvent->getSource()); EXPECT_EQ(action, motionEvent->getAction()); EXPECT_EQ(flags, motionEvent->getFlags()); EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags()); EXPECT_EQ(metaState, motionEvent->getMetaState()); EXPECT_EQ(buttonState, motionEvent->getButtonState()); EXPECT_EQ(xPrecision, motionEvent->getXPrecision()); EXPECT_EQ(yPrecision, motionEvent->getYPrecision()); EXPECT_EQ(downTime, motionEvent->getDownTime()); EXPECT_EQ(eventTime, motionEvent->getEventTime()); EXPECT_EQ(pointerCount, motionEvent->getPointerCount()); EXPECT_EQ(0U, motionEvent->getHistorySize()); for (size_t i = 0; i < pointerCount; i++) { SCOPED_TRACE(i); EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i)); EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), motionEvent->getRawX(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), motionEvent->getRawY(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset, motionEvent->getX(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset, motionEvent->getY(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i)); EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), motionEvent->getOrientation(i)); } status = mConsumer->sendFinishedSignal(seq, false); ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; uint32_t finishedSeq = 0; bool handled = true; status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled); ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK"; ASSERT_EQ(seq, finishedSeq) << "publisher receiveFinishedSignal should have returned the original sequence number"; ASSERT_FALSE(handled) << "publisher receiveFinishedSignal should have set handled to consumer's reply"; }
int NativeInputQueue::handleReceiveCallback(int receiveFd, int events, void* data) { NativeInputQueue* q = static_cast<NativeInputQueue*>(data); JNIEnv* env = AndroidRuntime::getJNIEnv(); sp<Connection> connection; InputEvent* inputEvent; jobject inputHandlerObjLocal; jlong finishedToken; { // acquire lock AutoMutex _l(q->mLock); ssize_t connectionIndex = q->mConnectionsByReceiveFd.indexOfKey(receiveFd); if (connectionIndex < 0) { ALOGE("Received spurious receive callback for unknown input channel. " "fd=%d, events=0x%x", receiveFd, events); return 0; // remove the callback } connection = q->mConnectionsByReceiveFd.valueAt(connectionIndex); if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) { ALOGE("channel '%s' ~ Publisher closed input channel or an error occurred. " "events=0x%x", connection->getInputChannelName(), events); return 0; // remove the callback } if (! (events & ALOOPER_EVENT_INPUT)) { ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " "events=0x%x", connection->getInputChannelName(), events); return 1; } status_t status = connection->inputConsumer.receiveDispatchSignal(); if (status) { ALOGE("channel '%s' ~ Failed to receive dispatch signal. status=%d", connection->getInputChannelName(), status); return 0; // remove the callback } if (connection->messageInProgress) { ALOGW("channel '%s' ~ Publisher sent spurious dispatch signal.", connection->getInputChannelName()); return 1; } status = connection->inputConsumer.consume(& connection->inputEventFactory, & inputEvent); if (status) { ALOGW("channel '%s' ~ Failed to consume input event. status=%d", connection->getInputChannelName(), status); connection->inputConsumer.sendFinishedSignal(false); return 1; } connection->messageInProgress = true; connection->messageSeqNum += 1; finishedToken = generateFinishedToken(receiveFd, connection->id, connection->messageSeqNum); inputHandlerObjLocal = env->NewLocalRef(connection->inputHandlerObjGlobal); } // release lock // Invoke the handler outside of the lock. // // Note: inputEvent is stored in a field of the connection object which could potentially // become disposed due to the input channel being unregistered concurrently. // For this reason, we explicitly keep the connection object alive by holding // a strong pointer to it within this scope. We also grabbed a local reference to // the input handler object itself for the same reason. int32_t inputEventType = inputEvent->getType(); jobject inputEventObj; jmethodID dispatchMethodId; switch (inputEventType) { case AINPUT_EVENT_TYPE_KEY: #if DEBUG_DISPATCH_CYCLE ALOGD("channel '%s' ~ Received key event.", connection->getInputChannelName()); #endif inputEventObj = android_view_KeyEvent_fromNative(env, static_cast<KeyEvent*>(inputEvent)); dispatchMethodId = gInputQueueClassInfo.dispatchKeyEvent; break; case AINPUT_EVENT_TYPE_MOTION: #if DEBUG_DISPATCH_CYCLE ALOGD("channel '%s' ~ Received motion event.", connection->getInputChannelName()); #endif inputEventObj = android_view_MotionEvent_obtainAsCopy(env, static_cast<MotionEvent*>(inputEvent)); dispatchMethodId = gInputQueueClassInfo.dispatchMotionEvent; break; default: assert(false); // InputConsumer should prevent this from ever happening inputEventObj = NULL; } if (! inputEventObj) { ALOGW("channel '%s' ~ Failed to obtain DVM event object.", connection->getInputChannelName()); env->DeleteLocalRef(inputHandlerObjLocal); q->finished(env, finishedToken, false, false); return 1; } #if DEBUG_DISPATCH_CYCLE ALOGD("Invoking input handler."); #endif env->CallStaticVoidMethod(gInputQueueClassInfo.clazz, dispatchMethodId, inputHandlerObjLocal, inputEventObj, jlong(finishedToken)); #if DEBUG_DISPATCH_CYCLE ALOGD("Returned from input handler."); #endif if (env->ExceptionCheck()) { ALOGE("An exception occurred while invoking the input handler for an event."); LOGE_EX(env); env->ExceptionClear(); q->finished(env, finishedToken, false, true /*ignoreSpuriousFinish*/); } env->DeleteLocalRef(inputEventObj); env->DeleteLocalRef(inputHandlerObjLocal); return 1; }