-
Notifications
You must be signed in to change notification settings - Fork 70
fix(android): crash indexoutofboundsexecption set span #583
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
eszlamczyk
wants to merge
3
commits into
main
Choose a base branch
from
fix/580/android-crash
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.
+84
−51
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,8 @@ package com.swmansion.enriched.markdown.utils.text.view | |
|
|
||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.text.Selection | ||
| import android.text.Spannable | ||
| import android.text.method.LinkMovementMethod | ||
| import android.text.method.ArrowKeyMovementMethod | ||
| import android.view.MotionEvent | ||
| import android.view.ViewConfiguration | ||
| import android.widget.TextView | ||
|
|
@@ -13,12 +12,22 @@ import com.swmansion.enriched.markdown.spans.SpoilerSpan | |
| import com.swmansion.enriched.markdown.spoiler.SpoilerCapable | ||
| import kotlin.math.abs | ||
|
|
||
| class LinkLongPressMovementMethod : LinkMovementMethod() { | ||
| /** | ||
| * Movement method that adds link tap / long-press and spoiler tap handling on | ||
| * top of [ArrowKeyMovementMethod], the method [setTextIsSelectable] installs. | ||
| * | ||
| * Must never mutate the buffer's Selection spans — the platform Editor | ||
| * manages them during long-press gestures, and removing or overwriting | ||
| * them mid-gesture crashes on some OEM skins. | ||
| * See: https://github.com/software-mansion/react-native-enriched-markdown/issues/580 | ||
| */ | ||
|
Collaborator
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. The same as above 🔝 |
||
| class LinkLongPressMovementMethod : ArrowKeyMovementMethod() { | ||
| private val handler = Handler(Looper.getMainLooper()) | ||
| private var longPressRunnable: Runnable? = null | ||
|
|
||
| private var startX = 0f | ||
| private var startY = 0f | ||
| private var pressedLink: LinkSpan? = null | ||
|
|
||
| var isLinkTouchActive: Boolean = false | ||
| private set | ||
|
|
@@ -34,10 +43,10 @@ class LinkLongPressMovementMethod : LinkMovementMethod() { | |
| startX = event.x | ||
| startY = event.y | ||
|
|
||
| val span = findLinkSpan(widget, buffer, event) | ||
| isLinkTouchActive = span != null | ||
| pressedLink = findLinkSpan(widget, buffer, event) | ||
| isLinkTouchActive = pressedLink != null | ||
| isTouchWithinTextBounds = charOffsetAt(widget, event) != null | ||
| span?.let { scheduleLongPress(widget, it) } | ||
| pressedLink?.let { scheduleLongPress(widget, it) } | ||
| } | ||
|
|
||
| MotionEvent.ACTION_MOVE -> { | ||
|
|
@@ -47,49 +56,45 @@ class LinkLongPressMovementMethod : LinkMovementMethod() { | |
| ) { | ||
| cancelLongPress() | ||
| isLinkTouchActive = false | ||
| pressedLink = null | ||
| } | ||
| } | ||
|
|
||
| MotionEvent.ACTION_UP -> { | ||
| cancelLongPress() | ||
| val tappedLink = pressedLink | ||
| isLinkTouchActive = false | ||
| if (widget.hasSelection()) { | ||
| Selection.removeSelection(buffer) | ||
| } | ||
| pressedLink = null | ||
|
|
||
| if (handleSpoilerTap(widget, buffer, event)) { | ||
| Selection.removeSelection(buffer) | ||
| return true | ||
| } | ||
|
|
||
| // LinkSpan.onClick itself swallows the click that follows a completed | ||
| // long-press (and resets its internal flag), so it is always invoked | ||
| // for a tap that started and ended on the same link. | ||
| if (tappedLink != null && findLinkSpan(widget, buffer, event) === tappedLink) { | ||
| tappedLink.onClick(widget) | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| MotionEvent.ACTION_CANCEL -> { | ||
| cancelLongPress() | ||
| isLinkTouchActive = false | ||
| if (widget.hasSelection()) { | ||
| Selection.removeSelection(buffer) | ||
| } | ||
| pressedLink = null | ||
| } | ||
| } | ||
|
|
||
| // LinkMovementMethod.onTouchEvent does its own getOffsetForHorizontal | ||
| // without bounds checking, so it would click the nearest link even for | ||
| // taps in empty space past the end of a line. Skip the super call when | ||
| // the ACTION_DOWN landed outside actual text content so the touch falls | ||
| // through to the parent (e.g. RNGH Pressable). | ||
| // getOffsetForHorizontal snaps to the nearest character, so without this | ||
| // guard taps in empty space past the end of a line would still be treated | ||
| // as text touches. Let them fall through to the parent (e.g. RNGH | ||
| // Pressable) instead. | ||
| if (!isTouchWithinTextBounds) { | ||
| return false | ||
| } | ||
|
|
||
| val result = super.onTouchEvent(widget, buffer, event) | ||
|
|
||
| // LinkMovementMethod sets a Selection highlight around the link on ACTION_DOWN, | ||
| // which causes a visible selection color on the link text while pressed. | ||
| // We remove that selection immediately so the user never sees it. | ||
| if (event.action == MotionEvent.ACTION_DOWN) { | ||
| Selection.removeSelection(buffer) | ||
| } | ||
|
|
||
| return result | ||
| return super.onTouchEvent(widget, buffer, event) | ||
| } | ||
|
|
||
| private fun scheduleLongPress( | ||
|
|
@@ -100,9 +105,6 @@ class LinkLongPressMovementMethod : LinkMovementMethod() { | |
|
|
||
| longPressRunnable = | ||
| Runnable { | ||
| if (widget.hasSelection()) { | ||
| Selection.removeSelection(widget.text as Spannable) | ||
| } | ||
| // Execute the long click logic on the span | ||
| if (span.onLongClick(widget)) { | ||
| // If consumed, cancel the system's own long-press logic (like context menus) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The crash forensics are already well documented in the PR description and commit. Let's replace with something like this: