123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package com.reactnativenavigation.react;
-
- import android.app.Activity;
- import android.content.Intent;
-
- import com.facebook.react.ReactInstanceManager;
- import com.facebook.react.ReactNativeHost;
- import com.facebook.react.ReactPackage;
- import com.facebook.react.bridge.ReactContext;
- import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
- import com.facebook.react.shell.MainReactPackage;
- import com.reactnativenavigation.NavigationApplication;
- import com.reactnativenavigation.bridge.NavigationReactEventEmitter;
- import com.reactnativenavigation.bridge.NavigationReactPackage;
- import com.reactnativenavigation.events.EventBus;
- import com.reactnativenavigation.events.JsDevReloadEvent;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.annotation.Nullable;
-
- public class NavigationReactGateway implements ReactGateway {
-
- private final ReactNativeHost host;
- private NavigationReactEventEmitter reactEventEmitter;
-
- public NavigationReactGateway() {
- host = new ReactNativeHostImpl();
- }
-
- @Override
- public void startReactContextOnceInBackgroundAndExecuteJS() {
- getReactInstanceManager().createReactContextInBackground();
- }
-
- public boolean isInitialized() {
- return host.hasInstance() && getReactInstanceManager().getCurrentReactContext() != null;
- }
-
- @Override
- public boolean hasStartedCreatingContext() {
- return getReactInstanceManager().hasStartedCreatingInitialContext();
- }
-
- public ReactContext getReactContext() {
- return getReactInstanceManager().getCurrentReactContext();
- }
-
- public NavigationReactEventEmitter getReactEventEmitter() {
- return reactEventEmitter;
- }
-
- @Override
- public ReactInstanceManager getReactInstanceManager() {
- return host.getReactInstanceManager();
- }
-
- public void onBackPressed() {
- getReactInstanceManager().onBackPressed();
- }
-
- public void onDestroyApp() {
- getReactInstanceManager().onHostDestroy();
- host.clear();
- }
-
- public void onPauseActivity() {
- getReactInstanceManager().onHostPause();
- }
-
- public void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler) {
- getReactInstanceManager().onHostResume(activity, defaultHardwareBackBtnHandler);
- }
-
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- Activity currentActivity = getReactInstanceManager().getCurrentReactContext().getCurrentActivity();
- getReactInstanceManager().onActivityResult(currentActivity, requestCode, resultCode, data);
- }
-
- public ReactNativeHost getReactNativeHost() {
- return host;
- }
-
- //TODO temp hack
- void onReactContextInitialized() {
- reactEventEmitter = new NavigationReactEventEmitter(getReactContext());
- }
-
- private static class ReactNativeHostImpl extends ReactNativeHost implements ReactInstanceManager.ReactInstanceEventListener {
-
- public ReactNativeHostImpl() {
- super(NavigationApplication.instance);
- }
-
- @Override
- protected boolean getUseDeveloperSupport() {
- return NavigationApplication.instance.isDebug();
- }
-
- @Override
- protected List<ReactPackage> getPackages() {
- List<ReactPackage> list = new ArrayList<>();
- list.add(new MainReactPackage());
- list.add(new NavigationReactPackage());
- addAdditionalReactPackagesIfNeeded(list);
- return list;
- }
-
- private void addAdditionalReactPackagesIfNeeded(List<ReactPackage> list) {
- List<ReactPackage> additionalReactPackages = NavigationApplication.instance.createAdditionalReactPackages();
- if (additionalReactPackages == null) {
- return;
- }
-
- for (ReactPackage reactPackage : additionalReactPackages) {
- if (reactPackage instanceof MainReactPackage)
- throw new RuntimeException("Do not create a new MainReactPackage. This is created for you.");
- if (reactPackage instanceof NavigationReactPackage)
- throw new RuntimeException("Do not create a new NavigationReactPackage. This is created for you.");
- }
-
- list.addAll(additionalReactPackages);
- }
-
- @Override
- protected ReactInstanceManager createReactInstanceManager() {
- ReactInstanceManager manager = super.createReactInstanceManager();
- if (NavigationApplication.instance.isDebug()) {
- replaceJsDevReloadListener(manager);
- }
- manager.addReactInstanceEventListener(this);
- return manager;
- }
-
- private void replaceJsDevReloadListener(ReactInstanceManager manager) {
- new JsDevReloadListenerReplacer(manager, new JsDevReloadListenerReplacer.Listener() {
- @Override
- public void onJsDevReload() {
- EventBus.instance.post(new JsDevReloadEvent());
- }
- }).replace();
- }
-
- @Override
- public void onReactContextInitialized(ReactContext context) {
- ((NavigationReactGateway) NavigationApplication.instance.getReactGateway()).onReactContextInitialized();
- NavigationApplication.instance.onReactInitialized(context);
- }
-
- @Override
- public void clear() {
- getReactInstanceManager().removeReactInstanceEventListener(this);
- super.clear();
- }
-
- @Override
- protected String getJSMainModuleName() {
- String jsMainModuleName = NavigationApplication.instance.getJSMainModuleName();
- if (jsMainModuleName != null)
- return jsMainModuleName;
- return super.getJSMainModuleName();
- }
-
- @Nullable
- @Override
- protected String getJSBundleFile() {
- String jsBundleFile = NavigationApplication.instance.getJSBundleFile();
- if (jsBundleFile != null)
- return jsBundleFile;
- return super.getJSBundleFile();
- }
-
- @Nullable
- @Override
- protected String getBundleAssetName() {
- String bundleAssetName = NavigationApplication.instance.getBundleAssetName();
- if (bundleAssetName != null)
- return bundleAssetName;
- return super.getBundleAssetName();
- }
- }
- }
|