From 6babf0ac313784bc2dab33c2421d2f7c19eb62bb Mon Sep 17 00:00:00 2001 From: Haoyu Zhang Date: Thu, 6 Jul 2023 15:31:38 -0700 Subject: [PATCH] Fix: setTransformationMethod in insert mode doesn't working after ag/23164813 CL ag/23164813 didn't update Editor#setTransformationMethod to use InsertModeController#setTransformationMethod but called TextView#setTransformationMethodInternal directly instead. This ends up triggers TextView#setText and then exit the insert mode due to text change. This CL fixed the issue and also refactory the logic of update transformationMethod into InsertModeController so that it's less error prone. Bug: 289953621 Test: atest TextViewHandwritingGestureTest Change-Id: I8373bf80e3ca9e417eaeee0446c124ba5cc5f5ac --- core/java/android/widget/Editor.java | 42 ++++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 3da9e96618f31..f39122a0a5c1e 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -8207,7 +8207,8 @@ public class Editor { */ void beforeSetText() { // TextView#setText is called because our call to - // TextView#setTransformationMethodInternal in enterInsertMode() or exitInsertMode(). + // TextView#setTransformationMethodInternal in enterInsertMode(), exitInsertMode() or + // updateTransformationMethod(). // Do nothing in this case. if (mUpdatingTransformationMethod) { return; @@ -8218,22 +8219,28 @@ public class Editor { } /** - * Notify the {@link InsertModeController} before the TextView's - * {@link TransformationMethod} is updated. If it's not in the insert mode, - * the given method is directly returned. Otherwise, it will wrap the given transformation - * method with an {@link InsertModeTransformationMethod} and then return. + * Notify the {@link InsertModeController} that TextView#setTransformationMethod is called. + * If it's not in the insert mode, the given transformation method is directly set to the + * TextView. Otherwise, it will wrap the given transformation method with an + * {@link InsertModeTransformationMethod} and then set it on the TextView. * - * @param oldTransformationMethod the new {@link TransformationMethod} to be set on the + * @param transformationMethod the new {@link TransformationMethod} to be set on the * TextView. - * @return the updated {@link TransformationMethod} to be set on the Textview. */ - TransformationMethod updateTransformationMethod( - TransformationMethod oldTransformationMethod) { - if (!mIsInsertModeActive) return oldTransformationMethod; + void updateTransformationMethod(TransformationMethod transformationMethod) { + if (!mIsInsertModeActive) { + setTransformationMethod(transformationMethod, /* updateText */ true); + return; + } + // Changing TransformationMethod will reset selection range to [0, 0), we need to + // manually restore the old selection range. + final int selectionStart = mTextView.getSelectionStart(); + final int selectionEnd = mTextView.getSelectionEnd(); mInsertModeTransformationMethod = mInsertModeTransformationMethod.update( - oldTransformationMethod, mTextView.isSingleLine()); - return mInsertModeTransformationMethod; + transformationMethod, mTextView.isSingleLine()); + setTransformationMethod(mInsertModeTransformationMethod, /* updateText */ true); + Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); } } @@ -8259,18 +8266,11 @@ public class Editor { * @param method the {@link TransformationMethod} to be set on the TextView. */ void setTransformationMethod(TransformationMethod method) { - if (mInsertModeController == null || !mInsertModeController.mIsInsertModeActive) { + if (mInsertModeController == null) { mTextView.setTransformationMethodInternal(method, /* updateText */ true); return; } - - // Changing TransformationMethod will reset selection range to [0, 0), we need to - // manually restore the old selection range. - final int selectionStart = mTextView.getSelectionStart(); - final int selectionEnd = mTextView.getSelectionEnd(); - method = mInsertModeController.updateTransformationMethod(method); - mTextView.setTransformationMethodInternal(method, /* updateText */ true); - Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); + mInsertModeController.updateTransformationMethod(method); } /**