Implement DevSdkIgnoreRule with SdkLevel

DevSdkIgnoreRule currently considers a build with SDK 30, version code T
as being S, but it is generally considered as being T for development
that happens in that branch.

Change the implementation of DevSdkIgnoreRule to be consistent with the
SdkLevel utility.

Test: tests using DevSdkIgnoreRule
Change-Id: Ie81b84dff8fef8eac2cfe9694f0265e5bf11b3e6
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
index 4a83f6f..201bf2d 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
@@ -17,6 +17,7 @@
 package com.android.testutils
 
 import android.os.Build
+import com.android.modules.utils.build.SdkLevel
 import org.junit.Assume.assumeTrue
 import org.junit.rules.TestRule
 import org.junit.runner.Description
@@ -29,14 +30,32 @@
  * [Build.VERSION.SDK_INT].
  */
 fun isDevSdkInRange(minExclusive: Int?, maxInclusive: Int?): Boolean {
-    // In-development API n+1 will have SDK_INT == n and CODENAME != REL.
-    // Stable API n has SDK_INT == n and CODENAME == REL.
-    val release = "REL" == Build.VERSION.CODENAME
-    val sdkInt = Build.VERSION.SDK_INT
-    val devApiLevel = sdkInt + if (release) 0 else 1
+    return (minExclusive == null || isDevSdkAfter(minExclusive)) &&
+            (maxInclusive == null || isDevSdkUpTo(maxInclusive))
+}
 
-    return (minExclusive == null || devApiLevel > minExclusive) &&
-            (maxInclusive == null || devApiLevel <= maxInclusive)
+private fun isDevSdkAfter(minExclusive: Int): Boolean {
+    // A development build for T typically has SDK_INT = 30 (R) or SDK_INT = 31 (S), so SDK_INT
+    // alone cannot be used to check the SDK version.
+    // For recent SDKs that still have development builds used for testing, use SdkLevel utilities
+    // instead of SDK_INT.
+    return when (minExclusive) {
+        // TODO: use Build.VERSION_CODES.S when it is not CURRENT_DEVELOPMENT
+        31 -> SdkLevel.isAtLeastT()
+        Build.VERSION_CODES.R -> SdkLevel.isAtLeastS()
+        // Development builds of SDK versions <= R are not used anymore
+        else -> Build.VERSION.SDK_INT > minExclusive
+    }
+}
+
+private fun isDevSdkUpTo(maxInclusive: Int): Boolean {
+    return when (maxInclusive) {
+        // TODO: use Build.VERSION_CODES.S when it is not CURRENT_DEVELOPMENT
+        31 -> !SdkLevel.isAtLeastT()
+        Build.VERSION_CODES.R -> !SdkLevel.isAtLeastS()
+        // Development builds of SDK versions <= R are not used anymore
+        else -> Build.VERSION.SDK_INT <= maxInclusive
+    }
 }
 
 /**