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">
<settings default="">
<module2copyright />
</settings>
<settings default="" />
</component>

View File

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

View File

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

View File

@ -3,8 +3,7 @@
<component name="EntryPointsManager">
<entry_points version="2.0" />
</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" />
</component>
</project>
</project>

View File

@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
<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" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<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();
}
protected Transport getMain(){
return main;
}
private Transport createTransport() throws IOException {
return new Transport(new Socket(host, port));
}
public Transport getFreshTransport() throws IOException {
return createTransport();
}
public void getHostVersion() throws IOException, JadbException {
main.send("host:version");
main.verifyResponse();
@ -53,14 +61,14 @@ public class JadbConnection {
{
String[] parts = line.split("\t");
if (parts.length > 1) {
devices.add(new JadbDevice(parts[0], parts[1], main));
devices.add(new JadbDevice(parts[0], parts[1], this));
}
}
return devices;
}
public JadbDevice getAnyDevice() {
return JadbDevice.createAny(main);
return JadbDevice.createAny(this);
}
public void close() throws IOException {

View File

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

View File

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

View File

@ -37,6 +37,12 @@ public class RealDeviceTestCases {
{
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
@ -45,6 +51,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
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)
@ -61,6 +69,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
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)
@ -70,4 +80,16 @@ public class RealDeviceTestCases {
JadbDevice any = jadb.getAnyDevice();
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);
}
}