From 1a5de0a925b90fa58cbe909dc204e30d6c806362 Mon Sep 17 00:00:00 2001 From: Jeff DeCew Date: Thu, 16 Dec 2021 12:22:38 -0500 Subject: [PATCH 1/2] Bold StyleSpan further for auto-bold StyleSpan with Typeface.BOLD will only bold if the text doesn't already have a BOLD style. But auto-bolded text needs to be further bolded. If the Typeface style is BOLD, bold more for auto-bold. (The fake bold works for Typefaces with weight < 700) NOTE: This backports a public API change from master but hides the public methods, so that we can use the new feature in SystemUI, which has access to hidden APIs. This does NOT backport the changes to HTML or StringBlock as those changes may have broad affects that we do not have time to validate before release. Test: manual - settings page, example StyleSpans, atest StyleSpanTest TextViewTests Bug: 175177807 Bug: 208592419 Merged-In: Iec7db7eadd18ed6a2efc6ee39b49c1d6cb15d870 Change-Id: I8da1d3f903b265e0331e7166a0afa0720c0eaa01 --- core/java/android/text/style/StyleSpan.java | 48 +++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/core/java/android/text/style/StyleSpan.java b/core/java/android/text/style/StyleSpan.java index bdfa700215f8a..9cdd54c16a42b 100644 --- a/core/java/android/text/style/StyleSpan.java +++ b/core/java/android/text/style/StyleSpan.java @@ -17,8 +17,10 @@ package android.text.style; import android.annotation.NonNull; +import android.content.res.Configuration; import android.graphics.Paint; import android.graphics.Typeface; +import android.graphics.fonts.FontStyle; import android.os.Parcel; import android.text.ParcelableSpan; import android.text.TextPaint; @@ -45,6 +47,7 @@ import android.text.TextUtils; public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { private final int mStyle; + private final int mFontWeightAdjustment; /** * Creates a {@link StyleSpan} from a style. @@ -54,7 +57,24 @@ public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { * in {@link Typeface}. */ public StyleSpan(int style) { + this(style, Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED); + } + + /** + * Creates a {@link StyleSpan} from a style and font weight adjustment. + * + * @param style An integer constant describing the style for this span. Examples + * include bold, italic, and normal. Values are constants defined + * in {@link Typeface}. + * @param fontWeightAdjustment An integer describing the adjustment to be made to the font + * weight. + * @see Configuration#fontWeightAdjustment This is the adjustment in text font weight + * that is used to reflect the current user's preference for increasing font weight. + * @hide + */ + public StyleSpan(@Typeface.Style int style, int fontWeightAdjustment) { mStyle = style; + mFontWeightAdjustment = fontWeightAdjustment; } /** @@ -64,6 +84,7 @@ public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { */ public StyleSpan(@NonNull Parcel src) { mStyle = src.readInt(); + mFontWeightAdjustment = src.readInt(); } @Override @@ -91,6 +112,7 @@ public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { @Override public void writeToParcelInternal(@NonNull Parcel dest, int flags) { dest.writeInt(mStyle); + dest.writeInt(mFontWeightAdjustment); } /** @@ -100,17 +122,25 @@ public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { return mStyle; } + /** + * Returns the font weight adjustment specified by this span. + * @hide + */ + public int getFontWeightAdjustment() { + return mFontWeightAdjustment; + } + @Override public void updateDrawState(TextPaint ds) { - apply(ds, mStyle); + apply(ds, mStyle, mFontWeightAdjustment); } @Override public void updateMeasureState(TextPaint paint) { - apply(paint, mStyle); + apply(paint, mStyle, mFontWeightAdjustment); } - private static void apply(Paint paint, int style) { + private static void apply(Paint paint, int style, int fontWeightAdjustment) { int oldStyle; Typeface old = paint.getTypeface(); @@ -129,6 +159,18 @@ public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { tf = Typeface.create(old, want); } + // Base typeface may already be bolded by auto bold. Bold further. + if ((style & Typeface.BOLD) != 0) { + if (fontWeightAdjustment != 0 + && fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED) { + int newWeight = Math.min( + Math.max(tf.getWeight() + fontWeightAdjustment, FontStyle.FONT_WEIGHT_MIN), + FontStyle.FONT_WEIGHT_MAX); + boolean italic = (want & Typeface.ITALIC) != 0; + tf = Typeface.create(tf, newWeight, italic); + } + } + int fake = want & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) { From 071ede77bf614e04efb4c6331b96d9524f4a4ee4 Mon Sep 17 00:00:00 2001 From: Jeff DeCew Date: Thu, 16 Dec 2021 12:24:35 -0500 Subject: [PATCH 2/2] Double-bold the snooze option text. Bug: 208592419 Test: manual Merged-In: Ic3e704c47ec5e3f8bc11050e6d7642ff5d3b6e80 Change-Id: Ic3e704c47ec5e3f8bc11050e6d7642ff5d3b6e80 --- .../systemui/statusbar/notification/row/NotificationSnooze.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationSnooze.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationSnooze.java index bfe352d57c24f..86c20a1258837 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationSnooze.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationSnooze.java @@ -254,7 +254,7 @@ public class NotificationSnooze extends LinearLayout return new NotificationSnoozeOption(null, minutes, description, resultText, action); } SpannableString string = new SpannableString(resultText); - string.setSpan(new StyleSpan(Typeface.BOLD), + string.setSpan(new StyleSpan(Typeface.BOLD, res.getConfiguration().fontWeightAdjustment), index, index + description.length(), 0 /* flags */); return new NotificationSnoozeOption(null, minutes, description, string, action);