1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compatibility.common.util;
18 
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.device.PackageInfo;
22 
23 /**
24  * Host-side utility class for PackageManager-related operations
25  */
26 public class PackageUtil {
27 
28     /** Returns true if a package with the given name exists on the device */
exists(ITestDevice device, String packageName)29     public static boolean exists(ITestDevice device, String packageName)
30             throws DeviceNotAvailableException {
31         return device.getInstalledPackageNames().contains(packageName);
32     }
33 
34     /** Returns true if the app for the given package name is a system app for this device */
isSystemApp(ITestDevice device, String packageName)35     public static boolean isSystemApp(ITestDevice device, String packageName)
36             throws DeviceNotAvailableException {
37         PackageInfo info = device.getAppPackageInfo(packageName);
38         return (info != null) && info.isSystemApp();
39     }
40 
41     /** Returns the version string of the package name, or null if the package can't be found */
getVersionString(ITestDevice device, String packageName)42     public static String getVersionString(ITestDevice device, String packageName)
43             throws DeviceNotAvailableException {
44         PackageInfo info = device.getAppPackageInfo(packageName);
45         return (info != null) ? info.getVersionName() : null;
46     }
47 }
48