Adding package manager

This commit is contained in:
Samuel Carlsson 2016-04-27 21:56:12 +02:00
parent 18375a8d02
commit dfb04af94b
3 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package se.vidstige.jadb.managers;
/**
* Android package
*/
public class Package {
private final String name;
public Package(String name) {
this.name = name;
}
@Override
public String toString() { return name; }
@Override
public boolean equals(Object o) { return name.equals(o); }
@Override
public int hashCode() { return name.hashCode(); }
}

View File

@ -0,0 +1,40 @@
package se.vidstige.jadb.managers;
import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.JadbException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* Java interface to package manager. Launches package manager through jadb
*/
public class PackageManager {
private final JadbDevice device;
public PackageManager(JadbDevice device) {
this.device = device;
}
public List<Package> getPackages() throws IOException, JadbException {
ArrayList<Package> result = new ArrayList<Package>();
BufferedReader input = null;
try {
input = new BufferedReader(new InputStreamReader(device.executeShell("pm", "list", "packages"), Charset.forName("UTF-8")));
String line;
while ((line = input.readLine()) != null) {
final String prefix = "package:";
if (line.startsWith(prefix)) {
result.add(new Package(line.substring(prefix.length())));
}
}
} finally {
if (input != null) input.close();
}
return result;
}
}

View File

@ -0,0 +1,40 @@
package se.vidstige.jadb.test;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.managers.Package;
import se.vidstige.jadb.managers.PackageManager;
import java.io.IOException;
import java.util.List;
public class PackageMangerTests {
private static JadbConnection jadb;
private PackageManager pm;
@BeforeClass
public static void connect() throws IOException {
try {
jadb = new JadbConnection();
jadb.getHostVersion();
} catch (Exception e) {
org.junit.Assume.assumeNoException(e);
}
}
@Before
public void createPackageManager()
{
pm = new PackageManager(jadb.getAnyDevice());
}
@Test
public void testListPackages() throws Exception {
List<Package> packages = pm.getPackages();
for (Package p : packages) {
System.out.println(p);
}
}
}