mirror of https://github.com/revanced/jadb.git
Refactor: Consistent whitespace & brace style
This commit is contained in:
parent
d716ba0325
commit
41872e3bb9
|
@ -5,57 +5,53 @@ import java.net.Socket;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JadbConnection implements ITransportFactory{
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
private static final int DEFAULTPORT = 5037;
|
||||
|
||||
public JadbConnection() throws IOException
|
||||
{
|
||||
this("localhost", DEFAULTPORT);
|
||||
}
|
||||
|
||||
public JadbConnection(String host, int port) throws IOException
|
||||
{
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
public class JadbConnection implements ITransportFactory {
|
||||
|
||||
public Transport createTransport() throws IOException {
|
||||
return new Transport(new Socket(host, port));
|
||||
}
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
public void getHostVersion() throws IOException, JadbException {
|
||||
Transport main = createTransport();
|
||||
main.send("host:version");
|
||||
main.verifyResponse();
|
||||
main.close();
|
||||
}
|
||||
private static final int DEFAULTPORT = 5037;
|
||||
|
||||
public List<JadbDevice> getDevices() throws IOException, JadbException
|
||||
{
|
||||
Transport devices = createTransport();
|
||||
|
||||
devices.send("host:devices");
|
||||
devices.verifyResponse();
|
||||
String body = devices.readString();
|
||||
return parseDevices(body);
|
||||
}
|
||||
public JadbConnection() throws IOException {
|
||||
this("localhost", DEFAULTPORT);
|
||||
}
|
||||
|
||||
private List<JadbDevice> parseDevices(String body) {
|
||||
String[] lines = body.split("\n");
|
||||
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
||||
for (String line : lines)
|
||||
{
|
||||
String[] parts = line.split("\t");
|
||||
public JadbConnection(String host, int port) throws IOException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Transport createTransport() throws IOException {
|
||||
return new Transport(new Socket(host, port));
|
||||
}
|
||||
|
||||
public void getHostVersion() throws IOException, JadbException {
|
||||
Transport main = createTransport();
|
||||
main.send("host:version");
|
||||
main.verifyResponse();
|
||||
main.close();
|
||||
}
|
||||
|
||||
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
||||
Transport devices = createTransport();
|
||||
|
||||
devices.send("host:devices");
|
||||
devices.verifyResponse();
|
||||
String body = devices.readString();
|
||||
return parseDevices(body);
|
||||
}
|
||||
|
||||
private List<JadbDevice> parseDevices(String body) {
|
||||
String[] lines = body.split("\n");
|
||||
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
||||
for (String line : lines) {
|
||||
String[] parts = line.split("\t");
|
||||
if (parts.length > 1) {
|
||||
devices.add(new JadbDevice(parts[0], parts[1], this));
|
||||
devices.add(new JadbDevice(parts[0], parts[1], this));
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
public JadbDevice getAnyDevice() {
|
||||
return JadbDevice.createAny(this);
|
||||
|
|
|
@ -5,58 +5,54 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public class JadbDevice {
|
||||
private final String serial;
|
||||
private final String serial;
|
||||
private final ITransportFactory transportFactory;
|
||||
|
||||
JadbDevice(String serial, String type, ITransportFactory tFactory) {
|
||||
this.serial = serial;
|
||||
JadbDevice(String serial, String type, ITransportFactory tFactory) {
|
||||
this.serial = serial;
|
||||
this.transportFactory = tFactory;
|
||||
}
|
||||
}
|
||||
|
||||
static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
|
||||
static JadbDevice createAny(JadbConnection connection) {
|
||||
return new JadbDevice(connection);
|
||||
}
|
||||
|
||||
private JadbDevice(ITransportFactory tFactory)
|
||||
{
|
||||
private JadbDevice(ITransportFactory tFactory) {
|
||||
serial = null;
|
||||
this.transportFactory = tFactory;
|
||||
}
|
||||
|
||||
private Transport getTransport() throws IOException, JadbException {
|
||||
Transport transport = transportFactory.createTransport();
|
||||
if (serial == null)
|
||||
{
|
||||
if (serial == null) {
|
||||
transport.send("host:transport-any");
|
||||
transport.verifyResponse();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
transport.send("host:transport:" + serial);
|
||||
transport.verifyResponse();
|
||||
}
|
||||
return transport;
|
||||
}
|
||||
|
||||
public String getSerial()
|
||||
{
|
||||
return serial;
|
||||
}
|
||||
public String getSerial() {
|
||||
return serial;
|
||||
}
|
||||
|
||||
public String getState() throws IOException, JadbException {
|
||||
public String getState() throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
transport.send("get-state");
|
||||
transport.verifyResponse();
|
||||
return transport.readString();
|
||||
}
|
||||
transport.send("get-state");
|
||||
transport.verifyResponse();
|
||||
return transport.readString();
|
||||
}
|
||||
|
||||
public void executeShell(String command, String ... args) throws IOException, JadbException {
|
||||
public void executeShell(String command, String... args) throws IOException, JadbException {
|
||||
executeShell(null, command, args);
|
||||
}
|
||||
}
|
||||
|
||||
public void executeShell(OutputStream stdout, String command, String ... args) throws IOException, JadbException {
|
||||
public void executeShell(OutputStream stdout, String command, String... args) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
StringBuilder shellLine = new StringBuilder(command);
|
||||
for (String arg : args)
|
||||
{
|
||||
for (String arg : args) {
|
||||
shellLine.append(" ");
|
||||
// TODO: throw if arg contains double quote
|
||||
// TODO: quote arg if it contains space
|
||||
|
@ -70,31 +66,29 @@ public class JadbDevice {
|
|||
|
||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("LIST", remotePath);
|
||||
|
||||
List<RemoteFile> result = new ArrayList<RemoteFile>();
|
||||
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry())
|
||||
{
|
||||
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) {
|
||||
result.add(dent);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private int getMode(File file)
|
||||
{
|
||||
private int getMode(File file) {
|
||||
//noinspection OctalInteger
|
||||
return 0664;
|
||||
}
|
||||
|
||||
public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("SEND", remote.getPath() + "," + Integer.toString(mode));
|
||||
|
||||
sync.sendStream(source);
|
||||
|
||||
sync.sendStatus("DONE", (int)lastModified);
|
||||
sync.sendStatus("DONE", (int) lastModified);
|
||||
sync.verifyStatus();
|
||||
}
|
||||
|
||||
|
@ -102,7 +96,7 @@ public class JadbDevice {
|
|||
FileInputStream fileStream = new FileInputStream(local);
|
||||
push(fileStream, local.lastModified(), getMode(local), remote);
|
||||
fileStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
|
@ -118,39 +112,38 @@ public class JadbDevice {
|
|||
fileStream.close();
|
||||
}
|
||||
|
||||
private void send(Transport transport, String command) throws IOException, JadbException {
|
||||
transport.send(command);
|
||||
private void send(Transport transport, String command) throws IOException, JadbException {
|
||||
transport.send(command);
|
||||
transport.verifyResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Android Device with serial " + serial;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((serial == null) ? 0 : serial.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Android Device with serial " + serial;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
JadbDevice other = (JadbDevice) obj;
|
||||
if (serial == null) {
|
||||
if (other.serial != null)
|
||||
return false;
|
||||
} else if (!serial.equals(other.serial))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((serial == null) ? 0 : serial.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
JadbDevice other = (JadbDevice) obj;
|
||||
if (serial == null) {
|
||||
if (other.serial != null)
|
||||
return false;
|
||||
} else if (!serial.equals(other.serial))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,9 @@ package se.vidstige.jadb;
|
|||
|
||||
public class JadbException extends Exception {
|
||||
|
||||
public JadbException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -3879283786835654165L;
|
||||
public JadbException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -3879283786835654165L;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ package se.vidstige.jadb;
|
|||
public class RemoteFile {
|
||||
private final String path;
|
||||
|
||||
public RemoteFile(String path) { this.path = path;}
|
||||
public RemoteFile(String path) { this.path = path; }
|
||||
|
||||
public String getName() { throw new UnsupportedOperationException(); }
|
||||
public int getSize() { throw new UnsupportedOperationException(); }
|
||||
|
|
|
@ -3,7 +3,7 @@ package se.vidstige.jadb;
|
|||
/**
|
||||
* Created by vidstige on 2014-03-19.
|
||||
*/
|
||||
class RemoteFileRecord extends RemoteFile{
|
||||
class RemoteFileRecord extends RemoteFile {
|
||||
public static final RemoteFileRecord DONE = new RemoteFileRecord(null, 0, 0, 0);
|
||||
|
||||
private final int mode;
|
||||
|
|
|
@ -20,6 +20,7 @@ public class SyncTransport {
|
|||
output = outputStream;
|
||||
input = inputStream;
|
||||
}
|
||||
|
||||
public void send(String syncCommand, String name) throws IOException {
|
||||
if (syncCommand.length() != 4) throw new IllegalArgumentException("sync commands must have length 4");
|
||||
output.writeBytes(syncCommand);
|
||||
|
@ -35,13 +36,11 @@ public class SyncTransport {
|
|||
public void verifyStatus() throws IOException, JadbException {
|
||||
String status = readString(4);
|
||||
int length = readInt();
|
||||
if ("FAIL".equals(status))
|
||||
{
|
||||
if ("FAIL".equals(status)) {
|
||||
String error = readString(length);
|
||||
throw new JadbException(error);
|
||||
}
|
||||
if (!"OKAY".equals(status))
|
||||
{
|
||||
if (!"OKAY".equals(status)) {
|
||||
throw new JadbException("Unknown error: " + status);
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +76,7 @@ public class SyncTransport {
|
|||
private int readChunk(byte[] buffer) throws IOException, JadbException {
|
||||
String id = readString(4);
|
||||
int n = readInt();
|
||||
if ("FAIL".equals(id))
|
||||
{
|
||||
if ("FAIL".equals(id)) {
|
||||
throw new JadbException(readString(n));
|
||||
}
|
||||
if (!"DATA".equals(id)) return -1;
|
||||
|
|
|
@ -6,63 +6,62 @@ import java.nio.charset.Charset;
|
|||
|
||||
class Transport {
|
||||
|
||||
private final OutputStream outputStream;
|
||||
private final InputStream inputStream;
|
||||
private final OutputStream outputStream;
|
||||
private final InputStream inputStream;
|
||||
|
||||
private Transport(OutputStream outputStream, InputStream inputStream) {
|
||||
this.outputStream = outputStream;
|
||||
this.inputStream = inputStream;
|
||||
}
|
||||
private Transport(OutputStream outputStream, InputStream inputStream) {
|
||||
this.outputStream = outputStream;
|
||||
this.inputStream = inputStream;
|
||||
}
|
||||
|
||||
public Transport(Socket socket) throws IOException {
|
||||
this(socket.getOutputStream(), socket.getInputStream());
|
||||
}
|
||||
public Transport(Socket socket) throws IOException {
|
||||
this(socket.getOutputStream(), socket.getInputStream());
|
||||
}
|
||||
|
||||
public String readString() throws IOException {
|
||||
String encodedLength = readString(4);
|
||||
int length = Integer.parseInt(encodedLength, 16);
|
||||
return readString(length);
|
||||
}
|
||||
public String readString() throws IOException {
|
||||
String encodedLength = readString(4);
|
||||
int length = Integer.parseInt(encodedLength, 16);
|
||||
return readString(length);
|
||||
}
|
||||
|
||||
|
||||
private static void copy(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buffer = new byte[1024 * 10];
|
||||
int len;
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
private static void copy(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buffer = new byte[1024 * 10];
|
||||
int len;
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
public void readResponseTo(OutputStream output) throws IOException {
|
||||
copy(inputStream, output);
|
||||
}
|
||||
public void readResponseTo(OutputStream output) throws IOException {
|
||||
copy(inputStream, output);
|
||||
}
|
||||
|
||||
public void verifyResponse() throws IOException, JadbException {
|
||||
String response = readString(4);
|
||||
if (!"OKAY".equals(response))
|
||||
{
|
||||
public void verifyResponse() throws IOException, JadbException {
|
||||
String response = readString(4);
|
||||
if (!"OKAY".equals(response)) {
|
||||
String error = readString();
|
||||
throw new JadbException("command failed: " + error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String readString(int length) throws IOException {
|
||||
DataInput reader = new DataInputStream(inputStream);
|
||||
byte[] responseBuffer = new byte[length];
|
||||
reader.readFully(responseBuffer);
|
||||
public String readString(int length) throws IOException {
|
||||
DataInput reader = new DataInputStream(inputStream);
|
||||
byte[] responseBuffer = new byte[length];
|
||||
reader.readFully(responseBuffer);
|
||||
return new String(responseBuffer, Charset.forName("utf-8"));
|
||||
}
|
||||
}
|
||||
|
||||
public String getCommandLength(String command) {
|
||||
return String.format("%04x", command.length());
|
||||
}
|
||||
|
||||
public void send(String command) throws IOException {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
|
||||
writer.write(getCommandLength(command));
|
||||
writer.write(command);
|
||||
writer.flush();
|
||||
}
|
||||
public String getCommandLength(String command) {
|
||||
return String.format("%04x", command.length());
|
||||
}
|
||||
|
||||
public void send(String command) throws IOException {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
|
||||
writer.write(getCommandLength(command));
|
||||
writer.write(command);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public SyncTransport startSync() throws IOException, JadbException {
|
||||
send("sync:");
|
||||
|
|
|
@ -10,27 +10,25 @@ import java.net.Socket;
|
|||
import java.nio.charset.Charset;
|
||||
|
||||
class AdbProtocolHandler implements Runnable {
|
||||
private final Socket socket;
|
||||
private final Socket socket;
|
||||
private final AdbResponder responder;
|
||||
private AdbDeviceResponder selected;
|
||||
|
||||
public AdbProtocolHandler(Socket socket, AdbResponder responder) {
|
||||
this.socket = socket;
|
||||
this.socket = socket;
|
||||
this.responder = responder;
|
||||
}
|
||||
|
||||
private AdbDeviceResponder findDevice(String serial) throws ProtocolException {
|
||||
for (AdbDeviceResponder d : responder.getDevices())
|
||||
{
|
||||
for (AdbDeviceResponder d : responder.getDevices()) {
|
||||
if (d.getSerial().equals(serial)) return d;
|
||||
}
|
||||
throw new ProtocolException("'" + serial + "' not connected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try{
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runServer();
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage() != null) // thrown when exiting for some reason
|
||||
|
@ -42,8 +40,7 @@ class AdbProtocolHandler implements Runnable {
|
|||
DataInput input = new DataInputStream(socket.getInputStream());
|
||||
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
byte[] buffer = new byte[4];
|
||||
input.readFully(buffer);
|
||||
String encodedLength = new String(buffer, Charset.forName("utf-8"));
|
||||
|
@ -55,47 +52,35 @@ class AdbProtocolHandler implements Runnable {
|
|||
|
||||
responder.onCommand(command);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
if ("host:version".equals(command)) {
|
||||
output.writeBytes("OKAY");
|
||||
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.
|
||||
selected = responder.getDevices().get(0);
|
||||
output.writeBytes("OKAY");
|
||||
}
|
||||
else if ("host:devices".equals(command)) {
|
||||
} else if ("host:devices".equals(command)) {
|
||||
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
|
||||
DataOutputStream writer = new DataOutputStream(tmp);
|
||||
for (AdbDeviceResponder d : responder.getDevices())
|
||||
{
|
||||
for (AdbDeviceResponder d : responder.getDevices()) {
|
||||
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
|
||||
}
|
||||
output.writeBytes("OKAY");
|
||||
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
|
||||
}
|
||||
else if (command.startsWith("host:transport:"))
|
||||
{
|
||||
} else if (command.startsWith("host:transport:")) {
|
||||
String serial = command.substring("host:transport:".length());
|
||||
selected = findDevice(serial);
|
||||
output.writeBytes("OKAY");
|
||||
}
|
||||
else if ("sync:".equals(command)) {
|
||||
} else if ("sync:".equals(command)) {
|
||||
output.writeBytes("OKAY");
|
||||
try
|
||||
{
|
||||
try {
|
||||
sync(output, input);
|
||||
}
|
||||
catch (JadbException e) { // sync response with a different type of fail message
|
||||
} catch (JadbException e) { // sync response with a different type of fail message
|
||||
SyncTransport sync = new SyncTransport(output, input);
|
||||
sync.send("FAIL", e.getMessage());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
throw new ProtocolException("Unknown command: " + command);
|
||||
}
|
||||
} catch (ProtocolException e) {
|
||||
|
@ -119,14 +104,12 @@ class AdbProtocolHandler implements Runnable {
|
|||
private void sync(DataOutput output, DataInput input) throws IOException, JadbException {
|
||||
String id = readString(input, 4);
|
||||
int length = readInt(input);
|
||||
if ("SEND".equals(id))
|
||||
{
|
||||
if ("SEND".equals(id)) {
|
||||
String remotePath = readString(input, length);
|
||||
int idx = remotePath.lastIndexOf(',');
|
||||
String path = remotePath;
|
||||
int mode = 0666;
|
||||
if (idx > 0)
|
||||
{
|
||||
if (idx > 0) {
|
||||
path = remotePath.substring(0, idx);
|
||||
mode = Integer.parseInt(remotePath.substring(idx + 1));
|
||||
}
|
||||
|
@ -135,24 +118,22 @@ class AdbProtocolHandler implements Runnable {
|
|||
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);
|
||||
SyncTransport transport = new SyncTransport(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 String getCommandLength(String command) {
|
||||
return String.format("%04x", command.length());
|
||||
}
|
||||
|
||||
public void send(DataOutput writer, String response) throws IOException {
|
||||
writer.writeBytes(getCommandLength(response));
|
||||
writer.writeBytes(response);
|
||||
}
|
||||
return String.format("%04x", command.length());
|
||||
}
|
||||
|
||||
public void send(DataOutput writer, String response) throws IOException {
|
||||
writer.writeBytes(getCommandLength(response));
|
||||
writer.writeBytes(response);
|
||||
}
|
||||
}
|
|
@ -10,8 +10,7 @@ public class AdbServer extends SocketServer {
|
|||
public static final int DEFAULT_PORT = 15037;
|
||||
private final AdbResponder responder;
|
||||
|
||||
public AdbServer(AdbResponder responder)
|
||||
{
|
||||
public AdbServer(AdbResponder responder) {
|
||||
this(responder, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,51 +7,47 @@ import java.net.Socket;
|
|||
// >set ANDROID_ADB_SERVER_PORT=15037
|
||||
public abstract class SocketServer implements Runnable {
|
||||
|
||||
private final int port;
|
||||
private ServerSocket socket;
|
||||
private Thread thread;
|
||||
private final Object lockObject = new Object();
|
||||
private final int port;
|
||||
private ServerSocket socket;
|
||||
private Thread thread;
|
||||
private final Object lockObject = new Object();
|
||||
|
||||
protected SocketServer(int port)
|
||||
{
|
||||
protected SocketServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void start() throws InterruptedException
|
||||
{
|
||||
thread = new Thread(this, "Fake Adb Server");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
synchronized (lockObject) {
|
||||
lockObject.wait();
|
||||
}
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("Starting on port " + port);
|
||||
socket = new ServerSocket(port);
|
||||
socket.setReuseAddress(true);
|
||||
|
||||
synchronized (lockObject) {
|
||||
lockObject.notify();
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
Socket c = socket.accept();
|
||||
Thread clientThread = new Thread(createResponder(c), "AdbClientWorker");
|
||||
clientThread.setDaemon(true);
|
||||
clientThread.start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
public void start() throws InterruptedException {
|
||||
thread = new Thread(this, "Fake Adb Server");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
synchronized (lockObject) {
|
||||
lockObject.wait();
|
||||
}
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
socket = new ServerSocket(port);
|
||||
socket.setReuseAddress(true);
|
||||
|
||||
synchronized (lockObject) {
|
||||
lockObject.notify();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
Socket c = socket.accept();
|
||||
Thread clientThread = new Thread(createResponder(c), "AdbClientWorker");
|
||||
clientThread.setDaemon(true);
|
||||
clientThread.start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Runnable createResponder(Socket socket);
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ public class MockedTestCases {
|
|||
private JadbConnection connection;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception{
|
||||
public void setUp() throws Exception {
|
||||
server = new FakeAdbServer(15037);
|
||||
server.start();
|
||||
connection = new JadbConnection("localhost", 15037);
|
||||
|
@ -81,5 +81,4 @@ public class MockedTestCases {
|
|||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
return dateFormat.parse(date).getTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,40 +11,35 @@ import java.util.List;
|
|||
|
||||
public class RealDeviceTestCases {
|
||||
|
||||
@Test
|
||||
public void testGetHostVersion() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
jadb.getHostVersion();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDevices() throws Exception
|
||||
{
|
||||
//JadbConnection jadb = new JadbConnection("localhost", 15037);
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
List<JadbDevice> actual = jadb.getDevices();
|
||||
//Assert.assertEquals("emulator-5554", actual.get(0).getSerial());
|
||||
}
|
||||
@Test
|
||||
public void testGetHostVersion() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
jadb.getHostVersion();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListFilesTwice() throws Exception
|
||||
{
|
||||
public void testGetDevices() throws Exception {
|
||||
//JadbConnection jadb = new JadbConnection("localhost", 15037);
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
List<JadbDevice> actual = jadb.getDevices();
|
||||
//Assert.assertEquals("emulator-5554", actual.get(0).getSerial());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListFilesTwice() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
for (RemoteFile f : any.list("/"))
|
||||
{
|
||||
for (RemoteFile f : any.list("/")) {
|
||||
System.out.println(f.getPath());
|
||||
}
|
||||
|
||||
for (RemoteFile f : any.list("/"))
|
||||
{
|
||||
for (RemoteFile f : any.list("/")) {
|
||||
System.out.println(f.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPushFile() throws Exception
|
||||
{
|
||||
public void testPushFile() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
|
||||
|
@ -53,16 +48,14 @@ public class RealDeviceTestCases {
|
|||
}
|
||||
|
||||
@Test(expected = JadbException.class)
|
||||
public void testPushFileToInvalidPath() throws Exception
|
||||
{
|
||||
public void testPushFileToInvalidPath() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
any.push(new File("README.md"), new RemoteFile("/no/such/directory/README.md"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPullFile() throws Exception
|
||||
{
|
||||
public void testPullFile() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
|
||||
|
@ -71,16 +64,14 @@ public class RealDeviceTestCases {
|
|||
}
|
||||
|
||||
@Test(expected = JadbException.class)
|
||||
public void testPullInvalidFile() throws Exception
|
||||
{
|
||||
public void testPullInvalidFile() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
any.pull(new RemoteFile("/file/does/not/exist"), new File("xyz"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShellExecuteTwice() throws Exception
|
||||
{
|
||||
public void testShellExecuteTwice() throws Exception {
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
any.executeShell(System.out, "ls /");
|
||||
|
|
|
@ -23,18 +23,19 @@ public class FakeAdbServer implements AdbResponder {
|
|||
server = new AdbServer(this, port);
|
||||
}
|
||||
|
||||
|
||||
public void start() throws InterruptedException {
|
||||
System.out.println("Starting fake on port " + server.getPort());
|
||||
server.start();
|
||||
}
|
||||
|
||||
public void stop() throws IOException, InterruptedException {
|
||||
System.out.println("Stopping fake on port " + server.getPort());
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(String command) {
|
||||
System.out.println("command: " +command);
|
||||
System.out.println("command: " + command);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -42,8 +43,7 @@ public class FakeAdbServer implements AdbResponder {
|
|||
return 31;
|
||||
}
|
||||
|
||||
public void add(String serial)
|
||||
{
|
||||
public void add(String serial) {
|
||||
devices.add(new DeviceResponder(serial));
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,9 @@ public class FakeAdbServer implements AdbResponder {
|
|||
|
||||
public interface ExpectationBuilder {
|
||||
void failWith(String message);
|
||||
|
||||
void withContent(byte[] content);
|
||||
|
||||
void withContent(String content);
|
||||
}
|
||||
|
||||
|
@ -65,8 +67,7 @@ public class FakeAdbServer implements AdbResponder {
|
|||
return null;
|
||||
}
|
||||
|
||||
public ExpectationBuilder expectPush(String serial, RemoteFile path)
|
||||
{
|
||||
public ExpectationBuilder expectPush(String serial, RemoteFile path) {
|
||||
return findBySerial(serial).expectPush(path);
|
||||
}
|
||||
|
||||
|
@ -100,8 +101,7 @@ public class FakeAdbServer implements AdbResponder {
|
|||
@Override
|
||||
public void filePushed(RemoteFile path, int mode, ByteArrayOutputStream buffer) throws JadbException {
|
||||
for (FileExpectation fe : expectations) {
|
||||
if (fe.matches(path))
|
||||
{
|
||||
if (fe.matches(path)) {
|
||||
expectations.remove(fe);
|
||||
fe.throwIfFail();
|
||||
fe.verifyContent(buffer.toByteArray());
|
||||
|
@ -114,8 +114,7 @@ public class FakeAdbServer implements AdbResponder {
|
|||
@Override
|
||||
public void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException {
|
||||
for (FileExpectation fe : expectations) {
|
||||
if (fe.matches(path))
|
||||
{
|
||||
if (fe.matches(path)) {
|
||||
expectations.remove(fe);
|
||||
fe.throwIfFail();
|
||||
fe.returnFile(buffer);
|
||||
|
@ -184,6 +183,5 @@ public class FakeAdbServer implements AdbResponder {
|
|||
expectations.add(expectation);
|
||||
return expectation;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue