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 /**
22  * Device-side compatibility utility class for reading device API level.
23  */
24 public class ApiLevelUtil {
25 
26     public static final String CODENAME = "ro.build.version.codename";
27 
isBefore(ITestDevice device, int version)28     public static boolean isBefore(ITestDevice device, int version)
29             throws DeviceNotAvailableException {
30         return device.getApiLevel() < version;
31     }
32 
isAfter(ITestDevice device, int version)33     public static boolean isAfter(ITestDevice device, int version)
34             throws DeviceNotAvailableException {
35         return device.getApiLevel() > version;
36     }
37 
isAtLeast(ITestDevice device, int version)38     public static boolean isAtLeast(ITestDevice device, int version)
39             throws DeviceNotAvailableException {
40         return device.getApiLevel() >= version;
41     }
42 
isAtMost(ITestDevice device, int version)43     public static boolean isAtMost(ITestDevice device, int version)
44             throws DeviceNotAvailableException {
45         return device.getApiLevel() <= version;
46     }
47 
getApiLevel(ITestDevice device)48     public static int getApiLevel(ITestDevice device) throws DeviceNotAvailableException {
49         return device.getApiLevel();
50     }
51 
codenameEquals(ITestDevice device, String name)52     public static boolean codenameEquals(ITestDevice device, String name)
53             throws DeviceNotAvailableException {
54         return device.getProperty(CODENAME).equalsIgnoreCase(name.trim());
55     }
56 
codenameStartsWith(ITestDevice device, String prefix)57     public static boolean codenameStartsWith(ITestDevice device, String prefix)
58             throws DeviceNotAvailableException {
59         return device.getProperty(CODENAME).startsWith(prefix);
60     }
61 
getCodename(ITestDevice device)62     public static String getCodename(ITestDevice device)
63             throws DeviceNotAvailableException {
64         return device.getProperty(CODENAME);
65     }
66 }
67