From 91373209da02813ed760d9ea201d6f917e2a1fc1 Mon Sep 17 00:00:00 2001 From: Victoria Lease Date: Fri, 7 Sep 2012 16:41:59 -0700 Subject: [PATCH] Avert crash when dragging text in same TextView The previous implementation made liberal assumptions about the size of the string being edited, as well as the position of the substring being dragged. Explicitly checking those assumptions fixes our IndexOutOfBoundsException. Bug: 7129392 Change-Id: I32560cf87fbbe703a8b26c7eb2a64c8f8c0bfcf1 --- core/java/android/widget/Editor.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 7f9dab9ad1cf3..237275ae597e1 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -1801,13 +1801,13 @@ public class Editor { mTextView.deleteText_internal(dragSourceStart, dragSourceEnd); // Make sure we do not leave two adjacent spaces. - CharSequence t = mTextView.getTransformedText(dragSourceStart - 1, dragSourceStart + 1); - if ( (dragSourceStart == 0 || Character.isSpaceChar(t.charAt(0))) && - (dragSourceStart == mTextView.getText().length() || - Character.isSpaceChar(t.charAt(1))) ) { - final int pos = dragSourceStart == mTextView.getText().length() ? - dragSourceStart - 1 : dragSourceStart; - mTextView.deleteText_internal(pos, pos + 1); + final int prevCharIdx = Math.max(0, dragSourceStart - 1); + final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1); + if (nextCharIdx > prevCharIdx + 1) { + CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx); + if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) { + mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1); + } } } }