Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [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 "process/SymbolTable.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include "androidfw/AssetManager.h" |
| 20 | #include "androidfw/ResourceTypes.h" |
| 21 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "ConfigDescription.h" |
| 23 | #include "Resource.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 24 | #include "ResourceUtils.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 25 | #include "ValueVisitor.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 26 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | using android::StringPiece; |
| 29 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) { |
| 33 | sources_.push_back(std::move(source)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | // We do not clear the cache, because sources earlier in the list take |
| 36 | // precedent. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) { |
| 40 | sources_.insert(sources_.begin(), std::move(source)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | // We must clear the cache in case we did a lookup before adding this |
| 43 | // resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | cache_.clear(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) { |
| 48 | if (const std::shared_ptr<Symbol>& s = cache_.get(name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | return s.get(); |
| 50 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | // We did not find it in the cache, so look through the sources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | for (auto& symbolSource : sources_) { |
| 54 | std::unique_ptr<Symbol> symbol = symbolSource->FindByName(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | if (symbol) { |
| 56 | // Take ownership of the symbol into a shared_ptr. We do this because |
| 57 | // LruCache |
| 58 | // doesn't support unique_ptr. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | std::shared_ptr<Symbol> shared_symbol = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | std::shared_ptr<Symbol>(symbol.release()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | cache_.put(name, shared_symbol); |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | if (shared_symbol->id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | // The symbol has an ID, so we can also cache this! |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | id_cache_.put(shared_symbol->id.value(), shared_symbol); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | return shared_symbol.get(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 68 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | } |
| 70 | return nullptr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) { |
| 74 | if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | return s.get(); |
| 76 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | // We did not find it in the cache, so look through the sources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | for (auto& symbolSource : sources_) { |
| 80 | std::unique_ptr<Symbol> symbol = symbolSource->FindById(id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | if (symbol) { |
| 82 | // Take ownership of the symbol into a shared_ptr. We do this because |
| 83 | // LruCache |
| 84 | // doesn't support unique_ptr. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | std::shared_ptr<Symbol> shared_symbol = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | std::shared_ptr<Symbol>(symbol.release()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | id_cache_.put(id, shared_symbol); |
| 88 | return shared_symbol.get(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 89 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | } |
| 91 | return nullptr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | // First try the ID. This is because when we lookup by ID, we only fill in the |
| 96 | // ID cache. |
| 97 | // Looking up by name fills in the name and ID cache. So a cache miss will |
| 98 | // cause a failed |
| 99 | // ID lookup, then a successful name lookup. Subsequent look ups will hit |
| 100 | // immediately |
| 101 | // because the ID is cached too. |
| 102 | // |
| 103 | // If we looked up by name first, a cache miss would mean we failed to lookup |
| 104 | // by name, then |
| 105 | // succeeded to lookup by ID. Subsequent lookups will miss then hit. |
| 106 | const SymbolTable::Symbol* symbol = nullptr; |
| 107 | if (ref.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | symbol = FindById(ref.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | } |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 110 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | if (ref.name && !symbol) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | symbol = FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } |
| 114 | return symbol; |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | const ResourceName& name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | Maybe<ResourceTable::SearchResult> result = table_->FindResource(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | if (!result) { |
| 121 | if (name.type == ResourceType::kAttr) { |
| 122 | // Recurse and try looking up a private attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | return FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); |
| 125 | } |
| 126 | return {}; |
| 127 | } |
| 128 | |
| 129 | ResourceTable::SearchResult sr = result.value(); |
| 130 | |
| 131 | std::unique_ptr<SymbolTable::Symbol> symbol = |
| 132 | util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | symbol->is_public = (sr.entry->symbol_status.state == SymbolState::kPublic); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | |
| 135 | if (sr.package->id && sr.type->id && sr.entry->id) { |
| 136 | symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), |
| 137 | sr.entry->id.value()); |
| 138 | } |
| 139 | |
| 140 | if (name.type == ResourceType::kAttr || |
| 141 | name.type == ResourceType::kAttrPrivate) { |
| 142 | const ConfigDescription kDefaultConfig; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig); |
| 144 | if (config_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | // This resource has an Attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | symbol->attribute = std::make_shared<Attribute>(*attr); |
| 148 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | } |
| 153 | return symbol; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | int32_t cookie = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | return assets_.addAssetPath(android::String8(path.data(), path.size()), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | &cookie); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | const android::ResTable& table, ResourceId id) { |
| 164 | // Try as a bag. |
| 165 | const android::ResTable::bag_entry* entry; |
| 166 | ssize_t count = table.lockBag(id.id, &entry); |
| 167 | if (count < 0) { |
| 168 | table.unlockBag(entry); |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
| 172 | // We found a resource. |
| 173 | std::unique_ptr<SymbolTable::Symbol> s = |
| 174 | util::make_unique<SymbolTable::Symbol>(); |
| 175 | s->id = id; |
| 176 | |
| 177 | // Check to see if it is an attribute. |
| 178 | for (size_t i = 0; i < (size_t)count; i++) { |
| 179 | if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) { |
| 180 | s->attribute = std::make_shared<Attribute>(false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | s->attribute->type_mask = entry[i].map.value.data; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | if (s->attribute) { |
| 187 | for (size_t i = 0; i < (size_t)count; i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | const android::ResTable_map& map_entry = entry[i].map; |
| 189 | if (Res_INTERNALID(map_entry.name.ident)) { |
| 190 | switch (map_entry.name.ident) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | case android::ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | s->attribute->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | break; |
| 194 | case android::ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | s->attribute->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 196 | break; |
| 197 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | continue; |
| 199 | } |
| 200 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | android::ResTable::resource_name entry_name; |
| 202 | if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | table.unlockBag(entry); |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | Maybe<ResourceName> parsed_name = |
| 208 | ResourceUtils::ToResourceName(entry_name); |
| 209 | if (!parsed_name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | return nullptr; |
| 211 | } |
| 212 | |
| 213 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | symbol.symbol.name = parsed_name.value(); |
| 215 | symbol.symbol.id = ResourceId(map_entry.name.ident); |
| 216 | symbol.value = map_entry.value.data; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | s->attribute->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | } |
| 220 | table.unlockBag(entry); |
| 221 | return s; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | const ResourceName& name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | const android::ResTable& table = assets_.getResources(false); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 227 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | const std::u16string package16 = util::Utf8ToUtf16(name.package); |
| 229 | const std::u16string type16 = util::Utf8ToUtf16(ToString(name.type)); |
| 230 | const std::u16string entry16 = util::Utf8ToUtf16(name.entry); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 231 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | uint32_t type_spec_flags = 0; |
| 233 | ResourceId res_id = table.identifierForName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | entry16.data(), entry16.size(), type16.data(), type16.size(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | package16.data(), package16.size(), &type_spec_flags); |
| 236 | if (!res_id.is_valid()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 237 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | std::unique_ptr<SymbolTable::Symbol> s; |
| 241 | if (name.type == ResourceType::kAttr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | s = LookupAttributeInTable(table, res_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | } else { |
| 244 | s = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | s->id = res_id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (s) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | s->is_public = |
| 250 | (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | return s; |
| 252 | } |
| 253 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | static Maybe<ResourceName> GetResourceName(const android::ResTable& table, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | ResourceId id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | android::ResTable::resource_name res_name = {}; |
| 259 | if (!table.getResourceName(id.id, true, &res_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 260 | return {}; |
| 261 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | return ResourceUtils::ToResourceName(res_name); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | ResourceId id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | const android::ResTable& table = assets_.getResources(false); |
| 268 | Maybe<ResourceName> maybe_name = GetResourceName(table, id); |
| 269 | if (!maybe_name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 270 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | uint32_t type_spec_flags = 0; |
| 274 | table.getResourceFlags(id.id, &type_spec_flags); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | |
| 276 | std::unique_ptr<SymbolTable::Symbol> s; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | if (maybe_name.value().type == ResourceType::kAttr) { |
| 278 | s = LookupAttributeInTable(table, id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | } else { |
| 280 | s = util::make_unique<SymbolTable::Symbol>(); |
| 281 | s->id = id; |
| 282 | } |
| 283 | |
| 284 | if (s) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | s->is_public = |
| 286 | (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | return s; |
| 288 | } |
| 289 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | const Reference& ref) { |
| 294 | // AssetManager always prefers IDs. |
| 295 | if (ref.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | return FindById(ref.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | } else if (ref.name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | return FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | } |
| 300 | return {}; |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | } // namespace aapt |