Return PackageInfo versionCode if not deprecated
Test: none
bug: 139855957
Change-Id: I131f72e4f44fc3053359b168d6f120645cb4b372
diff --git a/common/device-side/util-axt/src/com/android/compatibility/common/util/PackageUtil.java b/common/device-side/util-axt/src/com/android/compatibility/common/util/PackageUtil.java
index 28d8f0d..728cbc6 100644
--- a/common/device-side/util-axt/src/com/android/compatibility/common/util/PackageUtil.java
+++ b/common/device-side/util-axt/src/com/android/compatibility/common/util/PackageUtil.java
@@ -97,12 +97,18 @@
}
}
- /** Returns the version code for the package name, or null if the package can't be found */
+ /**
+ * Returns the version code for the package name, or null if the package can't be found.
+ * If before API Level 28, return a long version of the (otherwise deprecated) versionCode.
+ */
public static Long getLongVersionCode(String packageName) {
try {
PackageInfo info = getPackageManager().getPackageInfo(packageName,
PackageManager.GET_META_DATA);
- return info.getLongVersionCode();
+ // Make no assumptions about the device's API level, and use the (now deprecated)
+ // versionCode for older devices.
+ return (ApiLevelUtil.isAtLeast(28)) ?
+ info.getLongVersionCode() : (long) info.versionCode;
} catch (PackageManager.NameNotFoundException | NullPointerException e) {
Log.w(TAG, "Could not find version string for package " + packageName);
return null;