diff --git a/ReactAndroid/src/main/java/com/facebook/react/CompositeLazyReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CompositeLazyReactPackage.java index ff051ff67..a0404277d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/CompositeLazyReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/CompositeLazyReactPackage.java @@ -45,7 +45,7 @@ public class CompositeLazyReactPackage extends LazyReactPackage { // This would require us to use ModuleHolder here final Map, ModuleSpec> moduleMap = new HashMap<>(); for (LazyReactPackage reactPackage: mChildReactPackages) { - for (ModuleSpec module: reactPackage.getNativeModules(reactContext)) { + for (ModuleSpec module : reactPackage.getNativeModules(reactContext)) { moduleMap.put(module.getType(), module); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java index 1377c13ae..51f4776c5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java @@ -9,20 +9,23 @@ package com.facebook.react; +import javax.inject.Provider; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.ModuleSpec; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.common.build.ReactBuildConfig; +import com.facebook.react.devsupport.HMRClient; import com.facebook.react.devsupport.JSCHeapCapture; import com.facebook.react.devsupport.JSCSamplingProfiler; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.core.ExceptionsManagerModule; -import com.facebook.react.devsupport.HMRClient; import com.facebook.react.modules.core.JSTimersExecution; import com.facebook.react.modules.core.RCTNativeAppEventEmitter; import com.facebook.react.modules.core.Timing; @@ -42,7 +45,7 @@ import com.facebook.systrace.Systrace; * require special integration with other framework parts (e.g. with the list of packages to load * view managers from). */ -/* package */ class CoreModulesPackage implements ReactPackage { +/* package */ class CoreModulesPackage extends LazyReactPackage { private final ReactInstanceManager mReactInstanceManager; private final DefaultHardwareBackBtnHandler mHardwareBackBtnHandler; @@ -58,41 +61,85 @@ import com.facebook.systrace.Systrace; } @Override - public List createNativeModules( - ReactApplicationContext catalystApplicationContext) { - Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); - UIManagerModule uiManagerModule; - try { - List viewManagersList = mReactInstanceManager.createAllViewManagers( - catalystApplicationContext); - uiManagerModule = new UIManagerModule( - catalystApplicationContext, - viewManagersList, - mUIImplementationProvider.createUIImplementation( - catalystApplicationContext, - viewManagersList)); - } finally { - Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); - } - - List nativeModulesList = new ArrayList<>(Arrays.asList( - new AnimationsDebugModule( - catalystApplicationContext, - mReactInstanceManager.getDevSupportManager().getDevSettings()), - new AndroidInfoModule(), - new DeviceEventManagerModule(catalystApplicationContext, mHardwareBackBtnHandler), - new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()), - new Timing(catalystApplicationContext, mReactInstanceManager.getDevSupportManager()), - new SourceCodeModule(mReactInstanceManager.getSourceUrl()), - uiManagerModule, - new JSCSamplingProfiler(catalystApplicationContext), - new DebugComponentOwnershipModule(catalystApplicationContext))); + public List getNativeModules(final ReactApplicationContext reactContext) { + List moduleSpecList = new ArrayList(); + moduleSpecList.add( + new ModuleSpec(AnimationsDebugModule.class, new Provider() { + @Override + public NativeModule get() { + return new AnimationsDebugModule( + reactContext, + mReactInstanceManager.getDevSupportManager().getDevSettings()); + } + })); + moduleSpecList.add( + new ModuleSpec(AndroidInfoModule.class, new Provider() { + @Override + public NativeModule get() { + return new AndroidInfoModule(); + } + })); + moduleSpecList.add( + new ModuleSpec(DeviceEventManagerModule.class, new Provider() { + @Override + public NativeModule get() { + return new DeviceEventManagerModule(reactContext, mHardwareBackBtnHandler); + } + })); + moduleSpecList.add( + new ModuleSpec(ExceptionsManagerModule.class, new Provider() { + @Override + public NativeModule get() { + return new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()); + } + })); + moduleSpecList.add( + new ModuleSpec(Timing.class, new Provider() { + @Override + public NativeModule get() { + return new Timing(reactContext, mReactInstanceManager.getDevSupportManager()); + } + })); + moduleSpecList.add( + new ModuleSpec(SourceCodeModule.class, new Provider() { + @Override + public NativeModule get() { + return new SourceCodeModule(mReactInstanceManager.getSourceUrl()); + } + })); + moduleSpecList.add( + new ModuleSpec(UIManagerModule.class, new Provider() { + @Override + public NativeModule get() { + return createUIManager(reactContext); + } + })); if (ReactBuildConfig.DEBUG) { - nativeModulesList.add(new JSCHeapCapture(catalystApplicationContext)); + moduleSpecList.add( + new ModuleSpec(DebugComponentOwnershipModule.class, new Provider() { + @Override + public NativeModule get() { + return new DebugComponentOwnershipModule(reactContext); + } + })); + moduleSpecList.add( + new ModuleSpec(JSCHeapCapture.class, new Provider() { + @Override + public NativeModule get() { + return new JSCHeapCapture(reactContext); + } + })); + moduleSpecList.add( + new ModuleSpec(JSCSamplingProfiler.class, new Provider() { + @Override + public NativeModule get() { + return new JSCSamplingProfiler(reactContext); + } + })); } - return nativeModulesList; + return moduleSpecList; } @Override @@ -119,4 +166,20 @@ import com.facebook.systrace.Systrace; public List createViewManagers(ReactApplicationContext reactContext) { return new ArrayList<>(0); } + + private UIManagerModule createUIManager(ReactApplicationContext reactContext) { + Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); + try { + List viewManagersList = mReactInstanceManager.createAllViewManagers( + reactContext); + return new UIManagerModule( + reactContext, + viewManagersList, + mUIImplementationProvider.createUIImplementation( + reactContext, + viewManagersList)); + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); + } + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleSpec.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleSpec.java index 880296154..7c2704aef 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleSpec.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleSpec.java @@ -40,7 +40,7 @@ public class ModuleSpec { try { return getConstructor(type, EMPTY_SIGNATURE).newInstance(); } catch (Exception e) { - throw new RuntimeException(e); + throw new RuntimeException("ModuleSpec with class: " + type.getName(), e); } } }); @@ -58,7 +58,7 @@ public class ModuleSpec { try { return getConstructor(type, CONTEXT_SIGNATURE).newInstance(context); } catch (Exception e) { - throw new RuntimeException(e); + throw new RuntimeException("ModuleSpec with class: " + type.getName(), e); } } }); diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index 878247c0d..bf65ce841 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -9,13 +9,16 @@ package com.facebook.react.shell; +import javax.inject.Provider; + import java.util.Arrays; import java.util.Collections; import java.util.List; -import com.facebook.react.ReactPackage; +import com.facebook.react.LazyReactPackage; import com.facebook.react.animated.NativeAnimatedModule; import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.ModuleSpec; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.modules.appstate.AppStateModule; @@ -68,35 +71,149 @@ import com.facebook.react.views.webview.ReactWebViewManager; /** * Package defining basic modules and view managers. */ -public class MainReactPackage implements ReactPackage { +public class MainReactPackage extends LazyReactPackage { @Override - public List createNativeModules(ReactApplicationContext reactContext) { - return Arrays.asList( - new AppStateModule(reactContext), - new AsyncStorageModule(reactContext), - new CameraRollManager(reactContext), - new ClipboardModule(reactContext), - new DatePickerDialogModule(reactContext), - new DialogModule(reactContext), - new FrescoModule(reactContext), - new I18nManagerModule(reactContext), - new ImageEditingManager(reactContext), - new ImageLoaderModule(reactContext), - new ImageStoreManager(reactContext), - new IntentModule(reactContext), - new LocationModule(reactContext), - new NativeAnimatedModule(reactContext), - new NetworkingModule(reactContext), - new NetInfoModule(reactContext), - new PermissionsModule(reactContext), - new ShareModule(reactContext), - new StatusBarModule(reactContext), - new TimePickerDialogModule(reactContext), - new ToastModule(reactContext), - new VibrationModule(reactContext), - new WebSocketModule(reactContext) - ); + public List getNativeModules(final ReactApplicationContext context) { + return Arrays.asList( + new ModuleSpec(AppStateModule.class, new Provider() { + @Override + public NativeModule get() { + return new AppStateModule(context); + } + }), + new ModuleSpec(AsyncStorageModule.class, new Provider() { + @Override + public NativeModule get() { + return new AsyncStorageModule(context); + } + }), + new ModuleSpec(CameraRollManager.class, new Provider() { + @Override + public NativeModule get() { + return new CameraRollManager(context); + } + }), + new ModuleSpec(ClipboardModule.class, new Provider() { + @Override + public NativeModule get() { + return new ClipboardModule(context); + } + }), + new ModuleSpec(DatePickerDialogModule.class, new Provider() { + @Override + public NativeModule get() { + return new DatePickerDialogModule(context); + } + }), + new ModuleSpec(DialogModule.class, new Provider() { + @Override + public NativeModule get() { + return new DialogModule(context); + } + }), + new ModuleSpec(FrescoModule.class, new Provider() { + @Override + public NativeModule get() { + return new FrescoModule(context); + } + }), + new ModuleSpec(I18nManagerModule.class, new Provider() { + @Override + public NativeModule get() { + return new I18nManagerModule(context); + } + }), + new ModuleSpec(ImageEditingManager.class, new Provider() { + @Override + public NativeModule get() { + return new ImageEditingManager(context); + } + }), + new ModuleSpec(ImageLoaderModule.class, new Provider() { + @Override + public NativeModule get() { + return new ImageLoaderModule(context); + } + }), + new ModuleSpec(ImageStoreManager.class, new Provider() { + @Override + public NativeModule get() { + return new ImageStoreManager(context); + } + }), + new ModuleSpec(IntentModule.class, new Provider() { + @Override + public NativeModule get() { + return new IntentModule(context); + } + }), + new ModuleSpec(LocationModule.class, new Provider() { + @Override + public NativeModule get() { + return new LocationModule(context); + } + }), + new ModuleSpec(NativeAnimatedModule.class, new Provider() { + @Override + public NativeModule get() { + return new NativeAnimatedModule(context); + } + }), + new ModuleSpec(NetworkingModule.class, new Provider() { + @Override + public NativeModule get() { + return new NetworkingModule(context); + } + }), + new ModuleSpec(NetInfoModule.class, new Provider() { + @Override + public NativeModule get() { + return new NetInfoModule(context); + } + }), + new ModuleSpec(PermissionsModule.class, new Provider() { + @Override + public NativeModule get() { + return new PermissionsModule(context); + } + }), + new ModuleSpec(ShareModule.class, new Provider() { + @Override + public NativeModule get() { + return new ShareModule(context); + } + }), + new ModuleSpec(StatusBarModule.class, new Provider() { + @Override + public NativeModule get() { + return new StatusBarModule(context); + } + }), + new ModuleSpec(TimePickerDialogModule.class, new Provider() { + @Override + public NativeModule get() { + return new TimePickerDialogModule(context); + } + }), + new ModuleSpec(ToastModule.class, new Provider() { + @Override + public NativeModule get() { + return new ToastModule(context); + } + }), + new ModuleSpec(VibrationModule.class, new Provider() { + @Override + public NativeModule get() { + return new VibrationModule(context); + } + }), + new ModuleSpec(WebSocketModule.class, new Provider() { + @Override + public NativeModule get() { + return new WebSocketModule(context); + } + })); } @Override