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

Commit 13152d1c authored by Clara Bayarri's avatar Clara Bayarri
Browse files

Editor: Allow invalidation of the Text Selection Action Mode

This allow us to add/remove the "replace" item, which depends on
having a replacement span.

Change-Id: I47b6487fc91f80b9dab13e90b8d630194b3f9beb
parent 7a11a0e8
Loading
Loading
Loading
Loading
+45 −24
Original line number Original line Diff line number Diff line
@@ -1668,6 +1668,7 @@ public class Editor {
        if (mSelectionActionMode != null) {
        if (mSelectionActionMode != null) {
            // Selection action mode is already started
            // Selection action mode is already started
            // TODO: revisit invocations to minimize this case.
            // TODO: revisit invocations to minimize this case.
            mSelectionActionMode.invalidate();
            return false;
            return false;
        }
        }
        ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
        ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
@@ -1681,6 +1682,7 @@ public class Editor {
    boolean startSelectionActionModeWithSelection() {
    boolean startSelectionActionModeWithSelection() {
        if (mSelectionActionMode != null) {
        if (mSelectionActionMode != null) {
            // Selection action mode is already started
            // Selection action mode is already started
            mSelectionActionMode.invalidate();
            return false;
            return false;
        }
        }


@@ -2963,6 +2965,28 @@ public class Editor {


        @Override
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle(mTextView.getContext().getString(
                    com.android.internal.R.string.textSelectionCABTitle));
            mode.setSubtitle(null);
            mode.setTitleOptionalHint(true);
            populateMenuWithItems(menu);

            if (mCustomSelectionActionModeCallback != null) {
                if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) {
                    // The custom mode can choose to cancel the action mode
                    return false;
                }
            }

            if (menu.hasVisibleItems() || mode.getCustomView() != null) {
                mTextView.setHasTransientState(true);
                return true;
            } else {
                return false;
            }
        }

        private void populateMenuWithItems(Menu menu) {
            final boolean legacy = mTextView.getContext().getApplicationInfo().targetSdkVersion <
            final boolean legacy = mTextView.getContext().getApplicationInfo().targetSdkVersion <
                    Build.VERSION_CODES.LOLLIPOP;
                    Build.VERSION_CODES.LOLLIPOP;
            final Context context = !legacy && menu instanceof MenuBuilder ?
            final Context context = !legacy && menu instanceof MenuBuilder ?
@@ -2971,11 +2995,6 @@ public class Editor {
            final TypedArray styledAttributes = context.obtainStyledAttributes(
            final TypedArray styledAttributes = context.obtainStyledAttributes(
                    com.android.internal.R.styleable.SelectionModeDrawables);
                    com.android.internal.R.styleable.SelectionModeDrawables);


            mode.setTitle(mTextView.getContext().getString(
                    com.android.internal.R.string.textSelectionCABTitle));
            mode.setSubtitle(null);
            mode.setTitleOptionalHint(true);

            if (mTextView.canCut()) {
            if (mTextView.canCut()) {
                menu.add(0, TextView.ID_CUT, 0, com.android.internal.R.string.cut).
                menu.add(0, TextView.ID_CUT, 0, com.android.internal.R.string.cut).
                    setIcon(styledAttributes.getResourceId(
                    setIcon(styledAttributes.getResourceId(
@@ -3010,37 +3029,33 @@ public class Editor {
                    setShowAsAction(
                    setShowAsAction(
                            MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
                            MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


            if (mTextView.isSuggestionsEnabled() && isCursorInsideSuggestionSpan()) {
            updateReplaceItem(menu);
                menu.add(0, TextView.ID_REPLACE, 0, com.android.internal.R.string.replace).
                        setShowAsAction(
                                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            }


            styledAttributes.recycle();
            styledAttributes.recycle();

            if (mCustomSelectionActionModeCallback != null) {
                if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) {
                    // The custom mode can choose to cancel the action mode
                    return false;
                }
            }

            if (menu.hasVisibleItems() || mode.getCustomView() != null) {
                mTextView.setHasTransientState(true);
                return true;
            } else {
                return false;
            }
        }
        }


        @Override
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            updateReplaceItem(menu);

            if (mCustomSelectionActionModeCallback != null) {
            if (mCustomSelectionActionModeCallback != null) {
                return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu);
                return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu);
            }
            }
            return true;
            return true;
        }
        }


        private void updateReplaceItem(Menu menu) {
            boolean canReplace = mTextView.isSuggestionsEnabled() && isCursorInsideSuggestionSpan();
            boolean replaceItemExists = menu.findItem(TextView.ID_REPLACE) != null;
            if (canReplace && !replaceItemExists) {
                menu.add(0, TextView.ID_REPLACE, 0, com.android.internal.R.string.replace).
                setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            } else if (!canReplace && replaceItemExists) {
                menu.removeItem(TextView.ID_REPLACE);
            }
        }

        @Override
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (mCustomSelectionActionModeCallback != null &&
            if (mCustomSelectionActionModeCallback != null &&
@@ -3796,6 +3811,9 @@ public class Editor {
            Selection.setSelection((Spannable) mTextView.getText(), offset,
            Selection.setSelection((Spannable) mTextView.getText(), offset,
                    mTextView.getSelectionEnd());
                    mTextView.getSelectionEnd());
            updateDrawable();
            updateDrawable();
            if (mSelectionActionMode != null) {
                mSelectionActionMode.invalidate();
            }
        }
        }


        @Override
        @Override
@@ -3898,6 +3916,9 @@ public class Editor {
        public void updateSelection(int offset) {
        public void updateSelection(int offset) {
            Selection.setSelection((Spannable) mTextView.getText(),
            Selection.setSelection((Spannable) mTextView.getText(),
                    mTextView.getSelectionStart(), offset);
                    mTextView.getSelectionStart(), offset);
            if (mSelectionActionMode != null) {
                mSelectionActionMode.invalidate();
            }
            updateDrawable();
            updateDrawable();
        }
        }