Merge "Strips spans from AssistStructure text" into udc-qpr-dev am: 710ffbed47

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

Change-Id: Ie4329e31adbe1943747891462a47dffe7a64addb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/core/java/android/app/assist/AssistStructure.java b/core/java/android/app/assist/AssistStructure.java
index b7ec7b5..d66fca8 100644
--- a/core/java/android/app/assist/AssistStructure.java
+++ b/core/java/android/app/assist/AssistStructure.java
@@ -22,6 +22,7 @@
 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;
@@ -1557,6 +1558,10 @@
         /**
          * 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() {
@@ -1996,14 +2001,16 @@
         @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;
         }
@@ -2222,6 +2229,13 @@
         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 {