From 7b99c48cb7f1d60538a9e23abc5213c950c2e48b Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Mon, 31 Dec 2018 09:52:11 +0000 Subject: [PATCH 1/2] Adding the ability to enable tcpip from the library as per https://github.com/aosp-mirror/platform_system_core/blob/master/adb/daemon/services.cpp --- src/se/vidstige/jadb/JadbDevice.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 602fbbd..8f93643 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -20,6 +20,7 @@ public class JadbDevice { private static final int DEFAULT_MODE = 0664; private final String serial; private final ITransportFactory transportFactory; + private static final int DEFAULT_TCPIP_PORT = 5555; JadbDevice(String serial, ITransportFactory tFactory) { this.serial = serial; @@ -139,6 +140,31 @@ public class JadbDevice { return shellLine; } + /** + * Enable tcpip on the default port (5555) + * + * @return success or failure + */ + public boolean enableTcpip() throws IOException, JadbException { + Transport transport = getTransport(); + + send(transport, String.format("tcpip: %d", DEFAULT_TCPIP_PORT)); + return transport.readString().trim().equals(String.format("restarting in TCP Mode: %d", DEFAULT_TCPIP_PORT)); + } + + /** + * Enable tcpip on a specific port + * + * @param port for the device to bind on + * + * @return success or failure + */ + public boolean enableTcpip(int port) throws IOException, JadbException { + Transport transport = getTransport(); + send(transport, String.format("tcpip: %d", port)); + return transport.readString().trim().equals(String.format("restarting in TCP Mode: %d", port)); + } + public List list(String remotePath) throws IOException, JadbException { try (Transport transport = getTransport()) { SyncTransport sync = transport.startSync(); From 6466ee461ef5f3f0a8c384a3c72c58ad68c3636c Mon Sep 17 00:00:00 2001 From: llewellynd Date: Mon, 31 Dec 2018 10:58:07 +0000 Subject: [PATCH 2/2] Adding the ability to enable tcpip from the library as per https://github.com/aosp-mirror/platform_system_core/blob/master/adb/daemon/services.cpp --- src/se/vidstige/jadb/JadbDevice.java | 15 ++++++++------- src/se/vidstige/jadb/Util.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/se/vidstige/jadb/Util.java diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 8f93643..3c09e8b 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -1,11 +1,13 @@ package se.vidstige.jadb; import se.vidstige.jadb.managers.Bash; - import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import static se.vidstige.jadb.Util.inputStreamToString; + public class JadbDevice { @SuppressWarnings("squid:S00115") public enum State { @@ -146,10 +148,7 @@ public class JadbDevice { * @return success or failure */ public boolean enableTcpip() throws IOException, JadbException { - Transport transport = getTransport(); - - send(transport, String.format("tcpip: %d", DEFAULT_TCPIP_PORT)); - return transport.readString().trim().equals(String.format("restarting in TCP Mode: %d", DEFAULT_TCPIP_PORT)); + return enableTcpip(DEFAULT_TCPIP_PORT); } /** @@ -161,8 +160,10 @@ public class JadbDevice { */ public boolean enableTcpip(int port) throws IOException, JadbException { Transport transport = getTransport(); - send(transport, String.format("tcpip: %d", port)); - return transport.readString().trim().equals(String.format("restarting in TCP Mode: %d", port)); + send(transport, String.format("tcpip:%d", port)); + String expectedResult = String.format("restarting in TCP Mode: %d", port); + + return inputStreamToString(transport.getInputStream()).equals(expectedResult); } public List list(String remotePath) throws IOException, JadbException { diff --git a/src/se/vidstige/jadb/Util.java b/src/se/vidstige/jadb/Util.java new file mode 100644 index 0000000..0dcf202 --- /dev/null +++ b/src/se/vidstige/jadb/Util.java @@ -0,0 +1,28 @@ +package se.vidstige.jadb; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +public class Util { + /** + * Convert an input stream to string + * + * @param inputStream input stream + * + * @return string representation of the input stream + * + * @throws IOException if an error ocurrs reading the input stream + */ + public static String inputStreamToString(InputStream inputStream) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + + return result.toString(StandardCharsets.UTF_8.name()); + } +}