package androidx.core.graphics; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.graphics.Typeface; import android.os.Build; import android.os.CancellationSignal; import android.os.Handler; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import androidx.annotation.VisibleForTesting; import androidx.collection.LruCache; import androidx.core.content.res.FontResourcesParserCompat; import androidx.core.content.res.ResourcesCompat; import androidx.core.provider.FontsContractCompat; @SuppressLint({"NewApi"}) public class TypefaceCompat { private static final LruCache sTypefaceCache = new LruCache<>(16); private static final TypefaceCompatBaseImpl sTypefaceCompatImpl; @RestrictTo({RestrictTo.Scope.LIBRARY}) public static class ResourcesCallbackAdapter extends FontsContractCompat.FontRequestCallback { @Nullable private ResourcesCompat.FontCallback mFontCallback; public ResourcesCallbackAdapter(@Nullable ResourcesCompat.FontCallback fontCallback) { this.mFontCallback = fontCallback; } @Override // androidx.core.provider.FontsContractCompat.FontRequestCallback public void onTypefaceRequestFailed(int i) { ResourcesCompat.FontCallback fontCallback = this.mFontCallback; if (fontCallback != null) { fontCallback.onFontRetrievalFailed(i); } } @Override // androidx.core.provider.FontsContractCompat.FontRequestCallback public void onTypefaceRetrieved(@NonNull Typeface typeface) { ResourcesCompat.FontCallback fontCallback = this.mFontCallback; if (fontCallback != null) { fontCallback.onFontRetrieved(typeface); } } } static { int i = Build.VERSION.SDK_INT; if (i >= 29) { sTypefaceCompatImpl = new TypefaceCompatApi29Impl(); } else if (i >= 28) { sTypefaceCompatImpl = new TypefaceCompatApi28Impl(); } else if (i >= 26) { sTypefaceCompatImpl = new TypefaceCompatApi26Impl(); } else if (i < 24 || !TypefaceCompatApi24Impl.isUsable()) { sTypefaceCompatImpl = new TypefaceCompatApi21Impl(); } else { sTypefaceCompatImpl = new TypefaceCompatApi24Impl(); } } private TypefaceCompat() { } @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) @VisibleForTesting public static void clearCache() { sTypefaceCache.evictAll(); } @NonNull public static Typeface create(@NonNull Context context, @Nullable Typeface typeface, int i) { if (context != null) { return Typeface.create(typeface, i); } throw new IllegalArgumentException("Context cannot be null"); } @Nullable @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public static Typeface createFromFontInfo(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull FontsContractCompat.FontInfo[] fontInfoArr, int i) { return sTypefaceCompatImpl.createFromFontInfo(context, cancellationSignal, fontInfoArr, i); } @Nullable @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public static Typeface createFromResourcesFamilyXml(@NonNull Context context, @NonNull FontResourcesParserCompat.FamilyResourceEntry familyResourceEntry, @NonNull Resources resources, int i, int i2, @Nullable ResourcesCompat.FontCallback fontCallback, @Nullable Handler handler, boolean z2) { Typeface typeface; if (familyResourceEntry instanceof FontResourcesParserCompat.ProviderResourceEntry) { FontResourcesParserCompat.ProviderResourceEntry providerResourceEntry = (FontResourcesParserCompat.ProviderResourceEntry) familyResourceEntry; Typeface systemFontFamily = getSystemFontFamily(providerResourceEntry.getSystemFontFamilyName()); if (systemFontFamily != null) { if (fontCallback != null) { fontCallback.callbackSuccessAsync(systemFontFamily, handler); } return systemFontFamily; } typeface = FontsContractCompat.requestFont(context, providerResourceEntry.getRequest(), i2, !z2 ? fontCallback == null : providerResourceEntry.getFetchStrategy() == 0, z2 ? providerResourceEntry.getTimeout() : -1, ResourcesCompat.FontCallback.getHandler(handler), new ResourcesCallbackAdapter(fontCallback)); } else { typeface = sTypefaceCompatImpl.createFromFontFamilyFilesResourceEntry(context, (FontResourcesParserCompat.FontFamilyFilesResourceEntry) familyResourceEntry, resources, i2); if (fontCallback != null) { if (typeface != null) { fontCallback.callbackSuccessAsync(typeface, handler); } else { fontCallback.callbackFailAsync(-3, handler); } } } if (typeface != null) { sTypefaceCache.put(createResourceUid(resources, i, i2), typeface); } return typeface; } @Nullable @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public static Typeface createFromResourcesFontFile(@NonNull Context context, @NonNull Resources resources, int i, String str, int i2) { Typeface createFromResourcesFontFile = sTypefaceCompatImpl.createFromResourcesFontFile(context, resources, i, str, i2); if (createFromResourcesFontFile != null) { sTypefaceCache.put(createResourceUid(resources, i, i2), createFromResourcesFontFile); } return createFromResourcesFontFile; } private static String createResourceUid(Resources resources, int i, int i2) { return resources.getResourcePackageName(i) + "-" + i + "-" + i2; } @Nullable @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public static Typeface findFromCache(@NonNull Resources resources, int i, int i2) { return sTypefaceCache.get(createResourceUid(resources, i, i2)); } @Nullable private static Typeface getBestFontFromFamily(Context context, Typeface typeface, int i) { TypefaceCompatBaseImpl typefaceCompatBaseImpl = sTypefaceCompatImpl; FontResourcesParserCompat.FontFamilyFilesResourceEntry fontFamily = typefaceCompatBaseImpl.getFontFamily(typeface); if (fontFamily == null) { return null; } return typefaceCompatBaseImpl.createFromFontFamilyFilesResourceEntry(context, fontFamily, context.getResources(), i); } private static Typeface getSystemFontFamily(@Nullable String str) { if (str == null || str.isEmpty()) { return null; } Typeface create = Typeface.create(str, 0); Typeface create2 = Typeface.create(Typeface.DEFAULT, 0); if (create == null || create.equals(create2)) { return null; } return create; } }