1 /*
2  * Copyright (C) 2020 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.modules.utils.build;
18 
19 import static android.os.Build.VERSION.CODENAME;
20 import static android.os.Build.VERSION.SDK_INT;
21 
22 import androidx.annotation.ChecksSdkIntAtLeast;
23 import androidx.annotation.NonNull;
24 
25 /**
26  * Utility class to check SDK level on a device.
27  *
28  * @hide
29  */
30 public final class SdkLevel {
31 
SdkLevel()32     private SdkLevel() {}
33 
34     /** Checks if the device is running on release version of Android R or newer. */
35     @ChecksSdkIntAtLeast(api = 30 /* Build.VERSION_CODES.R */)
isAtLeastR()36     public static boolean isAtLeastR() {
37         return SDK_INT >= 30;
38     }
39 
40     /**
41      * Checks if the device is running on a pre-release version of Android S or a release version of
42      * Android S or newer.
43      */
44     @ChecksSdkIntAtLeast(api = 31 /* Build.VERSION_CODES.S */, codename = "S")
isAtLeastS()45     public static boolean isAtLeastS() {
46         return SDK_INT >= 31;
47     }
48 
49     /**
50      * Checks if the device is running on a pre-release version of Android T or a release version of
51      * Android T or newer.
52      */
53     @ChecksSdkIntAtLeast(codename = "T")
isAtLeastT()54     public static boolean isAtLeastT() {
55         return isAtLeastPreReleaseCodename("T");
56     }
57 
isAtLeastPreReleaseCodename(@onNull String codename)58     private static boolean isAtLeastPreReleaseCodename(@NonNull String codename) {
59         // Special case "REL", which means the build is not a pre-release build.
60         if ("REL".equals(CODENAME)) {
61             return false;
62         }
63 
64         // Otherwise lexically compare them. Return true if the build codename is equal to or
65         // greater than the requested codename.
66         return CODENAME.compareTo(codename) >= 0;
67     }
68 }
69