Skip to content

Commit e02a154

Browse files
stepanhrudafacebook-github-bot
authored andcommittedNov 6, 2018
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
1 parent 41eb2da commit e02a154

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@
55

66
package com.facebook.react.modules.systeminfo;
77

8+
import java.io.BufferedReader;
9+
import java.io.InputStreamReader;
10+
import java.nio.charset.StandardCharsets;
811
import java.util.Locale;
912

1013
import android.os.Build;
1114

15+
import com.facebook.common.logging.FLog;
16+
1217
public class AndroidInfoHelpers {
1318

1419
public static final String EMULATOR_LOCALHOST = "10.0.2.2";
1520
public static final String GENYMOTION_LOCALHOST = "10.0.3.2";
1621
public static final String DEVICE_LOCALHOST = "localhost";
1722

23+
public static final String METRO_HOST_PROP_NAME = "metro.host";
24+
1825
private static final int DEBUG_SERVER_HOST_PORT = 8081;
1926
private static final int INSPECTOR_PROXY_PORT = 8082;
2027

28+
private static final String TAG = AndroidInfoHelpers.class.getSimpleName();
29+
2130
private static boolean isRunningOnGenymotion() {
2231
return Build.FINGERPRINT.contains("vbox");
2332
}
@@ -49,7 +58,10 @@ private static String getServerIpAddress(int port) {
4958
// We detect whether app runs on genymotion and replace js bundle server hostname accordingly
5059

5160
String ipAddress;
52-
if (isRunningOnGenymotion()) {
61+
String metroHostProp = getMetroHostPropValue();
62+
if (!metroHostProp.equals("")) {
63+
ipAddress = metroHostProp;
64+
} else if (isRunningOnGenymotion()) {
5365
ipAddress = GENYMOTION_LOCALHOST;
5466
} else if (isRunningOnStockEmulator()) {
5567
ipAddress = EMULATOR_LOCALHOST;
@@ -59,4 +71,41 @@ private static String getServerIpAddress(int port) {
5971

6072
return String.format(Locale.US, "%s:%d", ipAddress, port);
6173
}
74+
75+
private static String metroHostPropValue = null;
76+
private static synchronized String getMetroHostPropValue() {
77+
if (metroHostPropValue != null) {
78+
return metroHostPropValue;
79+
}
80+
Process process = null;
81+
BufferedReader reader = null;
82+
try {
83+
process =
84+
Runtime.getRuntime().exec(new String[] {"/system/bin/getprop", METRO_HOST_PROP_NAME});
85+
reader =
86+
new BufferedReader(
87+
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
88+
89+
String lastLine = "";
90+
String line;
91+
while ((line = reader.readLine()) != null) {
92+
lastLine = line;
93+
}
94+
metroHostPropValue = lastLine;
95+
} catch (Exception e) {
96+
FLog.w(TAG, "Failed to query for metro.host prop:", e);
97+
metroHostPropValue = "";
98+
} finally {
99+
try {
100+
if (reader != null) {
101+
reader.close();
102+
}
103+
} catch (Exception exc) {
104+
}
105+
if (process != null) {
106+
process.destroy();
107+
}
108+
}
109+
return metroHostPropValue;
110+
}
62111
}

‎ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ rn_android_library(
2929
"PUBLIC",
3030
],
3131
deps = [
32+
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
3233
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
3334
react_native_dep("third-party/java/jsr-305:jsr-305"),
3435
],

0 commit comments

Comments
 (0)
Please sign in to comment.