mirror of
https://github.com/revanced/jadb.git
synced 2025-05-08 18:34:32 +02:00
Parsing devices
This commit is contained in:
parent
831ee1ecbc
commit
c0330b5a13
50
src/se/vidstige/jadb/AndroidDevice.java
Normal file
50
src/se/vidstige/jadb/AndroidDevice.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package se.vidstige.jadb;
|
||||||
|
|
||||||
|
public class AndroidDevice {
|
||||||
|
private final String serial;
|
||||||
|
|
||||||
|
public AndroidDevice(String serial, String type) {
|
||||||
|
this.serial = serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AndroidDevice(String serial) {
|
||||||
|
this(serial, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSerial()
|
||||||
|
{
|
||||||
|
return serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
AndroidDevice other = (AndroidDevice) obj;
|
||||||
|
if (serial == null) {
|
||||||
|
if (other.serial != null)
|
||||||
|
return false;
|
||||||
|
} else if (!serial.equals(other.serial))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,8 @@ package se.vidstige.jadb;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class JadbConnection {
|
public class JadbConnection {
|
||||||
|
|
||||||
@ -22,16 +24,27 @@ public class JadbConnection {
|
|||||||
transport.verifyResponse();
|
transport.verifyResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getDevices() throws IOException, JadbException
|
public List<AndroidDevice> getDevices() throws IOException, JadbException
|
||||||
{
|
{
|
||||||
transport.send("host:devices");
|
transport.send("host:devices");
|
||||||
transport.verifyResponse();
|
transport.verifyResponse();
|
||||||
String body = transport.readString();
|
String body = transport.readString();
|
||||||
System.out.println(body);
|
return parseDevices(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException
|
public void close() throws IOException
|
||||||
{
|
{
|
||||||
_socket.close();
|
_socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<AndroidDevice> parseDevices(String body) {
|
||||||
|
String[] lines = body.split("\n");
|
||||||
|
ArrayList<AndroidDevice> devices = new ArrayList<AndroidDevice>(lines.length);
|
||||||
|
for (String line : lines)
|
||||||
|
{
|
||||||
|
String[] parts = line.split("\t");
|
||||||
|
devices.add(new AndroidDevice(parts[0], parts[1]));
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package se.vidstige.jadb.test;
|
package se.vidstige.jadb.test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import se.vidstige.jadb.AndroidDevice;
|
||||||
import se.vidstige.jadb.JadbConnection;
|
import se.vidstige.jadb.JadbConnection;
|
||||||
|
|
||||||
public class JadbTestCases {
|
public class JadbTestCases {
|
||||||
@ -16,6 +21,8 @@ public class JadbTestCases {
|
|||||||
public void testGetDevices() throws Exception
|
public void testGetDevices() throws Exception
|
||||||
{
|
{
|
||||||
JadbConnection jadb = new JadbConnection();
|
JadbConnection jadb = new JadbConnection();
|
||||||
jadb.getDevices();
|
List<AndroidDevice> actual = jadb.getDevices();
|
||||||
|
AndroidDevice[] expected = { new AndroidDevice("emulator-5554") };
|
||||||
|
Assert.assertArrayEquals(expected, actual.toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user