Merge "Prevent exception when surrounding text retrieval" into rvc-dev am: 309cfa3431 am: f4fb8c1457

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/12167896

Change-Id: Ib618bddd560576fe1d7bbf1b6a78f70b4733eac9
This commit is contained in:
Wale Ogunwale
2020-07-21 01:41:57 +00:00
committed by Automerger Merge Worker
2 changed files with 25 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ import android.os.LocaleList;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.UserHandle; import android.os.UserHandle;
import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Printer; import android.util.Printer;
@@ -567,7 +568,8 @@ public class EditorInfo implements InputType, Parcelable {
* editor wants to trim out the first 10 chars, subTextStart should be 10. * editor wants to trim out the first 10 chars, subTextStart should be 10.
*/ */
public void setInitialSurroundingSubText(@NonNull CharSequence subText, int subTextStart) { public void setInitialSurroundingSubText(@NonNull CharSequence subText, int subTextStart) {
Objects.requireNonNull(subText); CharSequence newSubText = Editable.Factory.getInstance().newEditable(subText);
Objects.requireNonNull(newSubText);
// Swap selection start and end if necessary. // Swap selection start and end if necessary.
final int subTextSelStart = initialSelStart > initialSelEnd final int subTextSelStart = initialSelStart > initialSelEnd
@@ -575,7 +577,7 @@ public class EditorInfo implements InputType, Parcelable {
final int subTextSelEnd = initialSelStart > initialSelEnd final int subTextSelEnd = initialSelStart > initialSelEnd
? initialSelStart - subTextStart : initialSelEnd - subTextStart; ? initialSelStart - subTextStart : initialSelEnd - subTextStart;
final int subTextLength = subText.length(); final int subTextLength = newSubText.length();
// Unknown or invalid selection. // Unknown or invalid selection.
if (subTextStart < 0 || subTextSelStart < 0 || subTextSelEnd > subTextLength) { if (subTextStart < 0 || subTextSelStart < 0 || subTextSelEnd > subTextLength) {
mInitialSurroundingText = new InitialSurroundingText(); mInitialSurroundingText = new InitialSurroundingText();
@@ -589,12 +591,12 @@ public class EditorInfo implements InputType, Parcelable {
} }
if (subTextLength <= MEMORY_EFFICIENT_TEXT_LENGTH) { if (subTextLength <= MEMORY_EFFICIENT_TEXT_LENGTH) {
mInitialSurroundingText = new InitialSurroundingText(subText, subTextSelStart, mInitialSurroundingText = new InitialSurroundingText(newSubText, subTextSelStart,
subTextSelEnd); subTextSelEnd);
return; return;
} }
trimLongSurroundingText(subText, subTextSelStart, subTextSelEnd); trimLongSurroundingText(newSubText, subTextSelStart, subTextSelEnd);
} }
/** /**

View File

@@ -264,6 +264,25 @@ public class EditorInfoTest {
InputConnection.GET_TEXT_WITH_STYLES))); InputConnection.GET_TEXT_WITH_STYLES)));
} }
@Test
public void surroundingTextRetrieval_writeToParcel_noException() {
StringBuilder sb = new StringBuilder("abcdefg");
Parcel parcel = Parcel.obtain();
EditorInfo editorInfo = new EditorInfo();
editorInfo.initialSelStart = 2;
editorInfo.initialSelEnd = 5;
editorInfo.inputType = EditorInfo.TYPE_CLASS_TEXT;
editorInfo.setInitialSurroundingText(sb);
sb.setLength(0);
editorInfo.writeToParcel(parcel, 0);
try {
editorInfo.getInitialTextBeforeCursor(60, 1);
fail("Test shouldn't have exception");
} catch (AssertionError e) { }
}
private static void assertExpectedTextLength(EditorInfo editorInfo, private static void assertExpectedTextLength(EditorInfo editorInfo,
@Nullable Integer expectBeforeCursorLength, @Nullable Integer expectSelectionLength, @Nullable Integer expectBeforeCursorLength, @Nullable Integer expectSelectionLength,
@Nullable Integer expectAfterCursorLength) { @Nullable Integer expectAfterCursorLength) {