1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
|
/*
* Copyright (C) 2015 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 "ResourceTable.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <tuple>
#include "android-base/logging.h"
#include "androidfw/ConfigDescription.h"
#include "androidfw/ResourceTypes.h"
#include "NameMangler.h"
#include "ResourceUtils.h"
#include "ResourceValues.h"
#include "ValueVisitor.h"
#include "text/Unicode.h"
#include "trace/TraceBuffer.h"
#include "util/Util.h"
using ::aapt::text::IsValidResourceEntryName;
using ::android::ConfigDescription;
using ::android::StringPiece;
using ::android::base::StringPrintf;
namespace aapt {
const char* Overlayable::kActorScheme = "overlay";
namespace {
bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
return lhs->type < rhs;
}
template <typename T>
bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
}
template <typename T>
bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
}
template <typename T>
struct NameEqualRange {
bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
return less_than_struct_with_name<T>(lhs, rhs);
}
bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
return greater_than_struct_with_name<T>(lhs, rhs);
}
};
template <typename T, typename U>
bool less_than_struct_with_name_and_id(const T& lhs,
const std::pair<std::string_view, std::optional<U>>& rhs) {
if (lhs.id != rhs.second) {
return lhs.id < rhs.second;
}
return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
}
template <typename T, typename Func, typename Elements>
T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
const auto iter =
std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
const bool found = iter != entries.end() && name == (*iter)->name;
return action(found, iter);
}
struct ConfigKey {
const ConfigDescription* config;
const StringPiece& product;
};
template <typename T>
bool lt_config_key_ref(const T& lhs, const ConfigKey& rhs) {
int cmp = lhs->config.compare(*rhs.config);
if (cmp == 0) {
cmp = StringPiece(lhs->product).compare(rhs.product);
}
return cmp < 0;
}
} // namespace
ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
}
ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
return FindElementsRunAction<ResourceTablePackage>(
name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
}
ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
});
}
template <typename Func, typename Elements>
static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) {
const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
const bool found = iter != entries.end() && type == (*iter)->type;
return action(found, iter);
}
ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const {
return FindTypeRunAction(type, types,
[&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
}
ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
});
}
ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
return entries.emplace(iter, new ResourceEntry(name))->get();
});
}
ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
return FindElementsRunAction<ResourceEntry>(
name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
}
ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
});
}
ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
android::StringPiece product) {
auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
if (iter != values.end()) {
ResourceConfigValue* value = iter->get();
if (value->config == config && StringPiece(value->product) == product) {
return value;
}
}
return nullptr;
}
const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
android::StringPiece product) const {
auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
if (iter != values.end()) {
ResourceConfigValue* value = iter->get();
if (value->config == config && StringPiece(value->product) == product) {
return value;
}
}
return nullptr;
}
ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
const StringPiece& product) {
auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
if (iter != values.end()) {
ResourceConfigValue* value = iter->get();
if (value->config == config && StringPiece(value->product) == product) {
return value;
}
}
ResourceConfigValue* newValue =
values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
return newValue;
}
std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
std::vector<ResourceConfigValue*> results;
auto iter = values.begin();
for (; iter != values.end(); ++iter) {
ResourceConfigValue* value = iter->get();
if (value->config == config) {
results.push_back(value);
++iter;
break;
}
}
for (; iter != values.end(); ++iter) {
ResourceConfigValue* value = iter->get();
if (value->config == config) {
results.push_back(value);
}
}
return results;
}
bool ResourceEntry::HasDefaultValue() const {
const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
// The default config should be at the top of the list, since the list is sorted.
for (auto& config_value : values) {
if (config_value->config == default_config) {
return true;
}
}
return false;
}
// The default handler for collisions.
//
// Typically, a weak value will be overridden by a strong value. An existing weak
// value will not be overridden by an incoming weak value.
//
// There are some exceptions:
//
// Attributes: There are two types of Attribute values: USE and DECL.
//
// USE is anywhere an Attribute is declared without a format, and in a place that would
// be legal to declare if the Attribute already existed. This is typically in a
// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
//
// DECL is an absolute declaration of an Attribute and specifies an explicit format.
//
// A DECL will override a USE without error. Two DECLs must match in their format for there to be
// no error.
ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
Value* incoming) {
Attribute* existing_attr = ValueCast<Attribute>(existing);
Attribute* incoming_attr = ValueCast<Attribute>(incoming);
if (!incoming_attr) {
if (incoming->IsWeak()) {
// We're trying to add a weak resource but a resource
// already exists. Keep the existing.
return CollisionResult::kKeepOriginal;
} else if (existing->IsWeak()) {
// Override the weak resource with the new strong resource.
return CollisionResult::kTakeNew;
}
// The existing and incoming values are strong, this is an error
// if the values are not both attributes.
return CollisionResult::kConflict;
}
if (!existing_attr) {
if (existing->IsWeak()) {
// The existing value is not an attribute and it is weak,
// so take the incoming attribute value.
return CollisionResult::kTakeNew;
}
// The existing value is not an attribute and it is strong,
// so the incoming attribute value is an error.
return CollisionResult::kConflict;
}
CHECK(incoming_attr != nullptr && existing_attr != nullptr);
//
// Attribute specific handling. At this point we know both
// values are attributes. Since we can declare and define
// attributes all-over, we do special handling to see
// which definition sticks.
//
if (existing_attr->IsCompatibleWith(*incoming_attr)) {
// The two attributes are both DECLs, but they are plain attributes with compatible formats.
// Keep the strongest one.
return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
}
if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
// Any incoming attribute is better than this.
return CollisionResult::kTakeNew;
}
if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
// The incoming attribute may be a USE instead of a DECL.
// Keep the existing attribute.
return CollisionResult::kKeepOriginal;
}
return CollisionResult::kConflict;
}
namespace {
template <typename T, typename Comparer>
struct SortedVectorInserter : public Comparer {
std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
const T& value) {
auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
return Comparer::operator()(lhs, rhs);
});
bool found =
it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
return std::make_pair(found, it);
}
T* Insert(std::vector<T>& el, T&& value) {
auto [found, it] = LowerBound(el, value);
if (found) {
return &*it;
}
return &*el.insert(it, std::forward<T>(value));
}
};
struct PackageViewComparer {
bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
lhs, std::make_pair(rhs.name, rhs.id));
}
};
struct TypeViewComparer {
bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.type < rhs.type;
}
};
struct EntryViewComparer {
bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) {
return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>(
lhs, std::make_pair(rhs.name, rhs.id));
}
};
void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package,
const ResourceTableType* type, const std::string& entry_name,
const std::optional<ResourceId>& id, const Visibility& visibility,
const std::optional<AllowNew>& allow_new,
const std::optional<OverlayableItem>& overlayable_item,
const std::optional<StagedId>& staged_id,
const std::vector<std::unique_ptr<ResourceConfigValue>>& values) {
SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter;
ResourceTablePackageView new_package{package->name,
id ? id.value().package_id() : std::optional<uint8_t>{}};
auto view_package = package_inserter.Insert(table.packages, std::move(new_package));
ResourceTableTypeView new_type{type->type, id ? id.value().type_id() : std::optional<uint8_t>{}};
auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
if (visibility.level == Visibility::Level::kPublic) {
// Only mark the type visibility level as public, it doesn't care about being private.
view_type->visibility_level = Visibility::Level::kPublic;
}
ResourceTableEntryView new_entry{.name = entry_name,
.id = id ? id.value().entry_id() : std::optional<uint16_t>{},
.visibility = visibility,
.allow_new = allow_new,
.overlayable_item = overlayable_item,
.staged_id = staged_id};
for (auto& value : values) {
new_entry.values.emplace_back(value.get());
}
entry_inserter.Insert(view_type->entries, std::move(new_entry));
}
} // namespace
const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config,
android::StringPiece product) const {
auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
lt_config_key_ref<const ResourceConfigValue*>);
if (iter != values.end()) {
const ResourceConfigValue* value = *iter;
if (value->config == config && StringPiece(value->product) == product) {
return value;
}
}
return nullptr;
}
ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const {
ResourceTableView view;
for (const auto& package : packages) {
for (const auto& type : package->types) {
for (const auto& entry : type->entries) {
InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id,
entry->visibility, entry->allow_new, entry->overlayable_item,
entry->staged_id, entry->values);
if (options.create_alias_entries && entry->staged_id) {
auto alias_id = entry->staged_id.value().id;
InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id,
entry->visibility, entry->allow_new, entry->overlayable_item, {},
entry->values);
}
}
}
}
// The android runtime does not support querying resources when the there are multiple type ids
// for the same resource type within the same package. For this reason, if there are types with
// multiple type ids, each type needs to exist in its own package in order to be queried by name.
std::vector<ResourceTablePackageView> new_packages;
SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
for (auto& package : view.packages) {
// If a new package was already created for a different type within this package, then
// we can reuse those packages for other types that need to be extracted from this package.
// `start_index` is the index of the first newly created package that can be reused.
const size_t start_index = new_packages.size();
std::map<ResourceType, size_t> type_new_package_index;
for (auto type_it = package.types.begin(); type_it != package.types.end();) {
auto& type = *type_it;
auto type_index_iter = type_new_package_index.find(type.type);
if (type_index_iter == type_new_package_index.end()) {
// First occurrence of the resource type in this package. Keep it in this package.
type_new_package_index.insert(type_index_iter, std::make_pair(type.type, start_index));
++type_it;
continue;
}
// The resource type has already been seen for this package, so this type must be extracted to
// a new separate package.
const size_t index = type_index_iter->second;
if (new_packages.size() == index) {
new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
}
// Move the type into a new package
auto& other_package = new_packages[index];
type_new_package_index[type.type] = index + 1;
type_inserter.Insert(other_package.types, std::move(type));
type_it = package.types.erase(type_it);
}
}
for (auto& new_package : new_packages) {
// Insert newly created packages after their original packages
auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
view.packages.insert(++it, std::move(new_package));
}
return view;
}
bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
CHECK(diag != nullptr) << "Diagnostic pointer is null";
const bool validate = validation_ == Validation::kEnabled;
const Source source = res.value ? res.value->GetSource() : Source{};
if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
diag->Error(DiagMessage(source)
<< "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
return false;
}
if (res.id.has_value() && !res.id->first.is_valid()) {
diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
<< res.id->first << " but that ID is invalid");
return false;
}
auto package = FindOrCreatePackage(res.name.package);
auto type = package->FindOrCreateType(res.name.type.type);
auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
NameEqualRange<ResourceEntry>{});
const size_t entry_count = std::distance(entry_it.first, entry_it.second);
ResourceEntry* entry;
if (entry_count == 0) {
// Adding a new resource
entry = type->CreateEntry(res.name.entry);
} else if (entry_count == 1) {
// Assume that the existing resource is being modified
entry = entry_it.first->get();
} else {
// Multiple resources with the same name exist in the resource table. The only way to
// distinguish between them is using resource id since each resource should have a unique id.
CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
<< "' without specifying a resource id.";
entry = entry_it.first->get();
for (auto it = entry_it.first; it != entry_it.second; ++it) {
CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
<< "' with multiple entries without resource ids";
if ((*it)->id == res.id->first) {
entry = it->get();
break;
}
}
}
if (res.id.has_value()) {
if (entry->id && entry->id.value() != res.id->first) {
if (res.id->second != OnIdConflict::CREATE_ENTRY) {
diag->Error(DiagMessage(source)
<< "trying to add resource '" << res.name << "' with ID " << res.id->first
<< " but resource already has ID " << entry->id.value());
return false;
}
entry = type->CreateEntry(res.name.entry);
}
entry->id = res.id->first;
}
if (res.visibility.has_value()) {
// Only mark the type visibility level as public, it doesn't care about being private.
if (res.visibility->level == Visibility::Level::kPublic) {
type->visibility_level = Visibility::Level::kPublic;
}
if (res.visibility->level > entry->visibility.level) {
// This symbol definition takes precedence, replace.
entry->visibility = res.visibility.value();
}
if (res.visibility->staged_api) {
entry->visibility.staged_api = entry->visibility.staged_api;
}
}
if (res.overlayable.has_value()) {
if (entry->overlayable_item) {
diag->Error(DiagMessage(res.overlayable->source)
<< "duplicate overlayable declaration for resource '" << res.name << "'");
diag->Error(DiagMessage(entry->overlayable_item.value().source)
<< "previous declaration here");
return false;
}
entry->overlayable_item = res.overlayable.value();
}
if (res.allow_new.has_value()) {
entry->allow_new = res.allow_new.value();
}
if (res.staged_id.has_value()) {
entry->staged_id = res.staged_id.value();
}
if (res.value != nullptr) {
auto config_value = entry->FindOrCreateValue(res.config, res.product);
if (!config_value->value) {
// Resource does not exist, add it now.
config_value->value = std::move(res.value);
} else {
// When validation is enabled, ensure that a resource cannot have multiple values defined for
// the same configuration.
auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
: CollisionResult::kKeepBoth;
switch (result) {
case CollisionResult::kKeepBoth:
// Insert the value ignoring for duplicate configurations
entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
entry->values.back()->value = std::move(res.value);
break;
case CollisionResult::kTakeNew:
// Take the incoming value.
config_value->value = std::move(res.value);
break;
case CollisionResult::kConflict:
diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
<< "with config '" << res.config << "'");
diag->Error(DiagMessage(source) << "resource previously defined here");
return false;
case CollisionResult::kKeepOriginal:
break;
}
}
}
return true;
}
std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(
const ResourceNameRef& name) const {
ResourceTablePackage* package = FindPackage(name.package);
if (package == nullptr) {
return {};
}
ResourceTableType* type = package->FindType(name.type.type);
if (type == nullptr) {
return {};
}
ResourceEntry* entry = type->FindEntry(name.entry);
if (entry == nullptr) {
return {};
}
return SearchResult{package, type, entry};
}
std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
ResourceId id) const {
ResourceTablePackage* package = FindPackage(name.package);
if (package == nullptr) {
return {};
}
ResourceTableType* type = package->FindType(name.type.type);
if (type == nullptr) {
return {};
}
auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
NameEqualRange<ResourceEntry>{});
for (auto it = entry_it.first; it != entry_it.second; ++it) {
if ((*it)->id == id) {
return SearchResult{package, type, it->get()};
}
}
return {};
}
bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const {
ResourceTablePackage* package = FindPackage(name.package);
if (package == nullptr) {
return {};
}
ResourceTableType* type = package->FindType(name.type.type);
if (type == nullptr) {
return {};
}
auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
NameEqualRange<ResourceEntry>{});
for (auto it = entry_it.first; it != entry_it.second; ++it) {
if ((*it)->id == id) {
type->entries.erase(it);
return true;
}
}
return false;
}
std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
CloningValueTransformer cloner(&new_table->string_pool);
for (const auto& pkg : packages) {
ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
for (const auto& type : pkg->types) {
ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
new_type->visibility_level = type->visibility_level;
for (const auto& entry : type->entries) {
ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
new_entry->id = entry->id;
new_entry->visibility = entry->visibility;
new_entry->allow_new = entry->allow_new;
new_entry->overlayable_item = entry->overlayable_item;
for (const auto& config_value : entry->values) {
ResourceConfigValue* new_value =
new_entry->FindOrCreateValue(config_value->config, config_value->product);
new_value->value = config_value->value->Transform(cloner);
}
}
}
}
return new_table;
}
NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
res_.name = name.ToResourceName();
}
NewResourceBuilder::NewResourceBuilder(const std::string& name) {
ResourceNameRef ref;
CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
res_.name = ref.ToResourceName();
}
NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
android::ConfigDescription config,
std::string product) {
res_.value = std::move(value);
res_.config = std::move(config);
res_.product = std::move(product);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
res_.id = std::make_pair(id, on_conflict);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
res_.visibility = std::move(visibility);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
res_.overlayable = std::move(overlayable);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
res_.allow_new = std::move(allow_new);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) {
res_.staged_id = std::move(staged_alias);
return *this;
}
NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
res_.allow_mangled = allow_mangled;
return *this;
}
NewResource NewResourceBuilder::Build() {
return std::move(res_);
}
} // namespace aapt
|