浏览代码

[Android] Add backwards compatible support for react-native 0.52.0 (#2580)

Support for react-native 0.52.0

Rename `DevCommandsHandlerProxy` to `DevHelperProxy` so it reflects the changes in RN. See d19afc73f5
Krystof Celba 6 年前
父节点
当前提交
5b0b03984f

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java 查看文件

@@ -39,6 +39,7 @@ import com.reactnativenavigation.react.ReactGateway;
39 39
 import com.reactnativenavigation.screens.NavigationType;
40 40
 import com.reactnativenavigation.screens.Screen;
41 41
 import com.reactnativenavigation.utils.OrientationHelper;
42
+import com.reactnativenavigation.utils.ReflectionUtils;
42 43
 import com.reactnativenavigation.views.SideMenu.Side;
43 44
 
44 45
 import java.util.List;
@@ -455,6 +456,11 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
455 456
             public void run() {
456 457
                 layout.destroy();
457 458
                 modalController.destroy();
459
+
460
+                Object devSupportManager = ReflectionUtils.getDeclaredField(getReactGateway().getReactInstanceManager(), "mDevSupportManager");
461
+                if (ReflectionUtils.getDeclaredField(devSupportManager, "mRedBoxDialog") != null) { // RN >= 0.52
462
+                    ReflectionUtils.setField(devSupportManager, "mRedBoxDialog", null);
463
+                }
458 464
             }
459 465
         });
460 466
     }

+ 37
- 30
android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadListenerReplacer.java 查看文件

@@ -1,10 +1,12 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3 3
 import com.facebook.react.ReactInstanceManager;
4
-import com.facebook.react.bridge.JavaJSExecutor;
5
-import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
6 4
 import com.reactnativenavigation.utils.ReflectionUtils;
7 5
 
6
+import java.lang.reflect.InvocationHandler;
7
+import java.lang.reflect.Method;
8
+import java.lang.reflect.Proxy;
9
+
8 10
 class JsDevReloadListenerReplacer {
9 11
     private final ReactInstanceManager reactInstanceManager;
10 12
     private final Listener listener;
@@ -19,49 +21,54 @@ class JsDevReloadListenerReplacer {
19 21
     }
20 22
 
21 23
     void replace() {
22
-        ReactInstanceDevCommandsHandler originalHandler = getOriginalHandler();
23
-        DevCommandsHandlerProxy proxy = new DevCommandsHandlerProxy(originalHandler, listener);
24
-        replaceInReactInstanceManager(proxy);
25
-        replaceInDevSupportManager(proxy);
26
-    }
24
+        Object originalHelper = getOriginalHelper();
27 25
 
28
-    private void replaceInDevSupportManager(DevCommandsHandlerProxy proxy) {
29 26
         Object devSupportManager = ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevSupportManager");
30
-        ReflectionUtils.setField(devSupportManager, "mReactInstanceCommandsHandler", proxy);
31
-    }
32 27
 
33
-    private ReactInstanceDevCommandsHandler getOriginalHandler() {
34
-        return (ReactInstanceDevCommandsHandler) ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface");
28
+        Object proxy = Proxy.newProxyInstance(
29
+                originalHelper.getClass().getClassLoader(),
30
+                originalHelper.getClass().getInterfaces(),
31
+                new DevHelperProxy(originalHelper, listener));
32
+
33
+        if (ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface") == null) { // RN >= 0.52
34
+            ReflectionUtils.setField(devSupportManager, "mReactInstanceManagerHelper", proxy);
35
+        } else {                                                                                         // RN <= 0.51
36
+            ReflectionUtils.setField(reactInstanceManager, "mDevInterface", proxy);
37
+            ReflectionUtils.setField(devSupportManager, "mReactInstanceCommandsHandler", proxy);
38
+        }
35 39
     }
36 40
 
37
-    private void replaceInReactInstanceManager(DevCommandsHandlerProxy proxy) {
38
-        ReflectionUtils.setField(reactInstanceManager, "mDevInterface", proxy);
41
+
42
+    private Object getOriginalHelper() {
43
+        Object devInterface = ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface");
44
+
45
+        if (devInterface == null) { // RN >= 0.52
46
+            Object devSupportManager = ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevSupportManager");
47
+            return ReflectionUtils.getDeclaredField(devSupportManager, "mReactInstanceManagerHelper");
48
+        }
49
+
50
+        return devInterface;        // RN <= 0.51
39 51
     }
40 52
 
41
-    private static class DevCommandsHandlerProxy implements ReactInstanceDevCommandsHandler {
42
-        private ReactInstanceDevCommandsHandler originalReactHandler;
53
+
54
+    private static class DevHelperProxy implements InvocationHandler {
55
+        private Object originalReactHelper;
43 56
         private final Listener listener;
44 57
 
45
-        DevCommandsHandlerProxy(ReactInstanceDevCommandsHandler originalReactHandler, Listener listener) {
46
-            this.originalReactHandler = originalReactHandler;
58
+        DevHelperProxy(Object originalReactHelper, Listener listener) {
59
+            this.originalReactHelper = originalReactHelper;
47 60
             this.listener = listener;
48 61
         }
49 62
 
50 63
         @Override
51
-        public void onReloadWithJSDebugger(JavaJSExecutor.Factory proxyExecutorFactory) {
52
-            listener.onJsDevReload();
53
-            originalReactHandler.onReloadWithJSDebugger(proxyExecutorFactory);
54
-        }
64
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
65
+            String methodName = method.getName();
55 66
 
56
-        @Override
57
-        public void onJSBundleLoadedFromServer() {
58
-            listener.onJsDevReload();
59
-            originalReactHandler.onJSBundleLoadedFromServer();
60
-        }
67
+            if (methodName.equals("onJSBundleLoadedFromServer") || methodName.equals("onReloadWithJSDebugger")) {
68
+                listener.onJsDevReload();
69
+            }
61 70
 
62
-        @Override
63
-        public void toggleElementInspector() {
64
-            originalReactHandler.toggleElementInspector();
71
+            return method.invoke(originalReactHelper, args);
65 72
         }
66 73
     }
67 74
 }