-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathLibraryLoader.java
61 lines (53 loc) · 1.91 KB
/
LibraryLoader.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
package pl.droidsonroids.gif;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import java.lang.reflect.Method;
/**
* Helper used to work around native libraries loading on some systems.
* See <a href="https://medium.com/keepsafe-engineering/the-perils-of-loading-native-libraries-on-android-befa49dce2db">ReLinker</a> for more details.
*/
class LibraryLoader {
static final String SURFACE_LIBRARY_NAME = "pl_droidsonroids_gif_surface";
static final String BASE_LIBRARY_NAME = "pl_droidsonroids_gif";
private static Context sAppContext;
private LibraryLoader() {
}
/**
* Initializes loader with given `Context`. Subsequent calls should have no effect since application Context is retrieved.
* Libraries will not be loaded immediately but only when needed.
*
* @param context any Context except null
*/
public static void initialize(@NonNull final Context context) {
sAppContext = context.getApplicationContext();
}
private static Context getContext() {
if (sAppContext == null) {
try {
final Class<?> activityThread = Class.forName("android.app.ActivityThread");
final Method currentApplicationMethod = activityThread.getDeclaredMethod("currentApplication");
sAppContext = (Context) currentApplicationMethod.invoke(null);
} catch (Exception e) {
throw new IllegalStateException("LibraryLoader not initialized. Call LibraryLoader.initialize() before using library classes.", e);
}
}
return sAppContext;
}
static void loadLibrary(Context context, final String library) {
try {
System.loadLibrary(library);
} catch (final UnsatisfiedLinkError e) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
throw e;
}
if (SURFACE_LIBRARY_NAME.equals(library)) {
loadLibrary(context, BASE_LIBRARY_NAME);
}
if (context == null) {
context = getContext();
}
ReLinker.loadLibrary(context, library);
}
}
}