|
@@ -1,15 +1,21 @@
|
1
|
1
|
package com.reactnativenavigation.core;
|
2
|
2
|
|
3
|
3
|
import android.app.Application;
|
|
4
|
+import android.util.Log;
|
4
|
5
|
|
5
|
6
|
import com.facebook.react.LifecycleState;
|
6
|
7
|
import com.facebook.react.ReactInstanceManager;
|
7
|
8
|
import com.facebook.react.ReactPackage;
|
|
9
|
+import com.facebook.react.bridge.JavaJSExecutor;
|
8
|
10
|
import com.facebook.react.bridge.ReactContext;
|
9
|
11
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
10
|
12
|
import com.facebook.react.bridge.WritableMap;
|
|
13
|
+import com.facebook.react.devsupport.DevSupportManager;
|
|
14
|
+import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
|
11
|
15
|
import com.reactnativenavigation.activities.BaseReactActivity;
|
12
|
16
|
import com.reactnativenavigation.core.objects.Screen;
|
|
17
|
+import com.reactnativenavigation.utils.ContextProvider;
|
|
18
|
+import com.reactnativenavigation.utils.ReflectionUtils;
|
13
|
19
|
|
14
|
20
|
import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
|
15
|
21
|
|
|
@@ -17,6 +23,7 @@ import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDevice
|
17
|
23
|
* Created by guyc on 22/02/16.
|
18
|
24
|
*/
|
19
|
25
|
public class RctManager {
|
|
26
|
+ private static final String TAG = "RctManager";
|
20
|
27
|
private static final String KEY_EVENT_ID = "id";
|
21
|
28
|
private static RctManager sInstance;
|
22
|
29
|
|
|
@@ -68,9 +75,47 @@ public class RctManager {
|
68
|
75
|
}
|
69
|
76
|
|
70
|
77
|
mReactManager = builder.build();
|
|
78
|
+ if (reactActivity.getUseDeveloperSupport()) {
|
|
79
|
+ setupDevSupportHandler(mReactManager);
|
|
80
|
+ }
|
71
|
81
|
return mReactManager;
|
72
|
82
|
}
|
73
|
83
|
|
|
84
|
+ /**
|
|
85
|
+ * Inject our CustomDevCommandsHandler to {@code reactInstanceManager} so we can detect when the bundle was
|
|
86
|
+ * parsed and is about to load.
|
|
87
|
+ * @param reactInstanceManager
|
|
88
|
+ */
|
|
89
|
+ private void setupDevSupportHandler(ReactInstanceManager reactInstanceManager) {
|
|
90
|
+ final ReactInstanceDevCommandsHandler devInterface = (ReactInstanceDevCommandsHandler)
|
|
91
|
+ ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface");
|
|
92
|
+ if (devInterface == null) {
|
|
93
|
+ Log.e(TAG, "Could not get field mDevInterface");
|
|
94
|
+ return;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ // Create customDevCommandsHandler
|
|
98
|
+ CustomDevCommandsHandler customDevCommandsHandler = new CustomDevCommandsHandler(devInterface);
|
|
99
|
+ boolean success = ReflectionUtils.setField(reactInstanceManager, "mDevInterface", customDevCommandsHandler);
|
|
100
|
+ if (!success) {
|
|
101
|
+ Log.e(TAG, "Could not set field mDevInterface");
|
|
102
|
+ return;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ // Set customDevCommandsHandler in devSupportManager. Fun =).
|
|
106
|
+ DevSupportManager devSupportManager = (DevSupportManager)
|
|
107
|
+ ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevSupportManager");
|
|
108
|
+ if (devSupportManager == null) {
|
|
109
|
+ Log.e(TAG, "Could not get field mDevSupportManager");
|
|
110
|
+ return;
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ success = ReflectionUtils.setField(devSupportManager, "mReactInstanceCommandsHandler", customDevCommandsHandler);
|
|
114
|
+ if (!success) {
|
|
115
|
+ Log.e(TAG, "Could not set field mReactInstanceCommandsHandler");
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
74
|
119
|
public <T extends ReactContextBaseJavaModule> T getNativeModule(Class<T> nativeModuleClass) {
|
75
|
120
|
if (mReactManager == null || mReactManager.getCurrentReactContext() == null) {
|
76
|
121
|
return null;
|
|
@@ -113,5 +158,44 @@ public class RctManager {
|
113
|
158
|
mReactManager = null;
|
114
|
159
|
sInstance = null;
|
115
|
160
|
}
|
|
161
|
+
|
|
162
|
+ /**
|
|
163
|
+ * Proxy for {@link ReactInstanceDevCommandsHandler} used by {@link DevSupportManager} for requesting React
|
|
164
|
+ * instance recreation. Used to notify {@link BaseReactActivity} that the bundle has been reloaded.
|
|
165
|
+ */
|
|
166
|
+ private static class CustomDevCommandsHandler implements ReactInstanceDevCommandsHandler {
|
|
167
|
+ private ReactInstanceDevCommandsHandler mCommandsHandler;
|
|
168
|
+
|
|
169
|
+ public CustomDevCommandsHandler(ReactInstanceDevCommandsHandler commandsHandler) {
|
|
170
|
+ mCommandsHandler = commandsHandler;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ @Override
|
|
174
|
+ public void onReloadWithJSDebugger(JavaJSExecutor.Factory proxyExecutorFactory) {
|
|
175
|
+ onJSBundleReloaded();
|
|
176
|
+ mCommandsHandler.onReloadWithJSDebugger(proxyExecutorFactory);
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ @Override
|
|
180
|
+ public void onJSBundleLoadedFromServer() {
|
|
181
|
+ onJSBundleReloaded();
|
|
182
|
+ mCommandsHandler.onJSBundleLoadedFromServer();
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ /**
|
|
186
|
+ * Detach previously added ReactRootViews before handling bundle.
|
|
187
|
+ */
|
|
188
|
+ private void onJSBundleReloaded() {
|
|
189
|
+ BaseReactActivity context = ContextProvider.getActivityContext();
|
|
190
|
+ if (context != null) {
|
|
191
|
+ context.onJSBundleReloaded();
|
|
192
|
+ }
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ @Override
|
|
196
|
+ public void toggleElementInspector() {
|
|
197
|
+ mCommandsHandler.toggleElementInspector();
|
|
198
|
+ }
|
|
199
|
+ }
|
116
|
200
|
}
|
117
|
201
|
|