diff options
| author | lumark <lumark@google.com> | 2019-11-10 15:58:23 +0800 |
|---|---|---|
| committer | lumark <lumark@google.com> | 2019-11-15 11:59:15 +0800 |
| commit | bf3ce30a69ba94b05cb2ad3ba41330276ed6f86e (patch) | |
| tree | 62a9262a24e81ac8686b444714757525d66819f6 /core/java/android | |
| parent | a96a64faaae8073e6d27d7b041109d556ba50b5a (diff) | |
Skip deleteSurroundingText when there is an invalid selection
Since InputConnection#deleteSurroundingText the design is to delete the
text before the selection start and after the selection end range,
it would be possible that the exception may happened when the obsolete
selection range which is not aligned with the current text content.
Add a check to make sure the text will be deleted when the number of
text before / selection the selection is > 0.
Also, skip the delection when the selection is not yet attached
Bug: 130979263
Test: atest BaseInputConnectionTest
Change-Id: I93b69b71531bcab4ae204c1c1287b8fbe0224ea8
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/view/inputmethod/BaseInputConnection.java | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java index 090e19f91e38..ae2fb8ebc24a 100644 --- a/core/java/android/view/inputmethod/BaseInputConnection.java +++ b/core/java/android/view/inputmethod/BaseInputConnection.java @@ -211,6 +211,10 @@ public class BaseInputConnection implements InputConnection { * If this is greater than the number of existing characters between the cursor and * the end of the text, then this method does not fail but deletes all the characters in * that range. + * + * @return {@code true} when selected text is deleted, {@code false} when either the + * selection is invalid or not yet attached (i.e. selection start or end is -1), + * or the editable text is {@code null}. */ public boolean deleteSurroundingText(int beforeLength, int afterLength) { if (DEBUG) Log.v(TAG, "deleteSurroundingText " + beforeLength @@ -229,6 +233,12 @@ public class BaseInputConnection implements InputConnection { b = tmp; } + // Skip when the selection is not yet attached. + if (a == -1 || b == -1) { + endBatchEdit(); + return false; + } + // Ignore the composing text. int ca = getComposingSpanStart(content); int cb = getComposingSpanEnd(content); @@ -247,8 +257,12 @@ public class BaseInputConnection implements InputConnection { if (beforeLength > 0) { int start = a - beforeLength; if (start < 0) start = 0; - content.delete(start, a); - deleted = a - start; + + final int numDeleteBefore = a - start; + if (a >= 0 && numDeleteBefore > 0) { + content.delete(start, a); + deleted = numDeleteBefore; + } } if (afterLength > 0) { @@ -257,7 +271,10 @@ public class BaseInputConnection implements InputConnection { int end = b + afterLength; if (end > content.length()) end = content.length(); - content.delete(b, end); + final int numDeleteAfter = end - b; + if (b >= 0 && numDeleteAfter > 0) { + content.delete(b, end); + } } endBatchEdit(); |
