Skip to content

Commit

Permalink
Allow overriding Metro server host with a system prop
Browse files Browse the repository at this point in the history
Summary:
Allow a device to override the host to which to connect to for getting dev bundles, debugging etc.

This is read from a system prop so it can be shared across all React Native apps and persists between app installs.

Reviewed By: mdvacca

Differential Revision: D10842213

fbshipit-source-id: d15b7d0255130090744d60ffb239778cba15e49c
  • Loading branch information
stepanhruda authored and facebook-github-bot committed Nov 6, 2018
1 parent 41eb2da commit e02a154
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@

package com.facebook.react.modules.systeminfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import android.os.Build;

import com.facebook.common.logging.FLog;

public class AndroidInfoHelpers {

public static final String EMULATOR_LOCALHOST = "10.0.2.2";
public static final String GENYMOTION_LOCALHOST = "10.0.3.2";
public static final String DEVICE_LOCALHOST = "localhost";

public static final String METRO_HOST_PROP_NAME = "metro.host";

private static final int DEBUG_SERVER_HOST_PORT = 8081;
private static final int INSPECTOR_PROXY_PORT = 8082;

private static final String TAG = AndroidInfoHelpers.class.getSimpleName();

private static boolean isRunningOnGenymotion() {
return Build.FINGERPRINT.contains("vbox");
}
Expand Down Expand Up @@ -49,7 +58,10 @@ private static String getServerIpAddress(int port) {
// We detect whether app runs on genymotion and replace js bundle server hostname accordingly

String ipAddress;
if (isRunningOnGenymotion()) {
String metroHostProp = getMetroHostPropValue();
if (!metroHostProp.equals("")) {
ipAddress = metroHostProp;
} else if (isRunningOnGenymotion()) {
ipAddress = GENYMOTION_LOCALHOST;
} else if (isRunningOnStockEmulator()) {
ipAddress = EMULATOR_LOCALHOST;
Expand All @@ -59,4 +71,41 @@ private static String getServerIpAddress(int port) {

return String.format(Locale.US, "%s:%d", ipAddress, port);
}

private static String metroHostPropValue = null;
private static synchronized String getMetroHostPropValue() {
if (metroHostPropValue != null) {
return metroHostPropValue;
}
Process process = null;
BufferedReader reader = null;
try {
process =
Runtime.getRuntime().exec(new String[] {"/system/bin/getprop", METRO_HOST_PROP_NAME});
reader =
new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));

String lastLine = "";
String line;
while ((line = reader.readLine()) != null) {
lastLine = line;
}
metroHostPropValue = lastLine;
} catch (Exception e) {
FLog.w(TAG, "Failed to query for metro.host prop:", e);
metroHostPropValue = "";
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception exc) {
}
if (process != null) {
process.destroy();
}
}
return metroHostPropValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rn_android_library(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
],
Expand Down

0 comments on commit e02a154

Please sign in to comment.