blob: 78e00746f6cbb525bd12320a301e41109942b51c [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"
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080023#include "androidfw/Asset.h"
24#include "androidfw/AssetManager2.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080027#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080029#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070031#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070032#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070033#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080035using ::android::ApkAssets;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020036using ::android::ConfigDescription;
Adam Lesinskidc785052017-12-07 15:58:46 -080037using ::android::StringPiece;
38using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040namespace aapt {
41
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070042SymbolTable::SymbolTable(NameMangler* mangler)
43 : mangler_(mangler),
44 delegate_(util::make_unique<DefaultSymbolTableDelegate>()),
45 cache_(200),
46 id_cache_(200) {
47}
48
49void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) {
50 CHECK(delegate != nullptr) << "can't set a nullptr delegate";
51 delegate_ = std::move(delegate);
52
53 // Clear the cache in case this delegate changes the order of lookup.
54 cache_.clear();
55}
56
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
58 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // We do not clear the cache, because sources earlier in the list take
61 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080062}
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
65 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 // We must clear the cache in case we did a lookup before adding this
68 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080073 const ResourceName* name_with_package = &name;
74
75 // Fill in the package name if necessary.
76 // If there is no package in `name`, we will need to copy the ResourceName
77 // and store it somewhere; we use the Maybe<> class to reserve storage.
78 Maybe<ResourceName> name_with_package_impl;
79 if (name.package.empty()) {
80 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
81 name_with_package = &name_with_package_impl.value();
82 }
83
84 // We store the name unmangled in the cache, so look it up as-is.
85 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return s.get();
87 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080089 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
90 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
91 const ResourceName* mangled_name = name_with_package;
92 Maybe<ResourceName> mangled_name_impl;
93 if (mangler_->ShouldMangle(name_with_package->package)) {
94 mangled_name_impl = mangler_->MangleName(*name_with_package);
95 mangled_name = &mangled_name_impl.value();
96 }
97
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070098 std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_);
99 if (symbol == nullptr) {
100 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700102
103 // Take ownership of the symbol into a shared_ptr. We do this because
104 // LruCache doesn't support unique_ptr.
105 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
106
107 // Since we look in the cache with the unmangled, but package prefixed
108 // name, we must put the same name into the cache.
109 cache_.put(*name_with_package, shared_symbol);
110
111 if (shared_symbol->id) {
112 // The symbol has an ID, so we can also cache this!
113 id_cache_.put(shared_symbol->id.value(), shared_symbol);
114 }
115
116 // Returns the raw pointer. Callers are not expected to hold on to this
117 // between calls to Find*.
118 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
122 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return s.get();
124 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 // We did not find it in the cache, so look through the sources.
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700127 std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_);
128 if (symbol == nullptr) {
129 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700131
132 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
133 // doesn't support unique_ptr.
134 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
135 id_cache_.put(id, shared_symbol);
136
137 // Returns the raw pointer. Callers are not expected to hold on to this
138 // between calls to Find*.
139 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800140}
141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800143 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
144 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
145 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 // because the ID is cached too.
147 //
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800148 // 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 -0700149 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
150 const SymbolTable::Symbol* symbol = nullptr;
151 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
Adam Lesinski76565542016-03-10 21:55:04 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
158 return symbol;
Adam Lesinski76565542016-03-10 21:55:04 -0800159}
160
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700161std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName(
162 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
163 for (auto& source : sources) {
164 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name);
165 if (symbol) {
166 return symbol;
167 }
168 }
169 return {};
170}
171
172std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById(
173 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
174 for (auto& source : sources) {
175 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id);
176 if (symbol) {
177 return symbol;
178 }
179 }
180 return {};
181}
182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 if (!result) {
187 if (name.type == ResourceType::kAttr) {
188 // Recurse and try looking up a private attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800189 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 }
191 return {};
192 }
193
194 ResourceTable::SearchResult sr = result.value();
195
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800196 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski71be7052017-12-12 16:48:07 -0800197 symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198
199 if (sr.package->id && sr.type->id && sr.entry->id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800200 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Todd Kennedy196dbc32018-07-24 14:37:15 -0700201 symbol->is_dynamic = (sr.package->id.value() == 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
203
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800204 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
207 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 symbol->attribute = std::make_shared<Attribute>(*attr);
211 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
216 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217}
218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800220 if (std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path.data())) {
221 apk_assets_.push_back(std::move(apk));
222
223 std::vector<const ApkAssets*> apk_assets;
224 for (const std::unique_ptr<const ApkAssets>& apk_asset : apk_assets_) {
225 apk_assets.push_back(apk_asset.get());
226 }
227
228 asset_manager_.SetApkAssets(apk_assets, true /* invalidate_caches */,
229 false /* filter_incompatible_configs */);
230 return true;
231 }
232 return false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800233}
234
235std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
236 std::map<size_t, std::string> package_map;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800237 asset_manager_.ForEachPackage([&package_map](const std::string& name, uint8_t id) -> bool {
238 package_map.insert(std::make_pair(id, name));
239 return true;
240 });
241
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800242 return package_map;
Adam Lesinski64587af2016-02-18 18:33:06 -0800243}
244
Todd Kennedy32512992018-04-25 16:45:59 -0700245bool AssetManagerSymbolSource::IsPackageDynamic(uint32_t packageId) const {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800246 if (packageId == 0) {
247 return true;
248 }
249
250 for (const std::unique_ptr<const ApkAssets>& assets : apk_assets_) {
251 for (const std::unique_ptr<const android::LoadedPackage>& loaded_package
252 : assets->GetLoadedArsc()->GetPackages()) {
253 if (packageId == loaded_package->GetPackageId() && loaded_package->IsDynamic()) {
254 return true;
255 }
256 }
257 }
258
259 return false;
Todd Kennedy32512992018-04-25 16:45:59 -0700260}
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800263 android::AssetManager2& am, ResourceId id) {
264 if (am.GetApkAssets().empty()) {
265 return {};
266 }
267
268 const android::ResolvedBag* bag = am.GetBag(id.id);
269 if (bag == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 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
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800276 const size_t count = bag->entry_count;
277 for (uint32_t i = 0; i < count; i++) {
278 if (bag->entries[i].key == android::ResTable_map::ATTR_TYPE) {
279 s->attribute = std::make_shared<Attribute>(bag->entries[i].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) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800285 for (size_t i = 0; i < count; i++) {
286 const android::ResolvedBag::Entry& map_entry = bag->entries[i];
287 if (Res_INTERNALID(map_entry.key)) {
288 switch (map_entry.key) {
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
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800299 android::AssetManager2::ResourceName name;
300 if (!am.GetResourceName(map_entry.key, &name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return nullptr;
302 }
303
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800304 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return nullptr;
307 }
308
309 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 symbol.symbol.name = parsed_name.value();
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800311 symbol.symbol.id = ResourceId(map_entry.key);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700318}
319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 const ResourceName& name) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800322 const std::string mangled_entry = NameMangler::MangleEntry(name.package, name.entry);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700323
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800324 bool found = false;
325 ResourceId res_id = 0;
Adam Lesinskidc785052017-12-07 15:58:46 -0800326 uint32_t type_spec_flags;
Adam Lesinskidc785052017-12-07 15:58:46 -0800327
328 // There can be mangled resources embedded within other packages. Here we will
329 // look into each package and look-up the mangled name until we find the resource.
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800330 asset_manager_.ForEachPackage([&](const std::string& package_name, uint8_t id) -> bool {
331 ResourceName real_name(name.package, name.type, name.entry);
332
333 if (package_name != name.package) {
334 real_name.entry = mangled_entry;
335 real_name.package = package_name;
Adam Lesinskidc785052017-12-07 15:58:46 -0800336 }
337
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800338 res_id = asset_manager_.GetResourceId(real_name.to_string());
339 if (res_id.is_valid() && asset_manager_.GetResourceFlags(res_id.id, &type_spec_flags)) {
340 found = true;
341 return false;
Adam Lesinskidc785052017-12-07 15:58:46 -0800342 }
Adam Lesinskidc785052017-12-07 15:58:46 -0800343
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800344 return true;
345 });
346
347 if (!found) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800348 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
350
351 std::unique_ptr<SymbolTable::Symbol> s;
352 if (name.type == ResourceType::kAttr) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800353 s = LookupAttributeInTable(asset_manager_, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 } else {
355 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 s->id = res_id;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800357 s->is_dynamic = IsPackageDynamic(ResourceId(res_id).package_id());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 }
359
360 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800361 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 return s;
363 }
364 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700365}
366
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800367static Maybe<ResourceName> GetResourceName(android::AssetManager2& am,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 ResourceId id) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800369 android::AssetManager2::ResourceName name;
370 if (!am.GetResourceName(id.id, &name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 return {};
372 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800373 return ResourceUtils::ToResourceName(name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800374}
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 ResourceId id) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800378 if (!id.is_valid()) {
379 // Exit early and avoid the error logs from AssetManager.
380 return {};
381 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800382
383 if (apk_assets_.empty()) {
384 return {};
385 }
386
387 Maybe<ResourceName> maybe_name = GetResourceName(asset_manager_, id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 }
391
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800393 uint32_t type_spec_flags = 0;
394 if (!asset_manager_.GetResourceFlags(id.id, &type_spec_flags)) {
395 return {};
396 }
397
398 ResourceName& name = maybe_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 std::unique_ptr<SymbolTable::Symbol> s;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800400 if (name.type == ResourceType::kAttr) {
401 s = LookupAttributeInTable(asset_manager_, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 } else {
403 s = util::make_unique<SymbolTable::Symbol>();
404 s->id = id;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800405 s->is_dynamic = IsPackageDynamic(ResourceId(id).package_id());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 }
407
408 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800409 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 return s;
411 }
412 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700413}
414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 const Reference& ref) {
417 // AssetManager always prefers IDs.
418 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
423 return {};
Adam Lesinski76565542016-03-10 21:55:04 -0800424}
425
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426} // namespace aapt