diff options
| author | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-04-09 09:26:42 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-04-09 09:26:42 +0000 |
| commit | 1f2c6dea41adc4416e87aea76ad013aee22c00cf (patch) | |
| tree | 85f9e0caffbabd7a4d208811dcc6d5bc4f345850 /core/java/android | |
| parent | 219e139651f85423248459fb20cc06ad41a56307 (diff) | |
| parent | 4e51877f5cbdb4a92568dce50c2bdc381cfbe861 (diff) | |
Merge "Fix crash when modifying Selection" into pi-dev
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/text/Selection.java | 2 | ||||
| -rw-r--r-- | core/java/android/text/Spannable.java | 13 | ||||
| -rw-r--r-- | core/java/android/text/SpannableStringBuilder.java | 19 | ||||
| -rw-r--r-- | core/java/android/text/SpannableStringInternal.java | 13 | ||||
| -rw-r--r-- | core/java/android/view/inputmethod/BaseInputConnection.java | 2 | ||||
| -rw-r--r-- | core/java/android/widget/Editor.java | 4 | ||||
| -rw-r--r-- | core/java/android/widget/TextView.java | 2 |
7 files changed, 45 insertions, 10 deletions
diff --git a/core/java/android/text/Selection.java b/core/java/android/text/Selection.java index 34456580ee61..5256e471e974 100644 --- a/core/java/android/text/Selection.java +++ b/core/java/android/text/Selection.java @@ -180,7 +180,7 @@ public class Selection { * Remove the selection or cursor, if any, from the text. */ public static final void removeSelection(Spannable text) { - text.removeSpan(SELECTION_START); + text.removeSpan(SELECTION_START, Spanned.SPAN_INTERMEDIATE); text.removeSpan(SELECTION_END); removeMemory(text); } diff --git a/core/java/android/text/Spannable.java b/core/java/android/text/Spannable.java index 39b78eb056d3..8315b2aa52c6 100644 --- a/core/java/android/text/Spannable.java +++ b/core/java/android/text/Spannable.java @@ -46,6 +46,19 @@ extends Spanned public void removeSpan(Object what); /** + * Remove the specified object from the range of text to which it + * was attached, if any. It is OK to remove an object that was never + * attached in the first place. + * + * See {@link Spanned} for an explanation of what the flags mean. + * + * @hide + */ + default void removeSpan(Object what, int flags) { + removeSpan(what); + } + + /** * Factory used by TextView to create new {@link Spannable Spannables}. You can subclass * it to provide something other than {@link SpannableString}. * diff --git a/core/java/android/text/SpannableStringBuilder.java b/core/java/android/text/SpannableStringBuilder.java index d41dfdcd29fc..41a9c45aed57 100644 --- a/core/java/android/text/SpannableStringBuilder.java +++ b/core/java/android/text/SpannableStringBuilder.java @@ -312,7 +312,7 @@ public class SpannableStringBuilder implements CharSequence, GetChars, Spannable // The following condition indicates that the span would become empty (textIsRemoved || mSpanStarts[i] > start || mSpanEnds[i] < mGapStart)) { mIndexOfSpan.remove(mSpans[i]); - removeSpan(i); + removeSpan(i, 0 /* flags */); return true; } return resolveGap(mSpanStarts[i]) <= end && (i & 1) != 0 && @@ -472,7 +472,7 @@ public class SpannableStringBuilder implements CharSequence, GetChars, Spannable } // Note: caller is responsible for removing the mIndexOfSpan entry. - private void removeSpan(int i) { + private void removeSpan(int i, int flags) { Object object = mSpans[i]; int start = mSpanStarts[i]; @@ -496,7 +496,9 @@ public class SpannableStringBuilder implements CharSequence, GetChars, Spannable // Invariants must be restored before sending span removed notifications. restoreInvariants(); - sendSpanRemoved(object, start, end); + if ((flags & Spanned.SPAN_INTERMEDIATE) == 0) { + sendSpanRemoved(object, start, end); + } } // Documentation from interface @@ -782,10 +784,19 @@ public class SpannableStringBuilder implements CharSequence, GetChars, Spannable * Remove the specified markup object from the buffer. */ public void removeSpan(Object what) { + removeSpan(what, 0 /* flags */); + } + + /** + * Remove the specified markup object from the buffer. + * + * @hide + */ + public void removeSpan(Object what, int flags) { if (mIndexOfSpan == null) return; Integer i = mIndexOfSpan.remove(what); if (i != null) { - removeSpan(i.intValue()); + removeSpan(i.intValue(), flags); } } diff --git a/core/java/android/text/SpannableStringInternal.java b/core/java/android/text/SpannableStringInternal.java index 5dd1a52b4a7a..bcc2fda86074 100644 --- a/core/java/android/text/SpannableStringInternal.java +++ b/core/java/android/text/SpannableStringInternal.java @@ -249,6 +249,13 @@ import java.lang.reflect.Array; } /* package */ void removeSpan(Object what) { + removeSpan(what, 0 /* flags */); + } + + /** + * @hide + */ + public void removeSpan(Object what, int flags) { int count = mSpanCount; Object[] spans = mSpans; int[] data = mSpanData; @@ -262,11 +269,13 @@ import java.lang.reflect.Array; System.arraycopy(spans, i + 1, spans, i, c); System.arraycopy(data, (i + 1) * COLUMNS, - data, i * COLUMNS, c * COLUMNS); + data, i * COLUMNS, c * COLUMNS); mSpanCount--; - sendSpanRemoved(what, ostart, oend); + if ((flags & Spanned.SPAN_INTERMEDIATE) == 0) { + sendSpanRemoved(what, ostart, oend); + } return; } } diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java index 5f7a0f789fa4..090e19f91e38 100644 --- a/core/java/android/view/inputmethod/BaseInputConnection.java +++ b/core/java/android/view/inputmethod/BaseInputConnection.java @@ -522,7 +522,7 @@ public class BaseInputConnection implements InputConnection { b = tmp; } - if (a == b) return null; + if (a == b || a < 0) return null; if ((flags&GET_TEXT_WITH_STYLES) != 0) { return content.subSequence(a, b); diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 99467265b5c5..6af678b1053d 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -6031,7 +6031,9 @@ public class Editor { mSwitchedLines = false; final int selectionStart = mTextView.getSelectionStart(); final int selectionEnd = mTextView.getSelectionEnd(); - if (selectionStart > selectionEnd) { + if (selectionStart < 0 || selectionEnd < 0) { + Selection.removeSelection((Spannable) mTextView.getText()); + } else if (selectionStart > selectionEnd) { Selection.setSelection((Spannable) mTextView.getText(), selectionEnd, selectionStart); } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 11db6b650583..fae6db5d694f 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -9380,7 +9380,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener final int selectionStart = getSelectionStart(); final int selectionEnd = getSelectionEnd(); - return selectionStart >= 0 && selectionStart != selectionEnd; + return selectionStart >= 0 && selectionEnd > 0 && selectionStart != selectionEnd; } String getSelectedText() { |
