From e02a154787274be1da3632cb1412554cbd53928b Mon Sep 17 00:00:00 2001 From: Stepan Hruda Date: Tue, 6 Nov 2018 15:33:21 -0800 Subject: [PATCH] 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 --- .../systeminfo/AndroidInfoHelpers.java | 51 ++++++++++++++++++- .../facebook/react/modules/systeminfo/BUCK | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index 719d49a0e1d8f5..16dc414dcd690d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -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 @@ 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; @@ -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; + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK index 40e4da54a03137..396d6c574e3046 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK @@ -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"), ],