1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <string.h>
20 
21 #include <android/api-level.h>
22 #include <sys/system_properties.h>
23 
24 namespace android {
25 namespace modules {
26 namespace sdklevel {
27 
28 namespace detail {
29 
GetCodename(char (& codename)[PROP_VALUE_MAX])30 inline void GetCodename(char (&codename)[PROP_VALUE_MAX]) {
31   // ro. properties can be longer than PROP_VALUE_MAX, but *this* property
32   // is not likely to be very long.
33   __system_property_get("ro.build.version.codename", codename);
34 }
35 
36 // Checks if the codename is a matching or higher version than the device's
37 // codename.
IsAtLeastPreReleaseCodename(const char * codename)38 [[maybe_unused]] static bool IsAtLeastPreReleaseCodename(const char *codename) {
39   char deviceCodename[PROP_VALUE_MAX];
40   GetCodename(deviceCodename);
41   return strcmp(deviceCodename, "REL") != 0 &&
42          strcmp(deviceCodename, codename) >= 0;
43 }
44 
45 } // namespace detail
46 
47 // Checks if the device is running on release version of Android R or newer.
IsAtLeastR()48 inline bool IsAtLeastR() { return android_get_device_api_level() >= 30; }
49 
50 // Checks if the device is running on release version of Android S or newer.
IsAtLeastS()51 inline bool IsAtLeastS() { return android_get_device_api_level() >= 31; }
52 
53 // Checks if the device is running on release version of Android T or newer.
IsAtLeastT()54 inline bool IsAtLeastT() { return android_get_device_api_level() >= 33; }
55 
56 // Checks if the device is running on release version of Android U or newer.
IsAtLeastU()57 inline bool IsAtLeastU() { return android_get_device_api_level() >= 34; }
58 
59 // Checks if the device is running on release version of Android V or newer.
IsAtLeastV()60 inline bool IsAtLeastV() {
61   return android_get_device_api_level() >= 35 ||
62          (android_get_device_api_level() == 34 &&
63           detail::IsAtLeastPreReleaseCodename("VanillaIceCream"));
64 }
65 
66 } // namespace sdklevel
67 } // namespace modules
68 } // namespace android
69