1 /* 2 * Copyright (C) 2021 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.testing; 18 19 import androidx.annotation.ChecksSdkIntAtLeast; 20 import androidx.annotation.NonNull; 21 22 import com.android.tradefed.device.DeviceNotAvailableException; 23 import com.android.tradefed.device.DeviceProperties; 24 import com.android.tradefed.device.ITestDevice; 25 26 /** 27 * Utility class to check device SDK level from a host side test. 28 */ 29 public final class DeviceSdkLevel { 30 31 private final ITestDevice device; 32 DeviceSdkLevel(ITestDevice device)33 public DeviceSdkLevel(ITestDevice device) { 34 this.device = device; 35 } 36 37 /** Checks if the device is running on a release version of Android R or newer. */ isDeviceAtLeastR()38 public boolean isDeviceAtLeastR() throws DeviceNotAvailableException { 39 return device.getApiLevel() >= 30; 40 } 41 42 /** Checks if the device is running on a release version of Android S or newer. */ isDeviceAtLeastS()43 public boolean isDeviceAtLeastS() throws DeviceNotAvailableException { 44 return device.getApiLevel() >= 31; 45 } 46 47 /** Checks if the device is running on a release version of Android T or newer. */ isDeviceAtLeastT()48 public boolean isDeviceAtLeastT() throws DeviceNotAvailableException { 49 return device.getApiLevel() >= 33; 50 } 51 52 /** Checks if the device is running on a release version of Android U or newer. */ isDeviceAtLeastU()53 public boolean isDeviceAtLeastU() throws DeviceNotAvailableException { 54 return device.getApiLevel() >= 34; 55 } 56 57 /** Checks if the device is running on a (pre-)release version of Android V or newer. */ isDeviceAtLeastV()58 public boolean isDeviceAtLeastV() throws DeviceNotAvailableException { 59 return device.getApiLevel() >= 34 && isDeviceAtLeastPreReleaseCodename("VanillaIceCream"); 60 } 61 isDeviceAtLeastPreReleaseCodename(@onNull String codename)62 private boolean isDeviceAtLeastPreReleaseCodename(@NonNull String codename) 63 throws DeviceNotAvailableException { 64 String deviceCodename = device.getProperty(DeviceProperties.BUILD_CODENAME); 65 66 // Special case "REL", which means the build is not a pre-release build. 67 if ("REL".equals(deviceCodename)) { 68 return false; 69 } 70 71 // Otherwise lexically compare them. Return true if the build codename is equal to or 72 // greater than the requested codename. 73 return deviceCodename.compareTo(codename) >= 0; 74 } 75 76 } 77