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

Commit c01f3fee authored by Gilles Debunne's avatar Gilles Debunne
Browse files

Text selection is preserved upon device rotation.

Note that this used to create problems with ExtractEditText. These potential issues
were not handled in that CL.

Bug 3236848

Change-Id: I4b83ce8f66eba04f34c2faa8e38b83eb992637f0
parent cf7775a8
Loading
Loading
Loading
Loading
+23 −5
Original line number Original line Diff line number Diff line
@@ -321,6 +321,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    private Callback mCustomSelectionActionModeCallback;
    private Callback mCustomSelectionActionModeCallback;


    private final int mSquaredTouchSlopDistance;
    private final int mSquaredTouchSlopDistance;
    // Set when this TextView gained focus with some text selected. Will start selection mode.
    private boolean mCreatedWithASelection = false;


    /*
    /*
     * Kick-start the font cache for the zygote process (to pay the cost of
     * Kick-start the font cache for the zygote process (to pay the cost of
@@ -3887,10 +3889,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        // This has to be checked here since:
        // This has to be checked here since:
        // - onFocusChanged cannot start it when focus is given to a view with selected text (after
        // - onFocusChanged cannot start it when focus is given to a view with selected text (after
        //   a screen rotation) since layout is not yet initialized at that point.
        //   a screen rotation) since layout is not yet initialized at that point.
        // - ExtractEditText does not call onFocus when it is displayed. Fixing this issue would
        if (mCreatedWithASelection) {
        //   allow to test for hasSelection in onFocusChanged, which would trigger a
            startSelectionActionMode();
        //   startTextSelectionMode here. TODO
            mCreatedWithASelection = false;
        if (this instanceof ExtractEditText && hasSelection() && canSelectText()) {
        }

        // Phone specific code (there is no ExtractEditText on tablets).
        // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can
        // not be set. Do the test here instead.
        if (this instanceof ExtractEditText && hasSelection()) {
            startSelectionActionMode();
            startSelectionActionMode();
        }
        }


@@ -7008,6 +7015,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            int selStart = getSelectionStart();
            int selStart = getSelectionStart();
            int selEnd = getSelectionEnd();
            int selEnd = getSelectionEnd();


            // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
            // mode for these, unless there was a specific selection already started.
            final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 &&
                    selEnd == mText.length();
            mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted;

            if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
            if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
                // If a tap was used to give focus to that view, move cursor at tap position.
                // If a tap was used to give focus to that view, move cursor at tap position.
                // Has to be done before onTakeFocus, which can be overloaded.
                // Has to be done before onTakeFocus, which can be overloaded.
@@ -8179,10 +8192,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            return false;
            return false;
        }
        }


        if (!hasSelection()) {
            // If selection mode is started after a device rotation, there is already a selection.
            selectCurrentWord();
            selectCurrentWord();
        }

        final InputMethodManager imm = (InputMethodManager)
        final InputMethodManager imm = (InputMethodManager)
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(this, 0, null);
        imm.showSoftInput(this, 0, null);

        ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
        ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
        mSelectionActionMode = startActionMode(actionModeCallback);
        mSelectionActionMode = startActionMode(actionModeCallback);
        return mSelectionActionMode != null;
        return mSelectionActionMode != null;