From 284edaec88eaf37461a590f40776dd064599af44 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 23 Aug 2024 15:14:09 +0200 Subject: [PATCH] recording: capture touch interaction off of main thread to avoid ANRs --- CHANGELOG.md | 2 ++ .../replay/PostHogReplayIntegration.kt | 27 ++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d31883..eca2acf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next +- recording: capture touch interaction off of main thread to avoid ANRs ([#154](https://github.com/PostHog/posthog-android/pull/154)) + ## 3.5.0 - 2024-08-08 - feat: add emulator detection property to static context ([#154](https://github.com/PostHog/posthog-android/pull/154)) diff --git a/posthog-android/src/main/java/com/posthog/android/replay/PostHogReplayIntegration.kt b/posthog-android/src/main/java/com/posthog/android/replay/PostHogReplayIntegration.kt index fbc10471..02825e2e 100644 --- a/posthog-android/src/main/java/com/posthog/android/replay/PostHogReplayIntegration.kt +++ b/posthog-android/src/main/java/com/posthog/android/replay/PostHogReplayIntegration.kt @@ -203,22 +203,25 @@ public class PostHogReplayIntegration( private val onTouchEventListener = OnTouchEventListener { motionEvent -> - try { - if (!isSessionReplayEnabled) { - return@OnTouchEventListener - } - val timestamp = config.dateProvider.currentTimeMillis() - when (motionEvent.action.and(MotionEvent.ACTION_MASK)) { - MotionEvent.ACTION_DOWN -> { - generateMouseInteractions(timestamp, motionEvent, RRMouseInteraction.TouchStart) + executor.submit { + try { + if (!isSessionReplayEnabled) { + return@submit } - MotionEvent.ACTION_UP -> { - generateMouseInteractions(timestamp, motionEvent, RRMouseInteraction.TouchEnd) + val timestamp = config.dateProvider.currentTimeMillis() + when (motionEvent.action.and(MotionEvent.ACTION_MASK)) { + MotionEvent.ACTION_DOWN -> { + generateMouseInteractions(timestamp, motionEvent, RRMouseInteraction.TouchStart) + } + MotionEvent.ACTION_UP -> { + generateMouseInteractions(timestamp, motionEvent, RRMouseInteraction.TouchEnd) + } } + } catch (e: Throwable) { + config.logger.log("OnTouchEventListener $motionEvent failed: $e.") } - } catch (e: Throwable) { - config.logger.log("OnTouchEventListener $motionEvent failed: $e.") } + return@OnTouchEventListener } private fun generateMouseInteractions(