Allow overriding Metro server host with a system prop

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
This commit is contained in:
Stepan Hruda 2018-11-06 15:33:21 -08:00 committed by Facebook Github Bot
parent 41eb2da33b
commit e02a154787
2 changed files with 51 additions and 1 deletions

View File

@ -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");
}
@ -49,7 +58,10 @@ public class AndroidInfoHelpers {
// 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;
@ -59,4 +71,41 @@ public class AndroidInfoHelpers {
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;
}
}

View File

@ -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"),
],