Merge "Avoid massive package prints from Content provider"
diff --git a/src/com/android/tradefed/device/ITestDevice.java b/src/com/android/tradefed/device/ITestDevice.java
index ed87b03..c109d9a 100644
--- a/src/com/android/tradefed/device/ITestDevice.java
+++ b/src/com/android/tradefed/device/ITestDevice.java
@@ -561,6 +561,14 @@
public Set<String> getInstalledPackageNames() throws DeviceNotAvailableException;
/**
+ * Query the device for a given package name to check if it's currently installed or not.
+ *
+ * @return True if the package is reported as installed. False otherwise.
+ * @throws DeviceNotAvailableException
+ */
+ public boolean isPackageInstalled(String packageName) throws DeviceNotAvailableException;
+
+ /**
* Fetch the information about APEXes activated on the device.
*
* @return {@link Set} of {@link ApexInfo} currently activated on the device
diff --git a/src/com/android/tradefed/device/NativeDevice.java b/src/com/android/tradefed/device/NativeDevice.java
index c3b8fe4..8218bde 100644
--- a/src/com/android/tradefed/device/NativeDevice.java
+++ b/src/com/android/tradefed/device/NativeDevice.java
@@ -3580,6 +3580,12 @@
/** {@inheritDoc} */
@Override
+ public boolean isPackageInstalled(String packageName) throws DeviceNotAvailableException {
+ throw new UnsupportedOperationException("No support for Package's feature");
+ }
+
+ /** {@inheritDoc} */
+ @Override
public Set<ApexInfo> getActiveApexes() throws DeviceNotAvailableException {
throw new UnsupportedOperationException("No support for Package's feature");
}
diff --git a/src/com/android/tradefed/device/TestDevice.java b/src/com/android/tradefed/device/TestDevice.java
index ac4222d..7a9f159 100644
--- a/src/com/android/tradefed/device/TestDevice.java
+++ b/src/com/android/tradefed/device/TestDevice.java
@@ -909,12 +909,13 @@
*/
@Override
public Set<String> getInstalledPackageNames() throws DeviceNotAvailableException {
- return getInstalledPackageNames(new PkgFilter() {
- @Override
- public boolean accept(String pkgName, String apkPath) {
- return true;
- }
- });
+ return getInstalledPackageNames(null);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean isPackageInstalled(String packageName) throws DeviceNotAvailableException {
+ return getInstalledPackageNames(packageName).contains(packageName);
}
/** {@inheritDoc} */
@@ -994,10 +995,24 @@
}
// TODO: convert this to use DumpPkgAction
- private Set<String> getInstalledPackageNames(PkgFilter filter)
+ private Set<String> getInstalledPackageNames(String packageNameSearched)
throws DeviceNotAvailableException {
+ PkgFilter filter =
+ new PkgFilter() {
+ @Override
+ public boolean accept(String pkgName, String apkPath) {
+ if (packageNameSearched == null) {
+ return true;
+ }
+ return pkgName.equals(packageNameSearched);
+ }
+ };
Set<String> packages= new HashSet<String>();
- String output = executeShellCommand(LIST_PACKAGES_CMD);
+ String command = LIST_PACKAGES_CMD;
+ if (packageNameSearched != null) {
+ command += (" | grep " + packageNameSearched);
+ }
+ String output = executeShellCommand(command);
if (output != null) {
Matcher m = PACKAGE_REGEX.matcher(output);
while (m.find()) {
diff --git a/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java b/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
index 6bcd27b..7dd8c3d 100644
--- a/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
+++ b/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
@@ -36,7 +36,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
-import java.util.Set;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -88,8 +87,7 @@
* @return True if ready to be used, False otherwise.
*/
public boolean setUp() throws DeviceNotAvailableException {
- Set<String> packageNames = mDevice.getInstalledPackageNames();
- if (packageNames.contains(PACKAGE_NAME)) {
+ if (mDevice.isPackageInstalled(PACKAGE_NAME)) {
return true;
}
if (mContentProviderApk == null) {
diff --git a/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java b/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
index cd33cde..97f91ae 100644
--- a/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
+++ b/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
@@ -40,8 +40,6 @@
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Set;
/** Run unit tests for {@link ContentProviderHandler}. */
@RunWith(JUnit4.class)
@@ -64,8 +62,6 @@
/** Test the install flow. */
@Test
public void testSetUp_install() throws Exception {
- Set<String> set = new HashSet<>();
- doReturn(set).when(mMockDevice).getInstalledPackageNames();
doReturn(1).when(mMockDevice).getCurrentUser();
doReturn(null).when(mMockDevice).installPackage(any(), eq(true), eq(true));
doReturn(null)
@@ -86,17 +82,13 @@
@Test
public void testSetUp_alreadyInstalled() throws Exception {
- Set<String> set = new HashSet<>();
- set.add(ContentProviderHandler.PACKAGE_NAME);
- doReturn(set).when(mMockDevice).getInstalledPackageNames();
+ doReturn(true).when(mMockDevice).isPackageInstalled(ContentProviderHandler.PACKAGE_NAME);
assertTrue(mProvider.setUp());
}
@Test
public void testSetUp_installFail() throws Exception {
- Set<String> set = new HashSet<>();
- doReturn(set).when(mMockDevice).getInstalledPackageNames();
doReturn(1).when(mMockDevice).getCurrentUser();
doReturn("fail").when(mMockDevice).installPackage(any(), eq(true), eq(true));