AdbProtocolHandler: extract command handlers to separate methods

This commit is contained in:
Jano Svitok 2018-07-04 13:16:50 +02:00
parent 4761259137
commit f679f387eb

View File

@ -53,24 +53,13 @@ class AdbProtocolHandler implements Runnable {
try { try {
if ("host:version".equals(command)) { if ("host:version".equals(command)) {
output.writeBytes("OKAY"); hostVersion(output);
send(output, String.format("%04x", responder.getVersion()));
} else if ("host:transport-any".equals(command)) { } else if ("host:transport-any".equals(command)) {
// TODO: Check so that exactly one device is selected. hostTransportAny(output);
selected = responder.getDevices().get(0);
output.writeBytes("OKAY");
} else if ("host:devices".equals(command)) { } else if ("host:devices".equals(command)) {
ByteArrayOutputStream tmp = new ByteArrayOutputStream(); hostDevices(output);
DataOutputStream writer = new DataOutputStream(tmp);
for (AdbDeviceResponder d : responder.getDevices()) {
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
}
output.writeBytes("OKAY");
send(output, new String(tmp.toByteArray(), StandardCharsets.UTF_8));
} else if (command.startsWith("host:transport:")) { } else if (command.startsWith("host:transport:")) {
String serial = command.substring("host:transport:".length()); hostTransport(output, command);
selected = findDevice(serial);
output.writeBytes("OKAY");
} else if ("sync:".equals(command)) { } else if ("sync:".equals(command)) {
output.writeBytes("OKAY"); output.writeBytes("OKAY");
try { try {
@ -80,16 +69,24 @@ class AdbProtocolHandler implements Runnable {
sync.send("FAIL", e.getMessage()); sync.send("FAIL", e.getMessage());
} }
} else if (command.startsWith("shell:")) { } else if (command.startsWith("shell:")) {
String shellCommand = command.substring("shell:".length()); shell(input, output, command);
output.writeBytes("OKAY");
shell(shellCommand, output, input);
return false; return false;
} else if ("host:get-state".equals(command)) { } else if ("host:get-state".equals(command)) {
// TODO: Check so that exactly one device is selected. hostGetState(output);
AdbDeviceResponder device = responder.getDevices().get(0);
output.writeBytes("OKAY");
send(output, device.getType());
} else if (command.startsWith("host-serial:")) { } else if (command.startsWith("host-serial:")) {
hostSerial(output, command);
} else {
throw new ProtocolException("Unknown command: " + command);
}
} catch (ProtocolException e) {
output.writeBytes("FAIL");
send(output, e.getMessage());
}
output.flush();
return true;
}
private void hostSerial(DataOutputStream output, String command) throws IOException {
String[] strs = command.split(":",0); String[] strs = command.split(":",0);
if (strs.length != 3) { if (strs.length != 3) {
throw new ProtocolException("Invalid command: " + command); throw new ProtocolException("Invalid command: " + command);
@ -109,15 +106,46 @@ class AdbProtocolHandler implements Runnable {
if (!found) { if (!found) {
send(output, "unknown"); send(output, "unknown");
} }
} else {
throw new ProtocolException("Unknown command: " + command);
} }
} catch (ProtocolException e) {
output.writeBytes("FAIL"); private void hostGetState(DataOutputStream output) throws IOException {
send(output, e.getMessage()); // TODO: Check so that exactly one device is selected.
AdbDeviceResponder device = responder.getDevices().get(0);
output.writeBytes("OKAY");
send(output, device.getType());
} }
output.flush();
return true; private void shell(DataInput input, DataOutputStream output, String command) throws IOException {
String shellCommand = command.substring("shell:".length());
output.writeBytes("OKAY");
shell(shellCommand, output, input);
}
private void hostTransport(DataOutputStream output, String command) throws IOException {
String serial = command.substring("host:transport:".length());
selected = findDevice(serial);
output.writeBytes("OKAY");
}
private void hostDevices(DataOutputStream output) throws IOException {
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(tmp);
for (AdbDeviceResponder d : responder.getDevices()) {
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
}
output.writeBytes("OKAY");
send(output, new String(tmp.toByteArray(), StandardCharsets.UTF_8));
}
private void hostTransportAny(DataOutputStream output) throws IOException {
// TODO: Check so that exactly one device is selected.
selected = responder.getDevices().get(0);
output.writeBytes("OKAY");
}
private void hostVersion(DataOutputStream output) throws IOException {
output.writeBytes("OKAY");
send(output, String.format("%04x", responder.getVersion()));
} }
private void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException { private void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException {
@ -147,6 +175,22 @@ class AdbProtocolHandler implements Runnable {
String id = readString(input, 4); String id = readString(input, 4);
int length = readInt(input); int length = readInt(input);
if ("SEND".equals(id)) { if ("SEND".equals(id)) {
syncSend(output, input, length);
} else if ("RECV".equals(id)) {
syncRecv(output, input, length);
} else throw new JadbException("Unknown sync id " + id);
}
private void syncRecv(DataOutput output, DataInput input, int length) throws IOException, JadbException {
String remotePath = readString(input, length);
SyncTransport transport = getSyncTransport(output, input);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
selected.filePulled(new RemoteFile(remotePath), buffer);
transport.sendStream(new ByteArrayInputStream(buffer.toByteArray()));
transport.sendStatus("DONE", 0); // ignored
}
private void syncSend(DataOutput output, DataInput input, int length) throws IOException, JadbException {
String remotePath = readString(input, length); String remotePath = readString(input, length);
int idx = remotePath.lastIndexOf(','); int idx = remotePath.lastIndexOf(',');
String path = remotePath; String path = remotePath;
@ -160,14 +204,6 @@ class AdbProtocolHandler implements Runnable {
transport.readChunksTo(buffer); transport.readChunksTo(buffer);
selected.filePushed(new RemoteFile(path), mode, buffer); selected.filePushed(new RemoteFile(path), mode, buffer);
transport.sendStatus("OKAY", 0); // 0 = ignored transport.sendStatus("OKAY", 0); // 0 = ignored
} else if ("RECV".equals(id)) {
String remotePath = readString(input, length);
SyncTransport transport = getSyncTransport(output, input);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
selected.filePulled(new RemoteFile(remotePath), buffer);
transport.sendStream(new ByteArrayInputStream(buffer.toByteArray()));
transport.sendStatus("DONE", 0); // ignored
} else throw new JadbException("Unknown sync id " + id);
} }
private String getCommandLength(String command) { private String getCommandLength(String command) {