-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypefaceCompat.java
74 lines (62 loc) · 3.64 KB
/
TypefaceCompat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package android.support.v4.graphics;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.v4.content.res.FontResourcesParserCompat;
import android.support.v4.provider.FontsContractCompat;
import android.support.v4.util.LruCache;
import android.widget.TextView;
public class TypefaceCompat {
private static final String TAG = "TypefaceCompat";
private static final LruCache<String, Typeface> sTypefaceCache = new LruCache<>(16);
private static final TypefaceCompatImpl sTypefaceCompatImpl;
interface TypefaceCompatImpl {
Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry fontFamilyFilesResourceEntry, Resources resources, int i);
Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fontInfoArr, int i);
Typeface createFromResourcesFontFile(Context context, Resources resources, int i, String str, int i2);
}
static {
if (Build.VERSION.SDK_INT >= 26) {
sTypefaceCompatImpl = new TypefaceCompatApi26Impl();
} else if (Build.VERSION.SDK_INT >= 24 && TypefaceCompatApi24Impl.isUsable()) {
sTypefaceCompatImpl = new TypefaceCompatApi24Impl();
} else if (Build.VERSION.SDK_INT >= 21) {
sTypefaceCompatImpl = new TypefaceCompatApi21Impl();
} else {
sTypefaceCompatImpl = new TypefaceCompatBaseImpl();
}
}
private TypefaceCompat() {
}
public static Typeface findFromCache(Resources resources, int i, int i2) {
return sTypefaceCache.get(createResourceUid(resources, i, i2));
}
private static String createResourceUid(Resources resources, int i, int i2) {
return resources.getResourcePackageName(i) + "-" + i + "-" + i2;
}
public static Typeface createFromResourcesFamilyXml(Context context, FontResourcesParserCompat.FamilyResourceEntry familyResourceEntry, Resources resources, int i, int i2, TextView textView) {
Typeface typeface;
if (familyResourceEntry instanceof FontResourcesParserCompat.ProviderResourceEntry) {
FontResourcesParserCompat.ProviderResourceEntry providerResourceEntry = (FontResourcesParserCompat.ProviderResourceEntry) familyResourceEntry;
typeface = FontsContractCompat.getFontSync(context, providerResourceEntry.getRequest(), textView, providerResourceEntry.getFetchStrategy(), providerResourceEntry.getTimeout(), i2);
} else {
typeface = sTypefaceCompatImpl.createFromFontFamilyFilesResourceEntry(context, (FontResourcesParserCompat.FontFamilyFilesResourceEntry) familyResourceEntry, resources, i2);
}
if (typeface != null) {
sTypefaceCache.put(createResourceUid(resources, i, i2), typeface);
}
return typeface;
}
public static Typeface createFromResourcesFontFile(Context context, 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;
}
public static Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fontInfoArr, int i) {
return sTypefaceCompatImpl.createFromFontInfo(context, cancellationSignal, fontInfoArr, i);
}
}