Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Allow PackagerConnectionSettings to be provided by ReactNativeHost #16311

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
Expand Down Expand Up @@ -233,6 +234,7 @@ public static ReactInstanceManagerBuilder builder() {
boolean lazyNativeModulesEnabled,
boolean lazyViewManagersEnabled,
boolean delayViewManagerClassLoadsEnabled,
@Nullable PackagerConnectionSettings packagerConnectionSettings,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
boolean useSeparateUIBackgroundThread,
int minNumShakes,
Expand All @@ -259,6 +261,7 @@ public static ReactInstanceManagerBuilder builder() {
mJSMainModulePath,
useDeveloperSupport,
redBoxHandler,
packagerConnectionSettings,
devBundleDownloadListener,
minNumShakes);
mBridgeIdleDebugListener = bridgeIdleDebugListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
import com.facebook.react.uimanager.UIImplementationProvider;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class ReactInstanceManagerBuilder {
private boolean mLazyNativeModulesEnabled;
private boolean mLazyViewManagersEnabled;
private boolean mDelayViewManagerClassLoadsEnabled;
private @Nullable PackagerConnectionSettings mPackagerConnectionSettings;
private @Nullable DevBundleDownloadListener mDevBundleDownloadListener;
private @Nullable JavaScriptExecutorFactory mJavaScriptExecutorFactory;
private boolean mUseSeparateUIBackgroundThread;
Expand Down Expand Up @@ -210,6 +212,12 @@ public ReactInstanceManagerBuilder setDelayViewManagerClassLoadsEnabled(
return this;
}

public ReactInstanceManagerBuilder setPackagerConnectionSettings(
@Nullable PackagerConnectionSettings packagerConnectionSettings) {
mPackagerConnectionSettings = packagerConnectionSettings;
return this;
}

public ReactInstanceManagerBuilder setDevBundleDownloadListener(
@Nullable DevBundleDownloadListener listener) {
mDevBundleDownloadListener = listener;
Expand Down Expand Up @@ -297,6 +305,7 @@ public ReactInstanceManager build() {
mLazyNativeModulesEnabled,
mLazyViewManagersEnabled,
mDelayViewManagerClassLoadsEnabled,
mPackagerConnectionSettings,
mDevBundleDownloadListener,
mUseSeparateUIBackgroundThread,
mMinNumShakes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.devsupport.RedBoxHandler;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
import com.facebook.react.uimanager.UIImplementationProvider;

/**
Expand Down Expand Up @@ -66,6 +67,7 @@ public void clear() {
protected ReactInstanceManager createReactInstanceManager() {
ReactInstanceManagerBuilder builder = ReactInstanceManager.builder()
.setApplication(mApplication)
.setPackagerConnectionSettings(getPackagerConnectionSettings())
.setJSMainModulePath(getJSMainModuleName())
.setUseDeveloperSupport(getUseDeveloperSupport())
.setRedBoxHandler(getRedBoxHandler())
Expand Down Expand Up @@ -105,6 +107,10 @@ protected final Application getApplication() {
return mApplication;
}

protected PackagerConnectionSettings getPackagerConnectionSettings() {
return null;
}

/**
* Get the {@link UIImplementationProvider} to use. Override this method if you want to use a
* custom UI implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public class DevInternalSettings implements

public DevInternalSettings(
Context applicationContext,
@Nullable PackagerConnectionSettings packagerConnectionSettings,
Listener listener) {
mListener = listener;
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPreferences.registerOnSharedPreferenceChangeListener(this);
mPackagerConnectionSettings = new PackagerConnectionSettings(applicationContext);
mPackagerConnectionSettings = packagerConnectionSettings == null
? new PackagerConnectionSettings(applicationContext)
: packagerConnectionSettings;
}

public PackagerConnectionSettings getPackagerConnectionSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;

/**
* A simple factory that creates instances of {@link DevSupportManager} implementations. Uses
Expand All @@ -43,6 +44,7 @@ public static DevSupportManager create(
enableOnCreate,
null,
null,
null,
minNumShakes);
}

Expand All @@ -52,6 +54,7 @@ public static DevSupportManager create(
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable PackagerConnectionSettings packagerConnectionSettings,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
if (!enableOnCreate) {
Expand All @@ -75,6 +78,7 @@ public static DevSupportManager create(
String.class,
boolean.class,
RedBoxHandler.class,
PackagerConnectionSettings.class,
DevBundleDownloadListener.class,
int.class);
return (DevSupportManager) constructor.newInstance(
Expand All @@ -83,6 +87,7 @@ public static DevSupportManager create(
packagerPathForJSBundleName,
true,
redBoxHandler,
packagerConnectionSettings,
devBundleDownloadListener,
minNumShakes);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.devsupport.interfaces.StackFrame;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.packagerconnection.Responder;
import java.io.File;
Expand Down Expand Up @@ -185,6 +186,7 @@ public DevSupportManagerImpl(
enableOnCreate,
null,
null,
null,
minNumShakes);
}

Expand All @@ -194,12 +196,13 @@ public DevSupportManagerImpl(
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable PackagerConnectionSettings packagerConnectionSettings,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
mReactInstanceCommandsHandler = reactInstanceCommandsHandler;
mApplicationContext = applicationContext;
mJSAppBundleName = packagerPathForJSBundleName;
mDevSettings = new DevInternalSettings(applicationContext, this);
mDevSettings = new DevInternalSettings(applicationContext, packagerConnectionSettings, this);
mDevServerHelper = new DevServerHelper(mDevSettings, mApplicationContext.getPackageName());
mBundleDownloadListener = devBundleDownloadListener;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class AndroidInfoHelpers {
public static final String GENYMOTION_LOCALHOST = "10.0.3.2";
public static final String DEVICE_LOCALHOST = "localhost";

private static final int DEBUG_SERVER_HOST_PORT = 8081;
private static final int INSPECTOR_PROXY_PORT = 8082;
public static final int DEBUG_SERVER_HOST_PORT = 8081;
public static final int INSPECTOR_PROXY_PORT = 8082;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no compelling reason to keep these values private. My WebWorker library checks for conflicts against these defaults (and utilizes the getServerIpAddress below to avoid duplicating logic to find the loopback interface).


private static boolean isRunningOnGenymotion() {
return Build.FINGERPRINT.contains("vbox");
Expand Down Expand Up @@ -41,7 +41,7 @@ public static String getFriendlyDeviceName() {
}
}

private static String getServerIpAddress(int port) {
public static String getServerIpAddress(int port) {
// Since genymotion runs in vbox it use different hostname to refer to adb host.
// We detect whether app runs on genymotion and replace js bundle server hostname accordingly

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ public class PackagerConnectionSettings {

private final SharedPreferences mPreferences;
private final String mPackageName;
private final String mDebugServerHost;
private final String mInspectorServerHost;

public PackagerConnectionSettings(Context applicationContext) {
this(applicationContext, null, null);
}

public PackagerConnectionSettings(Context applicationContext, String debugServerHost, String inspectorServerHost) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPackageName = applicationContext.getPackageName();
mDebugServerHost = debugServerHost;
mInspectorServerHost = inspectorServerHost;
}

public String getDebugServerHost() {
if (mDebugServerHost != null) {
return mDebugServerHost;
}

// Check host setting first. If empty try to detect emulator type and use default
// hostname for those
String hostFromSettings = mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null);
Expand All @@ -54,6 +66,10 @@ public String getDebugServerHost() {
}

public String getInspectorServerHost() {
if (mInspectorServerHost != null) {
return mInspectorServerHost;
}

return AndroidInfoHelpers.getInspectorProxyHost();
}

Expand Down