mirror of
https://github.com/revanced/jadb.git
synced 2024-11-19 10:39:23 +01:00
device detection
This commit is contained in:
parent
70ea021bf0
commit
7a249c3476
15
src/se/vidstige/jadb/DeviceDetectionHandler.java
Normal file
15
src/se/vidstige/jadb/DeviceDetectionHandler.java
Normal file
@ -0,0 +1,15 @@
|
||||
package se.vidstige.jadb;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DeviceDetectionHandler {
|
||||
private Transport transport;
|
||||
public DeviceDetectionHandler(Transport transport) {
|
||||
this.transport = transport;
|
||||
};
|
||||
|
||||
public void stop() throws IOException {
|
||||
transport.close();
|
||||
}
|
||||
}
|
8
src/se/vidstige/jadb/DeviceDetectionListener.java
Normal file
8
src/se/vidstige/jadb/DeviceDetectionListener.java
Normal file
@ -0,0 +1,8 @@
|
||||
package se.vidstige.jadb;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DeviceDetectionListener {
|
||||
public boolean detect(List<JadbDevice> device);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package se.vidstige.jadb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -38,9 +39,35 @@ public class JadbConnection implements ITransportFactory {
|
||||
devices.send("host:devices");
|
||||
devices.verifyResponse();
|
||||
String body = devices.readString();
|
||||
devices.close();
|
||||
return parseDevices(body);
|
||||
}
|
||||
|
||||
public DeviceDetectionHandler getDevices(final DeviceDetectionListener listener) throws IOException, JadbException {
|
||||
final Transport devices = createTransport();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try
|
||||
{
|
||||
devices.send("host:track-devices");
|
||||
devices.verifyResponse();
|
||||
boolean r = false;
|
||||
do {
|
||||
List<JadbDevice> list = parseDevices(devices.readString());
|
||||
r = listener.detect(list);
|
||||
} while(r);
|
||||
} catch(SocketException e) {
|
||||
// socket closed from another thread
|
||||
} catch(Exception e) {
|
||||
Thread t = Thread.currentThread();
|
||||
t.getUncaughtExceptionHandler().uncaughtException(t, e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
return new DeviceDetectionHandler(devices);
|
||||
}
|
||||
|
||||
private List<JadbDevice> parseDevices(String body) {
|
||||
String[] lines = body.split("\n");
|
||||
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
||||
|
@ -11,12 +11,20 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class JadbDevice {
|
||||
public enum State {
|
||||
Unknown,
|
||||
Online,
|
||||
Offline
|
||||
};
|
||||
|
||||
private final String serial;
|
||||
private final ITransportFactory transportFactory;
|
||||
private State state = State.Unknown;
|
||||
|
||||
JadbDevice(String serial, String type, ITransportFactory tFactory) {
|
||||
this.serial = serial;
|
||||
this.transportFactory = tFactory;
|
||||
state = convertState(type);
|
||||
}
|
||||
|
||||
static JadbDevice createAny(JadbConnection connection) {
|
||||
@ -28,6 +36,14 @@ public class JadbDevice {
|
||||
this.transportFactory = tFactory;
|
||||
}
|
||||
|
||||
private State convertState(String type) {
|
||||
switch(type) {
|
||||
case "device": return State.Online;
|
||||
case "ofline": return State.Offline;
|
||||
default: return State.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
private Transport getTransport() throws IOException, JadbException {
|
||||
Transport transport = transportFactory.createTransport();
|
||||
if (serial == null) {
|
||||
@ -44,6 +60,10 @@ public class JadbDevice {
|
||||
return serial;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public InputStream executeShell(String command, String... args) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
StringBuilder shellLine = new StringBuilder(command);
|
||||
|
Loading…
Reference in New Issue
Block a user