mirror of
https://github.com/revanced/jadb.git
synced 2024-11-19 10:39:23 +01:00
Merge pull request #1 from tg44/master
reuseable jadbDevices and the shell command returns a string
This commit is contained in:
commit
6eb8817483
@ -1,5 +1,3 @@
|
||||
<component name="CopyrightManager">
|
||||
<settings default="">
|
||||
<module2copyright />
|
||||
</settings>
|
||||
<settings default="" />
|
||||
</component>
|
@ -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>
|
@ -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 />
|
||||
|
@ -3,8 +3,8 @@
|
||||
<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>
|
||||
|
||||
<component name="WebServicesPlugin" addRequiredLibraries="true" />
|
||||
</project>
|
@ -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
BIN
lib/commons-io-2.4.jar
Normal file
Binary file not shown.
10
src/se/vidstige/jadb/ITransportFactory.java
Normal file
10
src/se/vidstige/jadb/ITransportFactory.java
Normal file
@ -0,0 +1,10 @@
|
||||
package se.vidstige.jadb;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Törcsi on 2016. 03. 01..
|
||||
*/
|
||||
public interface ITransportFactory {
|
||||
public Transport createTransport() throws IOException;
|
||||
}
|
@ -5,15 +5,13 @@ import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JadbConnection {
|
||||
public class JadbConnection implements ITransportFactory{
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
private static final int DEFAULTPORT = 5037;
|
||||
|
||||
private final Transport main;
|
||||
|
||||
public JadbConnection() throws IOException
|
||||
{
|
||||
this("localhost", DEFAULTPORT);
|
||||
@ -23,17 +21,17 @@ public class JadbConnection {
|
||||
{
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
|
||||
main = createTransport();
|
||||
}
|
||||
|
||||
private Transport createTransport() throws IOException {
|
||||
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
|
||||
@ -53,17 +51,13 @@ 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);
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
main.close();
|
||||
return JadbDevice.createAny(this);
|
||||
}
|
||||
}
|
||||
|
@ -6,31 +6,27 @@ import java.util.List;
|
||||
|
||||
public class JadbDevice {
|
||||
private final String serial;
|
||||
private final Transport transport;
|
||||
private boolean selected = false;
|
||||
private Transport transport;
|
||||
private final ITransportFactory tFactory;
|
||||
|
||||
JadbDevice(String serial, String type, Transport transport) {
|
||||
JadbDevice(String serial, String type, ITransportFactory tFactory) {
|
||||
this.serial = serial;
|
||||
this.transport = transport;
|
||||
this.tFactory = tFactory;
|
||||
}
|
||||
|
||||
static JadbDevice createAny(Transport transport) { return new JadbDevice(transport); }
|
||||
static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
|
||||
|
||||
private JadbDevice(Transport transport)
|
||||
private JadbDevice(ITransportFactory tFactory)
|
||||
{
|
||||
serial = null;
|
||||
this.transport = transport;
|
||||
this.tFactory = tFactory;
|
||||
}
|
||||
|
||||
private void ensureTransportIsSelected() throws IOException, JadbException {
|
||||
if (!selected)
|
||||
{
|
||||
selectTransport();
|
||||
selected = true;
|
||||
private void getTransport() throws IOException, JadbException {
|
||||
if(transport!=null && !transport.isClosed()){
|
||||
transport.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void selectTransport() throws IOException, JadbException {
|
||||
transport = tFactory.createTransport();
|
||||
if (serial == null)
|
||||
{
|
||||
transport.send("host:transport-any");
|
||||
@ -50,28 +46,39 @@ public class JadbDevice {
|
||||
}
|
||||
|
||||
public String getState() throws IOException, JadbException {
|
||||
ensureTransportIsSelected();
|
||||
getTransport();
|
||||
transport.send("get-state");
|
||||
transport.verifyResponse();
|
||||
return transport.readString();
|
||||
}
|
||||
|
||||
public void executeShell(String command, String ... args) throws IOException, JadbException {
|
||||
ensureTransportIsSelected();
|
||||
|
||||
StringBuilder shellLine = new StringBuilder(command);
|
||||
for (String arg : args)
|
||||
{
|
||||
shellLine.append(" ");
|
||||
// TODO: throw if arg contains double quote
|
||||
// TODO: quote arg if it contains space
|
||||
shellLine.append(arg);
|
||||
}
|
||||
send("shell:" + shellLine.toString());
|
||||
public String executeShell(String command, String ... args) throws IOException, JadbException {
|
||||
execShell(command, args);
|
||||
String ret = this.transport.readResponse();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException {
|
||||
execShell(command, args);
|
||||
byte[] ret = this.transport.readResponseAsArray();
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void execShell(String command, String[] args) throws IOException, JadbException {
|
||||
getTransport();
|
||||
StringBuilder shellLine = new StringBuilder(command);
|
||||
for (String arg : args)
|
||||
{
|
||||
shellLine.append(" ");
|
||||
// TODO: throw if arg contains double quote
|
||||
// TODO: quote arg if it contains space
|
||||
shellLine.append(arg);
|
||||
}
|
||||
send("shell:" + shellLine.toString());
|
||||
}
|
||||
|
||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||
ensureTransportIsSelected();
|
||||
getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("LIST", remotePath);
|
||||
|
||||
@ -89,7 +96,7 @@ public class JadbDevice {
|
||||
}
|
||||
|
||||
public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException {
|
||||
ensureTransportIsSelected();
|
||||
getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("SEND", remote.getPath() + "," + Integer.toString(mode));
|
||||
|
||||
@ -106,7 +113,7 @@ public class JadbDevice {
|
||||
}
|
||||
|
||||
public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
|
||||
ensureTransportIsSelected();
|
||||
getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("RECV", remote.getPath());
|
||||
|
||||
|
@ -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;
|
||||
@ -8,6 +11,11 @@ class Transport {
|
||||
|
||||
private final OutputStream outputStream;
|
||||
private final InputStream inputStream;
|
||||
private boolean closed=false;
|
||||
|
||||
public boolean isClosed(){
|
||||
return closed;
|
||||
}
|
||||
|
||||
private Transport(OutputStream outputStream, InputStream inputStream) {
|
||||
this.outputStream = outputStream;
|
||||
@ -23,7 +31,15 @@ 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 byte[] readResponseAsArray() throws IOException {
|
||||
return repairTransportedArray(IOUtils.toByteArray(inputStream));
|
||||
}
|
||||
|
||||
public void verifyResponse() throws IOException, JadbException {
|
||||
String response = readString(4);
|
||||
if (!"OKAY".equals(response))
|
||||
@ -60,5 +76,23 @@ class Transport {
|
||||
public void close() throws IOException {
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
closed = true;
|
||||
}
|
||||
|
||||
private byte[] repairTransportedArray(byte[] encoded) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
for (int i=0; i<encoded.length; i++) {
|
||||
if (encoded.length > i+1 && encoded[i] == 0x0d && encoded[i+1] == 0x0a) {
|
||||
//skip 0x0d
|
||||
} else {
|
||||
baos.write(encoded[i]);
|
||||
}
|
||||
}
|
||||
try {
|
||||
baos.close();
|
||||
} catch (IOException ioe) {
|
||||
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class MockedTestCases {
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
connection.close();
|
||||
server.stop();
|
||||
server.verifyExpectations();
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package se.vidstige.jadb.test;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
@ -37,6 +40,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 +54,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 +72,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 +83,25 @@ 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShellArray() throws Exception
|
||||
{
|
||||
JadbConnection jadb = new JadbConnection();
|
||||
JadbDevice any = jadb.getAnyDevice();
|
||||
byte[] s=any.executeShellGetBytearr("screencap -p");
|
||||
FileUtils.writeByteArrayToFile(new File("screen.png"), s);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user