From 1733a2ab06c6c76e30c1489804b33e720d4af589 Mon Sep 17 00:00:00 2001 From: Haoyu Zhang Date: Sun, 12 Mar 2023 06:25:08 -0700 Subject: [PATCH] Fix: TextLine IndexOutOfBoundsException when input password Bug: 271500223 Test: manually tested Change-Id: If20f7b2a53972f9dfbf9b0c2d5d81b87f8eeffd7 --- core/java/android/widget/TextView.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 1600a16566a5a..fd80981fe4b83 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -462,12 +462,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private static final int CHANGE_WATCHER_PRIORITY = 100; /** - * The span priority of the {@link TransformationMethod} that is set on the text. It must be + * The span priority of the {@link OffsetMapping} that is set on the text. It must be * higher than the {@link DynamicLayout}'s {@link TextWatcher}, so that the transformed text is * updated before {@link DynamicLayout#reflow(CharSequence, int, int, int)} being triggered * by {@link TextWatcher#onTextChanged(CharSequence, int, int, int)}. */ - private static final int TRANSFORMATION_SPAN_PRIORITY = 200; + private static final int OFFSET_MAPPING_SPAN_PRIORITY = 200; // New state used to change background based on whether this TextView is multiline. private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline }; @@ -7033,9 +7033,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } final int textLength = text.length(); + final boolean isOffsetMapping = mTransformed instanceof OffsetMapping; - if (text instanceof Spannable && (!mAllowTransformationLengthChange - || text instanceof OffsetMapping)) { + if (text instanceof Spannable && (!mAllowTransformationLengthChange || isOffsetMapping)) { Spannable sp = (Spannable) text; // Remove any ChangeWatchers that might have come from other TextViews. @@ -7053,8 +7053,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (mEditor != null) mEditor.addSpanWatchers(sp); if (mTransformation != null) { + final int priority = isOffsetMapping ? OFFSET_MAPPING_SPAN_PRIORITY : 0; sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE - | (TRANSFORMATION_SPAN_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT)); + | (priority << Spanned.SPAN_PRIORITY_SHIFT)); } if (mMovement != null) {