Skip to content

Commit a7c3e0b

Browse files
hunterstichdrchen
authored andcommitted
[Typography] Automated g4 rollback of changelist 651033811.
*** Reason for rollback *** fontVariationSettings fix landed in AndroidX *** Original change description *** [Typography] Added workaround for fontVariationSettings being set through a TextAppearance A bug in AppCompatTextView causes fontVariationSettings set through a TextAppearance in xml to not take effect. This is a temporary woraround that cycles the settings to force them to be re-set until the fix is made in appcompat. Resolves #4235 PiperOrigin-RevId: 659534202
1 parent 629e24f commit a7c3e0b

File tree

3 files changed

+16
-66
lines changed

3 files changed

+16
-66
lines changed

lib/java/com/google/android/material/resources/MaterialResources.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private static int getComplexUnit(TypedValue tv) {
273273
* contain values, the first given index takes precedence and is returned.
274274
*/
275275
@StyleableRes
276-
public static int getIndexWithValue(
276+
static int getIndexWithValue(
277277
@NonNull TypedArray attributes, @StyleableRes int a, @StyleableRes int b) {
278278
if (attributes.hasValue(a)) {
279279
return a;

lib/java/com/google/android/material/resources/TextAppearance.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ public void updateTextPaintMeasureState(
364364
textPaint.setTextSize(textSize);
365365

366366
if (VERSION.SDK_INT >= VERSION_CODES.O) {
367-
// TODO(b/264321145): Remove once variation settings in text appearances are fixed.
368-
textPaint.setFontVariationSettings("");
369367
textPaint.setFontVariationSettings(fontVariationSettings);
370368
}
371369

lib/java/com/google/android/material/textview/MaterialTextView.java

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@
2424
import android.content.res.Resources;
2525
import android.content.res.Resources.Theme;
2626
import android.content.res.TypedArray;
27-
import android.os.Build.VERSION;
28-
import android.os.Build.VERSION_CODES;
2927
import androidx.appcompat.widget.AppCompatTextView;
3028
import android.util.AttributeSet;
3129
import androidx.annotation.NonNull;
3230
import androidx.annotation.Nullable;
33-
import androidx.annotation.RequiresApi;
3431
import androidx.annotation.StyleableRes;
3532
import com.google.android.material.resources.MaterialAttributes;
3633
import com.google.android.material.resources.MaterialResources;
@@ -112,87 +109,42 @@ public MaterialTextView(
112109
public void setTextAppearance(@NonNull Context context, int resId) {
113110
super.setTextAppearance(context, resId);
114111

115-
boolean canApplyLineHeight = canApplyTextAppearanceLineHeight(context);
116-
boolean canForceRefreshFontVariationSettings = VERSION.SDK_INT >= VERSION_CODES.O;
117-
if (!canApplyLineHeight && !canForceRefreshFontVariationSettings) {
118-
return;
112+
if (canApplyTextAppearanceLineHeight(context)) {
113+
applyLineHeightFromViewAppearance(context.getTheme(), resId);
119114
}
120-
121-
TypedArray appearance =
122-
context.getTheme().obtainStyledAttributes(resId, R.styleable.MaterialTextAppearance);
123-
if (canApplyLineHeight) {
124-
applyLineHeightFromViewAppearance(appearance);
125-
}
126-
if (canForceRefreshFontVariationSettings) {
127-
maybeForceApplyFontVariationSettingsFromViewAppearance(appearance);
128-
}
129-
appearance.recycle();
130115
}
131116

132117
private void initialize(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
133118
// Ensure we are using the correctly themed context rather than the context that was passed in.
134119
Context context = getContext();
135-
final Resources.Theme theme = context.getTheme();
136-
boolean canApplyLineHeight = canApplyTextAppearanceLineHeight(context)
137-
&& !viewAttrsHasLineHeight(context, theme, attrs, defStyleAttr, defStyleRes);
138-
boolean canForceRefreshFontVariationSettings = VERSION.SDK_INT >= VERSION_CODES.O;
139-
if (!canApplyLineHeight && !canForceRefreshFontVariationSettings) {
140-
return;
141-
}
142120

143-
int resId = findViewAppearanceResourceId(theme, attrs, defStyleAttr, defStyleRes);
144-
if (resId == -1) {
145-
return;
146-
}
121+
if (canApplyTextAppearanceLineHeight(context)) {
122+
final Resources.Theme theme = context.getTheme();
147123

148-
TypedArray appearance =
149-
context.getTheme().obtainStyledAttributes(resId, R.styleable.MaterialTextAppearance);
150-
if (canApplyLineHeight) {
151-
applyLineHeightFromViewAppearance(appearance);
152-
}
153-
if (canForceRefreshFontVariationSettings) {
154-
maybeForceApplyFontVariationSettingsFromViewAppearance(appearance);
124+
if (!viewAttrsHasLineHeight(context, theme, attrs, defStyleAttr, defStyleRes)) {
125+
int resId = findViewAppearanceResourceId(theme, attrs, defStyleAttr, defStyleRes);
126+
if (resId != -1) {
127+
applyLineHeightFromViewAppearance(theme, resId);
128+
}
129+
}
155130
}
156-
appearance.recycle();
157131
}
158132

159-
private void applyLineHeightFromViewAppearance(TypedArray appearance) {
133+
private void applyLineHeightFromViewAppearance(@NonNull Theme theme, int resId) {
134+
TypedArray attributes = theme.obtainStyledAttributes(resId, R.styleable.MaterialTextAppearance);
160135
int lineHeight =
161136
readFirstAvailableDimension(
162137
getContext(),
163-
appearance,
138+
attributes,
164139
R.styleable.MaterialTextAppearance_android_lineHeight,
165140
R.styleable.MaterialTextAppearance_lineHeight);
141+
attributes.recycle();
142+
166143
if (lineHeight >= 0) {
167144
setLineHeight(lineHeight);
168145
}
169146
}
170147

171-
/**
172-
* Maybe read and set font variation settings from a TextAppearance.
173-
*
174-
* <p>This is a workaround for a bug in appcompat where fontVariationSettings set in a
175-
* TextAppearance do not take effect.
176-
*
177-
* <p>TODO(b/264321145): Remove once AppCompatTextView fixes text appearance font variation
178-
* support
179-
*/
180-
@RequiresApi(VERSION_CODES.O)
181-
private void maybeForceApplyFontVariationSettingsFromViewAppearance(TypedArray appearance) {
182-
int fontVariationSettingsIndex =
183-
MaterialResources.getIndexWithValue(
184-
appearance,
185-
R.styleable.MaterialTextAppearance_fontVariationSettings,
186-
R.styleable.MaterialTextAppearance_android_fontVariationSettings);
187-
String fontVariationSettings = appearance.getString(fontVariationSettingsIndex);
188-
if (fontVariationSettings != null) {
189-
// Clear the font variation settings to force TextView to reset the text Paint's
190-
// settings.
191-
setFontVariationSettings("");
192-
setFontVariationSettings(fontVariationSettings);
193-
}
194-
}
195-
196148
private static boolean canApplyTextAppearanceLineHeight(Context context) {
197149
return MaterialAttributes.resolveBoolean(context, R.attr.textAppearanceLineHeightEnabled, true);
198150
}

0 commit comments

Comments
 (0)