From 9263eb5d3864a42925b699343db2c09cc8934ed0 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 28 Apr 2020 12:14:48 -0700 Subject: [PATCH] Part 2: Make CatalystInstanceImpl.getNativeModule Nullable Summary: This is the codemod portion of the parent diff. I modified all call-sites to `ReactContext.getNativeModule` to do a null check on the returned NativeModule. Changelog: [Android][Fixed] - Check if NativeModules returned from CatalystInstanceImpl.getNativeModule are null before using them. Reviewed By: JoshuaGross Differential Revision: D21272028 fbshipit-source-id: 6bd16c6bf30605f2dfdf4c481352063712965342 --- .../testing/rule/ReactNativeTestRule.java | 5 +++ .../facebook/react/ReactInstanceManager.java | 17 +++++++--- .../com/facebook/react/ReactRootView.java | 28 +++++++++------ .../react/animated/NativeAnimatedModule.java | 7 ++-- .../devsupport/DevSupportManagerBase.java | 27 ++++++++------- .../react/fabric/FabricJSIModuleProvider.java | 4 ++- .../modules/debug/FpsDebugFrameCallback.java | 3 +- .../react/uimanager/UIViewOperationQueue.java | 13 ++++--- .../drawer/ReactDrawerLayoutManager.java | 9 +++-- .../views/modal/ReactModalHostManager.java | 8 +++-- .../react/views/modal/ReactModalHostView.java | 11 ++++-- .../views/picker/ReactPickerManager.java | 10 ++++-- .../views/slider/ReactSliderManager.java | 34 ++++++++++++------- .../views/switchview/ReactSwitchManager.java | 10 ++++-- .../react/views/text/ReactTextView.java | 4 ++- .../react/views/textinput/ReactEditText.java | 4 ++- .../ReactEditTextInputConnectionWrapper.java | 5 ++- .../textinput/ReactTextInputManager.java | 9 +++-- .../react/views/view/ReactViewManager.java | 12 ++++--- .../react/views/viewpager/ReactViewPager.java | 5 ++- 20 files changed, 153 insertions(+), 72 deletions(-) diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java index 1dd5b381a79bc7..8d6252f28b8c88 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java @@ -95,6 +95,11 @@ public void run() { public void onReactContextInitialized(ReactContext reactContext) { final UIManagerModule uiManagerModule = reactContext.getCatalystInstance().getNativeModule(UIManagerModule.class); + + if (uiManagerModule == null) { + return; + } + uiManagerModule .getUIImplementation() .setLayoutUpdateListener( diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 17eae9a95057da..07f26501f6e283 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -470,7 +470,9 @@ public void onBackPressed() { } else { DeviceEventManagerModule deviceEventManagerModule = reactContext.getNativeModule(DeviceEventManagerModule.class); - deviceEventManagerModule.emitHardwareBackPressed(); + if (deviceEventManagerModule != null) { + deviceEventManagerModule.emitHardwareBackPressed(); + } } } @@ -497,7 +499,9 @@ public void onNewIntent(Intent intent) { || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) { DeviceEventManagerModule deviceEventManagerModule = currentContext.getNativeModule(DeviceEventManagerModule.class); - deviceEventManagerModule.emitNewIntentReceived(uri); + if (deviceEventManagerModule != null) { + deviceEventManagerModule.emitNewIntentReceived(uri); + } } currentContext.onNewIntent(mCurrentActivity, intent); } @@ -775,9 +779,12 @@ public void onConfigurationChanged(Context updatedContext, @Nullable Configurati ReactContext currentReactContext = getCurrentReactContext(); if (currentReactContext != null) { - currentReactContext - .getNativeModule(AppearanceModule.class) - .onConfigurationChanged(updatedContext); + AppearanceModule appearanceModule = + currentReactContext.getNativeModule(AppearanceModule.class); + + if (appearanceModule != null) { + appearanceModule.onConfigurationChanged(updatedContext); + } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index b40196b11e3ec1..26c39f9df01ae3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -200,9 +200,12 @@ public void onChildStartedNativeGesture(MotionEvent androidEvent) { return; } ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); - EventDispatcher eventDispatcher = - reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); - mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager != null) { + EventDispatcher eventDispatcher = uiManager.getEventDispatcher(); + mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher); + } } @Override @@ -285,9 +288,12 @@ private void dispatchJSTouchEvent(MotionEvent event) { return; } ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); - EventDispatcher eventDispatcher = - reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); - mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager != null) { + EventDispatcher eventDispatcher = uiManager.getEventDispatcher(); + mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher); + } } @Override @@ -749,10 +755,12 @@ private void emitOrientationChanged(final int newRotation) { } private void emitUpdateDimensionsEvent() { - mReactInstanceManager - .getCurrentReactContext() - .getNativeModule(DeviceInfoModule.class) - .emitUpdateDimensionsEvent(); + DeviceInfoModule deviceInfo = + mReactInstanceManager.getCurrentReactContext().getNativeModule(DeviceInfoModule.class); + + if (deviceInfo != null) { + deviceInfo.emitUpdateDimensionsEvent(); + } } private WritableMap createKeyboardEventPayload( diff --git a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java index 919b5a5f16e9f4..ead07f9512229b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java @@ -130,7 +130,8 @@ public void initialize() { // TODO T59412313 Implement this API on FabricUIManager to use in bridgeless mode if (reactApplicationContext != null && !reactApplicationContext.isBridgeless()) { reactApplicationContext.addLifecycleEventListener(this); - UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class); + UIManagerModule uiManager = + Assertions.assertNotNull(reactApplicationContext.getNativeModule(UIManagerModule.class)); uiManager.addUIManagerListener(this); } } @@ -191,7 +192,9 @@ private NativeAnimatedNodesManager getNodesManager() { ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); if (reactApplicationContext != null) { - UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class); + UIManagerModule uiManager = + Assertions.assertNotNull( + reactApplicationContext.getNativeModule(UIManagerModule.class)); mNodesManager = new NativeAnimatedNodesManager(uiManager); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index 8a99c522ba9fbf..e93b645cb83bd5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -962,19 +962,22 @@ private void handleCaptureHeap(final Responder responder) { return; } JSCHeapCapture heapCapture = mCurrentContext.getNativeModule(JSCHeapCapture.class); - heapCapture.captureHeap( - mApplicationContext.getCacheDir().getPath(), - new JSCHeapCapture.CaptureCallback() { - @Override - public void onSuccess(File capture) { - responder.respond(capture.toString()); - } - @Override - public void onFailure(JSCHeapCapture.CaptureException error) { - responder.error(error.toString()); - } - }); + if (heapCapture != null) { + heapCapture.captureHeap( + mApplicationContext.getCacheDir().getPath(), + new JSCHeapCapture.CaptureCallback() { + @Override + public void onSuccess(File capture) { + responder.respond(capture.toString()); + } + + @Override + public void onFailure(JSCHeapCapture.CaptureException error) { + responder.error(error.toString()); + } + }); + } } private void updateLastErrorInfo( diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java index 4d55cf236d6038..38b4c39cbfafb2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java @@ -8,6 +8,7 @@ package com.facebook.react.fabric; import androidx.annotation.NonNull; +import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.JSIModuleProvider; import com.facebook.react.bridge.JavaScriptContextHolder; import com.facebook.react.bridge.ReactApplicationContext; @@ -83,7 +84,8 @@ public UIManager get() { private FabricUIManager createUIManager(@NonNull EventBeatManager eventBeatManager) { Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricJSIModuleProvider.createUIManager"); - UIManagerModule nativeModule = mReactApplicationContext.getNativeModule(UIManagerModule.class); + UIManagerModule nativeModule = + Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class)); EventDispatcher eventDispatcher = nativeModule.getEventDispatcher(); FabricUIManager fabricUIManager = new FabricUIManager( diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java index 6eae4d7a5c8200..df4b653cc9bfce 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java @@ -75,7 +75,8 @@ public FpsInfo( public FpsDebugFrameCallback(ReactContext reactContext) { mReactContext = reactContext; - mUIManagerModule = reactContext.getNativeModule(UIManagerModule.class); + mUIManagerModule = + Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class)); mDidJSUpdateUiDuringFrameDetector = new DidJSUpdateUiDuringFrameDetector(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIViewOperationQueue.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIViewOperationQueue.java index 9c2eed060a7110..4f8844cba117ad 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIViewOperationQueue.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIViewOperationQueue.java @@ -113,11 +113,14 @@ public EmitOnLayoutEventOperation( @Override public void execute() { - mReactApplicationContext - .getNativeModule(UIManagerModule.class) - .getEventDispatcher() - .dispatchEvent( - OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight)); + UIManagerModule uiManager = mReactApplicationContext.getNativeModule(UIManagerModule.class); + + if (uiManager != null) { + uiManager + .getEventDispatcher() + .dispatchEvent( + OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight)); + } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java index d2f22623a81508..712353eb0a42f2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java @@ -56,9 +56,12 @@ public ReactDrawerLayoutManager() { @Override protected void addEventEmitters(ThemedReactContext reactContext, ReactDrawerLayout view) { - view.addDrawerListener( - new DrawerEventEmitter( - view, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher())); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + if (uiManager == null) { + return; + } + + view.addDrawerListener(new DrawerEventEmitter(view, uiManager.getEventDispatcher())); } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java index ac0cffd04979fa..00e0066b97a125 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java @@ -105,8 +105,12 @@ public void setIdentifier(ReactModalHostView view, int value) {} @Override protected void addEventEmitters(ThemedReactContext reactContext, final ReactModalHostView view) { - final EventDispatcher dispatcher = - reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + if (uiManager == null) { + return; + } + + final EventDispatcher dispatcher = uiManager.getEventDispatcher(); view.setOnRequestCloseListener( new ReactModalHostView.OnRequestCloseListener() { @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java index 110ebdf34be342..d5312acdb18672 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java @@ -414,9 +414,14 @@ private void updateFirstChildView() { new GuardedRunnable(reactContext) { @Override public void runGuarded() { - (getReactContext()) - .getNativeModule(UIManagerModule.class) - .updateNodeSize(viewTag, viewWidth, viewHeight); + UIManagerModule uiManager = + (getReactContext()).getNativeModule(UIManagerModule.class); + + if (uiManager == null) { + return; + } + + uiManager.updateNodeSize(viewTag, viewWidth, viewHeight); } }); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java index b03783ca3fea8f..e5c42e059905e6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java @@ -62,9 +62,13 @@ protected void onAfterUpdateTransaction(ReactPicker view) { @Override protected void addEventEmitters(final ThemedReactContext reactContext, final ReactPicker picker) { - picker.setOnSelectListener( - new PickerEventEmitter( - picker, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher())); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager == null) { + return; + } + + picker.setOnSelectListener(new PickerEventEmitter(picker, uiManager.getEventDispatcher())); } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java index 9215864a9c6391..2bab0c053204a3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java @@ -91,12 +91,17 @@ public long measure( @Override public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) { ReactContext reactContext = (ReactContext) seekbar.getContext(); - reactContext - .getNativeModule(UIManagerModule.class) - .getEventDispatcher() - .dispatchEvent( - new ReactSliderEvent( - seekbar.getId(), ((ReactSlider) seekbar).toRealProgress(progress), fromUser)); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager != null) { + uiManager + .getEventDispatcher() + .dispatchEvent( + new ReactSliderEvent( + seekbar.getId(), + ((ReactSlider) seekbar).toRealProgress(progress), + fromUser)); + } } @Override @@ -105,13 +110,16 @@ public void onStartTrackingTouch(SeekBar seekbar) {} @Override public void onStopTrackingTouch(SeekBar seekbar) { ReactContext reactContext = (ReactContext) seekbar.getContext(); - reactContext - .getNativeModule(UIManagerModule.class) - .getEventDispatcher() - .dispatchEvent( - new ReactSlidingCompleteEvent( - seekbar.getId(), - ((ReactSlider) seekbar).toRealProgress(seekbar.getProgress()))); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager != null) { + uiManager + .getEventDispatcher() + .dispatchEvent( + new ReactSlidingCompleteEvent( + seekbar.getId(), + ((ReactSlider) seekbar).toRealProgress(seekbar.getProgress()))); + } } }; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java index bd5336ad6b92d8..0a6b8c8762e740 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java @@ -80,8 +80,14 @@ public long measure( @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ReactContext reactContext = (ReactContext) buttonView.getContext(); - reactContext - .getNativeModule(UIManagerModule.class) + + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + + if (uiManager == null) { + return; + } + + uiManager .getEventDispatcher() .dispatchEvent(new ReactSwitchEvent(buttonView.getId(), isChecked)); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java index 68bfc3455e7fe6..3d7988087ffaeb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java @@ -25,6 +25,7 @@ import androidx.appcompat.widget.AppCompatTextView; import androidx.appcompat.widget.TintContextWrapper; import com.facebook.common.logging.FLog; +import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.WritableArray; @@ -118,7 +119,8 @@ protected void onLayout( } ReactContext reactContext = getReactContext(); - UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + UIManagerModule uiManager = + Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class)); Spanned text = (Spanned) getText(); Layout layout = getLayout(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index 86ae295b262c43..1638b75147d78f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -609,7 +609,9 @@ private void setIntrinsicContentSize() { ReactContext reactContext = getReactContext(this); final ReactTextInputLocalData localData = new ReactTextInputLocalData(this); UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); - uiManager.setViewLocalData(getId(), localData); + if (uiManager != null) { + uiManager.setViewLocalData(getId(), localData); + } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java index b56789ddf06e67..47b4391c327f11 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java @@ -12,6 +12,7 @@ import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import androidx.annotation.Nullable; +import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.ReactContext; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.events.EventDispatcher; @@ -62,7 +63,9 @@ class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper { public ReactEditTextInputConnectionWrapper( InputConnection target, final ReactContext reactContext, final ReactEditText editText) { super(target, false); - mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); + mEventDispatcher = + Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class)) + .getEventDispatcher(); mEditText = editText; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 2f228cd0566005..8b5b13a6a5be75 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -1090,18 +1090,23 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) { private class ReactContentSizeWatcher implements ContentSizeWatcher { private ReactEditText mEditText; - private EventDispatcher mEventDispatcher; + private @Nullable EventDispatcher mEventDispatcher; private int mPreviousContentWidth = 0; private int mPreviousContentHeight = 0; public ReactContentSizeWatcher(ReactEditText editText) { mEditText = editText; ReactContext reactContext = getReactContext(editText); - mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); + UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); + mEventDispatcher = uiManager != null ? uiManager.getEventDispatcher() : null; } @Override public void onLayout() { + if (mEventDispatcher == null) { + return; + } + int contentWidth = mEditText.getWidth(); int contentHeight = mEditText.getHeight(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java index cad4db8b4f14b5..beb51d86fb4e39 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java @@ -233,10 +233,14 @@ public void setFocusable(final ReactViewGroup view, boolean focusable) { new View.OnClickListener() { @Override public void onClick(View v) { - final EventDispatcher mEventDispatcher = - ((ReactContext) view.getContext()) - .getNativeModule(UIManagerModule.class) - .getEventDispatcher(); + UIManagerModule uiManager = + ((ReactContext) view.getContext()).getNativeModule(UIManagerModule.class); + + if (uiManager == null) { + return; + } + + final EventDispatcher mEventDispatcher = uiManager.getEventDispatcher(); mEventDispatcher.dispatchEvent(new ViewGroupClickEvent(view.getId())); } }); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java index 2c6fc370d0d15a..398751a8f6e79e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java @@ -13,6 +13,7 @@ import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import com.facebook.common.logging.FLog; +import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.ReactContext; import com.facebook.react.common.ReactConstants; import com.facebook.react.uimanager.UIManagerModule; @@ -154,7 +155,9 @@ public void onPageScrollStateChanged(int state) { public ReactViewPager(ReactContext reactContext) { super(reactContext); - mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); + mEventDispatcher = + Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class)) + .getEventDispatcher(); mIsCurrentItemFromJs = false; setOnPageChangeListener(new PageChangeListener()); setAdapter(new Adapter());