Fix crash during TextView.onCreateContextMenu
Test: bit CtsWidgetTestCases:TextViewTest
Bug:62450393
Change-Id: I162c28d253ca197eba9c4c148c76179c2880daa5
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 0e6e3ae..45e5f8a 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -2584,14 +2584,18 @@
if (offset == -1) {
return;
}
+
stopTextActionModeWithPreservingSelection();
- final boolean isOnSelection = mTextView.hasSelection()
- && offset >= mTextView.getSelectionStart() && offset <= mTextView.getSelectionEnd();
- if (!isOnSelection) {
- // Right clicked position is not on the selection. Remove the selection and move the
- // cursor to the right clicked position.
- Selection.setSelection((Spannable) mTextView.getText(), offset);
- stopTextActionMode();
+ if (mTextView.canSelectText()) {
+ final boolean isOnSelection = mTextView.hasSelection()
+ && offset >= mTextView.getSelectionStart()
+ && offset <= mTextView.getSelectionEnd();
+ if (!isOnSelection) {
+ // Right clicked position is not on the selection. Remove the selection and move the
+ // cursor to the right clicked position.
+ Selection.setSelection((Spannable) mTextView.getText(), offset);
+ stopTextActionMode();
+ }
}
if (shouldOfferToShowSuggestions()) {