Adding the ability to enable tcpip from the jadb

Using the information from https://github.com/aosp-mirror/platform_system_core/blob/master/adb/daemon/services.cpp
for the spec on how to construct the response. Usage is simply
```JadbDevice jadbDevice = ...; jadbDevice.enableTcpip()```
This commit is contained in:
Daniel Llewellyn 2019-01-06 12:18:38 +00:00
parent 4546c0583f
commit 01b5022d65
2 changed files with 56 additions and 1 deletions

View File

@ -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 {
@ -20,6 +22,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 +142,30 @@ public class JadbDevice {
return shellLine;
}
/**
* Enable tcpip on the default port (5555)
*
* @return success or failure
*/
public boolean enableTcpip() throws IOException, JadbException {
return enableTcpip(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));
String expectedResult = String.format("restarting in TCP Mode: %d", port);
return inputStreamToString(transport.getInputStream()).equals(expectedResult);
}
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
try (Transport transport = getTransport()) {
SyncTransport sync = transport.startSync();

View File

@ -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());
}
}