blob: 37b1687fd3f197199a78d280b3f2b68a20a5c87c [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinski75f3a552015-06-03 14:54:23 -070017#include "SdkConstants.h"
18
Chris Warrington057456d2022-07-18 11:06:17 +000019#include <stdint.h>
20
Adam Lesinski75f3a552015-06-03 14:54:23 -070021#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <string>
Chris Warrington057456d2022-07-18 11:06:17 +000023#include <string_view>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024
Adam Lesinskid5083f62017-01-16 15:07:21 -080025using android::StringPiece;
Chris Warrington057456d2022-07-18 11:06:17 +000026using namespace std::literals;
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
Chris Warrington057456d2022-07-18 11:06:17 +000030static constexpr ApiVersion sDevelopmentSdkLevel = 10000;
31static constexpr StringPiece sDevelopmentSdkCodeNames[] = {
32 "Q"sv, "R"sv, "S"sv, "Sv2"sv, "Tiramisu"sv, "UpsideDownCake"sv, "VanillaIceCream"sv};
Adam Lesinskifb6312f2016-06-28 14:40:32 -070033
Chris Warrington057456d2022-07-18 11:06:17 +000034static constexpr auto sPrivacySandboxSuffix = "PrivacySandbox"sv;
35
36static constexpr std::pair<uint16_t, ApiVersion> sAttrIdMap[] = {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 {0x021c, 1},
38 {0x021d, 2},
39 {0x0269, SDK_CUPCAKE},
40 {0x028d, SDK_DONUT},
41 {0x02ad, SDK_ECLAIR},
42 {0x02b3, SDK_ECLAIR_0_1},
43 {0x02b5, SDK_ECLAIR_MR1},
44 {0x02bd, SDK_FROYO},
45 {0x02cb, SDK_GINGERBREAD},
46 {0x0361, SDK_HONEYCOMB},
47 {0x0363, SDK_HONEYCOMB_MR1},
48 {0x0366, SDK_HONEYCOMB_MR2},
49 {0x03a6, SDK_ICE_CREAM_SANDWICH},
50 {0x03ae, SDK_JELLY_BEAN},
51 {0x03cc, SDK_JELLY_BEAN_MR1},
52 {0x03da, SDK_JELLY_BEAN_MR2},
53 {0x03f1, SDK_KITKAT},
54 {0x03f6, SDK_KITKAT_WATCH},
55 {0x04ce, SDK_LOLLIPOP},
Adam Lesinskic744ae82017-05-17 19:28:38 -070056 {0x04d8, SDK_LOLLIPOP_MR1},
57 {0x04f1, SDK_MARSHMALLOW},
58 {0x0527, SDK_NOUGAT},
59 {0x0530, SDK_NOUGAT_MR1},
60 {0x0568, SDK_O},
Adam Lesinskiec58a002017-10-04 14:20:06 -070061 {0x056d, SDK_O_MR1},
Ryan Mitchelleb9c9ac2018-08-09 09:18:26 -070062 {0x0586, SDK_P},
Svet Ganoved460aa2020-04-15 15:10:14 -070063 {0x0606, SDK_Q},
Jeff Sharkey7c5f4332021-05-27 09:26:59 -060064 {0x0616, SDK_R},
65 {0x064b, SDK_S},
Michael Wright534f2982021-10-28 17:05:25 +010066 {0x064c, SDK_S_V2},
Yurii Zubrytskyi7571c0b2024-07-11 15:35:02 -070067 // TODO(zyy): add these when we need more rules for converting new attributes to the
68 // older ones, but don't bother for now as this would increase the array size for no
69 // real benefit
70 // {0x0672, SDK_TIRAMISU},
71 // {0x0687, SDK_UPSIDE_DOWN_CAKE},
72 // {0x06a3, SDK_VANILLA_ICE_CREAM},
Adam Lesinski75f3a552015-06-03 14:54:23 -070073};
74
Chris Warrington057456d2022-07-18 11:06:17 +000075static_assert(std::is_sorted(std::begin(sAttrIdMap), std::end(sAttrIdMap),
76 [](auto&& l, auto&& r) { return l.first < r.first; }));
Adam Lesinski75f3a552015-06-03 14:54:23 -070077
Adam Lesinskic744ae82017-05-17 19:28:38 -070078ApiVersion FindAttributeSdkLevel(const ResourceId& id) {
79 if (id.package_id() != 0x01 || id.type_id() != 0x01) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 return 0;
81 }
Chris Warrington057456d2022-07-18 11:06:17 +000082 const auto it =
83 std::lower_bound(std::begin(sAttrIdMap), std::end(sAttrIdMap), id.entry_id(),
84 [](const auto& pair, uint16_t entryId) { return pair.first < entryId; });
85 if (it == std::end(sAttrIdMap)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return SDK_LOLLIPOP_MR1;
87 }
Chris Warrington057456d2022-07-18 11:06:17 +000088 return it->second;
Adam Lesinski75f3a552015-06-03 14:54:23 -070089}
90
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070091std::optional<ApiVersion> GetDevelopmentSdkCodeNameVersion(StringPiece code_name) {
Chris Warrington057456d2022-07-18 11:06:17 +000092 const auto it =
93 std::find_if(std::begin(sDevelopmentSdkCodeNames), std::end(sDevelopmentSdkCodeNames),
94 [code_name](const auto& item) { return code_name.starts_with(item); });
95 if (it == std::end(sDevelopmentSdkCodeNames)) {
96 return {};
97 }
98 if (code_name.size() == it->size()) {
99 return sDevelopmentSdkLevel;
100 }
101 if (code_name.size() == it->size() + sPrivacySandboxSuffix.size() &&
102 code_name.ends_with(sPrivacySandboxSuffix)) {
103 return sDevelopmentSdkLevel;
104 }
105 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700106}
107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108} // namespace aapt