From bec9715905c9666db2f0081fcc71abaea5b82cb0 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Wed, 24 Feb 2016 18:40:09 -0800 Subject: Refresh action mode always when selection is modified. Text action mode was not correctly updated when the selection is modified by keyboard, app or IME. With this CL, Editor#refreshTextActionMode is called always when selection is updated. This method properly starts, stops or invalidates text action mode if selection was modified outside of cursor controllers. Bug: 10527362 Bug: 22937774 Change-Id: Ic025c109539c3b59638226be4c4c9adf5ea0c38c --- core/java/android/widget/TextView.java | 57 +++++++++++----------------------- 1 file changed, 18 insertions(+), 39 deletions(-) (limited to 'core/java/android/widget/TextView.java') diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 95fcdc179643..6feefb4e673b 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -1509,6 +1509,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (result != null) { if (isTextEditable()) { replaceSelectionWithText(result); + if (mEditor != null) { + mEditor.refreshTextActionMode(); + } } else { if (result.length() > 0) { Toast.makeText(getContext(), String.valueOf(result), Toast.LENGTH_LONG) @@ -1518,12 +1521,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } else if (mText instanceof Spannable) { // Reset the selection. - stopTextActionMode(); - Selection.setSelection((Spannable) mText, getSelectionStart(), getSelectionEnd()); - } - - if (mEditor.hasSelectionController()) { - mEditor.startSelectionActionMode(); + Selection.setSelection((Spannable) mText, getSelectionEnd()); } } } @@ -5391,11 +5389,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // - 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. if (mEditor != null && mEditor.mCreatedWithASelection) { - if (mEditor.extractedTextModeWillBeStarted()) { - mEditor.checkFieldAndSelectCurrentWord(); - } else { - mEditor.startSelectionActionMode(); - } + mEditor.refreshTextActionMode(); mEditor.mCreatedWithASelection = false; } @@ -6593,6 +6587,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // in the extracted view. mEditor.hideCursorAndSpanControllers(); stopTextActionMode(); + if (mEditor.mSelectionModifierCursorController != null) { + mEditor.mSelectionModifierCursorController.resetTouchOffsets(); + } } /** @@ -8288,6 +8285,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (newSelEnd < 0) { newSelEnd = Selection.getSelectionEnd(buf); } + if (mEditor != null) { + mEditor.refreshTextActionMode(); + } onSelectionChanged(newSelStart, newSelEnd); } } @@ -9217,10 +9217,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } if (start >= 0 && start <= end && end <= text.length()) { Selection.setSelection((Spannable) text, start, end); - // Make sure selection mode is engaged. - if (mEditor != null) { - mEditor.startSelectionActionMode(); - } return true; } } @@ -9411,16 +9407,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener switch (id) { case ID_SELECT_ALL: - // This starts an action mode if triggered from another action mode. Text is - // highlighted, so that it can be bulk edited, like selectAllOnFocus does. Returns - // true even if text is empty. - boolean shouldRestartActionMode = - mEditor != null && mEditor.mTextActionMode != null; - stopTextActionMode(); selectAllText(); - if (shouldRestartActionMode) { - mEditor.startSelectionActionMode(); - } return true; case ID_UNDO: @@ -9446,7 +9433,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener case ID_CUT: setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max))); deleteText_internal(min, max); - stopTextActionMode(); return true; case ID_COPY: @@ -9702,12 +9688,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } boolean selectAllText() { - // Need to hide insert point cursor controller before settings selection, otherwise insert - // point cursor controller obtains cursor update event and update cursor with cancelling - // selection. - if (mEditor != null) { - mEditor.hideInsertionPointCursorController(); - } final int length = mText.length(); Selection.setSelection((Spannable) mText, 0, length); return length > 0; @@ -9746,7 +9726,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } } - stopTextActionMode(); sLastCutCopyOrTextChangedTime = 0; } } @@ -9759,7 +9738,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener sharingIntent.removeExtra(android.content.Intent.EXTRA_TEXT); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, selectedText); getContext().startActivity(Intent.createChooser(sharingIntent, null)); - stopTextActionMode(); + Selection.setSelection((Spannable) mText, getSelectionEnd()); } } @@ -10077,6 +10056,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener && getAccessibilitySelectionEnd() == end) { return; } + CharSequence text = getIterableTextForAccessibility(); + if (Math.min(start, end) >= 0 && Math.max(start, end) <= text.length()) { + Selection.setSelection((Spannable) text, start, end); + } else { + Selection.removeSelection((Spannable) text); + } // Hide all selection controllers used for adjusting selection // since we are doing so explicitlty by other means and these // controllers interact with how selection behaves. @@ -10084,12 +10069,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener mEditor.hideCursorAndSpanControllers(); mEditor.stopTextActionMode(); } - CharSequence text = getIterableTextForAccessibility(); - if (Math.min(start, end) >= 0 && Math.max(start, end) <= text.length()) { - Selection.setSelection((Spannable) text, start, end); - } else { - Selection.removeSelection((Spannable) text); - } } /** @hide */ -- cgit v1.2.3