get state by get-state command

This commit is contained in:
Giemsa 2016-10-24 00:32:57 +09:00 committed by Samuel Carlsson
parent e324cbe004
commit 76c9045b0c

View File

@ -13,18 +13,17 @@ import java.util.regex.Pattern;
public class JadbDevice { public class JadbDevice {
public enum State { public enum State {
Unknown, Unknown,
Online, Offline,
Offline Device,
BootLoader
}; };
private final String serial; private final String serial;
private final ITransportFactory transportFactory; private final ITransportFactory transportFactory;
private State state = State.Unknown;
JadbDevice(String serial, String type, ITransportFactory tFactory) { JadbDevice(String serial, String type, ITransportFactory tFactory) {
this.serial = serial; this.serial = serial;
this.transportFactory = tFactory; this.transportFactory = tFactory;
state = convertState(type);
} }
static JadbDevice createAny(JadbConnection connection) { static JadbDevice createAny(JadbConnection connection) {
@ -37,9 +36,10 @@ public class JadbDevice {
} }
private State convertState(String type) { private State convertState(String type) {
switch(type) { switch (type) {
case "device": return State.Online; case "device": return State.Device;
case "offline": return State.Offline; case "offline": return State.Offline;
case "bootloader": return State.BootLoader;
default: return State.Unknown; default: return State.Unknown;
} }
} }
@ -60,8 +60,17 @@ public class JadbDevice {
return serial; return serial;
} }
public State getState() { public State getState() throws IOException, JadbException {
return state; Transport transport = transportFactory.createTransport();
if (serial == null) {
transport.send("host:get-state");
transport.verifyResponse();
} else {
transport.send("host-serial:" + serial + ":get-state");
transport.verifyResponse();
}
return convertState(transport.readString());
} }
public InputStream executeShell(String command, String... args) throws IOException, JadbException { public InputStream executeShell(String command, String... args) throws IOException, JadbException {