diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index 9f4339bf490389..1ac3df2b2c08f5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -179,7 +179,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mWasMeasured = true; // Check if we were waiting for onMeasure to attach the root view. - if (mReactInstanceManager != null && !mIsAttachedToInstance) { + if (hasActiveReactInstance() && !isViewAttachedToReactInstance()) { attachToReactInstanceManager(); } else if (measureSpecsUpdated || mLastWidth != width || mLastHeight != height) { updateRootLayoutSpecs(true, mWidthMeasureSpec, mHeightMeasureSpec); @@ -203,7 +203,7 @@ public void onChildStartedNativeGesture(View childView, MotionEvent ev) { if (!isDispatcherReady()) { return; } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + ReactContext reactContext = getCurrentReactContext(); UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType()); if (uiManager != null) { @@ -220,7 +220,7 @@ public void onChildEndedNativeGesture(View childView, MotionEvent ev) { if (!isDispatcherReady()) { return; } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + ReactContext reactContext = getCurrentReactContext(); UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType()); if (uiManager != null) { @@ -233,9 +233,7 @@ public void onChildEndedNativeGesture(View childView, MotionEvent ev) { } private boolean isDispatcherReady() { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached"); return false; } @@ -303,9 +301,7 @@ protected void dispatchDraw(Canvas canvas) { @Override public boolean dispatchKeyEvent(KeyEvent ev) { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w(TAG, "Unable to handle key event as the catalyst instance has not been attached"); return super.dispatchKeyEvent(ev); } @@ -315,9 +311,7 @@ public boolean dispatchKeyEvent(KeyEvent ev) { @Override protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w( TAG, "Unable to handle focus changed event as the catalyst instance has not been attached"); @@ -330,9 +324,7 @@ protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyF @Override public void requestChildFocus(View child, View focused) { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w( TAG, "Unable to handle child focus changed event as the catalyst instance has not been attached"); @@ -344,9 +336,7 @@ public void requestChildFocus(View child, View focused) { } protected void dispatchJSPointerEvent(MotionEvent event, boolean isCapture) { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached"); return; } @@ -357,7 +347,7 @@ protected void dispatchJSPointerEvent(MotionEvent event, boolean isCapture) { FLog.w(TAG, "Unable to dispatch pointer events to JS before the dispatcher is available"); return; } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + ReactContext reactContext = getCurrentReactContext(); UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType()); if (uiManager != null) { @@ -367,9 +357,7 @@ protected void dispatchJSPointerEvent(MotionEvent event, boolean isCapture) { } protected void dispatchJSTouchEvent(MotionEvent event) { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached"); return; } @@ -377,7 +365,7 @@ protected void dispatchJSTouchEvent(MotionEvent event) { FLog.w(TAG, "Unable to dispatch touch to JS before the dispatcher is available"); return; } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + ReactContext reactContext = getCurrentReactContext(); UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType()); if (uiManager != null) { @@ -412,7 +400,7 @@ private boolean isFabric() { @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - if (mIsAttachedToInstance) { + if (isViewAttachedToReactInstance()) { removeOnGlobalLayoutListener(); getViewTreeObserver().addOnGlobalLayoutListener(getCustomGlobalLayoutListener()); } @@ -421,7 +409,7 @@ protected void onAttachedToWindow() { @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - if (mIsAttachedToInstance) { + if (isViewAttachedToReactInstance()) { removeOnGlobalLayoutListener(); } } @@ -571,7 +559,7 @@ public AtomicInteger getState() { private void updateRootLayoutSpecs( boolean measureSpecsChanged, final int widthMeasureSpec, final int heightMeasureSpec) { ReactMarker.logMarker(ReactMarkerConstants.ROOT_VIEW_UPDATE_LAYOUT_SPECS_START); - if (mReactInstanceManager == null) { + if (!hasActiveReactInstance()) { ReactMarker.logMarker(ReactMarkerConstants.ROOT_VIEW_UPDATE_LAYOUT_SPECS_END); FLog.w(TAG, "Unable to update root layout specs for uninitialized ReactInstanceManager"); return; @@ -585,7 +573,7 @@ private void updateRootLayoutSpecs( return; } - final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext(); + final ReactContext reactApplicationContext = getCurrentReactContext(); if (reactApplicationContext != null) { @Nullable @@ -629,8 +617,8 @@ public void unmountReactApplication() { // to be committed via the Scheduler, which will cause mounting instructions // to be queued up and synchronously executed to delete and remove // all the views in the hierarchy. - if (mReactInstanceManager != null) { - final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext(); + if (hasActiveReactInstance()) { + final ReactContext reactApplicationContext = getCurrentReactContext(); if (reactApplicationContext != null && isFabric()) { @Nullable UIManager uiManager = @@ -729,11 +717,11 @@ public void setAppProperties(@Nullable Bundle appProperties) { public void runApplication() { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication"); try { - if (mReactInstanceManager == null || !mIsAttachedToInstance) { + if (!hasActiveReactInstance() || !isViewAttachedToReactInstance()) { return; } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + ReactContext reactContext = getCurrentReactContext(); if (reactContext == null) { return; } @@ -853,12 +841,12 @@ public void setRootViewTag(int rootViewTag) { @Override public void handleException(final Throwable t) { - if (mReactInstanceManager == null || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext()) { throw new RuntimeException(t); } Exception e = new IllegalViewOperationException(t.getMessage(), this, t); - mReactInstanceManager.getCurrentReactContext().handleException(e); + getCurrentReactContext().handleException(e); } public void setIsFabric(boolean isFabric) { @@ -876,14 +864,30 @@ public ReactInstanceManager getReactInstanceManager() { } /* package */ void sendEvent(String eventName, @Nullable WritableMap params) { - if (mReactInstanceManager != null) { - mReactInstanceManager - .getCurrentReactContext() + if (hasActiveReactInstance()) { + getCurrentReactContext() .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(eventName, params); } } + public boolean hasActiveReactInstance() { + return mReactInstanceManager != null; + } + + public boolean hasActiveReactContext() { + return mReactInstanceManager != null && mReactInstanceManager.getCurrentReactContext() != null; + } + + @Nullable + public ReactContext getCurrentReactContext() { + return mReactInstanceManager.getCurrentReactContext(); + } + + public boolean isViewAttachedToReactInstance() { + return mIsAttachedToInstance; + } + private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { private final Rect mVisibleViewArea; private final int mMinKeyboardHeightDetected; @@ -900,9 +904,7 @@ private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLay @Override public void onGlobalLayout() { - if (mReactInstanceManager == null - || !mIsAttachedToInstance - || mReactInstanceManager.getCurrentReactContext() == null) { + if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) { return; } @@ -1061,7 +1063,7 @@ private void emitOrientationChanged(final int newRotation) { private void emitUpdateDimensionsEvent() { DeviceInfoModule deviceInfo = - mReactInstanceManager.getCurrentReactContext().getNativeModule(DeviceInfoModule.class); + getCurrentReactContext().getNativeModule(DeviceInfoModule.class); if (deviceInfo != null) { deviceInfo.emitUpdateDimensionsEvent();