@ReactMethod void callJavaScript() { Activity activity = getCurrentActivity(); if (activity != null) { MainApplication application = (MainApplication) activity.getApplication(); ReactNativeHost reactNativeHost = application.getReactNativeHost(); ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager(); ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); if (reactContext != null) { CatalystInstance catalystInstance = reactContext.getCatalystInstance(); WritableNativeArray params = new WritableNativeArray(); params.pushString("Hello, JavaScript!"); catalystInstance.callFunction("JavaScriptVisibleToJava", "alert", params); } } }
private void attachMeasuredRootViewToInstance( ReactRootView rootView, CatalystInstance catalystInstance) { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance"); UiThreadUtil.assertOnUiThread(); // Reset view content as it's going to be populated by the application content from JS rootView.removeAllViews(); rootView.setId(View.NO_ID); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); int rootTag = uiManagerModule.addMeasuredRootView(rootView); rootView.setRootViewTag(rootTag); @Nullable Bundle launchOptions = rootView.getLaunchOptions(); WritableMap initialProps = Arguments.makeNativeMap(launchOptions); String jsAppModuleName = rootView.getJSModuleName(); WritableNativeMap appParams = new WritableNativeMap(); appParams.putDouble("rootTag", rootTag); appParams.putMap("initialProps", initialProps); catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); rootView.onAttachedToReactInstance(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
private void setupReactContext(ReactApplicationContext reactContext) { UiThreadUtil.assertOnUiThread(); Assertions.assertCondition(mCurrentReactContext == null); mCurrentReactContext = Assertions.assertNotNull(reactContext); CatalystInstance catalystInstance = Assertions.assertNotNull(reactContext.getCatalystInstance()); catalystInstance.initialize(); mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); moveReactContextToCurrentLifecycleState(); for (ReactRootView rootView : mAttachedRootViews) { attachMeasuredRootViewToInstance(rootView, catalystInstance); } ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; listeners = mReactInstanceEventListeners.toArray(listeners); for (ReactInstanceEventListener listener : listeners) { listener.onReactContextInitialized(reactContext); } }
private void attachMeasuredRootViewToInstance( ReactRootView rootView, CatalystInstance catalystInstance) { UiThreadUtil.assertOnUiThread(); // Reset view content as it's going to be populated by the application content from JS rootView.removeAllViews(); rootView.setId(View.NO_ID); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); int rootTag = uiManagerModule.addMeasuredRootView(rootView); @Nullable Bundle launchOptions = rootView.getLaunchOptions(); WritableMap initialProps = launchOptions != null ? Arguments.fromBundle(launchOptions) : Arguments.createMap(); String jsAppModuleName = rootView.getJSModuleName(); WritableNativeMap appParams = new WritableNativeMap(); appParams.putDouble("rootTag", rootTag); appParams.putMap("initialProps", initialProps); catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); }
@Override public CatalystInstance build() { return new CatalystInstanceImpl.Builder() .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault()) .setJSExecutor(new JSCJavaScriptExecutor(new WritableNativeMap())) .setRegistry(mNativeModuleRegistryBuilder.build()) .setJSModulesConfig(mJSModulesConfigBuilder.build()) .setJSBundleLoader(JSBundleLoader.createFileLoader( mContext, "assets://AndroidTestBundle.js")) .setNativeModuleCallExceptionHandler( new NativeModuleCallExceptionHandler() { @Override public void handleException(Exception e) { throw new RuntimeException(e); } }) .build(); }
private void attachMeasuredRootViewToInstance( ReactRootView rootView, CatalystInstance catalystInstance) { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance"); UiThreadUtil.assertOnUiThread(); // Reset view content as it's going to be populated by the application content from JS rootView.removeAllViews(); rootView.setId(View.NO_ID); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); int rootTag = uiManagerModule.addMeasuredRootView(rootView); rootView.setRootViewTag(rootTag); @Nullable Bundle launchOptions = rootView.getLaunchOptions(); WritableMap initialProps = Arguments.makeNativeMap(launchOptions); String jsAppModuleName = rootView.getJSModuleName(); WritableNativeMap appParams = new WritableNativeMap(); appParams.putDouble("rootTag", rootTag); appParams.putMap("initialProps", initialProps); catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
@Override public void onCreate() { ReactInstanceManager reactInstanceManager = ReactInstanceSingleton.getReactInstanceManager(getApplication()); ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); if (reactContext != null) { CatalystInstance catalystInstance = reactContext.getCatalystInstance(); serviceTopLevel = catalystInstance.getJSModule(ServiceTopLevel.class); mRemoteViewsModule = catalystInstance.getJSModule(RemoteViewsModule.class); } else { Log.e(TAG, "Couldn't grab JS service code"); } serviceTopLevel.initService(0); }
public static void cleanDataFromModules(CatalystInstance catalystInstance) { for (NativeModule nativeModule : catalystInstance.getNativeModules()) { if (nativeModule instanceof Cleanable) { FLog.d(ReactConstants.TAG, "Cleaning data from " + nativeModule.getName()); ((Cleanable) nativeModule).clearSensitiveData(); } } }
private void setupReactContext(ReactApplicationContext reactContext) { ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_END); ReactMarker.logMarker(SETUP_REACT_CONTEXT_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "setupReactContext"); UiThreadUtil.assertOnUiThread(); Assertions.assertCondition(mCurrentReactContext == null); mCurrentReactContext = Assertions.assertNotNull(reactContext); CatalystInstance catalystInstance = Assertions.assertNotNull(reactContext.getCatalystInstance()); catalystInstance.initialize(); mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); moveReactContextToCurrentLifecycleState(); for (ReactRootView rootView : mAttachedRootViews) { attachMeasuredRootViewToInstance(rootView, catalystInstance); } ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; listeners = mReactInstanceEventListeners.toArray(listeners); for (ReactInstanceEventListener listener : listeners) { listener.onReactContextInitialized(reactContext); } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); }
private void detachViewFromInstance( ReactRootView rootView, CatalystInstance catalystInstance) { UiThreadUtil.assertOnUiThread(); catalystInstance.getJSModule(AppRegistry.class) .unmountApplicationComponentAtRootTag(rootView.getId()); }
@Before public void setup() { ReactApplicationContext mContext = new ReactApplicationContext(RuntimeEnvironment.application); CatalystInstance mCatalystInstanceMock = ReactTestHelper.createMockCatalystInstance(); mContext.initializeWithInstance(mCatalystInstanceMock); mThemedContext = new ThemedReactContext(mContext, mContext); mManager = new ReactSliderManager(); }
public static ReactTestFactory.ReactInstanceEasyBuilder catalystInstanceBuilder( final ReactIntegrationTestCase testCase) { final ReactTestFactory.ReactInstanceEasyBuilder builder = getReactTestFactory().getCatalystInstanceBuilder(); ReactTestFactory.ReactInstanceEasyBuilder postBuilder = new ReactTestFactory.ReactInstanceEasyBuilder() { @Override public ReactTestFactory.ReactInstanceEasyBuilder setContext(Context context) { builder.setContext(context); return this; } @Override public ReactTestFactory.ReactInstanceEasyBuilder addNativeModule(NativeModule module) { builder.addNativeModule(module); return this; } @Override public ReactTestFactory.ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) { builder.addJSModule(moduleInterfaceClass); return this; } @Override public CatalystInstance build() { CatalystInstance instance = builder.build(); testCase.initializeWithInstance(instance); instance.runJSBundle(); testCase.waitForBridgeAndUIIdle(); return instance; } }; postBuilder.setContext(testCase.getContext()); return postBuilder; }
@Before public void prepareModules() { PowerMockito.mockStatic(Arguments.class); when(Arguments.createArray()).thenAnswer( new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return new JavaOnlyArray(); } }); PowerMockito.mockStatic(SystemClock.class); when(SystemClock.currentTimeMillis()).thenReturn(mCurrentTimeNs / 1000000); when(SystemClock.nanoTime()).thenReturn(mCurrentTimeNs); mChoreographerMock = mock(ReactChoreographer.class); PowerMockito.mockStatic(ReactChoreographer.class); when(ReactChoreographer.getInstance()).thenReturn(mChoreographerMock); CatalystInstance reactInstance = mock(CatalystInstance.class); ReactApplicationContext reactContext = mock(ReactApplicationContext.class); when(reactContext.getCatalystInstance()).thenReturn(reactInstance); mCurrentTimeNs = 0; mPostFrameCallbackHandler = new PostFrameCallbackHandler(); doAnswer(mPostFrameCallbackHandler) .when(mChoreographerMock) .postFrameCallback( eq(ReactChoreographer.CallbackType.TIMERS_EVENTS), any(Choreographer.FrameCallback.class)); mTiming = new Timing(reactContext); mJSTimersMock = mock(JSTimersExecution.class); mExecutorTokenMock = mock(ExecutorToken.class); when(reactContext.getJSModule(mExecutorTokenMock, JSTimersExecution.class)).thenReturn(mJSTimersMock); mTiming.initialize(); }
private void setupReactContext(ReactApplicationContext reactContext) { ReactMarker.logMarker(SETUP_REACT_CONTEXT_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "setupReactContext"); UiThreadUtil.assertOnUiThread(); Assertions.assertCondition(mCurrentReactContext == null); mCurrentReactContext = Assertions.assertNotNull(reactContext); CatalystInstance catalystInstance = Assertions.assertNotNull(reactContext.getCatalystInstance()); catalystInstance.initialize(); mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); moveReactContextToCurrentLifecycleState(); for (ReactRootView rootView : mAttachedRootViews) { attachMeasuredRootViewToInstance(rootView, catalystInstance); } ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; listeners = mReactInstanceEventListeners.toArray(listeners); for (ReactInstanceEventListener listener : listeners) { listener.onReactContextInitialized(reactContext); } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); }
public static ReactTestFactory.ReactInstanceEasyBuilder catalystInstanceBuilder( final ReactIntegrationTestCase testCase) { final ReactTestFactory.ReactInstanceEasyBuilder builder = getReactTestFactory().getCatalystInstanceBuilder(); ReactTestFactory.ReactInstanceEasyBuilder postBuilder = new ReactTestFactory.ReactInstanceEasyBuilder() { @Override public ReactTestFactory.ReactInstanceEasyBuilder setContext(Context context) { builder.setContext(context); return this; } @Override public ReactTestFactory.ReactInstanceEasyBuilder addNativeModule(NativeModule module) { builder.addNativeModule(module); return this; } @Override public ReactTestFactory.ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) { builder.addJSModule(moduleInterfaceClass); return this; } @Override public CatalystInstance build() { final CatalystInstance instance = builder.build(); testCase.initializeWithInstance(instance); instance.runJSBundle(); InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { instance.initialize(); } }); testCase.waitForBridgeAndUIIdle(); return instance; } }; postBuilder.setContext(testCase.getContext()); return postBuilder; }
public void initializeWithInstance(CatalystInstance instance) { mInstance = instance; mBridgeIdleSignaler = new ReactBridgeIdleSignaler(); mInstance.addBridgeIdleDebugListener(mBridgeIdleSignaler); getContext().initializeWithInstance(mInstance); }