From 2c8b404b2967705e4730281f87612b49c81101ef Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Wed, 26 Apr 2023 19:56:02 +0900 Subject: [PATCH] Retruns true for hasSelection in reversed selection case Bug: 279701943 Test: atest android.widget.cts.TextViewTest#testCopyAndPaste_byKey_reversed Change-Id: I1686e82a3148aa7b367ee765b209273ffa12d5d4 --- core/java/android/widget/TextView.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 34fe935b55a0c..6c84f35f06f5a 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -11808,8 +11808,17 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener public boolean hasSelection() { final int selectionStart = getSelectionStart(); final int selectionEnd = getSelectionEnd(); + final int selectionMin; + final int selectionMax; + if (selectionStart < selectionEnd) { + selectionMin = selectionStart; + selectionMax = selectionEnd; + } else { + selectionMin = selectionEnd; + selectionMax = selectionStart; + } - return selectionStart >= 0 && selectionEnd > 0 && selectionStart != selectionEnd; + return selectionMin >= 0 && selectionMax > 0 && selectionMin != selectionMax; } String getSelectedText() {