-
-
Notifications
You must be signed in to change notification settings - Fork 467
fix(gestures): Replace GestureDetectorCompat with lightweight detector to fix ANR #5138
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
base: main
Are you sure you want to change the base?
Changes from all commits
41beaed
664f126
50ac45b
d5baf94
7aa100c
1a7624e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package io.sentry.android.core.internal.gestures; | ||
|
|
||
| import android.content.Context; | ||
| import android.view.GestureDetector; | ||
| import android.view.MotionEvent; | ||
| import android.view.VelocityTracker; | ||
| import android.view.ViewConfiguration; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * A lightweight gesture detector that replaces {@link | ||
| * androidx.core.view.GestureDetectorCompat}/{@link GestureDetector} to avoid ANRs caused by | ||
| * Handler/MessageQueue lock contention and IPC calls (FrameworkStatsLog.write). | ||
| * | ||
| * <p>Only detects click (tap), scroll, and fling — the gestures used by {@link | ||
| * SentryGestureListener}. Long-press, show-press, and double-tap detection (which require Handler | ||
| * message scheduling) are intentionally omitted. | ||
| */ | ||
| @ApiStatus.Internal | ||
| public final class SentryGestureDetector { | ||
|
|
||
| private final @NotNull GestureDetector.OnGestureListener listener; | ||
| private final int touchSlopSquare; | ||
| private final int minimumFlingVelocity; | ||
| private final int maximumFlingVelocity; | ||
|
|
||
| private boolean isInTapRegion; | ||
| private float downX; | ||
| private float downY; | ||
| private float lastX; | ||
| private float lastY; | ||
| private @Nullable MotionEvent currentDownEvent; | ||
| private @Nullable VelocityTracker velocityTracker; | ||
|
|
||
| SentryGestureDetector( | ||
| final @NotNull Context context, final @NotNull GestureDetector.OnGestureListener listener) { | ||
| this.listener = listener; | ||
| final ViewConfiguration config = ViewConfiguration.get(context); | ||
| final int touchSlop = config.getScaledTouchSlop(); | ||
| this.touchSlopSquare = touchSlop * touchSlop; | ||
| this.minimumFlingVelocity = config.getScaledMinimumFlingVelocity(); | ||
| this.maximumFlingVelocity = config.getScaledMaximumFlingVelocity(); | ||
| } | ||
|
|
||
| boolean onTouchEvent(final @NotNull MotionEvent event) { | ||
| final int action = event.getActionMasked(); | ||
|
|
||
| if (velocityTracker == null) { | ||
| velocityTracker = VelocityTracker.obtain(); | ||
| } | ||
|
|
||
| if (action == MotionEvent.ACTION_DOWN) { | ||
| velocityTracker.clear(); | ||
| } | ||
| velocityTracker.addMovement(event); | ||
|
|
||
| switch (action) { | ||
| case MotionEvent.ACTION_DOWN: | ||
| downX = event.getX(); | ||
| downY = event.getY(); | ||
| lastX = downX; | ||
| lastY = downY; | ||
| isInTapRegion = true; | ||
|
|
||
| if (currentDownEvent != null) { | ||
| currentDownEvent.recycle(); | ||
| } | ||
| currentDownEvent = MotionEvent.obtain(event); | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| listener.onDown(event); | ||
| break; | ||
|
|
||
| case MotionEvent.ACTION_MOVE: | ||
| { | ||
| final float x = event.getX(); | ||
| final float y = event.getY(); | ||
| final float dx = x - downX; | ||
| final float dy = y - downY; | ||
| final float distanceSquare = (dx * dx) + (dy * dy); | ||
|
|
||
| if (distanceSquare > touchSlopSquare) { | ||
| final float scrollX = lastX - x; | ||
| final float scrollY = lastY - y; | ||
| listener.onScroll(currentDownEvent, event, scrollX, scrollY); | ||
| isInTapRegion = false; | ||
| lastX = x; | ||
| lastY = y; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case MotionEvent.ACTION_UP: | ||
| if (isInTapRegion) { | ||
| listener.onSingleTapUp(event); | ||
|
Comment on lines
+86
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixIn the Prompt for AI Agent |
||
| } else if (velocityTracker != null) { | ||
| final int pointerId = event.getPointerId(0); | ||
| velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity); | ||
| final float velocityX = velocityTracker.getXVelocity(pointerId); | ||
| final float velocityY = velocityTracker.getYVelocity(pointerId); | ||
|
|
||
| if (Math.abs(velocityX) > minimumFlingVelocity | ||
| || Math.abs(velocityY) > minimumFlingVelocity) { | ||
| listener.onFling(currentDownEvent, event, velocityX, velocityY); | ||
| } | ||
| } | ||
| cleanup(); | ||
| break; | ||
|
|
||
| case MotionEvent.ACTION_CANCEL: | ||
| cleanup(); | ||
| break; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private void cleanup() { | ||
| if (velocityTracker != null) { | ||
| velocityTracker.recycle(); | ||
| velocityTracker = null; | ||
| } | ||
| if (currentDownEvent != null) { | ||
| currentDownEvent.recycle(); | ||
| currentDownEvent = null; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.