From 020ec7bfab57793431a5290cf8069e44dfb90fcd Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Mon, 21 Nov 2022 08:36:36 -0800 Subject: [PATCH] Adding Dev Loading controller without activity for VR (#35392) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35392 Changelog: [General][Added] - For supporting Dev Loading View across platforms, adding the DevLoadingViewController without an activity/context. Reviewed By: rshest Differential Revision: D40947239 fbshipit-source-id: fcc524b33d9df174cd0d11ae6c1a00a04f3042bc --- .../facebook/react/ReactInstanceManager.java | 7 ++++-- .../react/ReactInstanceManagerBuilder.java | 12 ++++++++- .../com/facebook/react/ReactNativeHost.java | 9 +++++++ .../devsupport/BridgeDevSupportManager.java | 7 ++++-- ... DefaultDevLoadingViewImplementation.java} | 9 ++++--- .../DefaultDevSupportManagerFactory.java | 11 +++++--- .../devsupport/DevSupportManagerBase.java | 25 +++++++++++-------- .../devsupport/DevSupportManagerFactory.java | 4 ++- 8 files changed, 62 insertions(+), 22 deletions(-) rename ReactAndroid/src/main/java/com/facebook/react/devsupport/{DevLoadingViewController.java => DefaultDevLoadingViewImplementation.java} (92%) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 6e7a9af39bd1c6..554144a8a8913a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -82,6 +82,7 @@ import com.facebook.react.devsupport.DevSupportManagerFactory; import com.facebook.react.devsupport.ReactInstanceDevHelper; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.PackagerStatusCallback; import com.facebook.react.devsupport.interfaces.RedBoxHandler; @@ -233,7 +234,8 @@ public static ReactInstanceManagerBuilder builder() { @Nullable JSIModulePackage jsiModulePackage, @Nullable Map customPackagerCommandHandlers, @Nullable ReactPackageTurboModuleManagerDelegate.Builder tmmDelegateBuilder, - @Nullable SurfaceDelegateFactory surfaceDelegateFactory) { + @Nullable SurfaceDelegateFactory surfaceDelegateFactory, + @Nullable DevLoadingViewManager devLoadingViewManager) { FLog.d(TAG, "ReactInstanceManager.ctor()"); initializeSoLoaderIfNecessary(applicationContext); @@ -261,7 +263,8 @@ public static ReactInstanceManagerBuilder builder() { devBundleDownloadListener, minNumShakes, customPackagerCommandHandlers, - surfaceDelegateFactory); + surfaceDelegateFactory, + devLoadingViewManager); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); mBridgeIdleDebugListener = bridgeIdleDebugListener; mLifecycleState = initialLifecycleState; diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index 68ac1ed63451e1..2715b01c75a87b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -28,6 +28,7 @@ import com.facebook.react.devsupport.DefaultDevSupportManagerFactory; import com.facebook.react.devsupport.DevSupportManagerFactory; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; import com.facebook.react.jscexecutor.JSCExecutor; @@ -67,6 +68,7 @@ public class ReactInstanceManagerBuilder { private @Nullable Map mCustomPackagerCommandHandlers; private @Nullable ReactPackageTurboModuleManagerDelegate.Builder mTMMDelegateBuilder; private @Nullable SurfaceDelegateFactory mSurfaceDelegateFactory; + private @Nullable DevLoadingViewManager mDevLoadingViewManager; private JSEngineResolutionAlgorithm jsEngineResolutionAlgorithm = null; /* package protected */ ReactInstanceManagerBuilder() {} @@ -216,6 +218,13 @@ public ReactInstanceManagerBuilder setSurfaceDelegateFactory( return this; } + /** Sets the Dev Loading View Manager. */ + public ReactInstanceManagerBuilder setDevLoadingViewManager( + @Nullable DevLoadingViewManager devLoadingViewManager) { + mDevLoadingViewManager = devLoadingViewManager; + return this; + } + /** * Sets the initial lifecycle state of the host. For example, if the host is already resumed at * creation time, we wouldn't expect an onResume call until we get an onPause call. @@ -337,7 +346,8 @@ public ReactInstanceManager build() { mJSIModulesPackage, mCustomPackagerCommandHandlers, mTMMDelegateBuilder, - mSurfaceDelegateFactory); + mSurfaceDelegateFactory, + mDevLoadingViewManager); } private JavaScriptExecutorFactory getDefaultJSExecutorFactory( diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java b/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java index d030019b035373..11880869d94349 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java @@ -18,6 +18,7 @@ import com.facebook.react.common.SurfaceDelegate; import com.facebook.react.common.SurfaceDelegateFactory; import com.facebook.react.devsupport.DevSupportManagerFactory; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; import java.util.List; @@ -71,6 +72,7 @@ protected ReactInstanceManager createReactInstanceManager() { .setJSMainModulePath(getJSMainModuleName()) .setUseDeveloperSupport(getUseDeveloperSupport()) .setDevSupportManagerFactory(getDevSupportManagerFactory()) + .setDevLoadingViewManager(getDevLoadingViewManager()) .setRequireActivity(getShouldRequireActivity()) .setSurfaceDelegateFactory(getSurfaceDelegateFactory()) .setLazyViewManagersEnabled(getLazyViewManagersEnabled()) @@ -149,6 +151,13 @@ public SurfaceDelegateFactory getSurfaceDelegateFactory() { }; } + /** + * Get the {@link DevLoadingViewManager}. Override this to use a custom dev loading view manager + */ + protected @Nullable DevLoadingViewManager getDevLoadingViewManager() { + return null; + } + /** * Returns the name of the main module. Determines the URL used to fetch the JS bundle from Metro. * It is only used when dev support is enabled. This is the first file to be executed once the 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 205de052d7cefc..fdec95c5696193 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BridgeDevSupportManager.java @@ -25,6 +25,7 @@ import com.facebook.react.common.SurfaceDelegateFactory; import com.facebook.react.common.futures.SimpleSettableFuture; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevOptionHandler; import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback; import com.facebook.react.devsupport.interfaces.RedBoxHandler; @@ -74,7 +75,8 @@ public BridgeDevSupportManager( @Nullable DevBundleDownloadListener devBundleDownloadListener, int minNumShakes, @Nullable Map customPackagerCommandHandlers, - @Nullable SurfaceDelegateFactory surfaceDelegateFactory) { + @Nullable SurfaceDelegateFactory surfaceDelegateFactory, + @Nullable DevLoadingViewManager devLoadingViewManager) { super( applicationContext, reactInstanceManagerHelper, @@ -84,7 +86,8 @@ public BridgeDevSupportManager( devBundleDownloadListener, minNumShakes, customPackagerCommandHandlers, - surfaceDelegateFactory); + surfaceDelegateFactory, + devLoadingViewManager); if (getDevSettings().isStartSamplingProfilerOnInit()) { // Only start the profiler. If its already running, there is an error diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java similarity index 92% rename from ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java rename to ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java index 95fafd3c5e2b65..ef203fbf125899 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java @@ -25,8 +25,11 @@ import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import java.util.Locale; -/** Controller to display loading messages on top of the screen. All methods are thread safe. */ -public class DevLoadingViewController implements DevLoadingViewManager { +/** + * Default implementation of Dev Loading View Manager to display loading messages on top of the + * screen. All methods are thread safe. + */ +public class DefaultDevLoadingViewImplementation implements DevLoadingViewManager { private static boolean sEnabled = true; private final ReactInstanceDevHelper mReactInstanceManagerHelper; private @Nullable TextView mDevLoadingView; @@ -36,7 +39,7 @@ public static void setDevLoadingEnabled(boolean enabled) { sEnabled = enabled; } - public DevLoadingViewController(ReactInstanceDevHelper reactInstanceManagerHelper) { + public DefaultDevLoadingViewImplementation(ReactInstanceDevHelper reactInstanceManagerHelper) { mReactInstanceManagerHelper = reactInstanceManagerHelper; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.java index a7bd81ad600ec9..0a9a2338ceb356 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.java @@ -11,6 +11,7 @@ import androidx.annotation.Nullable; import com.facebook.react.common.SurfaceDelegateFactory; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; import com.facebook.react.packagerconnection.RequestHandler; @@ -46,6 +47,7 @@ public DevSupportManager create( null, minNumShakes, null, + null, null); } @@ -59,7 +61,8 @@ public DevSupportManager create( @Nullable DevBundleDownloadListener devBundleDownloadListener, int minNumShakes, @Nullable Map customPackagerCommandHandlers, - @Nullable SurfaceDelegateFactory surfaceDelegateFactory) { + @Nullable SurfaceDelegateFactory surfaceDelegateFactory, + @Nullable DevLoadingViewManager devLoadingViewManager) { if (!enableOnCreate) { return new DisabledDevSupportManager(); } @@ -87,7 +90,8 @@ public DevSupportManager create( DevBundleDownloadListener.class, int.class, Map.class, - SurfaceDelegateFactory.class); + SurfaceDelegateFactory.class, + DevLoadingViewManager.class); return (DevSupportManager) constructor.newInstance( applicationContext, @@ -98,7 +102,8 @@ public DevSupportManager create( devBundleDownloadListener, minNumShakes, customPackagerCommandHandlers, - surfaceDelegateFactory); + surfaceDelegateFactory, + devLoadingViewManager); } catch (Exception e) { return new PerftestDevSupportManager(applicationContext); } 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 2bf304a6b1ea4c..c5ea09166e882d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -46,6 +46,7 @@ import com.facebook.react.devsupport.DevServerHelper.PackagerCommandListener; import com.facebook.react.devsupport.interfaces.BundleLoadCallback; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevOptionHandler; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.ErrorCustomizer; @@ -95,7 +96,7 @@ public interface CallbackWithBundleLoader { private final File mJSBundleDownloadedFile; private final File mJSSplitBundlesDir; private final DefaultJSExceptionHandler mDefaultJSExceptionHandler; - private final DevLoadingViewController mDevLoadingViewController; + private final DevLoadingViewManager mDevLoadingViewManager; private @Nullable SurfaceDelegate mRedBoxSurfaceDelegate; private @Nullable AlertDialog mDevOptionsDialog; @@ -132,7 +133,8 @@ public DevSupportManagerBase( @Nullable DevBundleDownloadListener devBundleDownloadListener, int minNumShakes, @Nullable Map customPackagerCommandHandlers, - @Nullable SurfaceDelegateFactory surfaceDelegateFactory) { + @Nullable SurfaceDelegateFactory surfaceDelegateFactory, + @Nullable DevLoadingViewManager devLoadingViewManager) { mReactInstanceDevHelper = reactInstanceDevHelper; mApplicationContext = applicationContext; mJSAppBundleName = packagerPathForJSBundleName; @@ -206,7 +208,10 @@ public void onReceive(Context context, Intent intent) { setDevSupportEnabled(enableOnCreate); mRedBoxHandler = redBoxHandler; - mDevLoadingViewController = new DevLoadingViewController(reactInstanceDevHelper); + mDevLoadingViewManager = + devLoadingViewManager != null + ? devLoadingViewManager + : new DefaultDevLoadingViewImplementation(reactInstanceDevHelper); mSurfaceDelegateFactory = surfaceDelegateFactory; }; @@ -766,7 +771,7 @@ private void showDevLoadingViewForUrl(String bundleUrl) { } int port = parsedURL.getPort() != -1 ? parsedURL.getPort() : parsedURL.getDefaultPort(); - mDevLoadingViewController.showMessage( + mDevLoadingViewManager.showMessage( mApplicationContext.getString( R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + port)); mDevLoadingViewVisible = true; @@ -778,14 +783,14 @@ protected void showDevLoadingViewForRemoteJSEnabled() { return; } - mDevLoadingViewController.showMessage( + mDevLoadingViewManager.showMessage( mApplicationContext.getString(R.string.catalyst_debug_connecting)); mDevLoadingViewVisible = true; } @UiThread protected void hideDevLoadingView() { - mDevLoadingViewController.hide(); + mDevLoadingViewManager.hide(); mDevLoadingViewVisible = false; } @@ -827,7 +832,7 @@ public void run() { @Override public void onProgress( @Nullable String status, @Nullable Integer done, @Nullable Integer total) { - mDevLoadingViewController.updateProgress(status, done, total); + mDevLoadingViewManager.updateProgress(status, done, total); } @Override @@ -983,7 +988,7 @@ public void onProgress( @Nullable final String status, @Nullable final Integer done, @Nullable final Integer total) { - mDevLoadingViewController.updateProgress(status, done, total); + mDevLoadingViewManager.updateProgress(status, done, total); if (mBundleDownloadListener != null) { mBundleDownloadListener.onProgress(status, done, total); } @@ -1125,7 +1130,7 @@ private void reload() { // show the dev loading if it should be if (mDevLoadingViewVisible) { - mDevLoadingViewController.showMessage("Reloading..."); + mDevLoadingViewManager.showMessage("Reloading..."); } mDevServerHelper.openPackagerConnection( @@ -1206,7 +1211,7 @@ public void run() { hideDevOptionsDialog(); // hide loading view - mDevLoadingViewController.hide(); + mDevLoadingViewManager.hide(); mDevServerHelper.closePackagerConnection(); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerFactory.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerFactory.java index 3b1c71dc75a986..fea7416c11c259 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerFactory.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerFactory.java @@ -11,6 +11,7 @@ import androidx.annotation.Nullable; import com.facebook.react.common.SurfaceDelegateFactory; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; +import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; import com.facebook.react.packagerconnection.RequestHandler; @@ -26,5 +27,6 @@ DevSupportManager create( @Nullable DevBundleDownloadListener devBundleDownloadListener, int minNumShakes, @Nullable Map customPackagerCommandHandlers, - @Nullable SurfaceDelegateFactory surfaceDelegateFactory); + @Nullable SurfaceDelegateFactory surfaceDelegateFactory, + @Nullable DevLoadingViewManager devLoadingViewManager); }