Skip to content

Commit

Permalink
fix: touches fall back to single touches if out of bounds (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Feb 4, 2025
1 parent 5e7fab8 commit 433db90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix: touches fall back to single touches if out of bounds ([#221](https://github.com/PostHog/posthog-android/pull/221))

## 3.11.1 - 2025-01-30

- fix: do not allow default integrations multiple times ([#219](https://github.com/PostHog/posthog-android/pull/219))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,24 @@ public class PostHogReplayIntegration(
) {
val mouseInteractions = mutableListOf<RRIncrementalMouseInteractionEvent>()
for (index in 0 until motionEvent.pointerCount) {
// if the id is 0, BE transformer will set it to the virtual bodyId
val id = motionEvent.getPointerId(index)
val absX = motionEvent.getRawXCompat(index).toInt().densityValue(displayMetrics.density)
val absY = motionEvent.getRawYCompat(index).toInt().densityValue(displayMetrics.density)

val mouseInteractionData =
RRIncrementalMouseInteractionData(
id = id,
type = type,
x = absX,
y = absY,
)
val mouseInteraction = RRIncrementalMouseInteractionEvent(mouseInteractionData, timestamp)
mouseInteractions.add(mouseInteraction)
try {
// if the id is 0, BE transformer will set it to the virtual bodyId
val id = motionEvent.getPointerId(index)
val absX = motionEvent.getRawXCompat(index).toInt().densityValue(displayMetrics.density)
val absY = motionEvent.getRawYCompat(index).toInt().densityValue(displayMetrics.density)

val mouseInteractionData =
RRIncrementalMouseInteractionData(
id = id,
type = type,
x = absX,
y = absY,
)
val mouseInteraction = RRIncrementalMouseInteractionEvent(mouseInteractionData, timestamp)
mouseInteractions.add(mouseInteraction)
} catch (e: Throwable) {
config.logger.log("Reading MotionEvent pointers failed: $e.")
}
}

if (mouseInteractions.isNotEmpty()) {
Expand Down Expand Up @@ -1237,18 +1241,26 @@ public class PostHogReplayIntegration(
}

private fun MotionEvent.getRawXCompat(index: Int): Float {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getRawX(index)
return if (index < 0 || index >= pointerCount) {
rawX // Fallback to single-touch `rawX` to prevent crashes
} else {
rawX
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getRawX(index)
} else {
rawX
}
}
}

private fun MotionEvent.getRawYCompat(index: Int): Float {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getRawY(index)
return if (index < 0 || index >= pointerCount) {
rawY // Fallback to single-touch `rawY` to prevent crashes
} else {
rawY
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getRawY(index)
} else {
rawY
}
}
}

Expand Down

0 comments on commit 433db90

Please sign in to comment.