blob: 6d8925ba48cafa14cc7b8b9bdfea1cb1833afd86 [file]
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "link/FlaggedBagVersioner.h"
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include "ResourceTable.h"
#include "SdkConstants.h"
#include "ValueVisitor.h"
#include "util/Util.h"
namespace aapt {
namespace {
struct CompareConfigValueDescending {
static bool operator()(const ResourceConfigValue* a, const ResourceConfigValue* b) {
int cmp = b->config.compare(a->config);
if (cmp == 0) {
cmp = b->product.compare(a->product);
}
return cmp < 0;
}
};
struct CompareConfigValueAscending {
static bool operator()(const std::unique_ptr<ResourceConfigValue>& a,
const std::unique_ptr<ResourceConfigValue>& b) {
return CompareConfigValueDescending{}(b.get(), a.get());
}
};
} // namespace
bool FlaggedBagVersioner::IsFlagEnabled(const FeatureFlagAttribute& flag) const {
auto it = feature_flag_values_.find(flag.name);
if (it != feature_flag_values_.end() && it->second.enabled.has_value()) {
return *it->second.enabled != flag.negated;
}
return false;
}
void FlaggedBagVersioner::FilterAndStripFlaggedElements(Value* value) const {
// For styles and arrays, we filter out elements whose flags are disabled. For elements
// whose flags are enabled, we strip the flag attributes (converting them to regular,
// unflagged elements).
//
// Additionally, for styles, if a flagged element is enabled, it overrides any unflagged
// fallback element for the same attribute (key) in the same style. Therefore, we collect
// the keys of all enabled flagged attributes first, and then remove any unflagged elements
// with those same keys.
if (auto* style = ValueCast<Style>(value)) {
std::set<Reference> flag_enabled_attributes;
for (const auto& entry : style->entries) {
if (entry.value->GetFlag().has_value()) {
if (IsFlagEnabled(entry.value->GetFlag().value())) {
flag_enabled_attributes.insert(entry.key);
}
}
}
std::erase_if(style->entries, [&](Style::Entry& entry) {
if (entry.value->GetFlag().has_value()) {
if (!IsFlagEnabled(entry.value->GetFlag().value())) {
return true;
}
entry.value->SetFlag(std::nullopt);
entry.value->SetFlagStatus(FlagStatus::NoFlag);
} else {
if (flag_enabled_attributes.contains(entry.key)) {
return true;
}
}
return false;
});
} else if (auto* arr = ValueCast<Array>(value)) {
std::erase_if(arr->elements, [&](std::unique_ptr<Item>& element) {
if (element->GetFlag().has_value()) {
if (!IsFlagEnabled(element->GetFlag().value())) {
return true;
}
element->SetFlag(std::nullopt);
element->SetFlagStatus(FlagStatus::NoFlag);
}
return false;
});
}
}
bool FlaggedBagVersioner::Process(IAaptContext* context, ResourceTable* table) {
if (IsSufficientForFlaggedBagElements(context->GetMinSdkVersion())) {
return true;
}
for (auto& package : table->packages) {
for (auto& type : package->types) {
if (type->named_type.type != ResourceType::kArray &&
type->named_type.type != ResourceType::kStyle) {
continue;
}
for (auto& entry : type->entries) {
// Optimization: Check if this entry even has flagged bag elements before processing.
bool has_flagged_elements = std::ranges::any_of(
entry->values, [](const auto& cv) { return cv->value->HasFlaggedElements(); });
if (!has_flagged_elements) {
continue;
}
// Reset containers to reuse memory allocations
sorted_values_.clear();
versions_to_add_.clear();
ProcessEntry(table, entry.get());
}
}
}
return true;
}
void FlaggedBagVersioner::ProcessEntry(ResourceTable* table, ResourceEntry* entry) {
for (auto& config_value : entry->values) {
sorted_values_.push_back(config_value.get());
}
// 1. We sort the configurations in descending order. This ensures that we process
// the highest SDK versions first.
std::sort(sorted_values_.begin(), sorted_values_.end(), CompareConfigValueDescending{});
std::set<std::string> products_seen;
for (auto* config_value : sorted_values_) {
const std::string& product = config_value->product;
// Track if this is the highest version of the resource for this product.
// Since we iterate in descending order, the first time we see a product,
// it is the highest version.
bool is_highest_version = products_seen.insert(product).second;
if (config_value->value->HasFlaggedElements()) {
const bool config_sufficient = IsSufficientForFlaggedBagElements(
config_value->config.sdkVersion, config_value->config.minorVersion);
if (!config_sufficient) {
// 2. If the configuration is not sufficient for flagged elements, we need to
// split it. We clone the highest flagged version of the resource for this product
// to the minimum sufficient SDK version (v37.2).
if (is_highest_version) {
android::ConfigDescription target_config = config_value->config;
target_config.sdkVersion = MIN_FLAGGED_BAG_ELEMENTS_SDK;
target_config.minorVersion = MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION;
// Only clone if the developer hasn't already defined a value for this target config.
if (entry->FindValue(target_config, product) == nullptr) {
CloningValueTransformer cloner(&table->string_pool);
std::unique_ptr<Value> cloned_value = config_value->value->Transform(cloner);
auto new_config_value = util::make_unique<ResourceConfigValue>(target_config, product);
new_config_value->value = std::move(cloned_value);
new_config_value->uses_readwrite_feature_flags = true;
versions_to_add_.push_back(std::move(new_config_value));
}
}
// 3. For the original insufficient configuration, we filter out disabled flagged
// elements and strip the flags from enabled ones.
FilterAndStripFlaggedElements(config_value->value.get());
config_value->uses_readwrite_feature_flags = false;
}
}
}
if (!versions_to_add_.empty()) {
for (auto& version : versions_to_add_) {
entry->values.push_back(std::move(version));
}
std::sort(entry->values.begin(), entry->values.end(), CompareConfigValueAscending{});
}
}
} // namespace aapt