Debian Codegraph @

core/jni/android_view_InputEventReceiver.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "InputEventReceiver"
18
19 //#define LOG_NDEBUG 0
20
21 #include <inttypes.h>
22
23 #include <nativehelper/JNIHelp.h>
24
25 #include <android-base/stringprintf.h>
26 #include <android_runtime/AndroidRuntime.h>
27 #include <input/InputTransport.h>
28 #include <log/log.h>
29 #include <utils/Looper.h>
30 #include <variant>
31 #include <vector>
32 #include "android_os_MessageQueue.h"
33 #include "android_view_InputChannel.h"
34 #include "android_view_KeyEvent.h"
35 #include "android_view_MotionEvent.h"
36
37 #include <nativehelper/ScopedLocalRef.h>
38
39 #include "core_jni_helpers.h"
40
41 namespace android {
42
43 static const bool kDebugDispatchCycle = false;
44
45 static const char* toString(bool value) {
46 return value ? "true" : "false";
47 }
48
49 static struct {
50 jclass clazz;
51
52 jmethodID dispatchInputEvent;
53 jmethodID onFocusEvent;
54 jmethodID onPointerCaptureEvent;
55 jmethodID onDragEvent;
56 jmethodID onBatchedInputEventPending;
57 jmethodID onTouchModeChanged;
58 } gInputEventReceiverClassInfo;
59
60 // Add prefix to the beginning of each line in 'str'
61 static std::string addPrefix(std::string str, std::string_view prefix) {
62 str.insert(0, prefix); // insert at the beginning of the first line
63 const size_t prefixLength = prefix.length();
64 size_t pos = prefixLength; // just inserted prefix. start at the end of it
65 while (true) { // process all newline characters in 'str'
66 pos = str.find('\n', pos);
67 if (pos == std::string::npos) {
68 break;
69 }
70 str.insert(pos + 1, prefix); // insert prefix just after the '\n' character
71 pos += prefixLength + 1; // advance the position past the newly inserted prefix
72 }
73 return str;
74 }
75
76 class NativeInputEventReceiver : public LooperCallback {
77 public:
78 NativeInputEventReceiver(JNIEnv* env, jobject receiverWeak,
79 const std::shared_ptr<InputChannel>& inputChannel,
80 const sp<MessageQueue>& messageQueue);
81
82 status_t initialize();
83 void dispose();
84 status_t finishInputEvent(uint32_t seq, bool handled);
85 status_t reportTimeline(int32_t inputEventId, nsecs_t gpuCompletedTime, nsecs_t presentTime);
86 status_t consumeEvents(JNIEnv* env, bool consumeBatches, nsecs_t frameTime,
87 bool* outConsumedBatch);
88 std::string dump(const char* prefix);
89
90 protected:
91 virtual ~NativeInputEventReceiver();
92
93 private:
94 struct Finish {
95 uint32_t seq;
96 bool handled;
97 };
98
99 struct Timeline {
100 int32_t inputEventId;
101 std::array<nsecs_t, GraphicsTimeline::SIZE> timeline;
102 };
103 typedef std::variant<Finish, Timeline> OutboundEvent;
104
105 jobject mReceiverWeakGlobal;
106 InputConsumer mInputConsumer;
107 sp<MessageQueue> mMessageQueue;
108 PreallocatedInputEventFactory mInputEventFactory;
109 bool mBatchedInputEventPending;
110 int mFdEvents;
111 std::vector<OutboundEvent> mOutboundQueue;
112
113 void setFdEvents(int events);
114
115 const std::string getInputChannelName() {
116 return mInputConsumer.getChannel()->getName();
117 }
118
119 status_t processOutboundEvents();
120 // From 'LooperCallback'
121 int handleEvent(int receiveFd, int events, void* data) override;
122 };
123
124 NativeInputEventReceiver::NativeInputEventReceiver(
125 JNIEnv* env, jobject receiverWeak, const std::shared_ptr<InputChannel>& inputChannel,
126 const sp<MessageQueue>& messageQueue)
127 : mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
128 mInputConsumer(inputChannel),
129 mMessageQueue(messageQueue),
130 mBatchedInputEventPending(false),
131 mFdEvents(0) {
132 if (kDebugDispatchCycle) {
133 ALOGD("channel '%s' ~ Initializing input event receiver.", getInputChannelName().c_str());
134 }
135 }
136
137 NativeInputEventReceiver::~NativeInputEventReceiver() {
138 JNIEnv* env = AndroidRuntime::getJNIEnv();
139 env->DeleteGlobalRef(mReceiverWeakGlobal);
140 }
141
142 status_t NativeInputEventReceiver::initialize() {
143 setFdEvents(ALOOPER_EVENT_INPUT);
144 return OK;
145 }
146
147 void NativeInputEventReceiver::dispose() {
148 if (kDebugDispatchCycle) {
149 ALOGD("channel '%s' ~ Disposing input event receiver.", getInputChannelName().c_str());
150 }
151
152 setFdEvents(0);
153 }
154
155 status_t NativeInputEventReceiver::finishInputEvent(uint32_t seq, bool handled) {
156 if (kDebugDispatchCycle) {
157 ALOGD("channel '%s' ~ Finished input event.", getInputChannelName().c_str());
158 }
159
160 Finish finish{
161 .seq = seq,
162 .handled = handled,
163 };
164 mOutboundQueue.push_back(finish);
165 return processOutboundEvents();
166 }
167
168 status_t NativeInputEventReceiver::reportTimeline(int32_t inputEventId, nsecs_t gpuCompletedTime,
169 nsecs_t presentTime) {
170 if (kDebugDispatchCycle) {
171 ALOGD("channel '%s' ~ %s", getInputChannelName().c_str(), __func__);
172 }
173 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
174 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = gpuCompletedTime;
175 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = presentTime;
176 Timeline timeline{
177 .inputEventId = inputEventId,
178 .timeline = graphicsTimeline,
179 };
180 mOutboundQueue.push_back(timeline);
181 return processOutboundEvents();
182 }
183
184 void NativeInputEventReceiver::setFdEvents(int events) {
185 if (mFdEvents != events) {
186 mFdEvents = events;
187 int fd = mInputConsumer.getChannel()->getFd();
188 if (events) {
189 mMessageQueue->getLooper()->addFd(fd, 0, events, this, nullptr);
190 } else {
191 mMessageQueue->getLooper()->removeFd(fd);
192 }
193 }
194 }
195
196 /**
197 * Receiver's primary role is to receive input events, but it has an additional duty of sending
198 * 'ack' for events (using the call 'finishInputEvent') and reporting input event timeline.
199 *
200 * If we are looking at the communication between InputPublisher and InputConsumer, we can say that
201 * from the InputConsumer's perspective, InputMessage's that are sent from publisher to consumer are
202 * called 'inbound / incoming' events, and the InputMessage's sent from InputConsumer to
203 * InputPublisher are 'outbound / outgoing' events.
204 *
205 * NativeInputEventReceiver owns (and acts like) an InputConsumer. So the finish events are outbound
206 * from InputEventReceiver (and will be sent to the InputPublisher). Likewise, timeline events are
207 * outbound events.
208 *
209 * In this function, send as many events from 'mOutboundQueue' as possible across the socket to the
210 * InputPublisher. If no events are remaining, let the looper know so that it doesn't wake up
211 * unnecessarily.
212 */
213 status_t NativeInputEventReceiver::processOutboundEvents() {
214 while (!mOutboundQueue.empty()) {
215 OutboundEvent& outbound = *mOutboundQueue.begin();
216 status_t status;
217
218 if (std::holds_alternative<Finish>(outbound)) {
219 const Finish& finish = std::get<Finish>(outbound);
220 status = mInputConsumer.sendFinishedSignal(finish.seq, finish.handled);
221 } else if (std::holds_alternative<Timeline>(outbound)) {
222 const Timeline& timeline = std::get<Timeline>(outbound);
223 status = mInputConsumer.sendTimeline(timeline.inputEventId, timeline.timeline);
224 } else {
225 LOG_ALWAYS_FATAL("Unexpected event type in std::variant");
226 status = BAD_VALUE;
227 }
228 if (status == OK) {
229 // Successful send. Erase the entry and keep trying to send more
230 mOutboundQueue.erase(mOutboundQueue.begin());
231 continue;
232 }
233
234 // Publisher is busy, try again later. Keep this entry (do not erase)
235 if (status == WOULD_BLOCK) {
236 if (kDebugDispatchCycle) {
237 ALOGD("channel '%s' ~ Remaining outbound events: %zu.",
238 getInputChannelName().c_str(), mOutboundQueue.size());
239 }
240 setFdEvents(ALOOPER_EVENT_INPUT | ALOOPER_EVENT_OUTPUT);
241 return WOULD_BLOCK; // try again later
242 }
243
244 // Some other error. Give up
245 ALOGW("Failed to send outbound event on channel '%s'. status=%s(%d)",
246 getInputChannelName().c_str(), statusToString(status).c_str(), status);
247 if (status != DEAD_OBJECT) {
248 JNIEnv* env = AndroidRuntime::getJNIEnv();
249 std::string message =
250 android::base::StringPrintf("Failed to send outbound event. status=%s(%d)",
251 statusToString(status).c_str(), status);
252 jniThrowRuntimeException(env, message.c_str());
253 mMessageQueue->raiseAndClearException(env, "finishInputEvent");
254 }
255 return status;
256 }
257
258 // The queue is now empty. Tell looper there's no more output to expect.
259 setFdEvents(ALOOPER_EVENT_INPUT);
260 return OK;
261 }
262
263 int NativeInputEventReceiver::handleEvent(int receiveFd, int events, void* data) {
264 // Allowed return values of this function as documented in LooperCallback::handleEvent
265 constexpr int REMOVE_CALLBACK = 0;
266 constexpr int KEEP_CALLBACK = 1;
267
268 if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
269 // This error typically occurs when the publisher has closed the input channel
270 // as part of removing a window or finishing an IME session, in which case
271 // the consumer will soon be disposed as well.
272 if (kDebugDispatchCycle) {
273 ALOGD("channel '%s' ~ Publisher closed input channel or an error occurred. events=0x%x",
274 getInputChannelName().c_str(), events);
275 }
276 return REMOVE_CALLBACK;
277 }
278
279 if (events & ALOOPER_EVENT_INPUT) {
280 JNIEnv* env = AndroidRuntime::getJNIEnv();
281 status_t status = consumeEvents(env, false /*consumeBatches*/, -1, nullptr);
282 mMessageQueue->raiseAndClearException(env, "handleReceiveCallback");
283 return status == OK || status == NO_MEMORY ? KEEP_CALLBACK : REMOVE_CALLBACK;
284 }
285
286 if (events & ALOOPER_EVENT_OUTPUT) {
287 const status_t status = processOutboundEvents();
288 if (status == OK || status == WOULD_BLOCK) {
289 return KEEP_CALLBACK;
290 } else {
291 return REMOVE_CALLBACK;
292 }
293 }
294
295 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. events=0x%x",
296 getInputChannelName().c_str(), events);
297 return KEEP_CALLBACK;
298 }
299
300 status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env,
301 bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) {
302 if (kDebugDispatchCycle) {
303 ALOGD("channel '%s' ~ Consuming input events, consumeBatches=%s, frameTime=%" PRId64,
304 getInputChannelName().c_str(), toString(consumeBatches), frameTime);
305 }
306
307 if (consumeBatches) {
308 mBatchedInputEventPending = false;
309 }
310 if (outConsumedBatch) {
311 *outConsumedBatch = false;
312 }
313
314 ScopedLocalRef<jobject> receiverObj(env, nullptr);
315 bool skipCallbacks = false;
316 for (;;) {
317 uint32_t seq;
318 InputEvent* inputEvent;
319
320 status_t status = mInputConsumer.consume(&mInputEventFactory,
321 consumeBatches, frameTime, &seq, &inputEvent);
322 if (status != OK && status != WOULD_BLOCK) {
323 ALOGE("channel '%s' ~ Failed to consume input event. status=%s(%d)",
324 getInputChannelName().c_str(), statusToString(status).c_str(), status);
325 return status;
326 }
327
328 if (status == WOULD_BLOCK) {
329 if (!skipCallbacks && !mBatchedInputEventPending && mInputConsumer.hasPendingBatch()) {
330 // There is a pending batch. Come back later.
331 if (!receiverObj.get()) {
332 receiverObj.reset(GetReferent(env, mReceiverWeakGlobal));
333 if (!receiverObj.get()) {
334 ALOGW("channel '%s' ~ Receiver object was finalized "
335 "without being disposed.",
336 getInputChannelName().c_str());
337 return DEAD_OBJECT;
338 }
339 }
340
341 mBatchedInputEventPending = true;
342 if (kDebugDispatchCycle) {
343 ALOGD("channel '%s' ~ Dispatching batched input event pending notification.",
344 getInputChannelName().c_str());
345 }
346
347 env->CallVoidMethod(receiverObj.get(),
348 gInputEventReceiverClassInfo.onBatchedInputEventPending,
349 mInputConsumer.getPendingBatchSource());
350 if (env->ExceptionCheck()) {
351 ALOGE("Exception dispatching batched input events.");
352 mBatchedInputEventPending = false; // try again later
353 }
354 }
355 return OK;
356 }
357 assert(inputEvent);
358
359 if (!skipCallbacks) {
360 if (!receiverObj.get()) {
361 receiverObj.reset(GetReferent(env, mReceiverWeakGlobal));
362 if (!receiverObj.get()) {
363 ALOGW("channel '%s' ~ Receiver object was finalized "
364 "without being disposed.", getInputChannelName().c_str());
365 return DEAD_OBJECT;
366 }
367 }
368
369 jobject inputEventObj;
370 switch (inputEvent->getType()) {
371 case AINPUT_EVENT_TYPE_KEY:
372 if (kDebugDispatchCycle) {
373 ALOGD("channel '%s' ~ Received key event.", getInputChannelName().c_str());
374 }
375 inputEventObj = android_view_KeyEvent_fromNative(env,
376 static_cast<KeyEvent*>(inputEvent));
377 break;
378
379 case AINPUT_EVENT_TYPE_MOTION: {
380 if (kDebugDispatchCycle) {
381 ALOGD("channel '%s' ~ Received motion event.", getInputChannelName().c_str());
382 }
383 MotionEvent* motionEvent = static_cast<MotionEvent*>(inputEvent);
384 if ((motionEvent->getAction() & AMOTION_EVENT_ACTION_MOVE) && outConsumedBatch) {
385 *outConsumedBatch = true;
386 }
387 inputEventObj = android_view_MotionEvent_obtainAsCopy(env, motionEvent);
388 break;
389 }
390 case AINPUT_EVENT_TYPE_FOCUS: {
391 FocusEvent* focusEvent = static_cast<FocusEvent*>(inputEvent);
392 if (kDebugDispatchCycle) {
393 ALOGD("channel '%s' ~ Received focus event: hasFocus=%s.",
394 getInputChannelName().c_str(), toString(focusEvent->getHasFocus()));
395 }
396 env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.onFocusEvent,
397 jboolean(focusEvent->getHasFocus()));
398 finishInputEvent(seq, true /* handled */);
399 continue;
400 }
401 case AINPUT_EVENT_TYPE_CAPTURE: {
402 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(inputEvent);
403 if (kDebugDispatchCycle) {
404 ALOGD("channel '%s' ~ Received capture event: pointerCaptureEnabled=%s",
405 getInputChannelName().c_str(),
406 toString(captureEvent->getPointerCaptureEnabled()));
407 }
408 env->CallVoidMethod(receiverObj.get(),
409 gInputEventReceiverClassInfo.onPointerCaptureEvent,
410 jboolean(captureEvent->getPointerCaptureEnabled()));
411 finishInputEvent(seq, true /* handled */);
412 continue;
413 }
414 case AINPUT_EVENT_TYPE_DRAG: {
415 const DragEvent* dragEvent = static_cast<DragEvent*>(inputEvent);
416 if (kDebugDispatchCycle) {
417 ALOGD("channel '%s' ~ Received drag event: isExiting=%s",
418 getInputChannelName().c_str(), toString(dragEvent->isExiting()));
419 }
420 env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.onDragEvent,
421 jboolean(dragEvent->isExiting()), dragEvent->getX(),
422 dragEvent->getY());
423 finishInputEvent(seq, true /* handled */);
424 continue;
425 }
426 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
427 const TouchModeEvent* touchModeEvent = static_cast<TouchModeEvent*>(inputEvent);
428 if (kDebugDispatchCycle) {
429 ALOGD("channel '%s' ~ Received touch mode event: isInTouchMode=%s",
430 getInputChannelName().c_str(), toString(touchModeEvent->isInTouchMode()));
431 }
432 env->CallVoidMethod(receiverObj.get(),
433 gInputEventReceiverClassInfo.onTouchModeChanged,
434 jboolean(touchModeEvent->isInTouchMode()));
435 finishInputEvent(seq, true /* handled */);
436 continue;
437 }
438
439 default:
440 assert(false); // InputConsumer should prevent this from ever happening
441 inputEventObj = nullptr;
442 }
443
444 if (inputEventObj) {
445 if (kDebugDispatchCycle) {
446 ALOGD("channel '%s' ~ Dispatching input event.", getInputChannelName().c_str());
447 }
448 env->CallVoidMethod(receiverObj.get(),
449 gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj);
450 if (env->ExceptionCheck()) {
451 ALOGE("Exception dispatching input event.");
452 skipCallbacks = true;
453 }
454 env->DeleteLocalRef(inputEventObj);
455 } else {
456 ALOGW("channel '%s' ~ Failed to obtain event object.",
457 getInputChannelName().c_str());
458 skipCallbacks = true;
459 }
460 }
461 }
462 }
463
464 std::string NativeInputEventReceiver::dump(const char* prefix) {
465 std::string out;
466 std::string consumerDump = addPrefix(mInputConsumer.dump(), " ");
467 out = out + "mInputConsumer:\n" + consumerDump + "\n";
468
469 out += android::base::StringPrintf("mBatchedInputEventPending: %s\n",
470 toString(mBatchedInputEventPending));
471 out = out + "mOutboundQueue:\n";
472 for (const OutboundEvent& outbound : mOutboundQueue) {
473 if (std::holds_alternative<Finish>(outbound)) {
474 const Finish& finish = std::get<Finish>(outbound);
475 out += android::base::StringPrintf(" Finish: seq=%" PRIu32 " handled=%s\n", finish.seq,
476 toString(finish.handled));
477 } else if (std::holds_alternative<Timeline>(outbound)) {
478 const Timeline& timeline = std::get<Timeline>(outbound);
479 out += android::base::
480 StringPrintf(" Timeline: inputEventId=%" PRId32 " gpuCompletedTime=%" PRId64
481 ", presentTime=%" PRId64 "\n",
482 timeline.inputEventId,
483 timeline.timeline[GraphicsTimeline::GPU_COMPLETED_TIME],
484 timeline.timeline[GraphicsTimeline::PRESENT_TIME]);
485 }
486 }
487 if (mOutboundQueue.empty()) {
488 out = out + " <empty>\n";
489 }
490 return addPrefix(out, prefix);
491 }
492
493 static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak,
494 jobject inputChannelObj, jobject messageQueueObj) {
495 std::shared_ptr<InputChannel> inputChannel =
496 android_view_InputChannel_getInputChannel(env, inputChannelObj);
497 if (inputChannel == nullptr) {
498 jniThrowRuntimeException(env, "InputChannel is not initialized.");
499 return 0;
500 }
501
502 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
503 if (messageQueue == nullptr) {
504 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
505 return 0;
506 }
507
508 sp<NativeInputEventReceiver> receiver = new NativeInputEventReceiver(env,
509 receiverWeak, inputChannel, messageQueue);
510 status_t status = receiver->initialize();
511 if (status) {
512 std::string message = android::base::
513 StringPrintf("Failed to initialize input event receiver. status=%s(%d)",
514 statusToString(status).c_str(), status);
515 jniThrowRuntimeException(env, message.c_str());
516 return 0;
517 }
518
519 receiver->incStrong(gInputEventReceiverClassInfo.clazz); // retain a reference for the object
520 return reinterpret_cast<jlong>(receiver.get());
521 }
522
523 static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) {
524 sp<NativeInputEventReceiver> receiver =
525 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
526 receiver->dispose();
527 receiver->decStrong(gInputEventReceiverClassInfo.clazz); // drop reference held by the object
528 }
529
530 static void nativeFinishInputEvent(JNIEnv* env, jclass clazz, jlong receiverPtr,
531 jint seq, jboolean handled) {
532 sp<NativeInputEventReceiver> receiver =
533 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
534 status_t status = receiver->finishInputEvent(seq, handled);
535 if (status == OK || status == WOULD_BLOCK) {
536 return; // normal operation
537 }
538 if (status != DEAD_OBJECT) {
539 std::string message =
540 android::base::StringPrintf("Failed to finish input event. status=%s(%d)",
541 statusToString(status).c_str(), status);
542 jniThrowRuntimeException(env, message.c_str());
543 }
544 }
545
546 static void nativeReportTimeline(JNIEnv* env, jclass clazz, jlong receiverPtr, jint inputEventId,
547 jlong gpuCompletedTime, jlong presentTime) {
548 if (IdGenerator::getSource(inputEventId) != IdGenerator::Source::INPUT_READER) {
549 // skip this event, it did not originate from hardware
550 return;
551 }
552 sp<NativeInputEventReceiver> receiver =
553 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
554 status_t status = receiver->reportTimeline(inputEventId, gpuCompletedTime, presentTime);
555 if (status == OK || status == WOULD_BLOCK) {
556 return; // normal operation
557 }
558 if (status != DEAD_OBJECT) {
559 std::string message = android::base::StringPrintf("Failed to send timeline. status=%s(%d)",
560 strerror(-status), status);
561 jniThrowRuntimeException(env, message.c_str());
562 }
563 }
564
565 static jboolean nativeConsumeBatchedInputEvents(JNIEnv* env, jclass clazz, jlong receiverPtr,
566 jlong frameTimeNanos) {
567 sp<NativeInputEventReceiver> receiver =
568 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
569 bool consumedBatch;
570 status_t status = receiver->consumeEvents(env, true /*consumeBatches*/, frameTimeNanos,
571 &consumedBatch);
572 if (status && status != DEAD_OBJECT && !env->ExceptionCheck()) {
573 std::string message =
574 android::base::StringPrintf("Failed to consume batched input event. status=%s(%d)",
575 statusToString(status).c_str(), status);
576 jniThrowRuntimeException(env, message.c_str());
577 return JNI_FALSE;
578 }
579 return consumedBatch ? JNI_TRUE : JNI_FALSE;
580 }
581
582 static jstring nativeDump(JNIEnv* env, jclass clazz, jlong receiverPtr, jstring prefix) {
583 sp<NativeInputEventReceiver> receiver =
584 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
585 ScopedUtfChars prefixChars(env, prefix);
586 return env->NewStringUTF(receiver->dump(prefixChars.c_str()).c_str());
587 }
588
589 static const JNINativeMethod gMethods[] = {
590 /* name, signature, funcPtr */
591 {"nativeInit",
592 "(Ljava/lang/ref/WeakReference;Landroid/view/InputChannel;Landroid/os/MessageQueue;)J",
593 (void*)nativeInit},
594 {"nativeDispose", "(J)V", (void*)nativeDispose},
595 {"nativeFinishInputEvent", "(JIZ)V", (void*)nativeFinishInputEvent},
596 {"nativeReportTimeline", "(JIJJ)V", (void*)nativeReportTimeline},
597 {"nativeConsumeBatchedInputEvents", "(JJ)Z", (void*)nativeConsumeBatchedInputEvents},
598 {"nativeDump", "(JLjava/lang/String;)Ljava/lang/String;", (void*)nativeDump},
599 };
600
601 int register_android_view_InputEventReceiver(JNIEnv* env) {
602 int res = RegisterMethodsOrDie(env, "android/view/InputEventReceiver",
603 gMethods, NELEM(gMethods));
604
605 jclass clazz = FindClassOrDie(env, "android/view/InputEventReceiver");
606 gInputEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
607
608 gInputEventReceiverClassInfo.dispatchInputEvent = GetMethodIDOrDie(env,
609 gInputEventReceiverClassInfo.clazz,
610 "dispatchInputEvent", "(ILandroid/view/InputEvent;)V");
611 gInputEventReceiverClassInfo.onFocusEvent =
612 GetMethodIDOrDie(env, gInputEventReceiverClassInfo.clazz, "onFocusEvent", "(Z)V");
613 gInputEventReceiverClassInfo.onPointerCaptureEvent =
614 GetMethodIDOrDie(env, gInputEventReceiverClassInfo.clazz, "onPointerCaptureEvent",
615 "(Z)V");
616 gInputEventReceiverClassInfo.onDragEvent =
617 GetMethodIDOrDie(env, gInputEventReceiverClassInfo.clazz, "onDragEvent", "(ZFF)V");
618 gInputEventReceiverClassInfo.onTouchModeChanged =
619 GetMethodIDOrDie(env, gInputEventReceiverClassInfo.clazz, "onTouchModeChanged", "(Z)V");
620 gInputEventReceiverClassInfo.onBatchedInputEventPending =
621 GetMethodIDOrDie(env, gInputEventReceiverClassInfo.clazz, "onBatchedInputEventPending",
622 "(I)V");
623
624 return res;
625 }
626
627 } // namespace android