mirror of https://github.com/revanced/jadb.git
91 lines
2.8 KiB
Java
91 lines
2.8 KiB
Java
package se.vidstige.jadb;
|
|
|
|
import se.vidstige.jadb.entities.TcpAddressEntity;
|
|
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class JadbConnection implements ITransportFactory {
|
|
|
|
private final String host;
|
|
private final int port;
|
|
|
|
private static final int DEFAULTPORT = 5037;
|
|
|
|
public JadbConnection() {
|
|
this("localhost", DEFAULTPORT);
|
|
}
|
|
|
|
public JadbConnection(String host, int port) {
|
|
this.host = host;
|
|
this.port = port;
|
|
}
|
|
|
|
public Transport createTransport() throws IOException {
|
|
return new Transport(new Socket(host, port));
|
|
}
|
|
|
|
public String getHostVersion() throws IOException, JadbException {
|
|
Transport main = createTransport();
|
|
main.send("host:version");
|
|
main.verifyResponse();
|
|
String version = main.readString();
|
|
main.close();
|
|
return version;
|
|
}
|
|
|
|
public TcpAddressEntity connectToTcpDevice(TcpAddressEntity tcpAddressEntity)
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
|
Transport transport = createTransport();
|
|
try {
|
|
return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity);
|
|
} finally {
|
|
transport.close();
|
|
}
|
|
}
|
|
|
|
public TcpAddressEntity connectFromTcpDevice(TcpAddressEntity tcpAddressEntity)
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
|
Transport transport = createTransport();
|
|
try {
|
|
return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity);
|
|
} finally {
|
|
transport.close();
|
|
}
|
|
}
|
|
|
|
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
|
Transport devices = createTransport();
|
|
devices.send("host:devices");
|
|
devices.verifyResponse();
|
|
String body = devices.readString();
|
|
devices.close();
|
|
return parseDevices(body);
|
|
}
|
|
|
|
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
|
Transport transport = createTransport();
|
|
transport.send("host:track-devices");
|
|
transport.verifyResponse();
|
|
return new DeviceWatcher(transport, listener, this);
|
|
}
|
|
|
|
public List<JadbDevice> parseDevices(String body) {
|
|
String[] lines = body.split("\n");
|
|
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
|
for (String line : lines) {
|
|
String[] parts = line.split("\t");
|
|
if (parts.length > 1) {
|
|
devices.add(new JadbDevice(parts[0], parts[1], this));
|
|
}
|
|
}
|
|
return devices;
|
|
}
|
|
|
|
public JadbDevice getAnyDevice() {
|
|
return JadbDevice.createAny(this);
|
|
}
|
|
}
|