mirror of
https://github.com/revanced/jadb.git
synced 2024-06-10 23:27:53 +02:00
remove Thread
This commit is contained in:
parent
706c64d36f
commit
4486bd0a7a
|
@ -1,15 +0,0 @@
|
|||
package se.vidstige.jadb;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AsyncActionHandler {
|
||||
private Transport transport;
|
||||
public AsyncActionHandler(Transport transport) {
|
||||
this.transport = transport;
|
||||
};
|
||||
|
||||
public void stop() throws IOException {
|
||||
transport.close();
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package se.vidstige.jadb;
|
|||
import java.util.List;
|
||||
|
||||
public interface DeviceDetectionListener {
|
||||
public boolean detect(List<JadbDevice> device);
|
||||
public void onDetect(List<JadbDevice> devices);
|
||||
public void onException(Exception e);
|
||||
}
|
||||
|
||||
|
|
44
src/se/vidstige/jadb/DeviceWatcher.java
Normal file
44
src/se/vidstige/jadb/DeviceWatcher.java
Normal file
|
@ -0,0 +1,44 @@
|
|||
package se.vidstige.jadb;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DeviceWatcher implements Runnable {
|
||||
private Transport transport;
|
||||
private final DeviceDetectionListener listener;
|
||||
private final JadbConnection connection;
|
||||
|
||||
public DeviceWatcher(Transport transport, DeviceDetectionListener listener, JadbConnection connection) {
|
||||
this.transport = transport;
|
||||
this.listener = listener;
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
watch();
|
||||
}
|
||||
|
||||
public void watch() {
|
||||
try {
|
||||
while (true) {
|
||||
listener.onDetect(connection.parseDevices(transport.readString()));
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
synchronized(this) {
|
||||
if (transport != null) {
|
||||
listener.onException(ioe);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
listener.onException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws IOException {
|
||||
synchronized(this) {
|
||||
transport.close();
|
||||
transport = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -43,32 +43,14 @@ public class JadbConnection implements ITransportFactory {
|
|||
return parseDevices(body);
|
||||
}
|
||||
|
||||
public AsyncActionHandler watchDevices(final DeviceDetectionListener listener) throws IOException, JadbException {
|
||||
final Transport transport = createTransport();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
transport.send("host:track-devices");
|
||||
transport.verifyResponse();
|
||||
boolean r = false;
|
||||
do {
|
||||
List<JadbDevice> list = parseDevices(transport.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 AsyncActionHandler(transport);
|
||||
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
||||
Transport transport = createTransport();
|
||||
transport.send("host:track-devices");
|
||||
transport.verifyResponse();
|
||||
return new DeviceWatcher(transport, listener, this);
|
||||
}
|
||||
|
||||
private List<JadbDevice> parseDevices(String body) {
|
||||
public List<JadbDevice> parseDevices(String body) {
|
||||
String[] lines = body.split("\n");
|
||||
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
||||
for (String line : lines) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user