diff options
author | Daniel <danieljkim@google.com> | 2023-08-03 06:09:51 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-09-20 20:01:36 +0000 |
commit | 788b51ddb14b8c79783c86c6a7862eaf6ad16a52 (patch) | |
tree | c810395aff97d909eca0491f4262224ad2a036da | |
parent | 1b4af73f6bcd187ddf4f85b2fb235a61530a7af5 (diff) | |
download | base-788b51ddb14b8c79783c86c6a7862eaf6ad16a52.tar.gz |
Strips spans from AssistStructure text
When a new view becomes visible on the screen, the view notifies
AutofillManager about its visibility. AutofillManager then requests the
activity to compile an AssistStructure object which will contain the
view hierarchy of the activity as well as texts contained inside all the
views. If there are Span text fields that contain custom implementation of ClickableSpan, these spans are also copied over during the construction of the AssistStructure. By keeping the references to the ClickableSpan, it causes memory leak when the AssistStructure object outlives the activity.
Test: atest CtsAutoFillServiceTestCases, atest CtsAssistTestCases, atest android.widget.cts.TextViewTest
Bug: 146100180
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:e42d05e3d01f4c3b8aa7bce29086cd968970b4ec)
Merged-In: I1fd97d9d6fdc569d14529347fcb85da409c3c1ff
Change-Id: I1fd97d9d6fdc569d14529347fcb85da409c3c1ff
-rw-r--r-- | core/java/android/app/assist/AssistStructure.java | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/core/java/android/app/assist/AssistStructure.java b/core/java/android/app/assist/AssistStructure.java index fc4a2b49d0c2..484965dc583a 100644 --- a/core/java/android/app/assist/AssistStructure.java +++ b/core/java/android/app/assist/AssistStructure.java @@ -22,6 +22,7 @@ import android.os.PooledStringWriter; import android.os.RemoteException; import android.os.SystemClock; import android.service.autofill.FillRequest; +import android.text.Spanned; import android.text.TextUtils; import android.util.Log; import android.util.Pair; @@ -1551,6 +1552,10 @@ public class AssistStructure implements Parcelable { /** * Returns any text associated with the node that is displayed to the user, or null * if there is none. + * + * <p> The text will be stripped of any spans that could potentially contain reference to + * the activity context, to avoid memory leak. If the text contained a span, a plain + * string version of the text will be returned. */ @Nullable public CharSequence getText() { @@ -1990,14 +1995,16 @@ public class AssistStructure implements Parcelable { @Override public void setText(CharSequence text) { ViewNodeText t = getNodeText(); - t.mText = TextUtils.trimNoCopySpans(text); + // Strip spans from the text to avoid memory leak + t.mText = TextUtils.trimToParcelableSize(stripAllSpansFromText(text)); t.mTextSelectionStart = t.mTextSelectionEnd = -1; } @Override public void setText(CharSequence text, int selectionStart, int selectionEnd) { ViewNodeText t = getNodeText(); - t.mText = TextUtils.trimNoCopySpans(text); + // Strip spans from the text to avoid memory leak + t.mText = stripAllSpansFromText(text); t.mTextSelectionStart = selectionStart; t.mTextSelectionEnd = selectionEnd; } @@ -2216,6 +2223,13 @@ public class AssistStructure implements Parcelable { public void setHtmlInfo(@NonNull HtmlInfo htmlInfo) { mNode.mHtmlInfo = htmlInfo; } + + private CharSequence stripAllSpansFromText(CharSequence text) { + if (text instanceof Spanned) { + return text.toString(); + } + return text; + } } private static final class HtmlInfoNode extends HtmlInfo implements Parcelable { |