Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf3ce30a authored by lumark's avatar lumark
Browse files

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
parent a96a64fa
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -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;

            final int numDeleteBefore = a - start;
            if (a >= 0 && numDeleteBefore > 0) {
                content.delete(start, a);
            deleted = a - start;
                deleted = numDeleteBefore;
            }
        }

        if (afterLength > 0) {
@@ -257,8 +271,11 @@ public class BaseInputConnection implements InputConnection {
            int end = b + afterLength;
            if (end > content.length()) end = content.length();

            final int numDeleteAfter = end - b;
            if (b >= 0 && numDeleteAfter > 0) {
                content.delete(b, end);
            }
        }

        endBatchEdit();