From 057456d40eebd7a1466729b67f69c33421971f20 Mon Sep 17 00:00:00 2001 From: Chris Warrington Date: Mon, 18 Jul 2022 11:06:17 +0000 Subject: [PATCH] AAPT2: Support PrivacySandbox development code names + get rid of dynamic memory allocation for static tables Test: new unit tests Bug: 74337026 Bug: 283007771 Bug: 283388596 Change-Id: I39fa8f55de107c5251568ef60b9ba5a98b67dc15 --- tools/aapt2/SdkConstants.cpp | 46 +++++++++++++++++++++---------- tools/aapt2/SdkConstants_test.cpp | 20 ++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/tools/aapt2/SdkConstants.cpp b/tools/aapt2/SdkConstants.cpp index a766bd4371209..83f2eb31aa573 100644 --- a/tools/aapt2/SdkConstants.cpp +++ b/tools/aapt2/SdkConstants.cpp @@ -16,20 +16,24 @@ #include "SdkConstants.h" +#include + #include #include -#include -#include +#include using android::StringPiece; +using namespace std::literals; namespace aapt { -static ApiVersion sDevelopmentSdkLevel = 10000; -static const auto sDevelopmentSdkCodeNames = std::unordered_set( - {"Q", "R", "S", "Sv2", "Tiramisu", "UpsideDownCake", "VanillaIceCream"}); +static constexpr ApiVersion sDevelopmentSdkLevel = 10000; +static constexpr StringPiece sDevelopmentSdkCodeNames[] = { + "Q"sv, "R"sv, "S"sv, "Sv2"sv, "Tiramisu"sv, "UpsideDownCake"sv, "VanillaIceCream"sv}; -static const std::vector> sAttrIdMap = { +static constexpr auto sPrivacySandboxSuffix = "PrivacySandbox"sv; + +static constexpr std::pair sAttrIdMap[] = { {0x021c, 1}, {0x021d, 2}, {0x0269, SDK_CUPCAKE}, @@ -62,25 +66,37 @@ static const std::vector> sAttrIdMap = { {0x064c, SDK_S_V2}, }; -static bool less_entry_id(const std::pair& p, uint16_t entryId) { - return p.first < entryId; -} +static_assert(std::is_sorted(std::begin(sAttrIdMap), std::end(sAttrIdMap), + [](auto&& l, auto&& r) { return l.first < r.first; })); ApiVersion FindAttributeSdkLevel(const ResourceId& id) { if (id.package_id() != 0x01 || id.type_id() != 0x01) { return 0; } - auto iter = std::lower_bound(sAttrIdMap.begin(), sAttrIdMap.end(), id.entry_id(), less_entry_id); - if (iter == sAttrIdMap.end()) { + const auto it = + std::lower_bound(std::begin(sAttrIdMap), std::end(sAttrIdMap), id.entry_id(), + [](const auto& pair, uint16_t entryId) { return pair.first < entryId; }); + if (it == std::end(sAttrIdMap)) { return SDK_LOLLIPOP_MR1; } - return iter->second; + return it->second; } std::optional GetDevelopmentSdkCodeNameVersion(StringPiece code_name) { - return (sDevelopmentSdkCodeNames.find(code_name) == sDevelopmentSdkCodeNames.end()) - ? std::optional() - : sDevelopmentSdkLevel; + const auto it = + std::find_if(std::begin(sDevelopmentSdkCodeNames), std::end(sDevelopmentSdkCodeNames), + [code_name](const auto& item) { return code_name.starts_with(item); }); + if (it == std::end(sDevelopmentSdkCodeNames)) { + return {}; + } + if (code_name.size() == it->size()) { + return sDevelopmentSdkLevel; + } + if (code_name.size() == it->size() + sPrivacySandboxSuffix.size() && + code_name.ends_with(sPrivacySandboxSuffix)) { + return sDevelopmentSdkLevel; + } + return {}; } } // namespace aapt diff --git a/tools/aapt2/SdkConstants_test.cpp b/tools/aapt2/SdkConstants_test.cpp index 61f4d71b7fb2c..0f645ffc1e8b6 100644 --- a/tools/aapt2/SdkConstants_test.cpp +++ b/tools/aapt2/SdkConstants_test.cpp @@ -28,4 +28,24 @@ TEST(SdkConstantsTest, NonFrameworkAttributeIsSdk0) { EXPECT_EQ(0, FindAttributeSdkLevel(ResourceId(0x7f010345))); } +TEST(SdkConstantsTest, GetDevelopmentSdkCodeNameVersionValid) { + EXPECT_EQ(std::optional(10000), GetDevelopmentSdkCodeNameVersion("Q")); + EXPECT_EQ(std::optional(10000), GetDevelopmentSdkCodeNameVersion("VanillaIceCream")); +} + +TEST(SdkConstantsTest, GetDevelopmentSdkCodeNameVersionPrivacySandbox) { + EXPECT_EQ(std::optional(10000), GetDevelopmentSdkCodeNameVersion("QPrivacySandbox")); + EXPECT_EQ(std::optional(10000), + GetDevelopmentSdkCodeNameVersion("VanillaIceCreamPrivacySandbox")); +} + +TEST(SdkConstantsTest, GetDevelopmentSdkCodeNameVersionInvalid) { + EXPECT_EQ(std::optional(), GetDevelopmentSdkCodeNameVersion("A")); + EXPECT_EQ(std::optional(), GetDevelopmentSdkCodeNameVersion("Sv3")); + EXPECT_EQ(std::optional(), + GetDevelopmentSdkCodeNameVersion("VanillaIceCream_PrivacySandbox")); + EXPECT_EQ(std::optional(), GetDevelopmentSdkCodeNameVersion("PrivacySandbox")); + EXPECT_EQ(std::optional(), GetDevelopmentSdkCodeNameVersion("QQQQQQQQQQQQQQQ")); +} + } // namespace aapt