blob: ef2e448b68b89c22371d73beedde29da331d668b [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 "process/SymbolTable.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080019#include <iostream>
20
21#include "android-base/logging.h"
22#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "androidfw/AssetManager.h"
24#include "androidfw/ResourceTypes.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ConfigDescription.h"
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080027#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070029#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070030#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070031#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinskidc785052017-12-07 15:58:46 -080033using ::android::StringPiece;
34using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080035
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036namespace aapt {
37
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070038SymbolTable::SymbolTable(NameMangler* mangler)
39 : mangler_(mangler),
40 delegate_(util::make_unique<DefaultSymbolTableDelegate>()),
41 cache_(200),
42 id_cache_(200) {
43}
44
45void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) {
46 CHECK(delegate != nullptr) << "can't set a nullptr delegate";
47 delegate_ = std::move(delegate);
48
49 // Clear the cache in case this delegate changes the order of lookup.
50 cache_.clear();
51}
52
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
54 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 // We do not clear the cache, because sources earlier in the list take
57 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
61 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 // We must clear the cache in case we did a lookup before adding this
64 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080066}
67
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080069 const ResourceName* name_with_package = &name;
70
71 // Fill in the package name if necessary.
72 // If there is no package in `name`, we will need to copy the ResourceName
73 // and store it somewhere; we use the Maybe<> class to reserve storage.
74 Maybe<ResourceName> name_with_package_impl;
75 if (name.package.empty()) {
76 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
77 name_with_package = &name_with_package_impl.value();
78 }
79
80 // We store the name unmangled in the cache, so look it up as-is.
81 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return s.get();
83 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080085 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
86 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
87 const ResourceName* mangled_name = name_with_package;
88 Maybe<ResourceName> mangled_name_impl;
89 if (mangler_->ShouldMangle(name_with_package->package)) {
90 mangled_name_impl = mangler_->MangleName(*name_with_package);
91 mangled_name = &mangled_name_impl.value();
92 }
93
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070094 std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_);
95 if (symbol == nullptr) {
96 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070098
99 // Take ownership of the symbol into a shared_ptr. We do this because
100 // LruCache doesn't support unique_ptr.
101 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
102
103 // Since we look in the cache with the unmangled, but package prefixed
104 // name, we must put the same name into the cache.
105 cache_.put(*name_with_package, shared_symbol);
106
107 if (shared_symbol->id) {
108 // The symbol has an ID, so we can also cache this!
109 id_cache_.put(shared_symbol->id.value(), shared_symbol);
110 }
111
112 // Returns the raw pointer. Callers are not expected to hold on to this
113 // between calls to Find*.
114 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800115}
116
Chris Warrington481f0272018-02-06 14:03:39 +0000117const SymbolTable::Symbol* SymbolTable::FindByNameInAnyPackage(const ResourceName& name) {
118 for (auto& source : sources_) {
119 std::string package = source->GetPackageForSymbol(name);
120 if (!package.empty()) {
121 return FindByName(ResourceName(package, name.type, name.entry));
122 }
123 }
124 return {};
125}
126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
128 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return s.get();
130 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 // We did not find it in the cache, so look through the sources.
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700133 std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_);
134 if (symbol == nullptr) {
135 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700137
138 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
139 // doesn't support unique_ptr.
140 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
141 id_cache_.put(id, shared_symbol);
142
143 // Returns the raw pointer. Callers are not expected to hold on to this
144 // between calls to Find*.
145 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800146}
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800149 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
150 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
151 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 // because the ID is cached too.
153 //
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800154 // If we looked up by name first, a cache miss would mean we failed to lookup by name, then
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
156 const SymbolTable::Symbol* symbol = nullptr;
157 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
Adam Lesinski76565542016-03-10 21:55:04 -0800160
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 }
164 return symbol;
Adam Lesinski76565542016-03-10 21:55:04 -0800165}
166
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700167std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName(
168 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
169 for (auto& source : sources) {
170 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name);
171 if (symbol) {
172 return symbol;
173 }
174 }
175 return {};
176}
177
178std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById(
179 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
180 for (auto& source : sources) {
181 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id);
182 if (symbol) {
183 return symbol;
184 }
185 }
186 return {};
187}
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 if (!result) {
193 if (name.type == ResourceType::kAttr) {
194 // Recurse and try looking up a private attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800195 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197 return {};
198 }
199
200 ResourceTable::SearchResult sr = result.value();
201
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800202 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski71be7052017-12-12 16:48:07 -0800203 symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204
205 if (sr.package->id && sr.type->id && sr.entry->id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800206 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
208
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800209 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
212 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 symbol->attribute = std::make_shared<Attribute>(*attr);
216 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 }
221 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222}
223
Chris Warrington481f0272018-02-06 14:03:39 +0000224std::string ResourceTableSymbolSource::GetPackageForSymbol(const ResourceName& name) {
225 for (auto& package : table_->packages) {
226 ResourceTableType* type = package->FindType(name.type);
227 if (type == nullptr) {
228 continue;
229 }
230 ResourceEntry* entry = type->FindEntry(name.entry);
231 if (entry == nullptr) {
232 continue;
233 }
234 return package->name;
235 }
236 if (name.type == ResourceType::kAttr) {
237 // Recurse and try looking up a private attribute.
238 return GetPackageForSymbol(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
239 }
240 return {};
241}
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 int32_t cookie = 0;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800245 return assets_.addAssetPath(android::String8(path.data(), path.size()), &cookie);
246}
247
248std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
249 std::map<size_t, std::string> package_map;
250 const android::ResTable& table = assets_.getResources(false);
251 const size_t package_count = table.getBasePackageCount();
252 for (size_t i = 0; i < package_count; i++) {
253 package_map[table.getBasePackageId(i)] =
254 util::Utf16ToUtf8(android::StringPiece16(table.getBasePackageName(i).string()));
255 }
256 return package_map;
Adam Lesinski64587af2016-02-18 18:33:06 -0800257}
258
Todd Kennedy32512992018-04-25 16:45:59 -0700259bool AssetManagerSymbolSource::IsPackageDynamic(uint32_t packageId) const {
260 return assets_.getResources(false).isPackageDynamic(packageId);
261}
262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 const android::ResTable& table, ResourceId id) {
265 // Try as a bag.
266 const android::ResTable::bag_entry* entry;
267 ssize_t count = table.lockBag(id.id, &entry);
268 if (count < 0) {
269 table.unlockBag(entry);
270 return nullptr;
271 }
272
273 // We found a resource.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700274 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275
276 // Check to see if it is an attribute.
277 for (size_t i = 0; i < (size_t)count; i++) {
278 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800279 s->attribute = std::make_shared<Attribute>(entry[i].map.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 if (s->attribute) {
285 for (size_t i = 0; i < (size_t)count; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 const android::ResTable_map& map_entry = entry[i].map;
287 if (Res_INTERNALID(map_entry.name.ident)) {
288 switch (map_entry.name.ident) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 case android::ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 break;
292 case android::ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700294 break;
295 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 continue;
297 }
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 android::ResTable::resource_name entry_name;
300 if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 table.unlockBag(entry);
302 return nullptr;
303 }
304
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800305 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(entry_name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return nullptr;
308 }
309
310 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 symbol.symbol.name = parsed_name.value();
312 symbol.symbol.id = ResourceId(map_entry.name.ident);
313 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 }
317 table.unlockBag(entry);
318 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319}
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 const android::ResTable& table = assets_.getResources(false);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 const std::u16string package16 = util::Utf8ToUtf16(name.package);
Adam Lesinski93190b72017-11-03 15:20:17 -0700326 const std::u16string type16 = util::Utf8ToUtf16(to_string(name.type));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 const std::u16string entry16 = util::Utf8ToUtf16(name.entry);
Adam Lesinskidc785052017-12-07 15:58:46 -0800328 const std::u16string mangled_entry16 =
329 util::Utf8ToUtf16(NameMangler::MangleEntry(name.package, name.entry));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700330
Adam Lesinskidc785052017-12-07 15:58:46 -0800331 uint32_t type_spec_flags;
332 ResourceId res_id;
333
334 // There can be mangled resources embedded within other packages. Here we will
335 // look into each package and look-up the mangled name until we find the resource.
336 const size_t count = table.getBasePackageCount();
337 for (size_t i = 0; i < count; i++) {
338 const android::String16 package_name = table.getBasePackageName(i);
339 StringPiece16 real_package16 = package16;
340 StringPiece16 real_entry16 = entry16;
341 std::u16string scratch_entry16;
342 if (StringPiece16(package_name) != package16) {
343 real_entry16 = mangled_entry16;
344 real_package16 = package_name.string();
345 }
346
347 type_spec_flags = 0;
348 res_id = table.identifierForName(real_entry16.data(), real_entry16.size(), type16.data(),
349 type16.size(), real_package16.data(), real_package16.size(),
350 &type_spec_flags);
351 if (res_id.is_valid()) {
352 break;
353 }
354 }
355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 if (!res_id.is_valid()) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800357 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 }
359
360 std::unique_ptr<SymbolTable::Symbol> s;
361 if (name.type == ResourceType::kAttr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 s = LookupAttributeInTable(table, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 } else {
364 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 s->id = res_id;
Todd Kennedy32512992018-04-25 16:45:59 -0700366 s->is_dynamic = table.isResourceDynamic(res_id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 }
368
369 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800370 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 return s;
372 }
373 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374}
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376static Maybe<ResourceName> GetResourceName(const android::ResTable& table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 ResourceId id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 android::ResTable::resource_name res_name = {};
379 if (!table.getResourceName(id.id, true, &res_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 return {};
381 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 return ResourceUtils::ToResourceName(res_name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800383}
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 ResourceId id) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800387 if (!id.is_valid()) {
388 // Exit early and avoid the error logs from AssetManager.
389 return {};
390 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 const android::ResTable& table = assets_.getResources(false);
392 Maybe<ResourceName> maybe_name = GetResourceName(table, id);
393 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700394 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 }
396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 uint32_t type_spec_flags = 0;
398 table.getResourceFlags(id.id, &type_spec_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399
400 std::unique_ptr<SymbolTable::Symbol> s;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 if (maybe_name.value().type == ResourceType::kAttr) {
402 s = LookupAttributeInTable(table, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 } else {
404 s = util::make_unique<SymbolTable::Symbol>();
405 s->id = id;
Todd Kennedy32512992018-04-25 16:45:59 -0700406 s->is_dynamic = table.isResourceDynamic(id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 }
408
409 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800410 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 return s;
412 }
413 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700414}
415
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 const Reference& ref) {
418 // AssetManager always prefers IDs.
419 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 }
424 return {};
Adam Lesinski76565542016-03-10 21:55:04 -0800425}
426
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427} // namespace aapt