summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ibrahim Yilmaz <iyz@google.com> 2024-04-18 14:20:14 +0000
committer Ibrahim Yilmaz <iyz@google.com> 2024-04-23 17:32:13 +0000
commit4ea853da646f5d7525aefbae42f17f95be26e3dd (patch)
treed61cf0ee7126cb9040e631928a709f72c3071afc
parentb12b4a1176a2a4f36ebf3621fe88a4a34e1df1b7 (diff)
[API Abuse - Text Consistency] Keep Messaging Message Style Spans
Bug: 313439845 Test: presubmit + compare before and after screenshot of each standard notifications by using spanned title, content, summary and action Flag: ACONFIG android.app.Flags.clean_up_spans_and_new_lines DEVELOPMENT Change-Id: I48d5d4c714fbf97f54cd3cb9383ffd45fc9b3d0d
-rw-r--r--core/java/android/app/Notification.java46
1 files changed, 39 insertions, 7 deletions
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 6ff1bfc5291c..e90e0a1af15c 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -94,7 +94,10 @@ import android.text.style.AbsoluteSizeSpan;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
+import android.text.style.StrikethroughSpan;
+import android.text.style.StyleSpan;
import android.text.style.TextAppearanceSpan;
+import android.text.style.UnderlineSpan;
import android.util.ArraySet;
import android.util.Log;
import android.util.Pair;
@@ -3136,9 +3139,6 @@ public class Notification implements Parcelable
+ " instance is a custom Parcelable and not allowed in Notification");
return cs.toString();
}
- if (Flags.cleanUpSpansAndNewLines()) {
- return stripStyling(cs);
- }
return removeTextSizeSpans(cs);
}
@@ -8126,9 +8126,6 @@ public class Notification implements Parcelable
*/
public BigTextStyle bigText(CharSequence cs) {
mBigText = safeCharSequence(cs);
- if (Flags.cleanUpSpansAndNewLines()) {
- mBigText = cleanUpNewLines(mBigText);
- }
return this;
}
@@ -8199,6 +8196,9 @@ public class Notification implements Parcelable
// Replace the text with the big text, but only if the big text is not empty.
CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
+ if (Flags.cleanUpSpansAndNewLines()) {
+ bigTextText = cleanUpNewLines(stripStyling(bigTextText));
+ }
if (!TextUtils.isEmpty(bigTextText)) {
p.text(bigTextText);
}
@@ -9114,11 +9114,43 @@ public class Notification implements Parcelable
*/
public void ensureColorContrastOrStripStyling(int backgroundColor) {
if (Flags.cleanUpSpansAndNewLines()) {
- mText = stripStyling(mText);
+ mText = stripNonStyleSpans(mText);
} else {
ensureColorContrast(backgroundColor);
}
}
+
+ private CharSequence stripNonStyleSpans(CharSequence text) {
+
+ if (text instanceof Spanned) {
+ Spanned ss = (Spanned) text;
+ Object[] spans = ss.getSpans(0, ss.length(), Object.class);
+ SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
+ for (Object span : spans) {
+ final Object resultSpan;
+ if (span instanceof StyleSpan
+ || span instanceof StrikethroughSpan
+ || span instanceof UnderlineSpan) {
+ resultSpan = span;
+ } else if (span instanceof TextAppearanceSpan) {
+ final TextAppearanceSpan originalSpan = (TextAppearanceSpan) span;
+ resultSpan = new TextAppearanceSpan(
+ null,
+ originalSpan.getTextStyle(),
+ -1,
+ null,
+ null);
+ } else {
+ continue;
+ }
+ builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
+ ss.getSpanFlags(span));
+ }
+ return builder;
+ }
+ return text;
+ }
+
/**
* Updates TextAppearance spans in the message text so it has sufficient contrast
* against its background.