From 6a51deb7c89cc06df63af2e50eff0b321c2d90c8 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 10 Jan 2024 11:27:12 +0100 Subject: [PATCH 1/6] fix: Check keyboard status during snapshot instead of using view listener --- .../replay/PostHogReplayIntegration.kt | 60 ++++++++++--------- .../replay/internal/ViewTreeSnapshotStatus.kt | 1 + .../posthog-android-sample/build.gradle.kts | 3 + 3 files changed, 36 insertions(+), 28 deletions(-) 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 bd4de33a..d323f9a4 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 @@ -111,34 +111,6 @@ public class PostHogReplayIntegration( } } - // keyboard events - var open = false - ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets -> - val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime()) - if (open == imeVisible) { - return@setOnApplyWindowInsetsListener insets - } - open = imeVisible - - val payload = mutableMapOf() - if (imeVisible) { - val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom - payload["open"] = true - payload["height"] = imeHeight.densityValue(displayMetrics.density) - } else { - payload["open"] = false - } - - val event = RRCustomEvent( - tag = "keyboard", - payload = payload, - config.dateProvider.currentTimeMillis(), - ) - listOf(event).capture() - - insets - } - val status = ViewTreeSnapshotStatus(listener) decorViews[decorView] = status } @@ -159,6 +131,31 @@ public class PostHogReplayIntegration( } } + private fun detectKeyboardOpen(view: View, open: Boolean): Pair { + val insets = ViewCompat.getRootWindowInsets(view) ?: return Pair(open, null) + val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime()) + if (open == imeVisible) { + return Pair(open, null) + } + + val payload = mutableMapOf() + if (imeVisible) { + val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom + payload["open"] = true + payload["height"] = imeHeight.densityValue(displayMetrics.density) + } else { + payload["open"] = false + } + + val event = RRCustomEvent( + tag = "keyboard", + payload = payload, + config.dateProvider.currentTimeMillis(), + ) + + return Pair(imeVisible, event) + } + private val onTouchEventListener = OnTouchEventListener { motionEvent -> if (!isSessionReplayEnabled) { return@OnTouchEventListener @@ -341,6 +338,13 @@ public class PostHogReplayIntegration( } } + // detect keyboard status + val (open, event) = detectKeyboardOpen(view, status.keyboardOpen) + status.keyboardOpen = open + event?.let { + events.add(it) + } + if (events.isNotEmpty()) { events.capture() } diff --git a/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt b/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt index 925a3c15..cad76cc0 100644 --- a/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt +++ b/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt @@ -6,5 +6,6 @@ internal class ViewTreeSnapshotStatus( val listener: NextDrawListener, var sentFullSnapshot: Boolean = false, var sentMetaEvent: Boolean = false, + var keyboardOpen: Boolean = false, var lastSnapshot: RRWireframe? = null, ) diff --git a/posthog-samples/posthog-android-sample/build.gradle.kts b/posthog-samples/posthog-android-sample/build.gradle.kts index 3f4afbf4..69f86d94 100644 --- a/posthog-samples/posthog-android-sample/build.gradle.kts +++ b/posthog-samples/posthog-android-sample/build.gradle.kts @@ -34,6 +34,9 @@ android { sourceCompatibility = PosthogBuildConfig.Build.JAVA_VERSION targetCompatibility = PosthogBuildConfig.Build.JAVA_VERSION } + kotlinOptions { + jvmTarget = "1.8" + } kotlinOptions.postHogConfig(false) From be15072030d3546b7fcda926dfcdabea7db95303 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 10 Jan 2024 11:27:54 +0100 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d445c3e6..d2af5f8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next +- fix: Check keyboard status during snapshot instead of using Window listener ([#72](https://github.com/PostHog/posthog-android/pull/72)) + ## 3.1.0 - 2024-01-08 - chore: Add mutations support to Session Recording ([#72](https://github.com/PostHog/posthog-android/pull/72)) From c5098d43b96c1f1bb06efaa732ae5bd492f24072 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 10 Jan 2024 11:30:12 +0100 Subject: [PATCH 3/6] fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2af5f8b..5e5a5336 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Next -- fix: Check keyboard status during snapshot instead of using Window listener ([#72](https://github.com/PostHog/posthog-android/pull/72)) +- fix: Check keyboard status during snapshot instead of using WindowInsets listener ([#77](https://github.com/PostHog/posthog-android/pull/77)) ## 3.1.0 - 2024-01-08 From e948177ccca8ab4cafa046b636334a1d9bb3f718 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 10 Jan 2024 11:32:54 +0100 Subject: [PATCH 4/6] rename --- .../android/replay/PostHogReplayIntegration.kt | 12 ++++++------ .../replay/internal/ViewTreeSnapshotStatus.kt | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) 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 d323f9a4..cce5dc70 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 @@ -131,11 +131,11 @@ public class PostHogReplayIntegration( } } - private fun detectKeyboardOpen(view: View, open: Boolean): Pair { - val insets = ViewCompat.getRootWindowInsets(view) ?: return Pair(open, null) + private fun detectKeyboardVisibility(view: View, visible: Boolean): Pair { + val insets = ViewCompat.getRootWindowInsets(view) ?: return Pair(visible, null) val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime()) - if (open == imeVisible) { - return Pair(open, null) + if (visible == imeVisible) { + return Pair(visible, null) } val payload = mutableMapOf() @@ -339,8 +339,8 @@ public class PostHogReplayIntegration( } // detect keyboard status - val (open, event) = detectKeyboardOpen(view, status.keyboardOpen) - status.keyboardOpen = open + val (visible, event) = detectKeyboardVisibility(view, status.keyboardVisible) + status.keyboardVisible = visible event?.let { events.add(it) } diff --git a/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt b/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt index cad76cc0..96cf90d8 100644 --- a/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt +++ b/posthog-android/src/main/java/com/posthog/android/replay/internal/ViewTreeSnapshotStatus.kt @@ -6,6 +6,6 @@ internal class ViewTreeSnapshotStatus( val listener: NextDrawListener, var sentFullSnapshot: Boolean = false, var sentMetaEvent: Boolean = false, - var keyboardOpen: Boolean = false, + var keyboardVisible: Boolean = false, var lastSnapshot: RRWireframe? = null, ) From 95d426bab579c6c2ad18ede65f7536ce49be65dc Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 10 Jan 2024 11:33:52 +0100 Subject: [PATCH 5/6] rename --- .../java/com/posthog/android/replay/PostHogReplayIntegration.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cce5dc70..2662a5d1 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 @@ -338,7 +338,7 @@ public class PostHogReplayIntegration( } } - // detect keyboard status + // detect keyboard visibility val (visible, event) = detectKeyboardVisibility(view, status.keyboardVisible) status.keyboardVisible = visible event?.let { From 2b370c63e7b931ff2cf6880da70a833dfa43dfac Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:26:36 +0100 Subject: [PATCH 6/6] Update posthog-samples/posthog-android-sample/build.gradle.kts --- posthog-samples/posthog-android-sample/build.gradle.kts | 3 --- 1 file changed, 3 deletions(-) diff --git a/posthog-samples/posthog-android-sample/build.gradle.kts b/posthog-samples/posthog-android-sample/build.gradle.kts index 69f86d94..3f4afbf4 100644 --- a/posthog-samples/posthog-android-sample/build.gradle.kts +++ b/posthog-samples/posthog-android-sample/build.gradle.kts @@ -34,9 +34,6 @@ android { sourceCompatibility = PosthogBuildConfig.Build.JAVA_VERSION targetCompatibility = PosthogBuildConfig.Build.JAVA_VERSION } - kotlinOptions { - jvmTarget = "1.8" - } kotlinOptions.postHogConfig(false)