DeviceInfoUtils: add isKernelVersionAtLeast method.
Add a method to check if the current kernel version is at least from a
given version.
Bug: 236783925
Test: atest com.android.testutils.DeviceInfoUtilsTest --iterations
Change-Id: If0509b8ccad895c31d9447fee06789540002f666
diff --git a/staticlibs/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java b/staticlibs/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java
index 6f603d5..e46dd59 100644
--- a/staticlibs/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java
+++ b/staticlibs/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java
@@ -106,4 +106,18 @@
assertFalse(v1.isAtLeast(v4));
assertFalse(v1.isAtLeast(v5));
}
+
+ @Test
+ public void testKernelVersionIsAtLeast() {
+ // Pick a lower kernel version 4.0 which was released at April 2015, the kernel
+ // version running on all test devices nowadays should be higher than it.
+ assertTrue(DeviceInfoUtils.isKernelVersionAtLeast("4.0"));
+
+ // Invalid kernel version.
+ assertTrue(DeviceInfoUtils.isKernelVersionAtLeast("0.0.0"));
+
+ // Pick a higher kernel version which isn't released yet, comparison should return false.
+ // Need to update the target version in the future to make sure the test still passes.
+ assertFalse(DeviceInfoUtils.isKernelVersionAtLeast("20.0.0"));
+ }
}
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java b/staticlibs/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java
index 1925b55..ea89eda 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java
+++ b/staticlibs/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java
@@ -16,6 +16,7 @@
package com.android.testutils;
+import android.os.VintfRuntimeInfo;
import android.text.TextUtils;
import android.util.Pair;
@@ -157,4 +158,18 @@
return new KVersion(0, 0, 0);
}
}
+
+ /**
+ * Check if the current kernel version is at least satisfied with the given version.
+ *
+ * @param version the start version to compare
+ * @return return true if the current version is at least satisfied with the given version.
+ * otherwise, return false.
+ */
+ public static boolean isKernelVersionAtLeast(final String version) {
+ final String kernelVersion = VintfRuntimeInfo.getKernelVersion();
+ final KVersion current = DeviceInfoUtils.getMajorMinorSubminorVersion(kernelVersion);
+ final KVersion from = DeviceInfoUtils.getMajorMinorSubminorVersion(version);
+ return current.isAtLeast(from);
+ }
}