From 72b517049aa91b0e9a1ce2914fb3f6e1f3c09a8f Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Mon, 14 Nov 2016 11:42:25 -0800 Subject: [PATCH] Add a debug support to load js bundle from external URL Summary: Code refactoring on the dev support class. The idea is to make the code more modular. Reviewed By: mhorowitz Differential Revision: D4164676 fbshipit-source-id: 0d29bdaf927cd0e9f399fe6f8e46a16dfa65fb69 --- .../react/devsupport/DevServerHelper.java | 19 +++++----- .../react/devsupport/DevSupportManager.java | 1 + .../devsupport/DevSupportManagerImpl.java | 35 ++++++++++++------- .../devsupport/DisabledDevSupportManager.java | 5 +++ 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java index 5ac7ebc90..cb0f509cf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -222,16 +222,19 @@ public class DevServerHelper { return String.format(Locale.US, RESOURCE_URL_FORMAT, host, resourcePath); } + public String getDevServerBundleURL(final String jsModulePath) { + return createBundleURL( + getDebugServerHost(), + jsModulePath, + getDevMode(), + getHMR(), + getJSMinifyMode()); + } + public void downloadBundleFromURL( final BundleDownloadCallback callback, - final String jsModulePath, - final File outputFile) { - final String bundleURL = createBundleURL( - getDebugServerHost(), - jsModulePath, - getDevMode(), - getHMR(), - getJSMinifyMode()); + final File outputFile, + final String bundleURL) { final Request request = new Request.Builder() .url(bundleURL) .build(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java index 26623ff45..a0e3f6d06 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java @@ -45,6 +45,7 @@ public interface DevSupportManager extends NativeModuleCallExceptionHandler { boolean hasUpToDateJSBundleInCache(); void reloadSettings(); void handleReloadJS(); + void reloadJSFromServer(final String bundleURL); void isPackagerRunning(DevServerHelper.PackagerStatusCallback callback); @Nullable File downloadBundleResourceFromUrlSync( final String resourceURL, diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java index 739116ede..bb163b31f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java @@ -635,18 +635,12 @@ public class DevSupportManagerImpl implements DevSupportManager, PackagerCommand mRedBoxDialog.dismiss(); } - AlertDialog dialog = new AlertDialog.Builder(mApplicationContext) - .setTitle(R.string.catalyst_jsload_title) - .setMessage(mApplicationContext.getString( - mDevSettings.isRemoteJSDebugEnabled() ? R.string.catalyst_remotedbg_message : R.string.catalyst_jsload_message)) - .create(); - dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); - dialog.show(); - if (mDevSettings.isRemoteJSDebugEnabled()) { - reloadJSInProxyMode(dialog); + reloadJSInProxyMode(showProgressDialog()); } else { - reloadJSFromServer(dialog); + String bundleURL = + mDevServerHelper.getDevServerBundleURL(Assertions.assertNotNull(mJSAppBundleName)); + reloadJSFromServer(bundleURL); } } @@ -741,7 +735,22 @@ public class DevSupportManagerImpl implements DevSupportManager, PackagerCommand }; } - private void reloadJSFromServer(final AlertDialog progressDialog) { + private AlertDialog showProgressDialog() { + AlertDialog dialog = new AlertDialog.Builder(mApplicationContext) + .setTitle(R.string.catalyst_jsload_title) + .setMessage(mApplicationContext.getString( + mDevSettings.isRemoteJSDebugEnabled() ? + R.string.catalyst_remotedbg_message : + R.string.catalyst_jsload_message)) + .create(); + dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + dialog.show(); + return dialog; + } + + public void reloadJSFromServer(final String bundleURL) { + final AlertDialog progressDialog = showProgressDialog(); + mDevServerHelper.downloadBundleFromURL( new DevServerHelper.BundleDownloadCallback() { @Override @@ -776,8 +785,8 @@ public class DevSupportManagerImpl implements DevSupportManager, PackagerCommand }); } }, - Assertions.assertNotNull(mJSAppBundleName), - mJSBundleTempFile); + mJSBundleTempFile, + bundleURL); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DisabledDevSupportManager.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DisabledDevSupportManager.java index 3844d5223..f5592d0f1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DisabledDevSupportManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DisabledDevSupportManager.java @@ -126,6 +126,11 @@ public class DisabledDevSupportManager implements DevSupportManager { } + @Override + public void reloadJSFromServer(String bundleURL) { + + } + @Override public void isPackagerRunning(DevServerHelper.PackagerStatusCallback callback) {