From 4923a0997b1a8c827b11ec15e45e6ce00f398d99 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Fri, 30 Dec 2022 14:39:20 -0800 Subject: [PATCH] Adding show/hide Native Implementation of Dev Loading Module (#35746) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35746 Changelog: [Android][Added] - For supporting Dev Loading View across multiple platforms, adding native implementation for showMessage() & hide() of Dev Loading Module Reviewed By: rshest Differential Revision: D42286425 fbshipit-source-id: b2aa160583bc86965501f3ed3027152bb0b47af0 --- .../facebook/react/bridge/ReactContext.java | 4 ++++ .../devsupport/BridgeDevSupportManager.java | 13 +++++++++++ .../facebook/react/modules/devloading/BUCK | 5 ++++ .../modules/devloading/DevLoadingModule.java | 23 ++++++++++++++++--- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index 77ee2489413dc6..ea6040e1cd245c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -444,6 +444,10 @@ public JSExceptionHandler getExceptionHandler() { return mExceptionHandlerWrapper; } + public JSExceptionHandler getJSExceptionHandler() { + return mJSExceptionHandler; + } + public boolean hasCurrentActivity() { return mCurrentActivity != null && mCurrentActivity.get() != null; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java index fdec95c5696193..4d7110c91eb954 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java @@ -65,6 +65,8 @@ */ public final class BridgeDevSupportManager extends DevSupportManagerBase { private boolean mIsSamplingProfilerEnabled = false; + private ReactInstanceDevHelper mReactInstanceManagerHelper; + private @Nullable DevLoadingViewManager mDevLoadingViewManager; public BridgeDevSupportManager( Context applicationContext, @@ -89,6 +91,9 @@ public BridgeDevSupportManager( surfaceDelegateFactory, devLoadingViewManager); + mReactInstanceManagerHelper = reactInstanceManagerHelper; + mDevLoadingViewManager = devLoadingViewManager; + if (getDevSettings().isStartSamplingProfilerOnInit()) { // Only start the profiler. If its already running, there is an error if (!mIsSamplingProfilerEnabled) { @@ -128,6 +133,14 @@ public void onOptionSelected() { } } + public DevLoadingViewManager getDevLoadingViewManager() { + return mDevLoadingViewManager; + } + + public ReactInstanceDevHelper getReactInstanceManagerHelper() { + return mReactInstanceManagerHelper; + } + @Override protected String getUniqueTag() { return "Bridge"; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/BUCK index ad5bec7029b9a2..f03e9081b66e54 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/BUCK @@ -8,6 +8,9 @@ rn_android_library( "pfh:ReactNative_CommonInfrastructurePlaceholder", ], language = "JAVA", + provided_deps = [ + react_native_dep("third-party/android/androidx:annotation"), + ], visibility = [ "PUBLIC", ], @@ -15,6 +18,8 @@ rn_android_library( react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/jsr-305:jsr-305"), react_native_target("java/com/facebook/react/bridge:bridge"), + react_native_target("java/com/facebook/react/devsupport:devsupport"), + react_native_target("java/com/facebook/react/devsupport:interfaces"), react_native_target("java/com/facebook/react/common:common"), react_native_target("java/com/facebook/react/module/annotations:annotations"), ], diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/DevLoadingModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/DevLoadingModule.java index 3036dceee976ab..8ad1004f414b32 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/DevLoadingModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devloading/DevLoadingModule.java @@ -7,19 +7,36 @@ package com.facebook.react.modules.devloading; -import android.util.Log; +import androidx.annotation.Nullable; import com.facebook.fbreact.specs.NativeDevLoadingViewSpec; +import com.facebook.react.bridge.JSExceptionHandler; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.devsupport.BridgeDevSupportManager; +import com.facebook.react.devsupport.DefaultDevLoadingViewImplementation; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.module.annotations.ReactModule; /** {@link NativeModule} that allows JS to show dev loading view. */ @ReactModule(name = NativeDevLoadingViewSpec.NAME) public class DevLoadingModule extends NativeDevLoadingViewSpec { + private final JSExceptionHandler mJSExceptionHandler; + private @Nullable DevLoadingViewManager mDevLoadingViewManager; + public DevLoadingModule(ReactApplicationContext reactContext) { super(reactContext); + mJSExceptionHandler = reactContext.getJSExceptionHandler(); + if (mJSExceptionHandler != null && mJSExceptionHandler instanceof BridgeDevSupportManager) { + mDevLoadingViewManager = + ((BridgeDevSupportManager) mJSExceptionHandler).getDevLoadingViewManager(); + mDevLoadingViewManager = + mDevLoadingViewManager != null + ? mDevLoadingViewManager + : new DefaultDevLoadingViewImplementation( + ((BridgeDevSupportManager) mJSExceptionHandler).getReactInstanceManagerHelper()); + } } @Override @@ -29,7 +46,7 @@ public void showMessage(final String message, final Double color, final Double b new Runnable() { @Override public void run() { - Log.w(NAME, "Showing Message in DevLoadingModule java."); + mDevLoadingViewManager.showMessage(message); } }); } @@ -41,7 +58,7 @@ public void hide() { new Runnable() { @Override public void run() { - Log.w(NAME, "Hiding Message in DevLoadingModule java."); + mDevLoadingViewManager.hide(); } }); }