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,35 +69,12 @@ 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:")) {
String[] strs = command.split(":",0); hostSerial(output, command);
if (strs.length != 3) {
throw new ProtocolException("Invalid command: " + command);
}
String serial = strs[1];
boolean found = false;
output.writeBytes("OKAY");
for (AdbDeviceResponder d : responder.getDevices()) {
if (d.getSerial().equals(serial)) {
send(output, d.getType());
found = true;
break;
}
}
if (!found) {
send(output, "unknown");
}
} else { } else {
throw new ProtocolException("Unknown command: " + command); throw new ProtocolException("Unknown command: " + command);
} }
@ -120,6 +86,68 @@ class AdbProtocolHandler implements Runnable {
return true; return true;
} }
private void hostSerial(DataOutputStream output, String command) throws IOException {
String[] strs = command.split(":",0);
if (strs.length != 3) {
throw new ProtocolException("Invalid command: " + command);
}
String serial = strs[1];
boolean found = false;
output.writeBytes("OKAY");
for (AdbDeviceResponder d : responder.getDevices()) {
if (d.getSerial().equals(serial)) {
send(output, d.getType());
found = true;
break;
}
}
if (!found) {
send(output, "unknown");
}
}
private void hostGetState(DataOutputStream output) throws IOException {
// TODO: Check so that exactly one device is selected.
AdbDeviceResponder device = responder.getDevices().get(0);
output.writeBytes("OKAY");
send(output, device.getType());
}
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 {
selected.shell(command, stdout, stdin); selected.shell(command, stdout, stdin);
} }
@ -147,29 +175,37 @@ 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)) {
String remotePath = readString(input, length); syncSend(output, input, length);
int idx = remotePath.lastIndexOf(',');
String path = remotePath;
int mode = 0666;
if (idx > 0) {
path = remotePath.substring(0, idx);
mode = Integer.parseInt(remotePath.substring(idx + 1));
}
SyncTransport transport = getSyncTransport(output, input);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
transport.readChunksTo(buffer);
selected.filePushed(new RemoteFile(path), mode, buffer);
transport.sendStatus("OKAY", 0); // 0 = ignored
} else if ("RECV".equals(id)) { } else if ("RECV".equals(id)) {
String remotePath = readString(input, length); syncRecv(output, 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); } 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);
int idx = remotePath.lastIndexOf(',');
String path = remotePath;
int mode = 0666;
if (idx > 0) {
path = remotePath.substring(0, idx);
mode = Integer.parseInt(remotePath.substring(idx + 1));
}
SyncTransport transport = getSyncTransport(output, input);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
transport.readChunksTo(buffer);
selected.filePushed(new RemoteFile(path), mode, buffer);
transport.sendStatus("OKAY", 0); // 0 = ignored
}
private String getCommandLength(String command) { private String getCommandLength(String command) {
return String.format("%04x", command.length()); return String.format("%04x", command.length());
} }