react-native-navigation的迁移库

NavigationReactGateway.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.reactnativenavigation.react;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import com.facebook.react.ReactInstanceManager;
  5. import com.facebook.react.ReactNativeHost;
  6. import com.facebook.react.ReactPackage;
  7. import com.facebook.react.bridge.ReactContext;
  8. import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
  9. import com.facebook.react.shell.MainReactPackage;
  10. import com.reactnativenavigation.NavigationApplication;
  11. import com.reactnativenavigation.bridge.NavigationReactEventEmitter;
  12. import com.reactnativenavigation.bridge.NavigationReactPackage;
  13. import com.reactnativenavigation.events.EventBus;
  14. import com.reactnativenavigation.events.JsDevReloadEvent;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import javax.annotation.Nullable;
  18. public class NavigationReactGateway implements ReactGateway {
  19. private final ReactNativeHost host;
  20. private NavigationReactEventEmitter reactEventEmitter;
  21. public NavigationReactGateway() {
  22. host = new ReactNativeHostImpl();
  23. }
  24. @Override
  25. public void startReactContextOnceInBackgroundAndExecuteJS() {
  26. getReactInstanceManager().createReactContextInBackground();
  27. }
  28. public boolean isInitialized() {
  29. return host.hasInstance() && getReactInstanceManager().getCurrentReactContext() != null;
  30. }
  31. @Override
  32. public boolean hasStartedCreatingContext() {
  33. return getReactInstanceManager().hasStartedCreatingInitialContext();
  34. }
  35. public ReactContext getReactContext() {
  36. return getReactInstanceManager().getCurrentReactContext();
  37. }
  38. public NavigationReactEventEmitter getReactEventEmitter() {
  39. return reactEventEmitter;
  40. }
  41. @Override
  42. public ReactInstanceManager getReactInstanceManager() {
  43. return host.getReactInstanceManager();
  44. }
  45. public void onBackPressed() {
  46. getReactInstanceManager().onBackPressed();
  47. }
  48. public void onDestroyApp() {
  49. getReactInstanceManager().onHostDestroy();
  50. host.clear();
  51. }
  52. public void onPauseActivity() {
  53. getReactInstanceManager().onHostPause();
  54. }
  55. public void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler) {
  56. getReactInstanceManager().onHostResume(activity, defaultHardwareBackBtnHandler);
  57. }
  58. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  59. Activity currentActivity = getReactInstanceManager().getCurrentReactContext().getCurrentActivity();
  60. getReactInstanceManager().onActivityResult(currentActivity, requestCode, resultCode, data);
  61. }
  62. public ReactNativeHost getReactNativeHost() {
  63. return host;
  64. }
  65. //TODO temp hack
  66. void onReactContextInitialized() {
  67. reactEventEmitter = new NavigationReactEventEmitter(getReactContext());
  68. }
  69. private static class ReactNativeHostImpl extends ReactNativeHost implements ReactInstanceManager.ReactInstanceEventListener {
  70. public ReactNativeHostImpl() {
  71. super(NavigationApplication.instance);
  72. }
  73. @Override
  74. protected boolean getUseDeveloperSupport() {
  75. return NavigationApplication.instance.isDebug();
  76. }
  77. @Override
  78. protected List<ReactPackage> getPackages() {
  79. List<ReactPackage> list = new ArrayList<>();
  80. list.add(new MainReactPackage());
  81. list.add(new NavigationReactPackage());
  82. addAdditionalReactPackagesIfNeeded(list);
  83. return list;
  84. }
  85. private void addAdditionalReactPackagesIfNeeded(List<ReactPackage> list) {
  86. List<ReactPackage> additionalReactPackages = NavigationApplication.instance.createAdditionalReactPackages();
  87. if (additionalReactPackages == null) {
  88. return;
  89. }
  90. for (ReactPackage reactPackage : additionalReactPackages) {
  91. if (reactPackage instanceof MainReactPackage)
  92. throw new RuntimeException("Do not create a new MainReactPackage. This is created for you.");
  93. if (reactPackage instanceof NavigationReactPackage)
  94. throw new RuntimeException("Do not create a new NavigationReactPackage. This is created for you.");
  95. }
  96. list.addAll(additionalReactPackages);
  97. }
  98. @Override
  99. protected ReactInstanceManager createReactInstanceManager() {
  100. ReactInstanceManager manager = super.createReactInstanceManager();
  101. if (NavigationApplication.instance.isDebug()) {
  102. replaceJsDevReloadListener(manager);
  103. }
  104. manager.addReactInstanceEventListener(this);
  105. return manager;
  106. }
  107. private void replaceJsDevReloadListener(ReactInstanceManager manager) {
  108. new JsDevReloadListenerReplacer(manager, new JsDevReloadListenerReplacer.Listener() {
  109. @Override
  110. public void onJsDevReload() {
  111. EventBus.instance.post(new JsDevReloadEvent());
  112. }
  113. }).replace();
  114. }
  115. @Override
  116. public void onReactContextInitialized(ReactContext context) {
  117. ((NavigationReactGateway) NavigationApplication.instance.getReactGateway()).onReactContextInitialized();
  118. NavigationApplication.instance.onReactInitialized(context);
  119. }
  120. @Override
  121. public void clear() {
  122. getReactInstanceManager().removeReactInstanceEventListener(this);
  123. super.clear();
  124. }
  125. @Override
  126. protected String getJSMainModuleName() {
  127. String jsMainModuleName = NavigationApplication.instance.getJSMainModuleName();
  128. if (jsMainModuleName != null)
  129. return jsMainModuleName;
  130. return super.getJSMainModuleName();
  131. }
  132. @Nullable
  133. @Override
  134. protected String getJSBundleFile() {
  135. String jsBundleFile = NavigationApplication.instance.getJSBundleFile();
  136. if (jsBundleFile != null)
  137. return jsBundleFile;
  138. return super.getJSBundleFile();
  139. }
  140. @Nullable
  141. @Override
  142. protected String getBundleAssetName() {
  143. String bundleAssetName = NavigationApplication.instance.getBundleAssetName();
  144. if (bundleAssetName != null)
  145. return bundleAssetName;
  146. return super.getBundleAssetName();
  147. }
  148. }
  149. }