diff options
Diffstat (limited to 'android/support/v7/widget/AppCompatTextHelper.java')
-rw-r--r-- | android/support/v7/widget/AppCompatTextHelper.java | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/android/support/v7/widget/AppCompatTextHelper.java b/android/support/v7/widget/AppCompatTextHelper.java index 51510aa2..fa6196f5 100644 --- a/android/support/v7/widget/AppCompatTextHelper.java +++ b/android/support/v7/widget/AppCompatTextHelper.java @@ -29,6 +29,7 @@ import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.annotation.RestrictTo; +import android.support.v4.content.res.ResourcesCompat; import android.support.v4.widget.TextViewCompat; import android.support.v7.appcompat.R; import android.text.method.PasswordTransformationMethod; @@ -36,6 +37,8 @@ import android.util.AttributeSet; import android.util.TypedValue; import android.widget.TextView; +import java.lang.ref.WeakReference; + @RequiresApi(9) class AppCompatTextHelper { @@ -63,6 +66,7 @@ class AppCompatTextHelper { private int mStyle = Typeface.NORMAL; private Typeface mFontTypeface; + private boolean mAsyncFontPending; AppCompatTextHelper(TextView view) { mView = view; @@ -213,8 +217,23 @@ class AppCompatTextHelper { ? R.styleable.TextAppearance_android_fontFamily : R.styleable.TextAppearance_fontFamily; if (!context.isRestricted()) { + final WeakReference<TextView> textViewWeak = new WeakReference<>(mView); + ResourcesCompat.FontCallback replyCallback = new ResourcesCompat.FontCallback() { + @Override + public void onFontRetrieved(@NonNull Typeface typeface) { + onAsyncTypefaceReceived(textViewWeak, typeface); + } + + @Override + public void onFontRetrievalFailed(int reason) { + // Do nothing. + } + }; try { - mFontTypeface = a.getFont(fontFamilyId, mStyle); + // Note the callback will be triggered on the UI thread. + mFontTypeface = a.getFont(fontFamilyId, mStyle, replyCallback); + // If this call gave us an immediate result, ignore any pending callbacks. + mAsyncFontPending = mFontTypeface == null; } catch (UnsupportedOperationException | Resources.NotFoundException e) { // Expected if it is not a font resource. } @@ -222,12 +241,16 @@ class AppCompatTextHelper { if (mFontTypeface == null) { // Try with String. This is done by TextView JB+, but fails in ICS String fontFamilyName = a.getString(fontFamilyId); - mFontTypeface = Typeface.create(fontFamilyName, mStyle); + if (fontFamilyName != null) { + mFontTypeface = Typeface.create(fontFamilyName, mStyle); + } } return; } if (a.hasValue(R.styleable.TextAppearance_android_typeface)) { + // Ignore previous pending fonts + mAsyncFontPending = false; int typefaceIndex = a.getInt(R.styleable.TextAppearance_android_typeface, SANS); switch (typefaceIndex) { case SANS: @@ -245,6 +268,16 @@ class AppCompatTextHelper { } } + private void onAsyncTypefaceReceived(WeakReference<TextView> textViewWeak, Typeface typeface) { + if (mAsyncFontPending) { + mFontTypeface = typeface; + final TextView textView = textViewWeak.get(); + if (textView != null) { + textView.setTypeface(typeface, mStyle); + } + } + } + void onSetTextAppearance(Context context, int resId) { final TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, resId, R.styleable.TextAppearance); |