blob: 5435cba290fc67059f8401bed4af80bcb20b0e56 [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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceTable.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <memory>
Ryan Mitchell4382e442021-07-14 12:53:01 -070021#include <optional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <tuple>
23
Adam Lesinski66ea8402017-06-28 11:44:11 -070024#include "android-base/logging.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070026#include "androidfw/ResourceTypes.h"
27
Adam Lesinski66ea8402017-06-28 11:44:11 -070028#include "NameMangler.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070029#include "ResourceUtils.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070030#include "ResourceValues.h"
31#include "ValueVisitor.h"
32#include "text/Unicode.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070033#include "trace/TraceBuffer.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070034#include "util/Util.h"
35
36using ::aapt::text::IsValidResourceEntryName;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski66ea8402017-06-28 11:44:11 -070038using ::android::StringPiece;
Adam Lesinski71be7052017-12-12 16:48:07 -080039using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080040
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041namespace aapt {
42
Ryan Mitchell54237ff2018-12-13 15:44:29 -080043const char* Overlayable::kActorScheme = "overlay";
44
Ryan Mitchell9634efb2021-03-19 14:53:17 -070045namespace {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +000046bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs,
47 const ResourceNamedTypeRef& rhs) {
48 return lhs->named_type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049}
50
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051template <typename T>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070052bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, StringPiece rhs) {
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070053 return lhs->name < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054}
55
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000056template <typename T>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070057bool greater_than_struct_with_name(StringPiece lhs, const std::unique_ptr<T>& rhs) {
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070058 return rhs->name > lhs;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000059}
60
Ryan Mitchell9634efb2021-03-19 14:53:17 -070061template <typename T>
62struct NameEqualRange {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070063 bool operator()(const std::unique_ptr<T>& lhs, StringPiece rhs) const {
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070064 return less_than_struct_with_name(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 }
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070066 bool operator()(StringPiece lhs, const std::unique_ptr<T>& rhs) const {
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070067 return greater_than_struct_with_name(lhs, rhs);
Ryan Mitchell9634efb2021-03-19 14:53:17 -070068 }
69};
70
71template <typename T, typename U>
72bool less_than_struct_with_name_and_id(const T& lhs,
Ryan Mitchell4382e442021-07-14 12:53:01 -070073 const std::pair<std::string_view, std::optional<U>>& rhs) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -070074 if (lhs.id != rhs.second) {
75 return lhs.id < rhs.second;
76 }
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070077 return lhs.name < rhs.first;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078}
79
Ryan Mitchell9634efb2021-03-19 14:53:17 -070080template <typename T, typename Func, typename Elements>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070081T* FindElementsRunAction(android::StringPiece name, Elements& entries, Func action) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -070082 const auto iter =
83 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
84 const bool found = iter != entries.end() && name == (*iter)->name;
85 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086}
87
Ryan Mitchell2fedba92021-04-23 07:47:38 -070088struct ConfigKey {
89 const ConfigDescription* config;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070090 StringPiece product;
Ryan Mitchell2fedba92021-04-23 07:47:38 -070091};
92
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -070093struct lt_config_key_ref {
94 template <typename T>
95 bool operator()(const T& lhs, const ConfigKey& rhs) const noexcept {
96 int cmp = lhs->config.compare(*rhs.config);
97 if (cmp == 0) {
98 cmp = lhs->product.compare(rhs.product);
99 }
100 return cmp < 0;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700101 }
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700102};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700103
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700104struct ConfigFlagKey {
105 const ConfigDescription* config;
106 StringPiece product;
107 const FeatureFlagAttribute& flag;
108};
109
110struct lt_config_flag_key_ref {
111 template <typename T>
112 bool operator()(const T& lhs, const ConfigFlagKey& rhs) const noexcept {
113 return std::tie(lhs->config, lhs->product, lhs->value->GetFlag()->name,
114 lhs->value->GetFlag()->negated) <
115 std::tie(*rhs.config, rhs.product, rhs.flag.name, rhs.flag.negated);
116 }
117};
118
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700119} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000120
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700121ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000122}
123
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700124ResourceTablePackage* ResourceTable::FindPackage(android::StringPiece name) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700125 return FindElementsRunAction<ResourceTablePackage>(
126 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127}
128
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700129ResourceTablePackage* ResourceTable::FindOrCreatePackage(android::StringPiece name) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700130 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
131 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
132 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133}
134
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700135template <typename Func, typename Elements>
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000136static ResourceTableType* FindTypeRunAction(const ResourceNamedTypeRef& type, Elements& entries,
137 Func action) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700138 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000139 const bool found = iter != entries.end() && type == (*iter)->named_type;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700140 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141}
142
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000143ResourceTableType* ResourceTablePackage::FindTypeWithDefaultName(const ResourceType type) const {
144 auto named_type = ResourceNamedTypeWithDefaultName(type);
145 return FindType(named_type);
146}
147
148ResourceTableType* ResourceTablePackage::FindType(const ResourceNamedTypeRef& type) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700149 return FindTypeRunAction(type, types,
150 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151}
152
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000153ResourceTableType* ResourceTablePackage::FindOrCreateType(const ResourceNamedTypeRef& type) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700154 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
155 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
156 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700159ResourceEntry* ResourceTableType::CreateEntry(android::StringPiece name) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700160 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
161 return entries.emplace(iter, new ResourceEntry(name))->get();
162 });
163}
164
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700165ResourceEntry* ResourceTableType::FindEntry(android::StringPiece name) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700166 return FindElementsRunAction<ResourceEntry>(
167 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
168}
169
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700170ResourceEntry* ResourceTableType::FindOrCreateEntry(android::StringPiece name) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700171 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
172 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
173 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800174}
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700177 android::StringPiece product) {
178 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700179 lt_config_key_ref());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700180 if (iter != values.end()) {
181 ResourceConfigValue* value = iter->get();
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700182 if (value->config == config && value->product == product) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700183 return value;
184 }
185 }
186 return nullptr;
187}
188
189const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
190 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800191 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700192 lt_config_key_ref());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 if (iter != values.end()) {
194 ResourceConfigValue* value = iter->get();
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700195 if (value->config == config && value->product == product) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800197 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
199 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800200}
201
Adam Lesinskib1afa072017-03-29 13:52:38 -0700202ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700203 StringPiece product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800204 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700205 lt_config_key_ref());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 if (iter != values.end()) {
207 ResourceConfigValue* value = iter->get();
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700208 if (value->config == config && value->product == product) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800210 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
212 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700213 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800215}
216
Adam Lesinskib1afa072017-03-29 13:52:38 -0700217std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 std::vector<ResourceConfigValue*> results;
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700219 auto iter =
220 std::lower_bound(values.begin(), values.end(), ConfigKey{&config, ""}, lt_config_key_ref());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 for (; iter != values.end(); ++iter) {
222 ResourceConfigValue* value = iter->get();
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700223 if (value->config != config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800225 }
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700226 results.push_back(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 }
228 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800229}
230
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700231ResourceConfigValue* ResourceEntry::FindOrCreateFlagDisabledValue(
232 const FeatureFlagAttribute& flag, const android::ConfigDescription& config,
233 android::StringPiece product) {
234 auto iter = std::lower_bound(flag_disabled_values.begin(), flag_disabled_values.end(),
235 ConfigFlagKey{&config, product, flag}, lt_config_flag_key_ref());
236 if (iter != flag_disabled_values.end()) {
237 ResourceConfigValue* value = iter->get();
238 const auto value_flag = value->value->GetFlag().value();
239 if (value_flag.name == flag.name && value_flag.negated == flag.negated &&
240 value->config == config && value->product == product) {
241 return value;
242 }
243 }
244 ResourceConfigValue* newValue =
245 flag_disabled_values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))
246 ->get();
247 return newValue;
248}
249
Adam Lesinski34a16872018-02-23 16:18:10 -0800250bool ResourceEntry::HasDefaultValue() const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800251 // The default config should be at the top of the list, since the list is sorted.
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700252 return !values.empty() && values.front()->config == ConfigDescription::DefaultConfig();
Adam Lesinski458b8772016-04-25 14:20:21 -0700253}
254
Jeremy Meyer211bec22024-06-04 14:22:03 -0700255ResourceTable::CollisionResult ResourceTable::ResolveFlagCollision(FlagStatus existing,
256 FlagStatus incoming) {
257 switch (existing) {
258 case FlagStatus::NoFlag:
259 switch (incoming) {
260 case FlagStatus::NoFlag:
261 return CollisionResult::kConflict;
262 case FlagStatus::Disabled:
263 return CollisionResult::kKeepOriginal;
264 case FlagStatus::Enabled:
265 return CollisionResult::kTakeNew;
266 default:
267 return CollisionResult::kConflict;
268 }
269 case FlagStatus::Disabled:
270 switch (incoming) {
271 case FlagStatus::NoFlag:
272 return CollisionResult::kTakeNew;
273 case FlagStatus::Disabled:
274 return CollisionResult::kKeepOriginal;
275 case FlagStatus::Enabled:
276 return CollisionResult::kTakeNew;
277 default:
278 return CollisionResult::kConflict;
279 }
280 case FlagStatus::Enabled:
281 switch (incoming) {
282 case FlagStatus::NoFlag:
283 return CollisionResult::kKeepOriginal;
284 case FlagStatus::Disabled:
285 return CollisionResult::kKeepOriginal;
286 case FlagStatus::Enabled:
287 return CollisionResult::kConflict;
288 default:
289 return CollisionResult::kConflict;
290 }
291 default:
292 return CollisionResult::kConflict;
293 }
294}
295
Adam Lesinski71be7052017-12-12 16:48:07 -0800296// The default handler for collisions.
297//
298// Typically, a weak value will be overridden by a strong value. An existing weak
299// value will not be overridden by an incoming weak value.
300//
301// There are some exceptions:
302//
303// Attributes: There are two types of Attribute values: USE and DECL.
304//
305// USE is anywhere an Attribute is declared without a format, and in a place that would
306// be legal to declare if the Attribute already existed. This is typically in a
307// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
308//
309// DECL is an absolute declaration of an Attribute and specifies an explicit format.
310//
311// A DECL will override a USE without error. Two DECLs must match in their format for there to be
312// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700313ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
314 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 Attribute* existing_attr = ValueCast<Attribute>(existing);
316 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
317 if (!incoming_attr) {
318 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 // We're trying to add a weak resource but a resource
320 // already exists. Keep the existing.
321 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 // Override the weak resource with the new strong resource.
324 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800325 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 // The existing and incoming values are strong, this is an error
327 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700328 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 }
330
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 if (!existing_attr) {
332 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 // The existing value is not an attribute and it is weak,
334 // so take the incoming attribute value.
335 return CollisionResult::kTakeNew;
336 }
337 // The existing value is not an attribute and it is strong,
338 // so the incoming attribute value is an error.
339 return CollisionResult::kConflict;
340 }
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343
344 //
345 // Attribute specific handling. At this point we know both
346 // values are attributes. Since we can declare and define
347 // attributes all-over, we do special handling to see
348 // which definition sticks.
349 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800350 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
351 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700353 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 }
355
Adam Lesinskib1afa072017-03-29 13:52:38 -0700356 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 // Any incoming attribute is better than this.
358 return CollisionResult::kTakeNew;
359 }
360
Adam Lesinskib1afa072017-03-29 13:52:38 -0700361 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 // The incoming attribute may be a USE instead of a DECL.
363 // Keep the existing attribute.
364 return CollisionResult::kKeepOriginal;
365 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700366
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368}
369
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700370namespace {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700371template <typename T, typename Comparer>
372struct SortedVectorInserter : public Comparer {
373 std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
374 const T& value) {
375 auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
376 return Comparer::operator()(lhs, rhs);
377 });
378 bool found =
379 it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
380 return std::make_pair(found, it);
381 }
382
383 T* Insert(std::vector<T>& el, T&& value) {
384 auto [found, it] = LowerBound(el, value);
385 if (found) {
386 return &*it;
387 }
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700388 return &*el.insert(it, std::move(value));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700389 }
390};
391
392struct PackageViewComparer {
393 bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
394 return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700395 lhs, std::tie(rhs.name, rhs.id));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700396 }
397};
398
399struct TypeViewComparer {
400 bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000401 return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.named_type < rhs.named_type;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700402 }
403};
404
405struct EntryViewComparer {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700406 bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) {
407 return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>(
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700408 lhs, std::tie(rhs.name, rhs.id));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700409 }
410};
411
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700412void InsertEntryIntoTableView(
413 ResourceTableView& table, const ResourceTablePackage* package, const ResourceTableType* type,
414 const std::string& entry_name, const std::optional<ResourceId>& id,
415 const Visibility& visibility, const std::optional<AllowNew>& allow_new,
416 const std::optional<OverlayableItem>& overlayable_item,
417 const std::optional<StagedId>& staged_id,
418 const std::vector<std::unique_ptr<ResourceConfigValue>>& values,
419 const std::vector<std::unique_ptr<ResourceConfigValue>>& flag_disabled_values) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700420 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
421 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700422 SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700423
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700424 ResourceTablePackageView new_package{package->name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700425 id ? id.value().package_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700426 auto view_package = package_inserter.Insert(table.packages, std::move(new_package));
427
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000428 ResourceTableTypeView new_type{type->named_type,
429 id ? id.value().type_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700430 auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
431
432 if (visibility.level == Visibility::Level::kPublic) {
433 // Only mark the type visibility level as public, it doesn't care about being private.
434 view_type->visibility_level = Visibility::Level::kPublic;
435 }
436
437 ResourceTableEntryView new_entry{.name = entry_name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700438 .id = id ? id.value().entry_id() : std::optional<uint16_t>{},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700439 .visibility = visibility,
440 .allow_new = allow_new,
441 .overlayable_item = overlayable_item,
442 .staged_id = staged_id};
443 for (auto& value : values) {
444 new_entry.values.emplace_back(value.get());
445 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700446 for (auto& value : flag_disabled_values) {
447 new_entry.flag_disabled_values.emplace_back(value.get());
448 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700449
450 entry_inserter.Insert(view_type->entries, std::move(new_entry));
451}
452} // namespace
453
454const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config,
455 android::StringPiece product) const {
456 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700457 lt_config_key_ref());
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700458 if (iter != values.end()) {
459 const ResourceConfigValue* value = *iter;
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700460 if (value->config == config && value->product == product) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700461 return value;
462 }
463 }
464 return nullptr;
465}
466
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700467const ResourceConfigValue* ResourceTableEntryView::FindFlagDisabledValue(
468 const FeatureFlagAttribute& flag, const ConfigDescription& config,
469 android::StringPiece product) const {
470 auto iter = std::lower_bound(flag_disabled_values.begin(), flag_disabled_values.end(),
471 ConfigFlagKey{&config, product, flag}, lt_config_flag_key_ref());
472 if (iter != values.end()) {
473 const ResourceConfigValue* value = *iter;
474 if (value->value->GetFlag() == flag && value->config == config &&
475 StringPiece(value->product) == product) {
476 return value;
477 }
478 }
479 return nullptr;
480}
481
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700482ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const {
483 ResourceTableView view;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700484 for (const auto& package : packages) {
485 for (const auto& type : package->types) {
486 for (const auto& entry : type->entries) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700487 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id,
488 entry->visibility, entry->allow_new, entry->overlayable_item,
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700489 entry->staged_id, entry->values, entry->flag_disabled_values);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700490
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700491 if (options.create_alias_entries && entry->staged_id) {
492 auto alias_id = entry->staged_id.value().id;
493 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id,
494 entry->visibility, entry->allow_new, entry->overlayable_item, {},
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700495 entry->values, entry->flag_disabled_values);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700496 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700497 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700501 // The android runtime does not support querying resources when the there are multiple type ids
502 // for the same resource type within the same package. For this reason, if there are types with
503 // multiple type ids, each type needs to exist in its own package in order to be queried by name.
504 std::vector<ResourceTablePackageView> new_packages;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700505 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
506 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700507 for (auto& package : view.packages) {
508 // If a new package was already created for a different type within this package, then
509 // we can reuse those packages for other types that need to be extracted from this package.
510 // `start_index` is the index of the first newly created package that can be reused.
511 const size_t start_index = new_packages.size();
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000512 std::map<ResourceNamedType, size_t> type_new_package_index;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700513 for (auto type_it = package.types.begin(); type_it != package.types.end();) {
514 auto& type = *type_it;
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000515 auto type_index_iter = type_new_package_index.find(type.named_type);
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700516 if (type_index_iter == type_new_package_index.end()) {
517 // First occurrence of the resource type in this package. Keep it in this package.
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000518 type_new_package_index.insert(type_index_iter,
519 std::make_pair(type.named_type, start_index));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700520 ++type_it;
521 continue;
522 }
523
524 // The resource type has already been seen for this package, so this type must be extracted to
525 // a new separate package.
526 const size_t index = type_index_iter->second;
527 if (new_packages.size() == index) {
528 new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700529 }
530
531 // Move the type into a new package
532 auto& other_package = new_packages[index];
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000533 type_new_package_index[type.named_type] = index + 1;
Greg Kaiserb40f3ab2021-10-18 06:37:26 -0700534 type_inserter.Insert(other_package.types, std::move(type));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700535 type_it = package.types.erase(type_it);
536 }
537 }
538
539 for (auto& new_package : new_packages) {
540 // Insert newly created packages after their original packages
541 auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
542 view.packages.insert(++it, std::move(new_package));
543 }
544
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700545 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546}
547
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000548bool ResourceTable::AddResource(NewResource&& res, android::IDiagnostics* diag) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700549 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700550
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700551 const bool validate = validation_ == Validation::kEnabled;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000552 const android::Source source = res.value ? res.value->GetSource() : android::Source{};
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700553 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000554 diag->Error(android::DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700555 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 return false;
557 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800558
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700559 if (res.id.has_value() && !res.id->first.is_valid()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000560 diag->Error(android::DiagMessage(source)
561 << "trying to add resource '" << res.name << "' with ID " << res.id->first
562 << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 return false;
564 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700565
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700566 auto package = FindOrCreatePackage(res.name.package);
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000567 auto type = package->FindOrCreateType(res.name.type);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700568 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
569 NameEqualRange<ResourceEntry>{});
570 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700571
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700572 ResourceEntry* entry;
573 if (entry_count == 0) {
574 // Adding a new resource
575 entry = type->CreateEntry(res.name.entry);
576 } else if (entry_count == 1) {
577 // Assume that the existing resource is being modified
578 entry = entry_it.first->get();
579 } else {
580 // Multiple resources with the same name exist in the resource table. The only way to
581 // distinguish between them is using resource id since each resource should have a unique id.
582 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
583 << "' without specifying a resource id.";
584 entry = entry_it.first->get();
585 for (auto it = entry_it.first; it != entry_it.second; ++it) {
586 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
587 << "' with multiple entries without resource ids";
588 if ((*it)->id == res.id->first) {
589 entry = it->get();
590 break;
591 }
592 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700595 if (res.id.has_value()) {
596 if (entry->id && entry->id.value() != res.id->first) {
597 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000598 diag->Error(android::DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700599 << "trying to add resource '" << res.name << "' with ID " << res.id->first
600 << " but resource already has ID " << entry->id.value());
601 return false;
602 }
603 entry = type->CreateEntry(res.name.entry);
604 }
605 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800607
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700608 if (res.visibility.has_value()) {
609 // Only mark the type visibility level as public, it doesn't care about being private.
610 if (res.visibility->level == Visibility::Level::kPublic) {
611 type->visibility_level = Visibility::Level::kPublic;
612 }
613
614 if (res.visibility->level > entry->visibility.level) {
615 // This symbol definition takes precedence, replace.
616 entry->visibility = res.visibility.value();
617 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700618
619 if (res.visibility->staged_api) {
620 entry->visibility.staged_api = entry->visibility.staged_api;
621 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800623
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700624 if (res.overlayable.has_value()) {
625 if (entry->overlayable_item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000626 diag->Error(android::DiagMessage(res.overlayable->source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700627 << "duplicate overlayable declaration for resource '" << res.name << "'");
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000628 diag->Error(android::DiagMessage(entry->overlayable_item.value().source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700629 << "previous declaration here");
630 return false;
631 }
632 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 }
634
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700635 if (res.allow_new.has_value()) {
636 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 }
638
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700639 if (res.staged_id.has_value()) {
640 entry->staged_id = res.staged_id.value();
641 }
642
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700643 if (res.value != nullptr && res.value->GetFlagStatus() == FlagStatus::Disabled) {
644 auto disabled_config_value =
645 entry->FindOrCreateFlagDisabledValue(res.value->GetFlag().value(), res.config, res.product);
646 if (!disabled_config_value->value) {
647 // Resource does not exist, add it now.
648 // Must clone the value since it might be in the values vector as well
649 CloningValueTransformer cloner(&string_pool);
650 disabled_config_value->value = res.value->Transform(cloner);
651 } else {
652 diag->Error(android::DiagMessage(source)
653 << "duplicate value for resource '" << res.name << "' " << "with config '"
654 << res.config << "' and flag '"
655 << (res.value->GetFlag().value().negated ? "!" : "")
656 << res.value->GetFlag().value().name << "'");
657 diag->Error(android::DiagMessage(source) << "resource previously defined here");
658 return false;
659 }
660 }
661
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700662 if (res.value != nullptr) {
663 auto config_value = entry->FindOrCreateValue(res.config, res.product);
664 if (!config_value->value) {
665 // Resource does not exist, add it now.
666 config_value->value = std::move(res.value);
667 } else {
668 // When validation is enabled, ensure that a resource cannot have multiple values defined for
Jeremy Meyer211bec22024-06-04 14:22:03 -0700669 // the same configuration unless protected by flags.
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700670 auto result = validate ? ResolveFlagCollision(config_value->value->GetFlagStatus(),
671 res.value->GetFlagStatus())
672 : CollisionResult::kKeepBoth;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700673 if (result == CollisionResult::kConflict) {
674 result = ResolveValueCollision(config_value->value.get(), res.value.get());
675 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700676 switch (result) {
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700677 case CollisionResult::kKeepBoth: {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700678 // Insert the value ignoring for duplicate configurations
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700679 auto it = entry->values.insert(
680 std::lower_bound(entry->values.begin(), entry->values.end(),
681 ConfigKey{&res.config, res.product}, lt_config_key_ref()),
682 util::make_unique<ResourceConfigValue>(res.config, res.product));
683 (*it)->value = std::move(res.value);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700684 break;
Yurii Zubrytskyia62e6be2024-08-27 14:20:51 -0700685 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700687 case CollisionResult::kTakeNew:
688 // Take the incoming value.
689 config_value->value = std::move(res.value);
690 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800691
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700692 case CollisionResult::kConflict:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000693 diag->Error(android::DiagMessage(source)
694 << "duplicate value for resource '" << res.name << "' "
695 << "with config '" << res.config << "'");
696 diag->Error(android::DiagMessage(source) << "resource previously defined here");
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700697 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800698
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700699 case CollisionResult::kKeepOriginal:
700 break;
701 }
702 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800703 }
704
Adam Lesinski71be7052017-12-12 16:48:07 -0800705 return true;
706}
707
Ryan Mitchell4382e442021-07-14 12:53:01 -0700708std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(
709 const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800711 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 return {};
713 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800714
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000715 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800716 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 return {};
718 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800721 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 return {};
723 }
724 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800725}
726
Ryan Mitchell4382e442021-07-14 12:53:01 -0700727std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
728 ResourceId id) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700729 ResourceTablePackage* package = FindPackage(name.package);
730 if (package == nullptr) {
731 return {};
732 }
733
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000734 ResourceTableType* type = package->FindType(name.type);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700735 if (type == nullptr) {
736 return {};
737 }
738
739 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
740 NameEqualRange<ResourceEntry>{});
741 for (auto it = entry_it.first; it != entry_it.second; ++it) {
742 if ((*it)->id == id) {
743 return SearchResult{package, type, it->get()};
744 }
745 }
746 return {};
747}
748
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700749bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const {
750 ResourceTablePackage* package = FindPackage(name.package);
751 if (package == nullptr) {
752 return {};
753 }
754
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000755 ResourceTableType* type = package->FindType(name.type);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700756 if (type == nullptr) {
757 return {};
758 }
759
760 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
761 NameEqualRange<ResourceEntry>{});
762 for (auto it = entry_it.first; it != entry_it.second; ++it) {
763 if ((*it)->id == id) {
764 type->entries.erase(it);
765 return true;
766 }
767 }
768 return false;
769}
770
Shane Farmer0a5b2012017-06-22 12:24:12 -0700771std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
772 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700773 CloningValueTransformer cloner(&new_table->string_pool);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700774 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700775 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700776 for (const auto& type : pkg->types) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000777 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->named_type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800778 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700779
780 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700781 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800782 new_entry->id = entry->id;
783 new_entry->visibility = entry->visibility;
784 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800785 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700786
787 for (const auto& config_value : entry->values) {
788 ResourceConfigValue* new_value =
789 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700790 new_value->value = config_value->value->Transform(cloner);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700791 }
792 }
793 }
794 }
795 return new_table;
796}
797
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700798NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
799 res_.name = name.ToResourceName();
800}
801
802NewResourceBuilder::NewResourceBuilder(const std::string& name) {
803 ResourceNameRef ref;
804 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
805 res_.name = ref.ToResourceName();
806}
807
808NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
809 android::ConfigDescription config,
810 std::string product) {
811 res_.value = std::move(value);
812 res_.config = std::move(config);
813 res_.product = std::move(product);
814 return *this;
815}
816
817NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
818 res_.id = std::make_pair(id, on_conflict);
819 return *this;
820}
821
822NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
823 res_.visibility = std::move(visibility);
824 return *this;
825}
826
827NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
828 res_.overlayable = std::move(overlayable);
829 return *this;
830}
831NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
832 res_.allow_new = std::move(allow_new);
833 return *this;
834}
835
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700836NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) {
837 res_.staged_id = std::move(staged_alias);
838 return *this;
839}
840
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700841NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
842 res_.allow_mangled = allow_mangled;
843 return *this;
844}
845
846NewResource NewResourceBuilder::Build() {
847 return std::move(res_);
848}
849
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850} // namespace aapt