Merge "Fix: setTransformationMethod in insert mode doesn't working after ag/23164813" into udc-qpr-dev

This commit is contained in:
Haoyu Zhang
2023-07-29 08:22:23 +00:00
committed by Android (Google) Code Review

View File

@@ -8207,7 +8207,8 @@ public class Editor {
*/ */
void beforeSetText() { void beforeSetText() {
// TextView#setText is called because our call to // 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. // Do nothing in this case.
if (mUpdatingTransformationMethod) { if (mUpdatingTransformationMethod) {
return; return;
@@ -8218,22 +8219,28 @@ public class Editor {
} }
/** /**
* Notify the {@link InsertModeController} before the TextView's * Notify the {@link InsertModeController} that TextView#setTransformationMethod is called.
* {@link TransformationMethod} is updated. If it's not in the insert mode, * If it's not in the insert mode, the given transformation method is directly set to the
* the given method is directly returned. Otherwise, it will wrap the given transformation * TextView. Otherwise, it will wrap the given transformation method with an
* method with an {@link InsertModeTransformationMethod} and then return. * {@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. * TextView.
* @return the updated {@link TransformationMethod} to be set on the Textview.
*/ */
TransformationMethod updateTransformationMethod( void updateTransformationMethod(TransformationMethod transformationMethod) {
TransformationMethod oldTransformationMethod) { if (!mIsInsertModeActive) {
if (!mIsInsertModeActive) return oldTransformationMethod; 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( mInsertModeTransformationMethod = mInsertModeTransformationMethod.update(
oldTransformationMethod, mTextView.isSingleLine()); transformationMethod, mTextView.isSingleLine());
return mInsertModeTransformationMethod; 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. * @param method the {@link TransformationMethod} to be set on the TextView.
*/ */
void setTransformationMethod(TransformationMethod method) { void setTransformationMethod(TransformationMethod method) {
if (mInsertModeController == null || !mInsertModeController.mIsInsertModeActive) { if (mInsertModeController == null) {
mTextView.setTransformationMethodInternal(method, /* updateText */ true); mTextView.setTransformationMethodInternal(method, /* updateText */ true);
return; return;
} }
mInsertModeController.updateTransformationMethod(method);
// 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);
} }
/** /**