From 6c4cbd4122edf8b78f34778400d485b11da98404 Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Mon, 10 Apr 2017 14:19:12 +0300 Subject: Android: Fix crash at exit We need to remove and release the surface imediately, otherwise setSurface might be called after the object is deleted. Task-number: QTBUG-59818 Change-Id: I3a09e3de1ceecc22d8d7a48e2fc1cfe40cf09f0a Reviewed-by: Laszlo Agocs Reviewed-by: Mathias Hasselmann Reviewed-by: Christian Stromme --- src/plugins/platforms/android/androidjnimain.cpp | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/plugins/platforms/android/androidjnimain.cpp') diff --git a/src/plugins/platforms/android/androidjnimain.cpp b/src/plugins/platforms/android/androidjnimain.cpp index 17c197ea38..bde7457c0b 100644 --- a/src/plugins/platforms/android/androidjnimain.cpp +++ b/src/plugins/platforms/android/androidjnimain.cpp @@ -402,11 +402,16 @@ namespace QtAndroid if (surfaceId == -1) return; - QJNIEnvironmentPrivate env; - if (!env) - return; + { + QMutexLocker lock(&m_surfacesMutex); + const auto &it = m_surfaces.find(surfaceId); + if (it != m_surfaces.end()) + m_surfaces.erase(it); + } - env->CallStaticVoidMethod(m_applicationClass, + QJNIEnvironmentPrivate env; + if (env) + env->CallStaticVoidMethod(m_applicationClass, m_destroySurfaceMethodID, surfaceId); } @@ -584,18 +589,12 @@ static void setSurface(JNIEnv *env, jobject /*thiz*/, jint id, jobject jSurface, { QMutexLocker lock(&m_surfacesMutex); const auto &it = m_surfaces.find(id); - if (it == m_surfaces.end()) { - qWarning()<<"Can't find surface" << id; - return; - } - auto surfaceClient = it.value(); - if (!surfaceClient) // This should never happen... + if (it == m_surfaces.end()) return; - surfaceClient->surfaceChanged(env, jSurface, w, h); - - if (!jSurface) - m_surfaces.erase(it); + auto surfaceClient = it.value(); + if (surfaceClient) + surfaceClient->surfaceChanged(env, jSurface, w, h); } static void setDisplayMetrics(JNIEnv */*env*/, jclass /*clazz*/, -- cgit v1.2.3 From 9091a058bc61e297abea03edca322edcd658cc1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Tue, 28 Mar 2017 15:53:42 +0200 Subject: Android: Fix application state tracking - Make sure we don't process state changes before the platform plugin is completely created and registered. - Protect shared data with mutexes. - Don't update the application state from different threads. This was causing issues when testing run-time permission checks, when the application quickly switches state due to permission dialog being shown. In this case the states would be incorrectly delivered when the application was made active again. Change-Id: I3446eab9414ee5437cd788c27d65f808d1314aa5 Reviewed-by: BogDan Vatra --- src/plugins/platforms/android/androidjnimain.cpp | 42 ++++++++++++++++-------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'src/plugins/platforms/android/androidjnimain.cpp') diff --git a/src/plugins/platforms/android/androidjnimain.cpp b/src/plugins/platforms/android/androidjnimain.cpp index bde7457c0b..e47dd91a3e 100644 --- a/src/plugins/platforms/android/androidjnimain.cpp +++ b/src/plugins/platforms/android/androidjnimain.cpp @@ -82,8 +82,8 @@ static jobject m_serviceObject = nullptr; static jmethodID m_setSurfaceGeometryMethodID = nullptr; static jmethodID m_destroySurfaceMethodID = nullptr; -static bool m_activityActive = true; // defaults to true because when the platform plugin is - // initialized, QtActivity::onResume() has already been called +static int m_pendingApplicationState = -1; +static QBasicMutex m_pendingAppStateMtx; static jclass m_bitmapClass = nullptr; static jmethodID m_createBitmapMethodID = nullptr; @@ -130,13 +130,22 @@ static const char m_qtTag[] = "Qt"; static const char m_classErrorMsg[] = "Can't find class \"%s\""; static const char m_methodErrorMsg[] = "Can't find method \"%s%s\""; +static void flushPendingApplicationState(); + namespace QtAndroid { void setAndroidPlatformIntegration(QAndroidPlatformIntegration *androidPlatformIntegration) { - m_surfacesMutex.lock(); + QMutexLocker lock(&m_surfacesMutex); m_androidPlatformIntegration = androidPlatformIntegration; - m_surfacesMutex.unlock(); + + // flush the pending state if necessary. + if (m_androidPlatformIntegration) { + flushPendingApplicationState(); + } else { + QMutexLocker locker(&m_pendingAppStateMtx); + m_pendingApplicationState = -1; + } } QAndroidPlatformIntegration *androidPlatformIntegration() @@ -215,12 +224,6 @@ namespace QtAndroid m_statusBarShowing = false; } - void setApplicationActive() - { - if (m_activityActive) - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive); - } - jobject createBitmap(QImage img, JNIEnv *env) { if (!m_bitmapClass) @@ -446,6 +449,16 @@ namespace QtAndroid } // namespace QtAndroid +// Force an update of the pending application state (state set before the platform plugin was created) +static void flushPendingApplicationState() +{ + QMutexLocker locker(&m_pendingAppStateMtx); + if (m_pendingApplicationState == -1) + return; + + QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState(m_pendingApplicationState)); + m_pendingApplicationState = -1; +} static jboolean startQtAndroidPlugin(JNIEnv* /*env*/, jobject /*object*//*, jobject applicationAssetManager*/) { @@ -655,13 +668,14 @@ static void updateWindow(JNIEnv */*env*/, jobject /*thiz*/) static void updateApplicationState(JNIEnv */*env*/, jobject /*thiz*/, jint state) { - m_activityActive = (state == Qt::ApplicationActive); - - if (!m_main || !m_androidPlatformIntegration || !QGuiApplicationPrivate::platformIntegration()) { - QAndroidPlatformIntegration::setDefaultApplicationState(Qt::ApplicationState(state)); + if (!m_main || !QtAndroid::androidPlatformIntegration()) { + QMutexLocker locker(&m_pendingAppStateMtx); + m_pendingApplicationState = Qt::ApplicationState(state); return; } + flushPendingApplicationState(); + if (state == Qt::ApplicationActive) QtAndroidPrivate::handleResume(); else if (state == Qt::ApplicationInactive) -- cgit v1.2.3