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 package com.android.compatibility.common.util;
17 
18 import com.android.tradefed.device.DeviceNotAvailableException;
19 import com.android.tradefed.device.ITestDevice;
20 
21 import java.lang.reflect.Field;
22 
23 /**
24  * Device-side compatibility utility class for reading device API level.
25  */
26 public class ApiLevelUtil {
27 
28     public static final String CODENAME = "ro.build.version.codename";
29 
isBefore(ITestDevice device, int version)30     public static boolean isBefore(ITestDevice device, int version)
31             throws DeviceNotAvailableException {
32         return device.getApiLevel() < version;
33     }
34 
isBefore(ITestDevice device, String version)35     public static boolean isBefore(ITestDevice device, String version)
36             throws DeviceNotAvailableException {
37         return device.getApiLevel() < resolveVersionString(version);
38     }
39 
isAfter(ITestDevice device, int version)40     public static boolean isAfter(ITestDevice device, int version)
41             throws DeviceNotAvailableException {
42         return device.getApiLevel() > version;
43     }
44 
isAfter(ITestDevice device, String version)45     public static boolean isAfter(ITestDevice device, String version)
46             throws DeviceNotAvailableException {
47         return device.getApiLevel() > resolveVersionString(version);
48     }
49 
isAtLeast(ITestDevice device, int version)50     public static boolean isAtLeast(ITestDevice device, int version)
51             throws DeviceNotAvailableException {
52         return device.getApiLevel() >= version;
53     }
54 
isAtLeast(ITestDevice device, String version)55     public static boolean isAtLeast(ITestDevice device, String version)
56             throws DeviceNotAvailableException {
57         return device.getApiLevel() >= resolveVersionString(version);
58     }
59 
isAtMost(ITestDevice device, int version)60     public static boolean isAtMost(ITestDevice device, int version)
61             throws DeviceNotAvailableException {
62         return device.getApiLevel() <= version;
63     }
64 
isAtMost(ITestDevice device, String version)65     public static boolean isAtMost(ITestDevice device, String version)
66             throws DeviceNotAvailableException {
67         return device.getApiLevel() <= resolveVersionString(version);
68     }
69 
getApiLevel(ITestDevice device)70     public static int getApiLevel(ITestDevice device) throws DeviceNotAvailableException {
71         return device.getApiLevel();
72     }
73 
codenameEquals(ITestDevice device, String name)74     public static boolean codenameEquals(ITestDevice device, String name)
75             throws DeviceNotAvailableException {
76         return device.getProperty(CODENAME).equalsIgnoreCase(name.trim());
77     }
78 
codenameStartsWith(ITestDevice device, String prefix)79     public static boolean codenameStartsWith(ITestDevice device, String prefix)
80             throws DeviceNotAvailableException {
81         return device.getProperty(CODENAME).startsWith(prefix);
82     }
83 
getCodename(ITestDevice device)84     public static String getCodename(ITestDevice device)
85             throws DeviceNotAvailableException {
86         return device.getProperty(CODENAME);
87     }
88 
resolveVersionString(String versionString)89     protected static int resolveVersionString(String versionString) {
90         try {
91             return Integer.parseInt(versionString); // e.g. "23" for M
92         } catch (NumberFormatException e1) {
93             try {
94                 Field versionField = VersionCodes.class.getField(
95                         versionString.toUpperCase());
96                 return versionField.getInt(null); // no instance for VERSION_CODES, use null
97             } catch (IllegalAccessException | NoSuchFieldException e2) {
98                 throw new RuntimeException(
99                         String.format("Failed to parse version string %s", versionString), e2);
100             }
101         }
102     }
103 }
104