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 #include "utils.h"
18 
19 #include <android-base/properties.h>
20 #include <gtest/gtest.h>
21 
22 #include <string>
23 #include <vector>
24 
25 // Returns true iff the device has the specified feature.
deviceSupportsFeature(const char * feature)26 bool deviceSupportsFeature(const char *feature) {
27     bool device_supports_feature = false;
28     FILE *p = popen("/system/bin/pm list features", "re");
29     if (p) {
30         char *line = NULL;
31         size_t len = 0;
32         while (getline(&line, &len, p) > 0) {
33             if (strstr(line, feature)) {
34                 device_supports_feature = true;
35                 break;
36             }
37         }
38         pclose(p);
39     }
40     return device_supports_feature;
41 }
42 
getFirstApiLevel()43 int getFirstApiLevel() {
44     int level = android::base::GetIntProperty("ro.product.first_api_level", 0);
45     if (level == 0) {
46         level = android::base::GetIntProperty("ro.build.version.sdk", 0);
47     }
48     if (level == 0) {
49         ADD_FAILURE() << "Failed to determine first API level";
50     }
51     return level;
52 }
53 
getVendorApiLevel()54 int getVendorApiLevel() {
55     std::vector<std::string> BOARD_API_LEVEL_PROPS = {"ro.board.api_level",
56                                                       "ro.board.first_api_level", "ro.vndk.version",
57                                                       "ro.vendor.build.version.sdk"};
58     const int API_LEVEL_CURRENT = 10000;
59     for (const auto &api_level_prop : BOARD_API_LEVEL_PROPS) {
60         int api_level = android::base::GetIntProperty(api_level_prop, API_LEVEL_CURRENT);
61         if (api_level != API_LEVEL_CURRENT) {
62             return api_level;
63         }
64     }
65     return API_LEVEL_CURRENT;
66 }