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