From de255528e0b65980d5531c477fb2b32bbbcd9b75 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 3 Aug 2021 10:54:25 -0700 Subject: [PATCH] Allow resolving view from FabricUIManager Summary: Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D30043188 fbshipit-source-id: d8675754b29fb58a28a06777f602098da6dbc27f --- .../java/com/facebook/react/bridge/UIManager.java | 10 ++++++++++ .../com/facebook/react/fabric/FabricUIManager.java | 8 ++++++++ .../fabric/mounting/SurfaceMountingManager.java | 14 ++++++++++++-- .../facebook/react/uimanager/UIManagerModule.java | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.java index 40776a84e829b3..50ab07a1b3b6fe 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.java @@ -117,6 +117,16 @@ void updateRootLayoutSpecs( */ void removeUIManagerEventListener(UIManagerListener listener); + /** + * Resolves a view based on its reactTag. Do not mutate properties on this view that are already + * managed by React, as there are no guarantees this changes will be preserved. + * + * @throws IllegalViewOperationException if tag could not be resolved. + * @param reactTag tag + * @return view if found + */ + View resolveView(int reactTag); + /** * This method dispatches events from RN Android code to JS. The delivery of this event will not * be queued in EventDispatcher class. diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index ea132670201f94..08f473d849a68e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -804,6 +804,14 @@ public void updateRootLayoutSpecs( doLeftAndRightSwapInRTL); } + @Override + public View resolveView(int reactTag) { + UiThreadUtil.assertOnUiThread(); + + SurfaceMountingManager surfaceManager = mMountingManager.getSurfaceManagerForView(reactTag); + return surfaceManager == null ? null : surfaceManager.getView(reactTag); + } + @Override public void receiveEvent(int reactTag, String eventName, @Nullable WritableMap params) { receiveEvent(View.NO_ID, reactTag, eventName, params); diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java index ca18fd89c69ff2..2727d5d047b7a6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java @@ -928,8 +928,18 @@ public void preallocateView( return viewState == null ? null : viewState.mEventEmitter; } - @NonNull - private ViewState getViewState(int tag) { + @UiThread + public View getView(int reactTag) { + ViewState state = getNullableViewState(reactTag); + View view = state == null ? null : state.mView; + if (view == null) { + throw new IllegalViewOperationException( + "Trying to resolve view with tag " + reactTag + " which doesn't exist"); + } + return view; + } + + private @NonNull ViewState getViewState(int tag) { ViewState viewState = mTagToViewState.get(tag); if (viewState == null) { throw new RetryableMountingLayerException("Unable to find viewState for tag " + tag); diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 6d745f384d71b6..b034d1c8faf87c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -955,6 +955,7 @@ public void onConfigurationChanged(Configuration newConfig) {} public void onLowMemory() {} } + @Override public View resolveView(int tag) { UiThreadUtil.assertOnUiThread(); return mUIImplementation