reuseable jadbDevices and the shell command returns a string

This commit is contained in:
Gergő Törcsvári 2016-02-28 03:04:59 +01:00
parent 31456f7a36
commit 5ab126ddbe
10 changed files with 69 additions and 23 deletions

View File

@ -1,5 +1,3 @@
<component name="CopyrightManager"> <component name="CopyrightManager">
<settings default=""> <settings default="" />
<module2copyright />
</settings>
</component> </component>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" /> <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false">
<file url="PROJECT" charset="UTF-8" />
</component>
</project> </project>

View File

@ -2,6 +2,7 @@
<library name="junit-4.10"> <library name="junit-4.10">
<CLASSES> <CLASSES>
<root url="jar://$PROJECT_DIR$/lib/junit-4.10.jar!/" /> <root url="jar://$PROJECT_DIR$/lib/junit-4.10.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/commons-io-2.4.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES /> <SOURCES />

View File

@ -3,8 +3,7 @@
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<entry_points version="2.0" /> <entry_points version="2.0" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="MockedTestCases" type="JUnit" factoryName="JUnit" nameIsGenerated="true"> <configuration default="false" name="MockedTestCases" type="JUnit" factoryName="JUnit" nameIsGenerated="true">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="jadb" /> <module name="jadb" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" /> <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" value="" /> <option name="ALTERNATIVE_JRE_PATH" value="" />

BIN
lib/commons-io-2.4.jar Normal file

Binary file not shown.

View File

@ -27,10 +27,18 @@ public class JadbConnection {
main = createTransport(); main = createTransport();
} }
protected Transport getMain(){
return main;
}
private Transport createTransport() throws IOException { private Transport createTransport() throws IOException {
return new Transport(new Socket(host, port)); return new Transport(new Socket(host, port));
} }
public Transport getFreshTransport() throws IOException {
return createTransport();
}
public void getHostVersion() throws IOException, JadbException { public void getHostVersion() throws IOException, JadbException {
main.send("host:version"); main.send("host:version");
main.verifyResponse(); main.verifyResponse();
@ -53,14 +61,14 @@ public class JadbConnection {
{ {
String[] parts = line.split("\t"); String[] parts = line.split("\t");
if (parts.length > 1) { if (parts.length > 1) {
devices.add(new JadbDevice(parts[0], parts[1], main)); devices.add(new JadbDevice(parts[0], parts[1], this));
} }
} }
return devices; return devices;
} }
public JadbDevice getAnyDevice() { public JadbDevice getAnyDevice() {
return JadbDevice.createAny(main); return JadbDevice.createAny(this);
} }
public void close() throws IOException { public void close() throws IOException {

View File

@ -6,28 +6,26 @@ import java.util.List;
public class JadbDevice { public class JadbDevice {
private final String serial; private final String serial;
private final Transport transport; private Transport transport;
private boolean selected = false; private final JadbConnection connection;
JadbDevice(String serial, String type, Transport transport) { JadbDevice(String serial, String type, JadbConnection connection) {
this.serial = serial; this.serial = serial;
this.transport = transport; this.connection = connection;
this.transport = connection.getMain();
} }
static JadbDevice createAny(Transport transport) { return new JadbDevice(transport); } static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
private JadbDevice(Transport transport) private JadbDevice(JadbConnection connection)
{ {
serial = null; serial = null;
this.transport = transport; this.connection = connection;
this.transport = connection.getMain();
} }
private void ensureTransportIsSelected() throws IOException, JadbException { private void ensureTransportIsSelected() throws IOException, JadbException {
if (!selected)
{
selectTransport(); selectTransport();
selected = true;
}
} }
private void selectTransport() throws IOException, JadbException { private void selectTransport() throws IOException, JadbException {
@ -56,7 +54,7 @@ public class JadbDevice {
return transport.readString(); return transport.readString();
} }
public void executeShell(String command, String ... args) throws IOException, JadbException { public String executeShell(String command, String ... args) throws IOException, JadbException {
ensureTransportIsSelected(); ensureTransportIsSelected();
StringBuilder shellLine = new StringBuilder(command); StringBuilder shellLine = new StringBuilder(command);
@ -68,6 +66,9 @@ public class JadbDevice {
shellLine.append(arg); shellLine.append(arg);
} }
send("shell:" + shellLine.toString()); send("shell:" + shellLine.toString());
String ret = this.transport.readResponse();
reOpenTransport();
return ret;
} }
public List<RemoteFile> list(String remotePath) throws IOException, JadbException { public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
@ -80,6 +81,7 @@ public class JadbDevice {
{ {
result.add(dent); result.add(dent);
} }
reOpenTransport();
return result; return result;
} }
@ -97,6 +99,7 @@ public class JadbDevice {
sync.sendStatus("DONE", (int)lastModified); sync.sendStatus("DONE", (int)lastModified);
sync.verifyStatus(); sync.verifyStatus();
reOpenTransport();
} }
public void push(File local, RemoteFile remote) throws IOException, JadbException { public void push(File local, RemoteFile remote) throws IOException, JadbException {
@ -111,6 +114,7 @@ public class JadbDevice {
sync.send("RECV", remote.getPath()); sync.send("RECV", remote.getPath());
sync.readChunksTo(destination); sync.readChunksTo(destination);
reOpenTransport();
} }
public void pull(RemoteFile remote, File local) throws IOException, JadbException { public void pull(RemoteFile remote, File local) throws IOException, JadbException {
@ -124,6 +128,11 @@ public class JadbDevice {
transport.verifyResponse(); transport.verifyResponse();
} }
private void reOpenTransport() throws IOException {
transport.close();
transport = connection.getFreshTransport();
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -1,5 +1,8 @@
package se.vidstige.jadb; package se.vidstige.jadb;
import org.apache.commons.io.IOUtils;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -24,6 +27,10 @@ class Transport {
return readString(length); return readString(length);
} }
public String readResponse() throws IOException {
return new String(IOUtils.toByteArray(inputStream), Charset.forName("utf-8"));
}
public void verifyResponse() throws IOException, JadbException { public void verifyResponse() throws IOException, JadbException {
String response = readString(4); String response = readString(4);
if (!"OKAY".equals(response)) if (!"OKAY".equals(response))

View File

@ -37,6 +37,12 @@ public class RealDeviceTestCases {
{ {
System.out.println(f.getPath()); System.out.println(f.getPath());
} }
//second read on the same device
List<RemoteFile> files2 = any.list("/");
for (RemoteFile f : files2)
{
System.out.println(f.getPath());
}
} }
@Test @Test
@ -45,6 +51,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md")); any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
//second read on the same device
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
} }
@Test(expected = JadbException.class) @Test(expected = JadbException.class)
@ -61,6 +69,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md")); any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
//second read on the same device
any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
} }
@Test(expected = JadbException.class) @Test(expected = JadbException.class)
@ -70,4 +80,16 @@ public class RealDeviceTestCases {
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/file/does/not/exist"), new File("xyz")); any.pull(new RemoteFile("/file/does/not/exist"), new File("xyz"));
} }
@Test
public void testShell() throws Exception
{
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
String s=any.executeShell("ls -la");
System.out.println(s);
//second read on the same device
String s2=any.executeShell("ls");
System.out.println(s2);
}
} }