2013-07-25 21:13:20 +02:00
|
|
|
package se.vidstige.jadb;
|
|
|
|
|
2017-03-17 17:48:13 +01:00
|
|
|
|
2013-07-25 21:13:20 +02:00
|
|
|
import java.io.IOException;
|
2017-03-20 13:17:38 +01:00
|
|
|
import java.net.InetSocketAddress;
|
2013-07-25 21:13:20 +02:00
|
|
|
import java.net.Socket;
|
2013-07-25 21:52:46 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2013-07-25 21:13:20 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public class JadbConnection implements ITransportFactory {
|
|
|
|
|
|
|
|
private final String host;
|
|
|
|
private final int port;
|
|
|
|
|
|
|
|
private static final int DEFAULTPORT = 5037;
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public JadbConnection() {
|
2016-03-20 09:13:30 +01:00
|
|
|
this("localhost", DEFAULTPORT);
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public JadbConnection(String host, int port) {
|
2016-03-20 09:13:30 +01:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Transport createTransport() throws IOException {
|
|
|
|
return new Transport(new Socket(host, port));
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public String getHostVersion() throws IOException, JadbException {
|
2018-08-06 11:09:27 +02:00
|
|
|
try (Transport transport = createTransport()) {
|
|
|
|
transport.send("host:version");
|
|
|
|
transport.verifyResponse();
|
|
|
|
return transport.readString();
|
|
|
|
}
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 13:17:38 +01:00
|
|
|
public InetSocketAddress connectToTcpDevice(InetSocketAddress inetSocketAddress)
|
2017-03-17 17:48:13 +01:00
|
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
2018-08-06 11:09:27 +02:00
|
|
|
try (Transport transport = createTransport()) {
|
2017-03-20 13:17:38 +01:00
|
|
|
return new HostConnectToRemoteTcpDevice(transport).connect(inetSocketAddress);
|
2017-03-17 17:48:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 13:17:38 +01:00
|
|
|
public InetSocketAddress disconnectFromTcpDevice(InetSocketAddress tcpAddressEntity)
|
2017-03-20 11:18:56 +01:00
|
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
2018-08-06 11:09:27 +02:00
|
|
|
try (Transport transport = createTransport()) {
|
2017-03-20 11:48:24 +01:00
|
|
|
return new HostDisconnectFromRemoteTcpDevice(transport).disconnect(tcpAddressEntity);
|
2017-03-20 11:18:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
2018-08-06 11:09:27 +02:00
|
|
|
try (Transport transport = createTransport()) {
|
|
|
|
transport.send("host:devices");
|
|
|
|
transport.verifyResponse();
|
|
|
|
String body = transport.readString();
|
|
|
|
return parseDevices(body);
|
|
|
|
}
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:49:14 +02:00
|
|
|
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
|
|
|
Transport transport = createTransport();
|
|
|
|
transport.send("host:track-devices");
|
|
|
|
transport.verifyResponse();
|
|
|
|
return new DeviceWatcher(transport, listener, this);
|
2016-10-23 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:49:14 +02:00
|
|
|
public List<JadbDevice> parseDevices(String body) {
|
2016-03-20 09:13:30 +01:00
|
|
|
String[] lines = body.split("\n");
|
2018-07-30 12:00:20 +02:00
|
|
|
ArrayList<JadbDevice> devices = new ArrayList<>(lines.length);
|
2016-03-20 09:13:30 +01:00
|
|
|
for (String line : lines) {
|
|
|
|
String[] parts = line.split("\t");
|
2014-03-20 17:30:21 +01:00
|
|
|
if (parts.length > 1) {
|
2018-08-06 10:30:18 +02:00
|
|
|
devices.add(new JadbDevice(parts[0], this)); // parts[1] is type
|
2014-03-20 17:30:21 +01:00
|
|
|
}
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
2014-03-19 09:41:14 +01:00
|
|
|
|
2014-03-20 09:50:17 +01:00
|
|
|
public JadbDevice getAnyDevice() {
|
2016-02-28 03:04:59 +01:00
|
|
|
return JadbDevice.createAny(this);
|
2014-03-19 09:41:14 +01:00
|
|
|
}
|
2013-07-25 21:13:20 +02:00
|
|
|
}
|