• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static constexpr StringPiece sDevelopmentSdkCodeNames[] = {
32     "Q"sv, "R"sv, "S"sv, "Sv2"sv, "Tiramisu"sv, "UpsideDownCake"sv, "VanillaIceCream"sv};
33 
34 static constexpr auto sPrivacySandboxSuffix = "PrivacySandbox"sv;
35 
36 static constexpr std::pair<uint16_t, ApiVersion> sAttrIdMap[] = {
37     {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},
56     {0x04d8, SDK_LOLLIPOP_MR1},
57     {0x04f1, SDK_MARSHMALLOW},
58     {0x0527, SDK_NOUGAT},
59     {0x0530, SDK_NOUGAT_MR1},
60     {0x0568, SDK_O},
61     {0x056d, SDK_O_MR1},
62     {0x0586, SDK_P},
63     {0x0606, SDK_Q},
64     {0x0616, SDK_R},
65     {0x064b, SDK_S},
66     {0x064c, SDK_S_V2},
67 };
68 
69 static_assert(std::is_sorted(std::begin(sAttrIdMap), std::end(sAttrIdMap),
__anonc2db22e40102(auto&& l, auto&& r) 70                              [](auto&& l, auto&& r) { return l.first < r.first; }));
71 
FindAttributeSdkLevel(const ResourceId & id)72 ApiVersion FindAttributeSdkLevel(const ResourceId& id) {
73   if (id.package_id() != 0x01 || id.type_id() != 0x01) {
74     return 0;
75   }
76   const auto it =
77       std::lower_bound(std::begin(sAttrIdMap), std::end(sAttrIdMap), id.entry_id(),
78                        [](const auto& pair, uint16_t entryId) { return pair.first < entryId; });
79   if (it == std::end(sAttrIdMap)) {
80     return SDK_LOLLIPOP_MR1;
81   }
82   return it->second;
83 }
84 
GetDevelopmentSdkCodeNameVersion(StringPiece code_name)85 std::optional<ApiVersion> GetDevelopmentSdkCodeNameVersion(StringPiece code_name) {
86   const auto it =
87       std::find_if(std::begin(sDevelopmentSdkCodeNames), std::end(sDevelopmentSdkCodeNames),
88                    [code_name](const auto& item) { return code_name.starts_with(item); });
89   if (it == std::end(sDevelopmentSdkCodeNames)) {
90     return {};
91   }
92   if (code_name.size() == it->size()) {
93     return sDevelopmentSdkLevel;
94   }
95   if (code_name.size() == it->size() + sPrivacySandboxSuffix.size() &&
96       code_name.ends_with(sPrivacySandboxSuffix)) {
97     return sDevelopmentSdkLevel;
98   }
99   return {};
100 }
101 
102 }  // namespace aapt
103