jadb/src/se/vidstige/jadb/JadbConnection.java

84 lines
2.7 KiB
Java
Raw Normal View History

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
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() {
this("localhost", DEFAULTPORT);
}
2016-12-08 18:46:03 +01:00
public JadbConnection(String host, int port) {
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();
}
}
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)
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);
}
}
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-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) {
String[] lines = body.split("\n");
ArrayList<JadbDevice> devices = new ArrayList<>(lines.length);
for (String line : lines) {
String[] parts = line.split("\t");
if (parts.length > 1) {
2018-08-06 10:30:18 +02:00
devices.add(new JadbDevice(parts[0], this)); // parts[1] is type
}
}
return devices;
}
2014-03-20 09:50:17 +01:00
public JadbDevice getAnyDevice() {
return JadbDevice.createAny(this);
}
2013-07-25 21:13:20 +02:00
}