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 import static android.os.Build.VERSION_CODES.CUR_DEVELOPMENT; 22 23 import androidx.annotation.ChecksSdkIntAtLeast; 24 import androidx.annotation.NonNull; 25 26 /** 27 * Utility class to check SDK level on a device. 28 * 29 * @hide 30 */ 31 public final class SdkLevel { 32 SdkLevel()33 private SdkLevel() { 34 } 35 36 /** Checks if the device is running on a release version of Android R or newer. */ 37 @ChecksSdkIntAtLeast(api = 30 /* Build.VERSION_CODES.R */) isAtLeastR()38 public static boolean isAtLeastR() { 39 return SDK_INT >= 30; 40 } 41 42 /** Checks if the device is running on a release version of Android S or newer. */ 43 @ChecksSdkIntAtLeast(api = 31 /* Build.VERSION_CODES.S */) isAtLeastS()44 public static boolean isAtLeastS() { 45 return SDK_INT >= 31; 46 } 47 48 /** Checks if the device is running on a release version of Android S_V2 or newer */ 49 @ChecksSdkIntAtLeast(api = 32 /* BUILD.VERSION_CODES.Sv2 */) isAtLeastSv2()50 public static boolean isAtLeastSv2() { 51 return SDK_INT >= 32; 52 } 53 54 /** Checks if the device is running on a release version of Android Tiramisu or newer */ 55 @ChecksSdkIntAtLeast(api = 33 /* BUILD_VERSION_CODES.Tiramisu */) isAtLeastT()56 public static boolean isAtLeastT() { 57 return SDK_INT >= 33; 58 } 59 60 /** Checks if the device is running on a release version of Android UpsideDownCake or newer */ 61 @ChecksSdkIntAtLeast(api = 34 /* VERSION_CODES.UpsideDownCake */, codename = "UpsideDownCake") isAtLeastU()62 public static boolean isAtLeastU() { 63 return SDK_INT >= 34 || 64 (SDK_INT == 33 && isAtLeastPreReleaseCodename("UpsideDownCake")); 65 } 66 67 /** Checks if the device is running on a release version of Android VanillaIceCream or newer */ 68 @ChecksSdkIntAtLeast(api = 35 /* BUILD_VERSION_CODES.VanillaIceCream */) isAtLeastV()69 public static boolean isAtLeastV() { 70 return SDK_INT >= 35 || 71 (SDK_INT == 34 && isAtLeastPreReleaseCodename("VanillaIceCream")); 72 } 73 isAtLeastPreReleaseCodename(@onNull String codename)74 private static boolean isAtLeastPreReleaseCodename(@NonNull String codename) { 75 // Special case "REL", which means the build is not a pre-release build. 76 if ("REL".equals(CODENAME)) { 77 return false; 78 } 79 80 // Otherwise lexically compare them. Return true if the build codename is equal to or 81 // greater than the requested codename. 82 return CODENAME.compareTo(codename) >= 0; 83 } 84 } 85