111 lines
5.0 KiB
Java
111 lines
5.0 KiB
Java
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.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<String, Typeface> sTypefaceCache = new LruCache<>(16);
|
|
private static final TypefaceCompatBaseImpl sTypefaceCompatImpl;
|
|
|
|
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() {
|
|
}
|
|
|
|
@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;
|
|
boolean z3 = false;
|
|
if (!z2 ? fontCallback == null : providerResourceEntry.getFetchStrategy() == 0) {
|
|
z3 = true;
|
|
}
|
|
typeface = FontsContractCompat.getFontSync(context, providerResourceEntry.getRequest(), fontCallback, handler, z3, z2 ? providerResourceEntry.getTimeout() : -1, i2);
|
|
} 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);
|
|
}
|
|
}
|