-
-
Notifications
You must be signed in to change notification settings - Fork 466
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
Open
romtsn
wants to merge
5
commits into
main
Choose a base branch
from
rz/fix/anr-gesture-detector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+417
−48
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41beaed
fix(android): Replace GestureDetectorCompat with lightweight SentryGe…
romtsn 664f126
test(android): Add unit tests for SentryGestureDetector
romtsn 50ac45b
changelog
romtsn d5baf94
fix(gestures): Clear VelocityTracker on ACTION_DOWN to prevent stale …
romtsn 7aa100c
fix(gestures): Remove stale GestureDetectorCompat class availability …
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...id-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } 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; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.