Refactor: Cleaning up forceInstall

This commit is contained in:
Samuel Carlsson 2016-05-13 21:40:14 +02:00
parent a1af30a81a
commit 2b71a04b4c
3 changed files with 23 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import se.vidstige.jadb.Stream;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -46,21 +47,31 @@ public class PackageManager {
if (!result.contains("Success")) throw new JadbException(getErrorMessage(operation, target, result)); if (!result.contains("Success")) throw new JadbException(getErrorMessage(operation, target, result));
} }
public void install(File apkFile,boolean forceInstall) throws IOException, JadbException { public void remove(RemoteFile file) throws IOException, JadbException {
InputStream s = device.executeShell("rm", "-f", Bash.quote(file.getPath()));
Stream.readAll(s, Charset.forName("UTF-8"));
}
public void install(File apkFile, List<String> extraArguments) throws IOException, JadbException {
RemoteFile remote = new RemoteFile("/sdcard/tmp/" + apkFile.getName()); RemoteFile remote = new RemoteFile("/sdcard/tmp/" + apkFile.getName());
device.push(apkFile, remote); device.push(apkFile, remote);
InputStream s; ArrayList<String> arguments = new ArrayList<String>();
if(forceInstall){ arguments.add("install");
s= device.executeShell("pm", "install", "-r", Bash.quote(remote.getPath())); arguments.addAll(extraArguments);
}else{ arguments.add(remote.getPath());
s= device.executeShell("pm", "install", Bash.quote(remote.getPath())); InputStream s = device.executeShell("pm", arguments.toArray(new String[arguments.size()]));
}
String result = Stream.readAll(s, Charset.forName("UTF-8")); String result = Stream.readAll(s, Charset.forName("UTF-8"));
s=device.executeShell("rm", "-f", Bash.quote(remote.getPath())); remove(remote);
Stream.readAll(s, Charset.forName("UTF-8"));
verifyOperation("install", apkFile.getName(), result); verifyOperation("install", apkFile.getName(), result);
} }
public void install(File apkFile) throws IOException, JadbException {
install(apkFile, new ArrayList<String>(0));
}
public void forceInstall(File apkFile) throws IOException, JadbException {
install(apkFile, Collections.singletonList("-r"));
}
public void uninstall(Package name) throws IOException, JadbException { public void uninstall(Package name) throws IOException, JadbException {
InputStream s = device.executeShell("pm", "uninstall", name.toString()); InputStream s = device.executeShell("pm", "uninstall", name.toString());

View File

@ -14,6 +14,7 @@ import java.util.List;
public class PackageMangerTests { public class PackageMangerTests {
private static JadbConnection jadb; private static JadbConnection jadb;
private PackageManager pm; private PackageManager pm;
private final File miniApk = new File("test/data/Tiniest Smallest APK ever.apk");
@BeforeClass @BeforeClass
public static void connect() throws IOException { public static void connect() throws IOException {
@ -46,8 +47,8 @@ public class PackageMangerTests {
@Test @Test
public void testInstallUninstallCycle() throws Exception { public void testInstallUninstallCycle() throws Exception {
File f = new File("test/data/Tiniest Smallest APK ever_v' platformBuildVersionName=_apkpure.com.apk"); pm.install(miniApk);
pm.install(f, false); pm.forceInstall(miniApk);
pm.uninstall(new Package("b.a")); pm.uninstall(new Package("b.a"));
} }
} }