From 22da5b3a1eb21b3ce8a37d42bfa7bc47b7ed1cb9 Mon Sep 17 00:00:00 2001 From: Justin Ghan Date: Tue, 28 Mar 2023 06:25:45 +0000 Subject: [PATCH] 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 --- .../text/method/LinkMovementMethod.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/java/android/text/method/LinkMovementMethod.java b/core/java/android/text/method/LinkMovementMethod.java index dae978e571b76..9f4a0aea72078 100644 --- a/core/java/android/text/method/LinkMovementMethod.java +++ b/core/java/android/text/method/LinkMovementMethod.java @@ -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) {