-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathUtils.java
More file actions
134 lines (120 loc) · 4.49 KB
/
Utils.java
File metadata and controls
134 lines (120 loc) · 4.49 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.example;
import android.content.Context;
import android.content.res.AssetManager;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.cxxbridge.CatalystInstanceImpl;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
/**
* Utils for sample, just some hacks since I can't change source code in this project.
*
* Created by desmond on 4/17/17.
*/
class Utils {
private static Set<String> sLoadedScript = new HashSet<>();
static void recreateReactContextInBackgroundInner(ReactInstanceManager manager) {
try {
Method method = ReactInstanceManager.class.getDeclaredMethod("recreateReactContextInBackgroundInner");
method.setAccessible(true);
method.invoke(manager);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
static void moveResume(ReactInstanceManager manager, boolean force) {
try {
Method method = ReactInstanceManager.class.getDeclaredMethod("moveToResumedLifecycleState", boolean.class);
method.setAccessible(true);
method.invoke(manager, force);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
static void setJsModuleName(ReactRootView rootView, String moduleName) {
try {
Field field = ReactRootView.class.getDeclaredField("mJSModuleName");
field.setAccessible(true);
field.set(rootView, moduleName);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void setViewAttached(ReactRootView rootView, boolean bAttached) {
try {
Field field = ReactRootView.class.getDeclaredField("mIsAttachedToInstance");
field.setAccessible(true);
field.set(rootView, bAttached);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Nullable
static CatalystInstance getCatalystInstance(ReactNativeHost host) {
ReactInstanceManager manager = host.getReactInstanceManager();
if (manager == null) {
return null;
}
ReactContext context = manager.getCurrentReactContext();
if (context == null) {
return null;
}
return context.getCatalystInstance();
}
@Nullable
public static String getSourceUrl(CatalystInstance instance) {
try {
Field field = CatalystInstanceImpl.class.getDeclaredField("mSourceURL");
field.setAccessible(true);
return (String) field.get(instance);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
@WorkerThread
static void loadScriptFromAsset(Context context,
CatalystInstance instance,
String assetName) {
if (sLoadedScript.contains(assetName)) {
return;
}
try {
String source = "assets://" + assetName;
Method method = CatalystInstanceImpl.class.getDeclaredMethod("loadScriptFromAssets",
AssetManager.class,
String.class);
method.setAccessible(true);
method.invoke(instance, context.getAssets(), source);
sLoadedScript.add(assetName);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}