Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check keyboard status during snapshot instead of using WindowInsets listener #77

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- 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

- chore: Add mutations support to Session Recording ([#72](https://github.com/PostHog/posthog-android/pull/72))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>()
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
}
Expand All @@ -159,6 +131,31 @@ public class PostHogReplayIntegration(
}
}

private fun detectKeyboardVisibility(view: View, visible: Boolean): Pair<Boolean, RRCustomEvent?> {
val insets = ViewCompat.getRootWindowInsets(view) ?: return Pair(visible, null)
val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
if (visible == imeVisible) {
return Pair(visible, null)
}

val payload = mutableMapOf<String, Any>()
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
Expand Down Expand Up @@ -341,6 +338,13 @@ public class PostHogReplayIntegration(
}
}

// detect keyboard visibility
val (visible, event) = detectKeyboardVisibility(view, status.keyboardVisible)
status.keyboardVisible = visible
event?.let {
events.add(it)
}

if (events.isNotEmpty()) {
events.capture()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal class ViewTreeSnapshotStatus(
val listener: NextDrawListener,
var sentFullSnapshot: Boolean = false,
var sentMetaEvent: Boolean = false,
var keyboardVisible: Boolean = false,
var lastSnapshot: RRWireframe? = null,
)
Loading