diff options
author | Clara Bayarri <clarabayarri@google.com> | 2015-04-09 12:02:04 +0100 |
---|---|---|
committer | Clara Bayarri <clarabayarri@google.com> | 2015-04-09 13:40:20 +0100 |
commit | 13152d1c725c6310a697ebc521cf8b57a52490ac (patch) | |
tree | 6493290e7aa389f3c5f3d5f8bd9c8160ce8528a7 /core/java/android | |
parent | 7a11a0e8c1bf452de0516b675b0180f63d9c5c5d (diff) | |
download | frameworks_base-13152d1c725c6310a697ebc521cf8b57a52490ac.zip frameworks_base-13152d1c725c6310a697ebc521cf8b57a52490ac.tar.gz frameworks_base-13152d1c725c6310a697ebc521cf8b57a52490ac.tar.bz2 |
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
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/widget/Editor.java | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 32b99a8..c0908b3 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -1668,6 +1668,7 @@ public class Editor { if (mSelectionActionMode != null) { // Selection action mode is already started // TODO: revisit invocations to minimize this case. + mSelectionActionMode.invalidate(); return false; } ActionMode.Callback actionModeCallback = new SelectionActionModeCallback(); @@ -1681,6 +1682,7 @@ public class Editor { boolean startSelectionActionModeWithSelection() { if (mSelectionActionMode != null) { // Selection action mode is already started + mSelectionActionMode.invalidate(); return false; } @@ -2963,6 +2965,28 @@ public class Editor { @Override 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 < Build.VERSION_CODES.LOLLIPOP; final Context context = !legacy && menu instanceof MenuBuilder ? @@ -2971,11 +2995,6 @@ public class Editor { final TypedArray styledAttributes = context.obtainStyledAttributes( 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()) { menu.add(0, TextView.ID_CUT, 0, com.android.internal.R.string.cut). setIcon(styledAttributes.getResourceId( @@ -3010,37 +3029,33 @@ public class Editor { setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - if (mTextView.isSuggestionsEnabled() && isCursorInsideSuggestionSpan()) { - 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); - } + updateReplaceItem(menu); 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 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + updateReplaceItem(menu); + if (mCustomSelectionActionModeCallback != null) { return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu); } 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 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (mCustomSelectionActionModeCallback != null && @@ -3796,6 +3811,9 @@ public class Editor { Selection.setSelection((Spannable) mTextView.getText(), offset, mTextView.getSelectionEnd()); updateDrawable(); + if (mSelectionActionMode != null) { + mSelectionActionMode.invalidate(); + } } @Override @@ -3898,6 +3916,9 @@ public class Editor { public void updateSelection(int offset) { Selection.setSelection((Spannable) mTextView.getText(), mTextView.getSelectionStart(), offset); + if (mSelectionActionMode != null) { + mSelectionActionMode.invalidate(); + } updateDrawable(); } |