Skip to content

Commit 9137320

Browse files
author
Victoria Lease
committed
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
1 parent 37ee534 commit 9137320

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

core/java/android/widget/Editor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,13 +1801,13 @@ void onDrop(DragEvent event) {
18011801
mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
18021802

18031803
// Make sure we do not leave two adjacent spaces.
1804-
CharSequence t = mTextView.getTransformedText(dragSourceStart - 1, dragSourceStart + 1);
1805-
if ( (dragSourceStart == 0 || Character.isSpaceChar(t.charAt(0))) &&
1806-
(dragSourceStart == mTextView.getText().length() ||
1807-
Character.isSpaceChar(t.charAt(1))) ) {
1808-
final int pos = dragSourceStart == mTextView.getText().length() ?
1809-
dragSourceStart - 1 : dragSourceStart;
1810-
mTextView.deleteText_internal(pos, pos + 1);
1804+
final int prevCharIdx = Math.max(0, dragSourceStart - 1);
1805+
final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
1806+
if (nextCharIdx > prevCharIdx + 1) {
1807+
CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
1808+
if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
1809+
mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
1810+
}
18111811
}
18121812
}
18131813
}

0 commit comments

Comments
 (0)