1 /*
2  * Copyright (C) 2017 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.compatibility.common.util;
18 
19 import android.os.Build;
20 import android.os.SystemProperties;
21 
22 import java.lang.reflect.Field;
23 
24 /**
25  * Device-side compatibility utility class for reading device API level.
26  */
27 public class ApiLevelUtil {
28     // os.Build.VERSION.DEVICE_INITIAL_SDK_INT can be used here, but it was called
29     // os.Build.VERSION.FIRST_SDK_INT in Android R and below. Using DEVICE_INITIAL_SDK_INT
30     // will mean that the tests built in Android S and above can't be run on Android R and below.
31     private static final int FIRST_API_LEVEL =
32             SystemProperties.getInt("ro.product.first_api_level", 0);
33 
verifyVersion(int version)34     private static void verifyVersion(int version) {
35         if (version == Build.VERSION_CODES.CUR_DEVELOPMENT) {
36             throw new RuntimeException("Invalid version: " + version);
37         }
38     }
39 
isBefore(int version)40     public static boolean isBefore(int version) {
41         verifyVersion(version);
42         return Build.VERSION.SDK_INT < version;
43     }
44 
isBefore(String version)45     public static boolean isBefore(String version) {
46         return isBefore(resolveVersionString(version));
47     }
48 
isAfter(int version)49     public static boolean isAfter(int version) {
50         verifyVersion(version);
51         return Build.VERSION.SDK_INT > version;
52     }
53 
isAfter(String version)54     public static boolean isAfter(String version) {
55         return isAfter(resolveVersionString(version));
56     }
57 
isAtLeast(int version)58     public static boolean isAtLeast(int version) {
59         verifyVersion(version);
60         return Build.VERSION.SDK_INT >= version;
61     }
62 
isAtLeast(String version)63     public static boolean isAtLeast(String version) {
64         return isAtLeast(resolveVersionString(version));
65     }
66 
isAtMost(int version)67     public static boolean isAtMost(int version) {
68         verifyVersion(version);
69         return Build.VERSION.SDK_INT <= version;
70     }
71 
isAtMost(String version)72     public static boolean isAtMost(String version) {
73         return isAtMost(resolveVersionString(version));
74     }
75 
getApiLevel()76     public static int getApiLevel() {
77         return Build.VERSION.SDK_INT;
78     }
79 
isFirstApiBefore(int version)80     public static boolean isFirstApiBefore(int version) {
81         verifyVersion(version);
82         return FIRST_API_LEVEL < version;
83     }
84 
isFirstApiBefore(String version)85     public static boolean isFirstApiBefore(String version) {
86         return isFirstApiBefore(resolveVersionString(version));
87     }
88 
isFirstApiAfter(int version)89     public static boolean isFirstApiAfter(int version) {
90         verifyVersion(version);
91         return FIRST_API_LEVEL > version;
92     }
93 
isFirstApiAfter(String version)94     public static boolean isFirstApiAfter(String version) {
95         return isFirstApiAfter(resolveVersionString(version));
96     }
97 
isFirstApiAtLeast(int version)98     public static boolean isFirstApiAtLeast(int version) {
99         return FIRST_API_LEVEL >= version;
100     }
101 
isFirstApiAtLeast(String version)102     public static boolean isFirstApiAtLeast(String version) {
103         return isFirstApiAtLeast(resolveVersionString(version));
104     }
105 
isFirstApiAtMost(int version)106     public static boolean isFirstApiAtMost(int version) {
107         return FIRST_API_LEVEL <= version;
108     }
109 
isFirstApiAtMost(String version)110     public static boolean isFirstApiAtMost(String version) {
111         return isFirstApiAtMost(resolveVersionString(version));
112     }
113 
getFirstApiLevel()114     public static int getFirstApiLevel() {
115         return FIRST_API_LEVEL;
116     }
117 
codenameEquals(String name)118     public static boolean codenameEquals(String name) {
119         return Build.VERSION.CODENAME.equalsIgnoreCase(name.trim());
120     }
121 
codenameStartsWith(String prefix)122     public static boolean codenameStartsWith(String prefix) {
123         return Build.VERSION.CODENAME.startsWith(prefix);
124     }
125 
getCodename()126     public static String getCodename() {
127         return Build.VERSION.CODENAME;
128     }
129 
resolveVersionString(String versionString)130     protected static int resolveVersionString(String versionString) {
131         // Attempt 1: Parse version string as an integer, e.g. "23" for M
132         try {
133             return Integer.parseInt(versionString);
134         } catch (NumberFormatException e) { /* ignore for alternate approaches below */ }
135         // Attempt 2: Find matching field in VersionCodes utility class, return value
136         try {
137             Field versionField = VersionCodes.class.getField(versionString.toUpperCase());
138             return versionField.getInt(null); // no instance for VERSION_CODES, use null
139         } catch (IllegalAccessException | NoSuchFieldException e) { /* ignore */ }
140         // Attempt 3: Find field within android.os.Build.VERSION_CODES
141         try {
142             Field versionField = Build.VERSION_CODES.class.getField(versionString.toUpperCase());
143             return versionField.getInt(null); // no instance for VERSION_CODES, use null
144         } catch (IllegalAccessException | NoSuchFieldException e) {
145             throw new RuntimeException(
146                     String.format("Failed to parse version string %s", versionString), e);
147         }
148     }
149 }
150