Enable flipper android to use different ports based on prop

Summary:
Part 1 of enabling flipper to run on custom ports: android SDK.
Still to go: iOS SDK and desktop

This should allow you to run mobile apps that use flipper on different ports than the default (8089,8088).

`adb shell`
`su`
`setprop flipper.ports 1111,2222`

From what I can tell, this only works on rooted devices.

Reviewed By: passy

Differential Revision: D13753238

fbshipit-source-id: c5f370c9d8c7382e8c17fb81d4010c642ef7c114
This commit is contained in:
John Knox
2019-01-24 03:08:42 -08:00
committed by Facebook Github Bot
parent c68e74c6a0
commit 324a7ae873
9 changed files with 171 additions and 92 deletions

View File

@@ -1,9 +1,8 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.flipper.android;
@@ -36,6 +35,8 @@ public final class AndroidFlipperClient {
FlipperClientImpl.init(
sFlipperThread.getEventBase(),
sConnectionThread.getEventBase(),
FlipperProps.getInsecurePort(),
FlipperProps.getSecurePort(),
getServerHost(app),
"Android",
getFriendlyDeviceName(),

View File

@@ -1,9 +1,8 @@
/*
* Copyright (c) Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
*/
package com.facebook.flipper.android;
@@ -36,6 +35,8 @@ class FlipperClientImpl implements FlipperClient {
public static native void init(
EventBase callbackWorker,
EventBase connectionWorker,
int insecurePort,
int securePort,
String host,
String os,
String device,

View File

@@ -0,0 +1,84 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.flipper.android;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
class FlipperProps {
private static final String FLIPPER_PORTS_PROP_NAME = "flipper.ports";
private static final int DEFAULT_INSECURE_PORT = 8089;
private static final int DEFAULT_SECURE_PORT = 8088;
private static final String TAG = "Flipper";
static int getInsecurePort() {
String propValue = getFlipperPortsPropValue();
return extractIntFromPropValue(propValue, 0, DEFAULT_INSECURE_PORT);
}
static int getSecurePort() {
String propValue = getFlipperPortsPropValue();
return extractIntFromPropValue(propValue, 1, DEFAULT_SECURE_PORT);
}
static int extractIntFromPropValue(String propValue, int index, int fallback) {
if (propValue != null && !propValue.isEmpty()) {
try {
String[] values = propValue.split(",");
if (values.length > index) {
return Integer.parseInt(values[index]);
}
} catch (NumberFormatException e) {
Log.e(TAG, "Failed to parse flipper.ports value: " + propValue);
}
}
return fallback;
}
private static String flipperPortsPropValue = null;
private static synchronized String getFlipperPortsPropValue() {
if (flipperPortsPropValue != null) {
return flipperPortsPropValue;
}
Process process = null;
BufferedReader reader = null;
try {
process =
Runtime.getRuntime().exec(new String[] {"/system/bin/getprop", FLIPPER_PORTS_PROP_NAME});
reader =
new BufferedReader(
new InputStreamReader(process.getInputStream(), Charset.forName("UTF-8")));
String lastLine = "";
String line;
while ((line = reader.readLine()) != null) {
lastLine = line;
}
flipperPortsPropValue = lastLine;
} catch (IOException e) {
Log.e(TAG, "Failed to query for flipper.ports prop", e);
flipperPortsPropValue = "";
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Log.e(TAG, "Failed to close BufferedReader when reading flipper.ports prop", e);
}
if (process != null) {
process.destroy();
}
}
return flipperPortsPropValue;
}
}