Don't trigger links for touches outside of line bounds

LinkMovementMethod currently allows triggering links for touches outside of line bounds. A link on the top line can be triggered by a touch above the top of the layout, and a link at the end of a line can be triggered by a touch to the right of the end of the line.

This changes the behavior to only trigger links for touches within the line bounds.

Bug: 271473145
Test: atest LinkMovementMethodTest
Change-Id: Ib049c231cef08fa5f55160d32cb78eb993de93d9
This commit is contained in:
Justin Ghan
2023-03-28 06:25:45 +00:00
parent 130babbc82
commit 22da5b3a1e

View File

@@ -221,12 +221,20 @@ public class LinkMovementMethod extends ScrollingMovementMethod {
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] links;
if (y < 0 || y > layout.getHeight()) {
links = null;
} else {
int line = layout.getLineForVertical(y);
if (x < layout.getLineLeft(line) || x > layout.getLineRight(line)) {
links = null;
} else {
int off = layout.getOffsetForHorizontal(line, x);
links = buffer.getSpans(off, off, ClickableSpan.class);
}
}
ClickableSpan[] links = buffer.getSpans(off, off, ClickableSpan.class);
if (links.length != 0) {
if (links != null && links.length != 0) {
ClickableSpan link = links[0];
if (action == MotionEvent.ACTION_UP) {
if (link instanceof TextLinkSpan) {