From 0d92cf5450184f81c6589c7d6bcf8d6fb3083587 Mon Sep 17 00:00:00 2001 From: David 'Digit' Turner Date: Wed, 20 Mar 2013 11:13:38 +0100 Subject: [PATCH] ndk: Fix potential event handling issue in android_native_app_glue Developers report that concurrent events generated from different sources/devices can lead to application freezes then ANRs. See https://code.google.com/p/android/issues/detail?id=41755 where it is suggested that handling all incoming events in the "process_input" callback solves the issue. Hence this patch implements the suggestion, however it's unclear whether this solves the symptom, or the root cause of the problem. Change-Id: Ic6b0ad05d192763a6d8842c8befcb87db9714a3c --- .../android/native_app_glue/android_native_app_glue.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ndk/sources/android/native_app_glue/android_native_app_glue.c b/ndk/sources/android/native_app_glue/android_native_app_glue.c index 82fc03017..0c526fa84 100644 --- a/ndk/sources/android/native_app_glue/android_native_app_glue.c +++ b/ndk/sources/android/native_app_glue/android_native_app_glue.c @@ -186,15 +186,18 @@ static void android_app_destroy(struct android_app* android_app) { static void process_input(struct android_app* app, struct android_poll_source* source) { AInputEvent* event = NULL; - if (AInputQueue_getEvent(app->inputQueue, &event) >= 0) { + int processed = 0; + while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) { LOGV("New input event: type=%d\n", AInputEvent_getType(event)); if (AInputQueue_preDispatchEvent(app->inputQueue, event)) { - return; + continue; } int32_t handled = 0; if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event); AInputQueue_finishEvent(app->inputQueue, event, handled); - } else { + processed = 1; + } + if (processed == 0) { LOGE("Failure reading next input event: %s\n", strerror(errno)); } }