blob: 3c35d9b33fc8c4c5a49a8d24d9d74df0747bdf49 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
2 * Copyright (C) 2016 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
17#define ATRACE_TAG ATRACE_TAG_RESOURCES
18
19#include "androidfw/AssetManager2.h"
20
y57cd1952018-04-12 14:26:23 -070021#include <algorithm>
Adam Lesinski30080e22017-10-16 16:18:09 -070022#include <iterator>
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -070023#include <map>
Winson2f3669b2019-01-11 11:28:34 -080024#include <set>
25#include <sstream>
Adam Lesinski0c405242017-01-13 20:47:26 -080026
Adam Lesinski7ad11102016-10-28 16:39:15 -070027#include "android-base/logging.h"
28#include "android-base/stringprintf.h"
29#include "utils/ByteOrder.h"
30#include "utils/Trace.h"
31
32#ifdef _WIN32
33#ifdef ERROR
34#undef ERROR
35#endif
36#endif
37
Adam Lesinski929d6512017-01-16 19:11:19 -080038#include "androidfw/ResourceUtils.h"
39
Adam Lesinski7ad11102016-10-28 16:39:15 -070040namespace android {
41
Adam Lesinskibebfcc42018-02-12 14:27:46 -080042struct FindEntryResult {
43 // A pointer to the resource table entry for this resource.
44 // If the size of the entry is > sizeof(ResTable_entry), it can be cast to
45 // a ResTable_map_entry and processed as a bag/map.
46 const ResTable_entry* entry;
47
48 // The configuration for which the resulting entry was defined. This is already swapped to host
49 // endianness.
50 ResTable_config config;
51
52 // The bitmask of configuration axis with which the resource value varies.
53 uint32_t type_flags;
54
55 // The dynamic package ID map for the package from which this resource came from.
56 const DynamicRefTable* dynamic_ref_table;
57
58 // The string pool reference to the type's name. This uses a different string pool than
59 // the global string pool, but this is hidden from the caller.
60 StringPoolRef type_string_ref;
61
62 // The string pool reference to the entry's name. This uses a different string pool than
63 // the global string pool, but this is hidden from the caller.
64 StringPoolRef entry_string_ref;
65};
66
Adam Lesinski970bd8d2017-09-25 13:21:55 -070067AssetManager2::AssetManager2() {
68 memset(&configuration_, 0, sizeof(configuration_));
69}
Adam Lesinski7ad11102016-10-28 16:39:15 -070070
71bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets,
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020072 bool invalidate_caches, bool filter_incompatible_configs) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070073 apk_assets_ = apk_assets;
Adam Lesinskida431a22016-12-29 16:08:16 -050074 BuildDynamicRefTable();
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020075 RebuildFilterList(filter_incompatible_configs);
Adam Lesinski7ad11102016-10-28 16:39:15 -070076 if (invalidate_caches) {
77 InvalidateCaches(static_cast<uint32_t>(-1));
78 }
79 return true;
80}
81
Adam Lesinskida431a22016-12-29 16:08:16 -050082void AssetManager2::BuildDynamicRefTable() {
83 package_groups_.clear();
84 package_ids_.fill(0xff);
85
86 // 0x01 is reserved for the android package.
87 int next_package_id = 0x02;
88 const size_t apk_assets_count = apk_assets_.size();
89 for (size_t i = 0; i < apk_assets_count; i++) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070090 const LoadedArsc* loaded_arsc = apk_assets_[i]->GetLoadedArsc();
91
92 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
Adam Lesinskida431a22016-12-29 16:08:16 -050093 // Get the package ID or assign one if a shared library.
94 int package_id;
95 if (package->IsDynamic()) {
96 package_id = next_package_id++;
97 } else {
98 package_id = package->GetPackageId();
99 }
100
101 // Add the mapping for package ID to index if not present.
102 uint8_t idx = package_ids_[package_id];
103 if (idx == 0xff) {
104 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
105 package_groups_.push_back({});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800106 DynamicRefTable& ref_table = package_groups_.back().dynamic_ref_table;
107 ref_table.mAssignedPackageId = package_id;
108 ref_table.mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500109 }
110 PackageGroup* package_group = &package_groups_[idx];
111
112 // Add the package and to the set of packages with the same ID.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800113 package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
Adam Lesinskida431a22016-12-29 16:08:16 -0500114 package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i));
115
116 // Add the package name -> build time ID mappings.
117 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
118 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
119 package_group->dynamic_ref_table.mEntries.replaceValueFor(
120 package_name, static_cast<uint8_t>(entry.package_id));
121 }
122 }
123 }
124
125 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
126 const auto package_groups_end = package_groups_.end();
127 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800128 const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500129 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
130 iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()),
131 iter->dynamic_ref_table.mAssignedPackageId);
132 }
133 }
134}
135
136void AssetManager2::DumpToLog() const {
137 base::ScopedLogSeverity _log(base::INFO);
138
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800139 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
140
Adam Lesinskida431a22016-12-29 16:08:16 -0500141 std::string list;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800142 for (const auto& apk_assets : apk_assets_) {
143 base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str());
144 }
145 LOG(INFO) << "ApkAssets: " << list;
146
147 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500148 for (size_t i = 0; i < package_ids_.size(); i++) {
149 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800150 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500151 }
152 }
153 LOG(INFO) << "Package ID map: " << list;
154
Adam Lesinski0dd36992018-01-25 15:38:38 -0800155 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800156 list = "";
157 for (const auto& package : package_group.packages_) {
158 const LoadedPackage* loaded_package = package.loaded_package_;
159 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
160 loaded_package->GetPackageId(),
161 (loaded_package->IsDynamic() ? " dynamic" : ""));
162 }
163 LOG(INFO) << base::StringPrintf("PG (%02x): ",
164 package_group.dynamic_ref_table.mAssignedPackageId)
165 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800166
167 for (size_t i = 0; i < 256; i++) {
168 if (package_group.dynamic_ref_table.mLookupTable[i] != 0) {
169 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
170 package_group.dynamic_ref_table.mLookupTable[i]);
171 }
172 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500173 }
174}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700175
176const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
177 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
178 return nullptr;
179 }
180 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
181}
182
Adam Lesinskida431a22016-12-29 16:08:16 -0500183const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
184 if (package_id >= package_ids_.size()) {
185 return nullptr;
186 }
187
188 const size_t idx = package_ids_[package_id];
189 if (idx == 0xff) {
190 return nullptr;
191 }
192 return &package_groups_[idx].dynamic_ref_table;
193}
194
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800195const DynamicRefTable* AssetManager2::GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const {
196 for (const PackageGroup& package_group : package_groups_) {
197 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
198 if (package_cookie == cookie) {
199 return &package_group.dynamic_ref_table;
200 }
201 }
202 }
203 return nullptr;
204}
205
Adam Lesinski7ad11102016-10-28 16:39:15 -0700206void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
207 const int diff = configuration_.diff(configuration);
208 configuration_ = configuration;
209
210 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800211 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700212 InvalidateCaches(static_cast<uint32_t>(diff));
213 }
214}
215
Adam Lesinski0c405242017-01-13 20:47:26 -0800216std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800217 bool exclude_mipmap) const {
218 ATRACE_NAME("AssetManager::GetResourceConfigurations");
Adam Lesinski0c405242017-01-13 20:47:26 -0800219 std::set<ResTable_config> configurations;
220 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800221 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800222 for (const ConfiguredPackage& package : package_group.packages_) {
223 if (exclude_system && package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800224 found_system_package = true;
Adam Lesinski0c405242017-01-13 20:47:26 -0800225 continue;
226 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800227
228 if (exclude_system && package.loaded_package_->IsOverlay() && found_system_package) {
229 // Overlays must appear after the target package to take effect. Any overlay found in the
230 // same package as a system package is able to overlay system resources.
231 continue;
232 }
233
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800234 package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
Adam Lesinski0c405242017-01-13 20:47:26 -0800235 }
236 }
237 return configurations;
238}
239
240std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800241 bool merge_equivalent_languages) const {
242 ATRACE_NAME("AssetManager::GetResourceLocales");
Adam Lesinski0c405242017-01-13 20:47:26 -0800243 std::set<std::string> locales;
244 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800245 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800246 for (const ConfiguredPackage& package : package_group.packages_) {
247 if (exclude_system && package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800248 found_system_package = true;
Adam Lesinski0c405242017-01-13 20:47:26 -0800249 continue;
250 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800251
252 if (exclude_system && package.loaded_package_->IsOverlay() && found_system_package) {
253 // Overlays must appear after the target package to take effect. Any overlay found in the
254 // same package as a system package is able to overlay system resources.
255 continue;
256 }
257
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800258 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800259 }
260 }
261 return locales;
262}
263
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800264std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
265 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700266 const std::string new_path = "assets/" + filename;
267 return OpenNonAsset(new_path, mode);
268}
269
270std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800271 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700272 const std::string new_path = "assets/" + filename;
273 return OpenNonAsset(new_path, cookie, mode);
274}
275
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800276std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
277 ATRACE_NAME("AssetManager::OpenDir");
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800278
279 std::string full_path = "assets/" + dirname;
280 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
281 util::make_unique<SortedVector<AssetDir::FileInfo>>();
282
283 // Start from the back.
284 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
285 const ApkAssets* apk_assets = *iter;
286
287 auto func = [&](const StringPiece& name, FileType type) {
288 AssetDir::FileInfo info;
289 info.setFileName(String8(name.data(), name.size()));
290 info.setFileType(type);
291 info.setSourceName(String8(apk_assets->GetPath().c_str()));
292 files->add(info);
293 };
294
295 if (!apk_assets->ForEachFile(full_path, func)) {
296 return {};
297 }
298 }
299
300 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
301 asset_dir->setFileList(files.release());
302 return asset_dir;
303}
304
Adam Lesinski7ad11102016-10-28 16:39:15 -0700305// Search in reverse because that's how we used to do it and we need to preserve behaviour.
306// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
307// is inconsistent for split APKs.
308std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
309 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800310 ApkAssetsCookie* out_cookie) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700311 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
312 std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode);
313 if (asset) {
314 if (out_cookie != nullptr) {
315 *out_cookie = i;
316 }
317 return asset;
318 }
319 }
320
321 if (out_cookie != nullptr) {
322 *out_cookie = kInvalidCookie;
323 }
324 return {};
325}
326
327std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800328 ApkAssetsCookie cookie,
329 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700330 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
331 return {};
332 }
333 return apk_assets_[cookie]->Open(filename, mode);
334}
335
336ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800337 bool /*stop_at_first_match*/,
338 FindEntryResult* out_entry) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700339 // Might use this if density_override != 0.
340 ResTable_config density_override_config;
341
342 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800343 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700344 if (density_override != 0 && density_override != configuration_.density) {
345 density_override_config = configuration_;
346 density_override_config.density = density_override;
347 desired_config = &density_override_config;
348 }
349
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800350 if (!is_valid_resid(resid)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500351 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
352 return kInvalidCookie;
353 }
354
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800355 const uint32_t package_id = get_package_id(resid);
356 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800357 const uint16_t entry_idx = get_entry_id(resid);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800358
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800359 const uint8_t package_idx = package_ids_[package_id];
360 if (package_idx == 0xff) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500361 LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid);
362 return kInvalidCookie;
363 }
364
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800365 const PackageGroup& package_group = package_groups_[package_idx];
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800366 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800367
368 ApkAssetsCookie best_cookie = kInvalidCookie;
369 const LoadedPackage* best_package = nullptr;
370 const ResTable_type* best_type = nullptr;
371 const ResTable_config* best_config = nullptr;
372 ResTable_config best_config_copy;
373 uint32_t best_offset = 0u;
374 uint32_t type_flags = 0u;
375
Winson2f3669b2019-01-11 11:28:34 -0800376 Resolution::Step::Type resolution_type;
377 std::vector<Resolution::Step> resolution_steps;
378
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800379 // If desired_config is the same as the set configuration, then we can use our filtered list
380 // and we don't need to match the configurations, since they already matched.
381 const bool use_fast_path = desired_config == &configuration_;
382
383 for (size_t pi = 0; pi < package_count; pi++) {
384 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
385 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
386 ApkAssetsCookie cookie = package_group.cookies_[pi];
387
388 // If the type IDs are offset in this package, we need to take that into account when searching
389 // for a type.
390 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
391 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700392 continue;
393 }
394
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800395 uint16_t local_entry_idx = entry_idx;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700396
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800397 // If there is an IDMAP supplied with this package, translate the entry ID.
398 if (type_spec->idmap_entries != nullptr) {
399 if (!LoadedIdmap::Lookup(type_spec->idmap_entries, local_entry_idx, &local_entry_idx)) {
400 // There is no mapping, so the resource is not meant to be in this overlay package.
401 continue;
402 }
403 }
404
405 type_flags |= type_spec->GetFlagsForEntryIndex(local_entry_idx);
406
407 // If the package is an overlay, then even configurations that are the same MUST be chosen.
408 const bool package_is_overlay = loaded_package->IsOverlay();
409
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800410 if (use_fast_path) {
Winson2f3669b2019-01-11 11:28:34 -0800411 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800412 const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations;
413 const size_t type_count = candidate_configs.size();
414 for (uint32_t i = 0; i < type_count; i++) {
415 const ResTable_config& this_config = candidate_configs[i];
416
417 // We can skip calling ResTable_config::match() because we know that all candidate
418 // configurations that do NOT match have been filtered-out.
Winson2f3669b2019-01-11 11:28:34 -0800419 if (best_config == nullptr) {
420 resolution_type = Resolution::Step::Type::INITIAL;
421 } else if (this_config.isBetterThan(*best_config, desired_config)) {
422 resolution_type = Resolution::Step::Type::BETTER_MATCH;
423 } else if (package_is_overlay && this_config.compare(*best_config) == 0) {
424 resolution_type = Resolution::Step::Type::OVERLAID;
425 } else {
426 continue;
427 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800428
Winson2f3669b2019-01-11 11:28:34 -0800429 // The configuration matches and is better than the previous selection.
430 // Find the entry value if it exists for this configuration.
431 const ResTable_type* type = filtered_group.types[i];
432 const uint32_t offset = LoadedPackage::GetEntryOffset(type, local_entry_idx);
433 if (offset == ResTable_type::NO_ENTRY) {
434 continue;
435 }
436
437 best_cookie = cookie;
438 best_package = loaded_package;
439 best_type = type;
440 best_config = &this_config;
441 best_offset = offset;
442
443 if (resource_resolution_logging_enabled_) {
444 resolution_steps.push_back(Resolution::Step{resolution_type,
445 this_config.toString(),
446 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800447 }
448 }
449 } else {
450 // This is the slower path, which doesn't use the filtered list of configurations.
451 // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness
452 // and fill in any new fields that did not exist when the APK was compiled.
453 // Furthermore when selecting configurations we can't just record the pointer to the
454 // ResTable_config, we must copy it.
455 const auto iter_end = type_spec->types + type_spec->type_count;
456 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
457 ResTable_config this_config;
458 this_config.copyFromDtoH((*iter)->config);
459
Winson2f3669b2019-01-11 11:28:34 -0800460 if (!this_config.match(*desired_config)) {
461 continue;
462 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800463
Winson2f3669b2019-01-11 11:28:34 -0800464 if (best_config == nullptr) {
465 resolution_type = Resolution::Step::Type::INITIAL;
466 } else if (this_config.isBetterThan(*best_config, desired_config)) {
467 resolution_type = Resolution::Step::Type::BETTER_MATCH;
468 } else if (package_is_overlay && this_config.compare(*best_config) == 0) {
469 resolution_type = Resolution::Step::Type::OVERLAID;
470 } else {
471 continue;
472 }
473
474 // The configuration matches and is better than the previous selection.
475 // Find the entry value if it exists for this configuration.
476 const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, local_entry_idx);
477 if (offset == ResTable_type::NO_ENTRY) {
478 continue;
479 }
480
481 best_cookie = cookie;
482 best_package = loaded_package;
483 best_type = *iter;
484 best_config_copy = this_config;
485 best_config = &best_config_copy;
486 best_offset = offset;
487
488 if (resource_resolution_logging_enabled_) {
489 resolution_steps.push_back(Resolution::Step{resolution_type,
490 this_config.toString(),
491 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800492 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700493 }
494 }
495 }
496
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800497 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700498 return kInvalidCookie;
499 }
500
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800501 const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
502 if (UNLIKELY(best_entry == nullptr)) {
503 return kInvalidCookie;
504 }
505
506 out_entry->entry = best_entry;
507 out_entry->config = *best_config;
508 out_entry->type_flags = type_flags;
509 out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1);
510 out_entry->entry_string_ref =
511 StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index);
Adam Lesinskida431a22016-12-29 16:08:16 -0500512 out_entry->dynamic_ref_table = &package_group.dynamic_ref_table;
Winson2f3669b2019-01-11 11:28:34 -0800513
514 if (resource_resolution_logging_enabled_) {
515 last_resolution.resid = resid;
516 last_resolution.cookie = best_cookie;
517 last_resolution.steps = resolution_steps;
518
519 // Cache only the type/entry refs since that's all that's needed to build name
520 last_resolution.type_string_ref =
521 StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1);
522 last_resolution.entry_string_ref =
523 StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index);
524 }
525
Adam Lesinskida431a22016-12-29 16:08:16 -0500526 return best_cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700527}
528
Winson2f3669b2019-01-11 11:28:34 -0800529void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
530 resource_resolution_logging_enabled_ = enabled;
531
532 if (!enabled) {
533 last_resolution.cookie = kInvalidCookie;
534 last_resolution.resid = 0;
535 last_resolution.steps.clear();
536 last_resolution.type_string_ref = StringPoolRef();
537 last_resolution.entry_string_ref = StringPoolRef();
538 }
539}
540
541std::string AssetManager2::GetLastResourceResolution() const {
542 if (!resource_resolution_logging_enabled_) {
543 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
544 return std::string();
545 }
546
547 auto cookie = last_resolution.cookie;
548 if (cookie == kInvalidCookie) {
549 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
550 return std::string();
551 }
552
553 uint32_t resid = last_resolution.resid;
554 std::vector<Resolution::Step>& steps = last_resolution.steps;
555
556 ResourceName resource_name;
557 std::string resource_name_string;
558
559 const LoadedPackage* package =
560 apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
561
562 if (package != nullptr) {
563 ToResourceName(last_resolution.type_string_ref,
564 last_resolution.entry_string_ref,
565 package,
566 &resource_name);
567 resource_name_string = ToFormattedResourceString(&resource_name);
568 }
569
570 std::stringstream log_stream;
571 log_stream << base::StringPrintf("Resolution for 0x%08x ", resid)
572 << resource_name_string
573 << "\n\tFor config -"
574 << configuration_.toString();
575
576 std::string prefix;
577 for (Resolution::Step step : steps) {
578 switch (step.type) {
579 case Resolution::Step::Type::INITIAL:
580 prefix = "Found initial";
581 break;
582 case Resolution::Step::Type::BETTER_MATCH:
583 prefix = "Found better";
584 break;
585 case Resolution::Step::Type::OVERLAID:
586 prefix = "Overlaid";
587 break;
588 }
589
590 if (!prefix.empty()) {
591 log_stream << "\n\t" << prefix << ": " << *step.package_name;
592
593 if (!step.config_name.isEmpty()) {
594 log_stream << " -" << step.config_name;
595 }
596 }
597 }
598
599 return log_stream.str();
600}
601
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800602bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700603 FindEntryResult entry;
604 ApkAssetsCookie cookie =
605 FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700606 if (cookie == kInvalidCookie) {
607 return false;
608 }
609
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800610 const LoadedPackage* package =
611 apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
Adam Lesinskida431a22016-12-29 16:08:16 -0500612 if (package == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700613 return false;
614 }
615
Winson2f3669b2019-01-11 11:28:34 -0800616 return ToResourceName(entry.type_string_ref,
617 entry.entry_string_ref,
618 package,
619 out_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700620}
621
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800622bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700623 FindEntryResult entry;
624 ApkAssetsCookie cookie =
625 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
626 if (cookie != kInvalidCookie) {
627 *out_flags = entry.type_flags;
628 return cookie;
629 }
630 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700631}
632
633ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
634 uint16_t density_override, Res_value* out_value,
635 ResTable_config* out_selected_config,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800636 uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700637 FindEntryResult entry;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700638 ApkAssetsCookie cookie =
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700639 FindEntry(resid, density_override, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700640 if (cookie == kInvalidCookie) {
641 return kInvalidCookie;
642 }
643
Adam Lesinski498f6052017-11-29 13:24:29 -0800644 if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700645 if (!may_be_bag) {
646 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800647 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700648 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800649
650 // Create a reference since we can't represent this complex type as a Res_value.
651 out_value->dataType = Res_value::TYPE_REFERENCE;
652 out_value->data = resid;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800653 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700654 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800655 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700656 }
657
658 const Res_value* device_value = reinterpret_cast<const Res_value*>(
659 reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size));
660 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500661
662 // Convert the package ID to the runtime assigned package ID.
663 entry.dynamic_ref_table->lookupResourceValue(out_value);
664
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800665 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700666 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700667 return cookie;
668}
669
Adam Lesinski0c405242017-01-13 20:47:26 -0800670ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
671 ResTable_config* in_out_selected_config,
672 uint32_t* in_out_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800673 uint32_t* out_last_reference) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800674 constexpr const int kMaxIterations = 20;
675
Adam Lesinski0c405242017-01-13 20:47:26 -0800676 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
677 in_out_value->data != 0u && iteration < kMaxIterations;
678 iteration++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800679 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800680 uint32_t new_flags = 0u;
681 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
682 in_out_value, in_out_selected_config, &new_flags);
683 if (cookie == kInvalidCookie) {
684 return kInvalidCookie;
685 }
686 if (in_out_flags != nullptr) {
687 *in_out_flags |= new_flags;
688 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800689 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800690 // This reference can't be resolved, so exit now and let the caller deal with it.
691 return cookie;
692 }
693 }
694 return cookie;
695}
696
Adam Lesinski7ad11102016-10-28 16:39:15 -0700697const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
y57cd1952018-04-12 14:26:23 -0700698 auto found_resids = std::vector<uint32_t>();
699 return GetBag(resid, found_resids);
700}
701
702const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800703 ATRACE_NAME("AssetManager::GetBag");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700704
705 auto cached_iter = cached_bags_.find(resid);
706 if (cached_iter != cached_bags_.end()) {
707 return cached_iter->second.get();
708 }
709
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700710 FindEntryResult entry;
711 ApkAssetsCookie cookie =
712 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700713 if (cookie == kInvalidCookie) {
714 return nullptr;
715 }
716
717 // Check that the size of the entry header is at least as big as
718 // the desired ResTable_map_entry. Also verify that the entry
719 // was intended to be a map.
720 if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) ||
721 (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
722 // Not a bag, nothing to do.
723 return nullptr;
724 }
725
726 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry);
727 const ResTable_map* map_entry =
728 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
729 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
730
y57cd1952018-04-12 14:26:23 -0700731 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
732 // dependencies between bags
733 child_resids.push_back(resid);
734
Adam Lesinskida431a22016-12-29 16:08:16 -0500735 uint32_t parent_resid = dtohl(map->parent.ident);
y57cd1952018-04-12 14:26:23 -0700736 if (parent_resid == 0 || std::find(child_resids.begin(), child_resids.end(), parent_resid)
737 != child_resids.end()) {
738 // There is no parent or that a circular dependency exist, meaning there is nothing to
739 // inherit and we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700740 const size_t entry_count = map_entry_end - map_entry;
741 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
742 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
743 ResolvedBag::Entry* new_entry = new_bag->entries;
744 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500745 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800746 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500747 // Attributes, arrays, etc don't have a resource id as the name. They specify
748 // other data, which would be wrong to change via a lookup.
749 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800750 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
751 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500752 return nullptr;
753 }
754 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700755 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500756 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700757 new_entry->key_pool = nullptr;
758 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800759 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700760 new_entry->value.copyFrom_dtoh(map_entry->value);
761 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
762 if (err != NO_ERROR) {
763 LOG(ERROR) << base::StringPrintf(
764 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
765 new_entry->value.data, new_key);
766 return nullptr;
767 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700768 ++new_entry;
769 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700770 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700771 new_bag->entry_count = static_cast<uint32_t>(entry_count);
772 ResolvedBag* result = new_bag.get();
773 cached_bags_[resid] = std::move(new_bag);
774 return result;
775 }
776
Adam Lesinskida431a22016-12-29 16:08:16 -0500777 // In case the parent is a dynamic reference, resolve it.
778 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
779
Adam Lesinski7ad11102016-10-28 16:39:15 -0700780 // Get the parent and do a merge of the keys.
y57cd1952018-04-12 14:26:23 -0700781 const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700782 if (parent_bag == nullptr) {
783 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800784 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
785 resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700786 return nullptr;
787 }
788
Adam Lesinski7ad11102016-10-28 16:39:15 -0700789 // Create the max possible entries we can make. Once we construct the bag,
790 // we will realloc to fit to size.
791 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -0700792 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
793 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700794 ResolvedBag::Entry* new_entry = new_bag->entries;
795
796 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
797 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
798
799 // The keys are expected to be in sorted order. Merge the two bags.
800 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500801 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800802 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500803 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800804 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
805 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500806 return nullptr;
807 }
808 }
809
Adam Lesinski7ad11102016-10-28 16:39:15 -0700810 if (child_key <= parent_entry->key) {
811 // Use the child key if it comes before the parent
812 // or is equal to the parent (overrides).
813 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700814 new_entry->key = child_key;
815 new_entry->key_pool = nullptr;
816 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700817 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800818 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700819 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
820 if (err != NO_ERROR) {
821 LOG(ERROR) << base::StringPrintf(
822 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
823 new_entry->value.data, child_key);
824 return nullptr;
825 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700826 ++map_entry;
827 } else {
828 // Take the parent entry as-is.
829 memcpy(new_entry, parent_entry, sizeof(*new_entry));
830 }
831
832 if (child_key >= parent_entry->key) {
833 // Move to the next parent entry if we used it or it was overridden.
834 ++parent_entry;
835 }
836 // Increment to the next entry to fill.
837 ++new_entry;
838 }
839
840 // Finish the child entries if they exist.
841 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500842 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800843 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500844 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800845 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
846 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500847 return nullptr;
848 }
849 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700850 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500851 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700852 new_entry->key_pool = nullptr;
853 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700854 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800855 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700856 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
857 if (err != NO_ERROR) {
858 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
859 new_entry->value.dataType, new_entry->value.data, new_key);
860 return nullptr;
861 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700862 ++map_entry;
863 ++new_entry;
864 }
865
866 // Finish the parent entries if they exist.
867 if (parent_entry != parent_entry_end) {
868 // Take the rest of the parent entries as-is.
869 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
870 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
871 new_entry += num_entries_to_copy;
872 }
873
874 // Resize the resulting array to fit.
875 const size_t actual_count = new_entry - new_bag->entries;
876 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -0700877 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
878 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700879 }
880
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700881 // Combine flags from the parent and our own bag.
882 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -0700883 new_bag->entry_count = static_cast<uint32_t>(actual_count);
884 ResolvedBag* result = new_bag.get();
885 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700886 return result;
887}
888
Adam Lesinski929d6512017-01-16 19:11:19 -0800889static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
890 ssize_t len =
891 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
892 if (len < 0) {
893 return false;
894 }
895 out->resize(static_cast<size_t>(len));
896 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
897 static_cast<size_t>(len + 1));
898 return true;
899}
900
Adam Lesinski0c405242017-01-13 20:47:26 -0800901uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
902 const std::string& fallback_type,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800903 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -0800904 StringPiece package_name, type, entry;
905 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
906 return 0u;
907 }
908
909 if (entry.empty()) {
910 return 0u;
911 }
912
913 if (package_name.empty()) {
914 package_name = fallback_package;
915 }
916
917 if (type.empty()) {
918 type = fallback_type;
919 }
920
921 std::u16string type16;
922 if (!Utf8ToUtf16(type, &type16)) {
923 return 0u;
924 }
925
926 std::u16string entry16;
927 if (!Utf8ToUtf16(entry, &entry16)) {
928 return 0u;
929 }
930
931 const StringPiece16 kAttr16 = u"attr";
932 const static std::u16string kAttrPrivate16 = u"^attr-private";
933
934 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800935 for (const ConfiguredPackage& package_impl : package_group.packages_) {
936 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -0800937 if (package_name != package->GetPackageName()) {
938 // All packages in the same group are expected to have the same package name.
939 break;
940 }
941
942 uint32_t resid = package->FindEntryByName(type16, entry16);
943 if (resid == 0u && kAttr16 == type16) {
944 // Private attributes in libraries (such as the framework) are sometimes encoded
945 // under the type '^attr-private' in order to leave the ID space of public 'attr'
946 // free for future additions. Check '^attr-private' for the same name.
947 resid = package->FindEntryByName(kAttrPrivate16, entry16);
948 }
949
950 if (resid != 0u) {
951 return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId);
952 }
953 }
954 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800955 return 0u;
956}
957
Mårten Kongstad668ec5b2018-06-11 14:11:33 +0200958void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800959 for (PackageGroup& group : package_groups_) {
960 for (ConfiguredPackage& impl : group.packages_) {
961 // Destroy it.
962 impl.filtered_configs_.~ByteBucketArray();
963
964 // Re-create it.
965 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
966
967 // Create the filters here.
968 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) {
969 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index);
970 const auto iter_end = spec->types + spec->type_count;
971 for (auto iter = spec->types; iter != iter_end; ++iter) {
972 ResTable_config this_config;
973 this_config.copyFromDtoH((*iter)->config);
Mårten Kongstad668ec5b2018-06-11 14:11:33 +0200974 if (!filter_incompatible_configs || this_config.match(configuration_)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800975 group.configurations.push_back(this_config);
976 group.types.push_back(*iter);
977 }
978 }
979 });
980 }
981 }
982}
983
Adam Lesinski7ad11102016-10-28 16:39:15 -0700984void AssetManager2::InvalidateCaches(uint32_t diff) {
985 if (diff == 0xffffffffu) {
986 // Everything must go.
987 cached_bags_.clear();
988 return;
989 }
990
991 // Be more conservative with what gets purged. Only if the bag has other possible
992 // variations with respect to what changed (diff) should we remove it.
993 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
994 if (diff & iter->second->type_spec_flags) {
995 iter = cached_bags_.erase(iter);
996 } else {
997 ++iter;
998 }
999 }
1000}
1001
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001002uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) {
1003 for (auto& package_group : package_groups_) {
1004 for (auto& package2 : package_group.packages_) {
1005 if (package2.loaded_package_ == package) {
1006 return package_group.dynamic_ref_table.mAssignedPackageId;
1007 }
1008 }
1009 }
1010 return 0;
1011}
1012
Adam Lesinski30080e22017-10-16 16:18:09 -07001013std::unique_ptr<Theme> AssetManager2::NewTheme() {
1014 return std::unique_ptr<Theme>(new Theme(this));
1015}
1016
1017Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1018}
1019
1020Theme::~Theme() = default;
1021
1022namespace {
1023
1024struct ThemeEntry {
1025 ApkAssetsCookie cookie;
1026 uint32_t type_spec_flags;
1027 Res_value value;
1028};
1029
1030struct ThemeType {
1031 int entry_count;
1032 ThemeEntry entries[0];
1033};
1034
1035constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
1036
1037} // namespace
1038
1039struct Theme::Package {
1040 // Each element of Type will be a dynamically sized object
1041 // allocated to have the entries stored contiguously with the Type.
1042 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
1043};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001044
1045bool Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001046 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001047
1048 const ResolvedBag* bag = asset_manager_->GetBag(resid);
1049 if (bag == nullptr) {
1050 return false;
1051 }
1052
1053 // Merge the flags from this style.
1054 type_spec_flags_ |= bag->type_spec_flags;
1055
Adam Lesinski30080e22017-10-16 16:18:09 -07001056 int last_type_idx = -1;
1057 int last_package_idx = -1;
1058 Package* last_package = nullptr;
1059 ThemeType* last_type = nullptr;
1060
1061 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
1062 // need to perform one resize per type.
1063 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
1064 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
1065 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001066 const uint32_t attr_resid = bag_iter->key;
1067
Adam Lesinski30080e22017-10-16 16:18:09 -07001068 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1069 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -08001070 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001071 return false;
1072 }
1073
Adam Lesinski30080e22017-10-16 16:18:09 -07001074 // We don't use the 0-based index for the type so that we can avoid doing ID validation
1075 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
1076 // the construction of this type is guarded with a resource ID check, it will never be
1077 // populated, and querying type ID 0 will always fail.
1078 const int package_idx = get_package_id(attr_resid);
1079 const int type_idx = get_type_id(attr_resid);
1080 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001081
Adam Lesinski30080e22017-10-16 16:18:09 -07001082 if (last_package_idx != package_idx) {
1083 std::unique_ptr<Package>& package = packages_[package_idx];
1084 if (package == nullptr) {
1085 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001086 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001087 last_package_idx = package_idx;
1088 last_package = package.get();
1089 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001090 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001091
1092 if (last_type_idx != type_idx) {
1093 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
1094 if (type == nullptr) {
1095 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
1096 // a sorted list of attributes, this shouldn't be resized again during this method call.
1097 type.reset(reinterpret_cast<ThemeType*>(
1098 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
1099 type->entry_count = entry_idx + 1;
1100 } else if (entry_idx >= type->entry_count) {
1101 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
1102 // a sorted list of attributes, this shouldn't be resized again during this method call.
1103 const int new_count = entry_idx + 1;
1104 type.reset(reinterpret_cast<ThemeType*>(
1105 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
1106
1107 // Clear out the newly allocated space (which isn't zeroed).
1108 memset(type->entries + type->entry_count, 0,
1109 (new_count - type->entry_count) * sizeof(ThemeEntry));
1110 type->entry_count = new_count;
1111 }
1112 last_type_idx = type_idx;
1113 last_type = type.get();
1114 }
1115
1116 ThemeEntry& entry = last_type->entries[entry_idx];
1117 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
1118 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001119 entry.cookie = bag_iter->cookie;
1120 entry.type_spec_flags |= bag->type_spec_flags;
1121 entry.value = bag_iter->value;
1122 }
1123 }
1124 return true;
1125}
1126
1127ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
1128 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -07001129 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001130
1131 uint32_t type_spec_flags = 0u;
1132
Adam Lesinski30080e22017-10-16 16:18:09 -07001133 do {
1134 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001135 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -07001136 if (package != nullptr) {
1137 // The themes are constructed with a 1-based type ID, so no need to decrement here.
1138 const int type_idx = get_type_id(resid);
1139 const ThemeType* type = package->types[type_idx].get();
1140 if (type != nullptr) {
1141 const int entry_idx = get_entry_id(resid);
1142 if (entry_idx < type->entry_count) {
1143 const ThemeEntry& entry = type->entries[entry_idx];
1144 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001145
Adam Lesinski30080e22017-10-16 16:18:09 -07001146 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
1147 if (cnt > 0) {
1148 cnt--;
1149 resid = entry.value.data;
1150 continue;
1151 }
1152 return kInvalidCookie;
1153 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001154
Adam Lesinski30080e22017-10-16 16:18:09 -07001155 // @null is different than @empty.
1156 if (entry.value.dataType == Res_value::TYPE_NULL &&
1157 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1158 return kInvalidCookie;
1159 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001160
Adam Lesinski30080e22017-10-16 16:18:09 -07001161 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -05001162 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -07001163 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001164 }
Adam Lesinskida431a22016-12-29 16:08:16 -05001165 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001166 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001167 break;
1168 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001169 return kInvalidCookie;
1170}
1171
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001172ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
1173 ResTable_config* in_out_selected_config,
1174 uint32_t* in_out_type_spec_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001175 uint32_t* out_last_ref) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001176 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
1177 uint32_t new_flags;
1178 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
1179 if (cookie == kInvalidCookie) {
1180 return kInvalidCookie;
1181 }
1182
1183 if (in_out_type_spec_flags != nullptr) {
1184 *in_out_type_spec_flags |= new_flags;
1185 }
1186 }
1187 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
1188 in_out_type_spec_flags, out_last_ref);
1189}
1190
Adam Lesinski7ad11102016-10-28 16:39:15 -07001191void Theme::Clear() {
1192 type_spec_flags_ = 0u;
1193 for (std::unique_ptr<Package>& package : packages_) {
1194 package.reset();
1195 }
1196}
1197
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001198void Theme::SetTo(const Theme& o) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001199 if (this == &o) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001200 return;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001201 }
1202
Adam Lesinski7ad11102016-10-28 16:39:15 -07001203 type_spec_flags_ = o.type_spec_flags_;
1204
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001205 if (asset_manager_ == o.asset_manager_) {
1206 // The theme comes from the same asset manager so all theme data can be copied exactly
1207 for (size_t p = 0; p < packages_.size(); p++) {
1208 const Package *package = o.packages_[p].get();
1209 if (package == nullptr) {
1210 // The other theme doesn't have this package, clear ours.
1211 packages_[p].reset();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001212 continue;
1213 }
1214
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001215 if (packages_[p] == nullptr) {
1216 // The other theme has this package, but we don't. Make one.
1217 packages_[p].reset(new Package());
1218 }
1219
1220 for (size_t t = 0; t < package->types.size(); t++) {
1221 const ThemeType *type = package->types[t].get();
1222 if (type == nullptr) {
1223 // The other theme doesn't have this type, clear ours.
1224 packages_[p]->types[t].reset();
1225 continue;
1226 }
1227
1228 // Create a new type and update it to theirs.
1229 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
1230 void *copied_data = malloc(type_alloc_size);
1231 memcpy(copied_data, type, type_alloc_size);
1232 packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data));
1233 }
1234 }
1235 } else {
1236 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1237 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1238 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1239
1240 // Determine which ApkAssets are loaded in both theme AssetManagers
1241 std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets();
1242 for (size_t i = 0; i < src_assets.size(); i++) {
1243 const ApkAssets* src_asset = src_assets[i];
1244
1245 std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets();
1246 for (size_t j = 0; j < dest_assets.size(); j++) {
1247 const ApkAssets* dest_asset = dest_assets[j];
1248
1249 // Map the runtime package of the source apk asset to the destination apk asset
1250 if (src_asset->GetPath() == dest_asset->GetPath()) {
1251 const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages =
1252 src_asset->GetLoadedArsc()->GetPackages();
1253 const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages =
1254 dest_asset->GetLoadedArsc()->GetPackages();
1255
1256 SourceToDestinationRuntimePackageMap package_map;
1257
1258 // The source and destination package should have the same number of packages loaded in
1259 // the same order.
1260 const size_t N = src_packages.size();
1261 CHECK(N == dest_packages.size())
1262 << " LoadedArsc " << src_asset->GetPath() << " differs number of packages.";
1263 for (size_t p = 0; p < N; p++) {
1264 auto& src_package = src_packages[p];
1265 auto& dest_package = dest_packages[p];
1266 CHECK(src_package->GetPackageName() == dest_package->GetPackageName())
1267 << " Package " << src_package->GetPackageName() << " differs in load order.";
1268
1269 int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get());
1270 int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get());
1271 package_map[src_package_id] = dest_package_id;
1272 }
1273
1274 src_to_dest_asset_cookies.insert(std::pair<ApkAssetsCookie, ApkAssetsCookie>(i, j));
1275 src_asset_cookie_id_map.insert(
1276 std::pair<ApkAssetsCookie, SourceToDestinationRuntimePackageMap>(i, package_map));
1277 break;
1278 }
1279 }
1280 }
1281
1282 // Reset the data in the destination theme
1283 for (size_t p = 0; p < packages_.size(); p++) {
1284 if (packages_[p] != nullptr) {
1285 packages_[p].reset();
1286 }
1287 }
1288
1289 for (size_t p = 0; p < packages_.size(); p++) {
1290 const Package *package = o.packages_[p].get();
1291 if (package == nullptr) {
1292 continue;
1293 }
1294
1295 for (size_t t = 0; t < package->types.size(); t++) {
1296 const ThemeType *type = package->types[t].get();
1297 if (type == nullptr) {
1298 continue;
1299 }
1300
1301 for (size_t e = 0; e < type->entry_count; e++) {
1302 const ThemeEntry &entry = type->entries[e];
1303 if (entry.value.dataType == Res_value::TYPE_NULL &&
1304 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1305 continue;
1306 }
1307
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001308 // If the attribute value represents an attribute or reference, the package id of the
1309 // value needs to be rewritten to the package id of the value in the destination
1310 uint32_t attribue_data = entry.value.data;
1311 if ((entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1312 || entry.value.dataType == Res_value::TYPE_REFERENCE
1313 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1314 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1315 && attribue_data != 0x0) {
1316
1317 // Determine the package id of the reference in the destination AssetManager
1318 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1319 if (value_package_map == src_asset_cookie_id_map.end()) {
1320 continue;
1321 }
1322
1323 auto value_dest_package = value_package_map->second.find(
1324 get_package_id(entry.value.data));
1325 if (value_dest_package == value_package_map->second.end()) {
1326 continue;
1327 }
1328
1329 attribue_data = fix_package_id(entry.value.data, value_dest_package->second);
1330 }
1331
1332 // The package id of the attribute needs to be rewritten to the package id of the
1333 // attribute in the destination
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001334 int attribute_dest_package_id = p;
1335 if (attribute_dest_package_id != 0x01) {
1336 // Find the cookie of the attribute resource id
1337 FindEntryResult attribute_entry_result;
1338 ApkAssetsCookie attribute_cookie =
1339 o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ , false,
1340 &attribute_entry_result);
1341
1342 // Determine the package id of the attribute in the destination AssetManager
1343 auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie);
1344 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1345 continue;
1346 }
1347 auto attribute_dest_package = attribute_package_map->second.find(
1348 attribute_dest_package_id);
1349 if (attribute_dest_package == attribute_package_map->second.end()) {
1350 continue;
1351 }
1352 attribute_dest_package_id = attribute_dest_package->second;
1353 }
1354
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001355 // Lazily instantiate the destination package
1356 std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id];
1357 if (dest_package == nullptr) {
1358 dest_package.reset(new Package());
1359 }
1360
1361 // Lazily instantiate and resize the destination type
1362 util::unique_cptr<ThemeType>& dest_type = dest_package->types[t];
1363 if (dest_type == nullptr || dest_type->entry_count < type->entry_count) {
1364 const size_t type_alloc_size = sizeof(ThemeType)
1365 + (type->entry_count * sizeof(ThemeEntry));
1366 void* dest_data = malloc(type_alloc_size);
1367 memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry));
1368
1369 // Copy the existing destination type values if the type is resized
1370 if (dest_type != nullptr) {
1371 memcpy(dest_data, type, sizeof(ThemeType)
1372 + (dest_type->entry_count * sizeof(ThemeEntry)));
1373 }
1374
1375 dest_type.reset(reinterpret_cast<ThemeType *>(dest_data));
1376 dest_type->entry_count = type->entry_count;
1377 }
1378
1379 // Find the cookie of the value in the destination
1380 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1381 if (value_dest_cookie == src_to_dest_asset_cookies.end()) {
1382 continue;
1383 }
1384
1385 dest_type->entries[e].cookie = value_dest_cookie->second;
1386 dest_type->entries[e].value.dataType = entry.value.dataType;
1387 dest_type->entries[e].value.data = attribue_data;
1388 dest_type->entries[e].type_spec_flags = entry.type_spec_flags;
1389 }
1390 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001391 }
1392 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001393}
1394
1395void Theme::Dump() const {
1396 base::ScopedLogSeverity _log(base::INFO);
1397 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
1398
1399 for (int p = 0; p < packages_.size(); p++) {
1400 auto& package = packages_[p];
1401 if (package == nullptr) {
1402 continue;
1403 }
1404
1405 for (int t = 0; t < package->types.size(); t++) {
1406 auto& type = package->types[t];
1407 if (type == nullptr) {
1408 continue;
1409 }
1410
1411 for (int e = 0; e < type->entry_count; e++) {
1412 auto& entry = type->entries[e];
1413 if (entry.value.dataType == Res_value::TYPE_NULL &&
1414 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1415 continue;
1416 }
1417
1418 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1419 make_resid(p, t, e), entry.value.data,
1420 entry.value.dataType, entry.cookie);
1421 }
1422 }
1423 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001424}
1425
1426} // namespace android