Skip to content

Commit

Permalink
Convert Bridge-only checks to overridable functions
Browse files Browse the repository at this point in the history
Summary:
Convert Bridge-only checks to overridable functions and make Bridgeless override them in ReactSurfaceView so that the checks will work for Bridgeless as well.

Issue fixed:
https://fb.workplace.com/groups/rn.support/permalink/24231137939841493/

Changelog:
[Android][Changed] - Convert Bridge-only calls to overridable functions

Reviewed By: javache

Differential Revision: D43063348

fbshipit-source-id: a1c181d27c1669f6033f3fb783c5a668b7c2585b
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Feb 7, 2023
1 parent 9d98e2c commit 1058bb8
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -367,17 +357,15 @@ 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;
}
if (mJSTouchDispatcher == null) {
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) {
Expand Down Expand Up @@ -412,7 +400,7 @@ private boolean isFabric() {
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mIsAttachedToInstance) {
if (isViewAttachedToReactInstance()) {
removeOnGlobalLayoutListener();
getViewTreeObserver().addOnGlobalLayoutListener(getCustomGlobalLayoutListener());
}
Expand All @@ -421,7 +409,7 @@ protected void onAttachedToWindow() {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mIsAttachedToInstance) {
if (isViewAttachedToReactInstance()) {
removeOnGlobalLayoutListener();
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -585,7 +573,7 @@ private void updateRootLayoutSpecs(
return;
}

final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext();
final ReactContext reactApplicationContext = getCurrentReactContext();

if (reactApplicationContext != null) {
@Nullable
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1058bb8

Please sign in to comment.