From 272a29a7503dc817d935a6c75167ba16bd2aa089 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Wed, 19 Mar 2014 10:06:40 +0100 Subject: [PATCH] Now possible to list files on device --- src/se/vidstige/jadb/AndroidDevice.java | 15 +++++- src/se/vidstige/jadb/DirectoryEntry.java | 20 ++++++++ src/se/vidstige/jadb/SyncTransport.java | 47 +++++++++++++++++++ src/se/vidstige/jadb/Transport.java | 13 ++--- test/se/vidstige/jadb/test/JadbTestCases.java | 14 ++++-- 5 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 src/se/vidstige/jadb/DirectoryEntry.java create mode 100644 src/se/vidstige/jadb/SyncTransport.java diff --git a/src/se/vidstige/jadb/AndroidDevice.java b/src/se/vidstige/jadb/AndroidDevice.java index 4b555d1..0eb0680 100644 --- a/src/se/vidstige/jadb/AndroidDevice.java +++ b/src/se/vidstige/jadb/AndroidDevice.java @@ -63,8 +63,19 @@ public class AndroidDevice { } send("shell:" + shellLine.toString()); } - - public void push(String localPath, String remotePath) throws IOException, JadbException { + + public void list(String remotePath) throws IOException, JadbException { + selectTransport(); + SyncTransport sync = transport.startSync(); + sync.send("LIST", remotePath); + + for (DirectoryEntry dent = sync.readDirectoryEntry(); dent != DirectoryEntry.DONE; dent = sync.readDirectoryEntry()) + { + System.out.println(dent.getName()); + } + } + + public void push(String localPath, String remotePath) throws IOException, JadbException { selectTransport(); } diff --git a/src/se/vidstige/jadb/DirectoryEntry.java b/src/se/vidstige/jadb/DirectoryEntry.java new file mode 100644 index 0000000..bce4cc8 --- /dev/null +++ b/src/se/vidstige/jadb/DirectoryEntry.java @@ -0,0 +1,20 @@ +package se.vidstige.jadb; + +import java.io.File; + +/** + * Created by vidstige on 2014-03-19. + */ +class DirectoryEntry { + public static final DirectoryEntry DONE = new DirectoryEntry("DONE", null); + private String name; + + public DirectoryEntry(String id, String name) { + this.name = name; + } + + public String getName() { + + return name; + } +} diff --git a/src/se/vidstige/jadb/SyncTransport.java b/src/se/vidstige/jadb/SyncTransport.java new file mode 100644 index 0000000..58ba9a7 --- /dev/null +++ b/src/se/vidstige/jadb/SyncTransport.java @@ -0,0 +1,47 @@ +package se.vidstige.jadb; + +import java.io.*; +import java.nio.charset.Charset; + +/** + * Created by vidstige on 2014-03-19. + */ +class SyncTransport { + + private final DataOutputStream output; + private final DataInputStream input; + + public SyncTransport(OutputStream outputStream, InputStream inputStream) { + output = new DataOutputStream(outputStream); + input = new DataInputStream(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); + output.writeInt(Integer.reverseBytes(name.length())); + output.writeBytes(name); + } + + private int readInt() throws IOException { + return Integer.reverseBytes(input.readInt()); + } + + private String readString(int length) throws IOException { + byte[] buffer = new byte[length]; + input.read(buffer); + return new String(buffer, Charset.forName("utf-8")); + } + + public DirectoryEntry readDirectoryEntry() throws IOException { + String id = readString(4); + int mode = readInt(); + int size = readInt(); + int time = readInt(); + int nameLenght = readInt(); + String name = readString(nameLenght); + + if ("DENT".equals(id) == false) return DirectoryEntry.DONE; + return new DirectoryEntry(id, name); + } +} diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 4de3dee..7897847 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -1,11 +1,6 @@ package se.vidstige.jadb; -import java.io.DataInput; -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; +import java.io.*; import java.net.Socket; import java.nio.charset.Charset; @@ -56,4 +51,10 @@ class Transport { writer.write(command); writer.flush(); } + + public SyncTransport startSync() throws IOException, JadbException { + send("sync:"); + verifyResponse(); + return new SyncTransport(outputStream, inputStream); + } } diff --git a/test/se/vidstige/jadb/test/JadbTestCases.java b/test/se/vidstige/jadb/test/JadbTestCases.java index 958094d..56aeced 100644 --- a/test/se/vidstige/jadb/test/JadbTestCases.java +++ b/test/se/vidstige/jadb/test/JadbTestCases.java @@ -20,9 +20,17 @@ public class JadbTestCases { @Test public void testGetDevices() throws Exception { - JadbConnection jadb = new JadbConnection("localhost", 15037); - //JadbConnection jadb = new JadbConnection(); + //JadbConnection jadb = new JadbConnection("localhost", 15037); + JadbConnection jadb = new JadbConnection(); List actual = jadb.getDevices(); - Assert.assertEquals("emulator-5554", actual.get(0).getSerial()); + //Assert.assertEquals("emulator-5554", actual.get(0).getSerial()); } + + @Test + public void testListFiles() throws Exception + { + JadbConnection jadb = new JadbConnection(); + AndroidDevice any = jadb.getAnyDevice(); + any.list("/"); + } }