Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba819fce authored by Sally's avatar Sally Committed by Sally Yuen
Browse files

Change forceBoldText to fontWeightAdjustment in TextView

Use the weight adjustment instead of a binary on/off

Bug: b/170966021, b/110991537
Test: Manual with bold text toggle,
atest CtsWidgetTestCases:TextViewTest, CtsTextTestCases

Change-Id: Ic60ca7eed497bfc798d653a07824a9e5fabe5bc2
parent 3be3d323
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -742,8 +742,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    private boolean mLocalesChanged = false;
    private int mTextSizeUnit = -1;
    // True if force bold text feature is enabled. This feature makes all text bolder.
    private boolean mForceBoldTextEnabled;
    // This is used to reflect the current user preference for changing font weight and making text
    // more bold.
    private int mFontWeightAdjustment;
    private Typeface mOriginalTypeface;
    // True if setKeyListener() has been explicitly called
@@ -1647,8 +1648,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            attributes.mTypefaceIndex = MONOSPACE;
        }
        mForceBoldTextEnabled = getContext().getResources().getConfiguration().forceBoldText
                == Configuration.FORCE_BOLD_TEXT_YES;
        mFontWeightAdjustment = getContext().getResources().getConfiguration().fontWeightAdjustment;
        applyTextAppearance(attributes);
        if (isPassword) {
@@ -4273,12 +4273,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                invalidate();
            }
        }
        if (newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_YES) {
            mForceBoldTextEnabled = true;
            setTypeface(getTypeface());
        } else  if (newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_NO
                || newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_UNDEFINED) {
            mForceBoldTextEnabled = false;
        if (mFontWeightAdjustment != newConfig.fontWeightAdjustment) {
            mFontWeightAdjustment = newConfig.fontWeightAdjustment;
            setTypeface(getTypeface());
        }
    }
@@ -4433,13 +4429,19 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     */
    public void setTypeface(@Nullable Typeface tf) {
        mOriginalTypeface = tf;
        if (mForceBoldTextEnabled) {
            int newWeight = tf != null ? tf.getWeight() + 300 : 400;
            newWeight = Math.min(newWeight, 1000);
        if (mFontWeightAdjustment != 0
                && mFontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED) {
            if (tf == null) {
                tf = Typeface.DEFAULT;
            } else {
                int newWeight = Math.min(
                        Math.max(tf.getWeight() + mFontWeightAdjustment, FontStyle.FONT_WEIGHT_MIN),
                        FontStyle.FONT_WEIGHT_MAX);
                int typefaceStyle = tf != null ? tf.getStyle() : 0;
                boolean italic = (typefaceStyle & Typeface.ITALIC) != 0;
                tf = Typeface.create(tf, newWeight, italic);
            }
        }
        if (mTextPaint.getTypeface() != tf) {
            mTextPaint.setTypeface(tf);