-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypefaceCompatApi24Impl.java
More file actions
114 lines (104 loc) · 5.08 KB
/
TypefaceCompatApi24Impl.java
File metadata and controls
114 lines (104 loc) · 5.08 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package android.support.v4.graphics;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.CancellationSignal;
import android.support.v4.content.res.FontResourcesParserCompat;
import android.support.v4.provider.FontsContractCompat;
import android.support.v4.util.SimpleArrayMap;
import android.util.Log;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.List;
class TypefaceCompatApi24Impl extends TypefaceCompatBaseImpl {
private static final String ADD_FONT_WEIGHT_STYLE_METHOD = "addFontWeightStyle";
private static final String CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD = "createFromFamiliesWithDefault";
private static final String FONT_FAMILY_CLASS = "android.graphics.FontFamily";
private static final String TAG = "TypefaceCompatApi24Impl";
private static final Method sAddFontWeightStyle;
private static final Method sCreateFromFamiliesWithDefault;
private static final Class sFontFamily;
private static final Constructor sFontFamilyCtor;
TypefaceCompatApi24Impl() {
}
static {
Method method;
Method method2;
Class<?> cls;
Constructor<?> constructor = null;
try {
cls = Class.forName(FONT_FAMILY_CLASS);
Constructor<?> constructor2 = cls.getConstructor(new Class[0]);
method = cls.getMethod(ADD_FONT_WEIGHT_STYLE_METHOD, new Class[]{ByteBuffer.class, Integer.TYPE, List.class, Integer.TYPE, Boolean.TYPE});
method2 = Typeface.class.getMethod(CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD, new Class[]{Array.newInstance(cls, 1).getClass()});
constructor = constructor2;
} catch (ClassNotFoundException | NoSuchMethodException e) {
Log.e(TAG, e.getClass().getName(), e);
cls = null;
method2 = null;
method = null;
}
sFontFamilyCtor = constructor;
sFontFamily = cls;
sAddFontWeightStyle = method;
sCreateFromFamiliesWithDefault = method2;
}
public static boolean isUsable() {
if (sAddFontWeightStyle == null) {
Log.w(TAG, "Unable to collect necessary private methods.Fallback to legacy implementation.");
}
return sAddFontWeightStyle != null;
}
private static Object newFamily() {
try {
return sFontFamilyCtor.newInstance(new Object[0]);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static boolean addFontWeightStyle(Object obj, ByteBuffer byteBuffer, int i, int i2, boolean z) {
try {
return ((Boolean) sAddFontWeightStyle.invoke(obj, new Object[]{byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Boolean.valueOf(z)})).booleanValue();
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static Typeface createFromFamiliesWithDefault(Object obj) {
try {
Object newInstance = Array.newInstance(sFontFamily, 1);
Array.set(newInstance, 0, obj);
return (Typeface) sCreateFromFamiliesWithDefault.invoke((Object) null, new Object[]{newInstance});
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fontInfoArr, int i) {
Object newFamily = newFamily();
SimpleArrayMap simpleArrayMap = new SimpleArrayMap();
for (FontsContractCompat.FontInfo fontInfo : fontInfoArr) {
Uri uri = fontInfo.getUri();
ByteBuffer byteBuffer = (ByteBuffer) simpleArrayMap.get(uri);
if (byteBuffer == null) {
byteBuffer = TypefaceCompatUtil.mmap(context, cancellationSignal, uri);
simpleArrayMap.put(uri, byteBuffer);
}
if (!addFontWeightStyle(newFamily, byteBuffer, fontInfo.getTtcIndex(), fontInfo.getWeight(), fontInfo.isItalic())) {
return null;
}
}
return createFromFamiliesWithDefault(newFamily);
}
public Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry fontFamilyFilesResourceEntry, Resources resources, int i) {
Object newFamily = newFamily();
for (FontResourcesParserCompat.FontFileResourceEntry fontFileResourceEntry : fontFamilyFilesResourceEntry.getEntries()) {
if (!addFontWeightStyle(newFamily, TypefaceCompatUtil.copyToDirectBuffer(context, resources, fontFileResourceEntry.getResourceId()), 0, fontFileResourceEntry.getWeight(), fontFileResourceEntry.isItalic())) {
return null;
}
}
return createFromFamiliesWithDefault(newFamily);
}
}