1 /*
2 * Copyright (C) 2015 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 "SdkConstants.h"
18
19 #include <stdint.h>
20
21 #include <algorithm>
22 #include <string>
23 #include <string_view>
24
25 using android::StringPiece;
26 using namespace std::literals;
27
28 namespace aapt {
29
30 static constexpr ApiVersion sDevelopmentSdkLevel = 10000;
31
32 // clang-format off
33 static constexpr StringPiece sDevelopmentSdkCodeNames[] = {
34 "Q"sv,
35 "R"sv,
36 "S"sv,
37 "Sv2"sv,
38 "Tiramisu"sv,
39 "UpsideDownCake"sv,
40 "VanillaIceCream"sv,
41 "Baklava"sv,
42 };
43 // clang-format on
44
45 static constexpr auto sPrivacySandboxSuffix = "PrivacySandbox"sv;
46
47 static constexpr std::pair<uint16_t, ApiVersion> sAttrIdMap[] = {
48 {0x021c, 1},
49 {0x021d, 2},
50 {0x0269, SDK_CUPCAKE},
51 {0x028d, SDK_DONUT},
52 {0x02ad, SDK_ECLAIR},
53 {0x02b3, SDK_ECLAIR_0_1},
54 {0x02b5, SDK_ECLAIR_MR1},
55 {0x02bd, SDK_FROYO},
56 {0x02cb, SDK_GINGERBREAD},
57 {0x0361, SDK_HONEYCOMB},
58 {0x0363, SDK_HONEYCOMB_MR1},
59 {0x0366, SDK_HONEYCOMB_MR2},
60 {0x03a6, SDK_ICE_CREAM_SANDWICH},
61 {0x03ae, SDK_JELLY_BEAN},
62 {0x03cc, SDK_JELLY_BEAN_MR1},
63 {0x03da, SDK_JELLY_BEAN_MR2},
64 {0x03f1, SDK_KITKAT},
65 {0x03f6, SDK_KITKAT_WATCH},
66 {0x04ce, SDK_LOLLIPOP},
67 {0x04d8, SDK_LOLLIPOP_MR1},
68 {0x04f1, SDK_MARSHMALLOW},
69 {0x0527, SDK_NOUGAT},
70 {0x0530, SDK_NOUGAT_MR1},
71 {0x0568, SDK_O},
72 {0x056d, SDK_O_MR1},
73 {0x0586, SDK_P},
74 {0x0606, SDK_Q},
75 {0x0616, SDK_R},
76 {0x064b, SDK_S},
77 {0x064c, SDK_S_V2},
78 // TODO(zyy): add these when we need more rules for converting new attributes to the
79 // older ones, but don't bother for now as this would increase the array size for no
80 // real benefit
81 // {0x0672, SDK_TIRAMISU},
82 // {0x0687, SDK_UPSIDE_DOWN_CAKE},
83 // {0x06a3, SDK_VANILLA_ICE_CREAM},
84 };
85
86 static_assert(std::is_sorted(std::begin(sAttrIdMap), std::end(sAttrIdMap),
__anon1b0f84860102(auto&& l, auto&& r) 87 [](auto&& l, auto&& r) { return l.first < r.first; }));
88
FindAttributeSdkLevel(const ResourceId & id)89 ApiVersion FindAttributeSdkLevel(const ResourceId& id) {
90 if (id.package_id() != 0x01 || id.type_id() != 0x01) {
91 return 0;
92 }
93 const auto it =
94 std::lower_bound(std::begin(sAttrIdMap), std::end(sAttrIdMap), id.entry_id(),
95 [](const auto& pair, uint16_t entryId) { return pair.first < entryId; });
96 if (it == std::end(sAttrIdMap)) {
97 return SDK_LOLLIPOP_MR1;
98 }
99 return it->second;
100 }
101
GetDevelopmentSdkCodeNameVersion(StringPiece code_name)102 std::optional<ApiVersion> GetDevelopmentSdkCodeNameVersion(StringPiece code_name) {
103 const auto it =
104 std::find_if(std::begin(sDevelopmentSdkCodeNames), std::end(sDevelopmentSdkCodeNames),
105 [code_name](const auto& item) { return code_name.starts_with(item); });
106 if (it == std::end(sDevelopmentSdkCodeNames)) {
107 return {};
108 }
109 if (code_name.size() == it->size()) {
110 return sDevelopmentSdkLevel;
111 }
112 if (code_name.size() == it->size() + sPrivacySandboxSuffix.size() &&
113 code_name.ends_with(sPrivacySandboxSuffix)) {
114 return sDevelopmentSdkLevel;
115 }
116 return {};
117 }
118
119 } // namespace aapt
120