From 532f3c989ec02bc63ed0e88095a2f7588af72be4 Mon Sep 17 00:00:00 2001 From: Siyamed Sinir Date: Thu, 15 Jun 2017 18:22:31 -0700 Subject: [PATCH] Fix crash during TextView.onCreateContextMenu Test: bit CtsWidgetTestCases:TextViewTest Bug:62450393 Change-Id: I162c28d253ca197eba9c4c148c76179c2880daa5 --- core/java/android/widget/Editor.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 0e6e3aee28b1f..45e5f8adc4681 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -2584,14 +2584,18 @@ public class Editor { 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()) {