|
24 | 24 | import android.content.res.Resources; |
25 | 25 | import android.content.res.Resources.Theme; |
26 | 26 | import android.content.res.TypedArray; |
27 | | -import android.os.Build.VERSION; |
28 | | -import android.os.Build.VERSION_CODES; |
29 | 27 | import androidx.appcompat.widget.AppCompatTextView; |
30 | 28 | import android.util.AttributeSet; |
31 | 29 | import androidx.annotation.NonNull; |
32 | 30 | import androidx.annotation.Nullable; |
33 | | -import androidx.annotation.RequiresApi; |
34 | 31 | import androidx.annotation.StyleableRes; |
35 | 32 | import com.google.android.material.resources.MaterialAttributes; |
36 | 33 | import com.google.android.material.resources.MaterialResources; |
@@ -112,87 +109,42 @@ public MaterialTextView( |
112 | 109 | public void setTextAppearance(@NonNull Context context, int resId) { |
113 | 110 | super.setTextAppearance(context, resId); |
114 | 111 |
|
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); |
119 | 114 | } |
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(); |
130 | 115 | } |
131 | 116 |
|
132 | 117 | private void initialize(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
133 | 118 | // Ensure we are using the correctly themed context rather than the context that was passed in. |
134 | 119 | 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 | | - } |
142 | 120 |
|
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(); |
147 | 123 |
|
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 | + } |
155 | 130 | } |
156 | | - appearance.recycle(); |
157 | 131 | } |
158 | 132 |
|
159 | | - private void applyLineHeightFromViewAppearance(TypedArray appearance) { |
| 133 | + private void applyLineHeightFromViewAppearance(@NonNull Theme theme, int resId) { |
| 134 | + TypedArray attributes = theme.obtainStyledAttributes(resId, R.styleable.MaterialTextAppearance); |
160 | 135 | int lineHeight = |
161 | 136 | readFirstAvailableDimension( |
162 | 137 | getContext(), |
163 | | - appearance, |
| 138 | + attributes, |
164 | 139 | R.styleable.MaterialTextAppearance_android_lineHeight, |
165 | 140 | R.styleable.MaterialTextAppearance_lineHeight); |
| 141 | + attributes.recycle(); |
| 142 | + |
166 | 143 | if (lineHeight >= 0) { |
167 | 144 | setLineHeight(lineHeight); |
168 | 145 | } |
169 | 146 | } |
170 | 147 |
|
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 | | - |
196 | 148 | private static boolean canApplyTextAppearanceLineHeight(Context context) { |
197 | 149 | return MaterialAttributes.resolveBoolean(context, R.attr.textAppearanceLineHeightEnabled, true); |
198 | 150 | } |
|
0 commit comments