Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4e30bad5 authored by Haoyu Zhang's avatar Haoyu Zhang Committed by Automerger Merge Worker
Browse files

Merge "Exit insert mode when TextView#setText is called" into udc-qpr-dev am:...

Merge "Exit insert mode when TextView#setText is called" into udc-qpr-dev am: 8f7f72a5 am: dac2d6a5

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23164813



Change-Id: I35db3862af6ee613fd6e2834dd48252a4928a2ed
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents ebedb1af dac2d6a5
Loading
Loading
Loading
Loading
+55 −4
Original line number Diff line number Diff line
@@ -8105,6 +8105,16 @@ public class Editor {
        private final Paint mHighlightPaint;
        private final Path mHighlightPath;

        /**
         * Whether it is in the progress of updating transformation method. It's needed because
         * {@link TextView#setTransformationMethod(TransformationMethod)} will eventually call
         * {@link TextView#setText(CharSequence)}.
         * Because it normally should exit insert mode when {@link TextView#setText(CharSequence)}
         * is called externally, we need this boolean to distinguish whether setText is triggered
         * by setTransformation or not.
         */
        private boolean mUpdatingTransformationMethod;

        InsertModeController(@NonNull TextView textView) {
            mTextView = Objects.requireNonNull(textView);
            mIsInsertModeActive = false;
@@ -8137,7 +8147,7 @@ public class Editor {
            final boolean isSingleLine = mTextView.isSingleLine();
            mInsertModeTransformationMethod = new InsertModeTransformationMethod(offset,
                    isSingleLine, oldTransformationMethod);
            mTextView.setTransformationMethodInternal(mInsertModeTransformationMethod);
            setTransformationMethod(mInsertModeTransformationMethod, true);
            Selection.setSelection((Spannable) mTextView.getText(), offset);

            mIsInsertModeActive = true;
@@ -8145,6 +8155,10 @@ public class Editor {
        }

        void exitInsertMode() {
            exitInsertMode(true);
        }

        void exitInsertMode(boolean updateText) {
            if (!mIsInsertModeActive) return;
            if (mInsertModeTransformationMethod == null
                    || mInsertModeTransformationMethod != mTextView.getTransformationMethod()) {
@@ -8157,7 +8171,7 @@ public class Editor {
            final int selectionEnd = mTextView.getSelectionEnd();
            final TransformationMethod oldTransformationMethod =
                    mInsertModeTransformationMethod.getOldTransformationMethod();
            mTextView.setTransformationMethodInternal(oldTransformationMethod);
            setTransformationMethod(oldTransformationMethod, updateText);
            Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
            mIsInsertModeActive = false;
        }
@@ -8177,6 +8191,32 @@ public class Editor {
            }
        }

        /**
         * Update the TransformationMethod on the {@link TextView}.
         * @param method the new method to be set on the {@link TextView}/
         * @param updateText whether to update the text during setTransformationMethod call.
         */
        private void setTransformationMethod(TransformationMethod method, boolean updateText) {
            mUpdatingTransformationMethod = true;
            mTextView.setTransformationMethodInternal(method, updateText);
            mUpdatingTransformationMethod = false;
        }

        /**
         * Notify the InsertMode controller that the {@link TextView} is about to set its text.
         */
        void beforeSetText() {
            // TextView#setText is called because our call to
            // TextView#setTransformationMethodInternal in enterInsertMode() or exitInsertMode().
            // Do nothing in this case.
            if (mUpdatingTransformationMethod) {
                return;
            }
            // TextView#setText is called externally. Exit InsertMode but don't update text again
            // when calling setTransformationMethod.
            exitInsertMode(/* updateText */ false);
        }

        /**
         * Notify the {@link InsertModeController} before the TextView's
         * {@link TransformationMethod} is updated. If it's not in the insert mode,
@@ -8205,6 +8245,9 @@ public class Editor {
        return mInsertModeController.enterInsertMode(offset);
    }

    /**
     * Exit insert mode if this editor is in insert mode.
     */
    void exitInsertMode() {
        if (mInsertModeController == null) return;
        mInsertModeController.exitInsertMode();
@@ -8217,7 +8260,7 @@ public class Editor {
     */
    void setTransformationMethod(TransformationMethod method) {
        if (mInsertModeController == null || !mInsertModeController.mIsInsertModeActive) {
            mTextView.setTransformationMethodInternal(method);
            mTextView.setTransformationMethodInternal(method, /* updateText */ true);
            return;
        }

@@ -8226,10 +8269,18 @@ public class Editor {
        final int selectionStart = mTextView.getSelectionStart();
        final int selectionEnd = mTextView.getSelectionEnd();
        method = mInsertModeController.updateTransformationMethod(method);
        mTextView.setTransformationMethodInternal(method);
        mTextView.setTransformationMethodInternal(method, /* updateText */ true);
        Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
    }

    /**
     * Notify that the Editor that the associated {@link TextView} is about to set its text.
     */
    void beforeSetText() {
        if (mInsertModeController == null) return;
        mInsertModeController.beforeSetText();
    }

    /**
     * Initializes the nodeInfo with smart actions.
     */
+19 −3
Original line number Diff line number Diff line
@@ -2795,11 +2795,22 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        if (mEditor != null) {
            mEditor.setTransformationMethod(method);
        } else {
            setTransformationMethodInternal(method);
            setTransformationMethodInternal(method, /* updateText */ true);
        }
    }
    void setTransformationMethodInternal(@Nullable TransformationMethod method) {
    /**
     * Set the transformation that is applied to the text that this TextView is displaying,
     * optionally call the setText.
     * @param method the new transformation method to be set.
     * @param updateText whether the call {@link #setText} which will update the TextView to display
     *                   the new content. This method is helpful when updating
     *                   {@link TransformationMethod} inside {@link #setText}. It should only be
     *                   false if text will be updated immediately after this call, otherwise the
     *                   TextView will enter an inconsistent state.
     */
    void setTransformationMethodInternal(@Nullable TransformationMethod method,
            boolean updateText) {
        if (method == mTransformation) {
            // Avoid the setText() below if the transformation is
            // the same.
@@ -2821,7 +2832,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            mAllowTransformationLengthChange = false;
        }
        if (updateText) {
            setText(mText);
        }
        if (hasPasswordTransformationMethod()) {
            notifyViewAccessibilityStateChangedIfNeeded(
@@ -7000,6 +7013,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    @UnsupportedAppUsage
    private void setText(CharSequence text, BufferType type,
                         boolean notifyBefore, int oldlen) {
        if (mEditor != null) {
            mEditor.beforeSetText();
        }
        mTextSetFromXmlOrResourceId = false;
        if (text == null) {
            text = "";