blob: 5983cf165839743613c256e5b14bcb51df224262 [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;
Mårten Kongstadbc7e7472024-09-25 09:59:05 +020031
32// clang-format off
Chris Warrington057456d2022-07-18 11:06:17 +000033static constexpr StringPiece sDevelopmentSdkCodeNames[] = {
Mårten Kongstadbc7e7472024-09-25 09:59:05 +020034 "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
Adam Lesinskifb6312f2016-06-28 14:40:32 -070044
Chris Warrington057456d2022-07-18 11:06:17 +000045static constexpr auto sPrivacySandboxSuffix = "PrivacySandbox"sv;
46
47static constexpr std::pair<uint16_t, ApiVersion> sAttrIdMap[] = {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 {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},
Adam Lesinskic744ae82017-05-17 19:28:38 -070067 {0x04d8, SDK_LOLLIPOP_MR1},
68 {0x04f1, SDK_MARSHMALLOW},
69 {0x0527, SDK_NOUGAT},
70 {0x0530, SDK_NOUGAT_MR1},
71 {0x0568, SDK_O},
Adam Lesinskiec58a002017-10-04 14:20:06 -070072 {0x056d, SDK_O_MR1},
Ryan Mitchelleb9c9ac2018-08-09 09:18:26 -070073 {0x0586, SDK_P},
Svet Ganoved460aa2020-04-15 15:10:14 -070074 {0x0606, SDK_Q},
Jeff Sharkey7c5f4332021-05-27 09:26:59 -060075 {0x0616, SDK_R},
76 {0x064b, SDK_S},
Michael Wright534f2982021-10-28 17:05:25 +010077 {0x064c, SDK_S_V2},
Yurii Zubrytskyi7571c0b2024-07-11 15:35:02 -070078 // 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},
Adam Lesinski75f3a552015-06-03 14:54:23 -070084};
85
Chris Warrington057456d2022-07-18 11:06:17 +000086static_assert(std::is_sorted(std::begin(sAttrIdMap), std::end(sAttrIdMap),
87 [](auto&& l, auto&& r) { return l.first < r.first; }));
Adam Lesinski75f3a552015-06-03 14:54:23 -070088
Adam Lesinskic744ae82017-05-17 19:28:38 -070089ApiVersion FindAttributeSdkLevel(const ResourceId& id) {
90 if (id.package_id() != 0x01 || id.type_id() != 0x01) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return 0;
92 }
Chris Warrington057456d2022-07-18 11:06:17 +000093 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)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 return SDK_LOLLIPOP_MR1;
98 }
Chris Warrington057456d2022-07-18 11:06:17 +000099 return it->second;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700100}
101
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700102std::optional<ApiVersion> GetDevelopmentSdkCodeNameVersion(StringPiece code_name) {
Chris Warrington057456d2022-07-18 11:06:17 +0000103 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 {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700117}
118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119} // namespace aapt