blob: 20303eba6667167f74ef2a54d67ab97c0b018c90 [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,
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800565 package->GetPackageName(),
Winson2f3669b2019-01-11 11:28:34 -0800566 &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
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800610 const uint8_t package_idx = package_ids_[get_package_id(resid)];
611 if (package_idx == 0xff) {
612 LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.",
613 get_package_id(resid), resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700614 return false;
615 }
616
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800617 const PackageGroup& package_group = package_groups_[package_idx];
618 auto cookie_iter = std::find(package_group.cookies_.begin(),
619 package_group.cookies_.end(), cookie);
620 if (cookie_iter == package_group.cookies_.end()) {
621 return false;
622 }
623
624 long package_pos = std::distance(package_group.cookies_.begin(), cookie_iter);
625 const LoadedPackage* package = package_group.packages_[package_pos].loaded_package_;
Winson2f3669b2019-01-11 11:28:34 -0800626 return ToResourceName(entry.type_string_ref,
627 entry.entry_string_ref,
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800628 package->GetPackageName(),
Winson2f3669b2019-01-11 11:28:34 -0800629 out_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700630}
631
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800632bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700633 FindEntryResult entry;
634 ApkAssetsCookie cookie =
635 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
636 if (cookie != kInvalidCookie) {
637 *out_flags = entry.type_flags;
638 return cookie;
639 }
640 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700641}
642
643ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
644 uint16_t density_override, Res_value* out_value,
645 ResTable_config* out_selected_config,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800646 uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700647 FindEntryResult entry;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700648 ApkAssetsCookie cookie =
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700649 FindEntry(resid, density_override, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700650 if (cookie == kInvalidCookie) {
651 return kInvalidCookie;
652 }
653
Adam Lesinski498f6052017-11-29 13:24:29 -0800654 if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700655 if (!may_be_bag) {
656 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800657 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700658 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800659
660 // Create a reference since we can't represent this complex type as a Res_value.
661 out_value->dataType = Res_value::TYPE_REFERENCE;
662 out_value->data = resid;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800663 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700664 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800665 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700666 }
667
668 const Res_value* device_value = reinterpret_cast<const Res_value*>(
669 reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size));
670 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500671
672 // Convert the package ID to the runtime assigned package ID.
673 entry.dynamic_ref_table->lookupResourceValue(out_value);
674
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800675 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700676 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700677 return cookie;
678}
679
Adam Lesinski0c405242017-01-13 20:47:26 -0800680ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
681 ResTable_config* in_out_selected_config,
682 uint32_t* in_out_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800683 uint32_t* out_last_reference) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800684 constexpr const int kMaxIterations = 20;
685
Adam Lesinski0c405242017-01-13 20:47:26 -0800686 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
687 in_out_value->data != 0u && iteration < kMaxIterations;
688 iteration++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800689 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800690 uint32_t new_flags = 0u;
691 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
692 in_out_value, in_out_selected_config, &new_flags);
693 if (cookie == kInvalidCookie) {
694 return kInvalidCookie;
695 }
696 if (in_out_flags != nullptr) {
697 *in_out_flags |= new_flags;
698 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800699 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800700 // This reference can't be resolved, so exit now and let the caller deal with it.
701 return cookie;
702 }
703 }
704 return cookie;
705}
706
Adam Lesinski7ad11102016-10-28 16:39:15 -0700707const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
y57cd1952018-04-12 14:26:23 -0700708 auto found_resids = std::vector<uint32_t>();
709 return GetBag(resid, found_resids);
710}
711
712const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800713 ATRACE_NAME("AssetManager::GetBag");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700714
715 auto cached_iter = cached_bags_.find(resid);
716 if (cached_iter != cached_bags_.end()) {
717 return cached_iter->second.get();
718 }
719
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700720 FindEntryResult entry;
721 ApkAssetsCookie cookie =
722 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700723 if (cookie == kInvalidCookie) {
724 return nullptr;
725 }
726
727 // Check that the size of the entry header is at least as big as
728 // the desired ResTable_map_entry. Also verify that the entry
729 // was intended to be a map.
730 if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) ||
731 (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
732 // Not a bag, nothing to do.
733 return nullptr;
734 }
735
736 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry);
737 const ResTable_map* map_entry =
738 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
739 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
740
y57cd1952018-04-12 14:26:23 -0700741 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
742 // dependencies between bags
743 child_resids.push_back(resid);
744
Adam Lesinskida431a22016-12-29 16:08:16 -0500745 uint32_t parent_resid = dtohl(map->parent.ident);
y57cd1952018-04-12 14:26:23 -0700746 if (parent_resid == 0 || std::find(child_resids.begin(), child_resids.end(), parent_resid)
747 != child_resids.end()) {
748 // There is no parent or that a circular dependency exist, meaning there is nothing to
749 // inherit and we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700750 const size_t entry_count = map_entry_end - map_entry;
751 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
752 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
753 ResolvedBag::Entry* new_entry = new_bag->entries;
754 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500755 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800756 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500757 // Attributes, arrays, etc don't have a resource id as the name. They specify
758 // other data, which would be wrong to change via a lookup.
759 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800760 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
761 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500762 return nullptr;
763 }
764 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700765 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500766 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700767 new_entry->key_pool = nullptr;
768 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800769 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700770 new_entry->value.copyFrom_dtoh(map_entry->value);
771 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
772 if (err != NO_ERROR) {
773 LOG(ERROR) << base::StringPrintf(
774 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
775 new_entry->value.data, new_key);
776 return nullptr;
777 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700778 ++new_entry;
779 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700780 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700781 new_bag->entry_count = static_cast<uint32_t>(entry_count);
782 ResolvedBag* result = new_bag.get();
783 cached_bags_[resid] = std::move(new_bag);
784 return result;
785 }
786
Adam Lesinskida431a22016-12-29 16:08:16 -0500787 // In case the parent is a dynamic reference, resolve it.
788 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
789
Adam Lesinski7ad11102016-10-28 16:39:15 -0700790 // Get the parent and do a merge of the keys.
y57cd1952018-04-12 14:26:23 -0700791 const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700792 if (parent_bag == nullptr) {
793 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800794 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
795 resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700796 return nullptr;
797 }
798
Adam Lesinski7ad11102016-10-28 16:39:15 -0700799 // Create the max possible entries we can make. Once we construct the bag,
800 // we will realloc to fit to size.
801 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -0700802 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
803 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700804 ResolvedBag::Entry* new_entry = new_bag->entries;
805
806 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
807 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
808
809 // The keys are expected to be in sorted order. Merge the two bags.
810 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500811 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800812 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500813 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800814 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
815 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500816 return nullptr;
817 }
818 }
819
Adam Lesinski7ad11102016-10-28 16:39:15 -0700820 if (child_key <= parent_entry->key) {
821 // Use the child key if it comes before the parent
822 // or is equal to the parent (overrides).
823 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700824 new_entry->key = child_key;
825 new_entry->key_pool = nullptr;
826 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700827 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800828 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700829 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
830 if (err != NO_ERROR) {
831 LOG(ERROR) << base::StringPrintf(
832 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
833 new_entry->value.data, child_key);
834 return nullptr;
835 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700836 ++map_entry;
837 } else {
838 // Take the parent entry as-is.
839 memcpy(new_entry, parent_entry, sizeof(*new_entry));
840 }
841
842 if (child_key >= parent_entry->key) {
843 // Move to the next parent entry if we used it or it was overridden.
844 ++parent_entry;
845 }
846 // Increment to the next entry to fill.
847 ++new_entry;
848 }
849
850 // Finish the child entries if they exist.
851 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500852 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800853 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500854 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800855 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
856 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500857 return nullptr;
858 }
859 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700860 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500861 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700862 new_entry->key_pool = nullptr;
863 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700864 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800865 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -0700866 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
867 if (err != NO_ERROR) {
868 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
869 new_entry->value.dataType, new_entry->value.data, new_key);
870 return nullptr;
871 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700872 ++map_entry;
873 ++new_entry;
874 }
875
876 // Finish the parent entries if they exist.
877 if (parent_entry != parent_entry_end) {
878 // Take the rest of the parent entries as-is.
879 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
880 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
881 new_entry += num_entries_to_copy;
882 }
883
884 // Resize the resulting array to fit.
885 const size_t actual_count = new_entry - new_bag->entries;
886 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -0700887 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
888 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700889 }
890
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700891 // Combine flags from the parent and our own bag.
892 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -0700893 new_bag->entry_count = static_cast<uint32_t>(actual_count);
894 ResolvedBag* result = new_bag.get();
895 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700896 return result;
897}
898
Adam Lesinski929d6512017-01-16 19:11:19 -0800899static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
900 ssize_t len =
901 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
902 if (len < 0) {
903 return false;
904 }
905 out->resize(static_cast<size_t>(len));
906 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
907 static_cast<size_t>(len + 1));
908 return true;
909}
910
Adam Lesinski0c405242017-01-13 20:47:26 -0800911uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
912 const std::string& fallback_type,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800913 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -0800914 StringPiece package_name, type, entry;
915 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
916 return 0u;
917 }
918
919 if (entry.empty()) {
920 return 0u;
921 }
922
923 if (package_name.empty()) {
924 package_name = fallback_package;
925 }
926
927 if (type.empty()) {
928 type = fallback_type;
929 }
930
931 std::u16string type16;
932 if (!Utf8ToUtf16(type, &type16)) {
933 return 0u;
934 }
935
936 std::u16string entry16;
937 if (!Utf8ToUtf16(entry, &entry16)) {
938 return 0u;
939 }
940
941 const StringPiece16 kAttr16 = u"attr";
942 const static std::u16string kAttrPrivate16 = u"^attr-private";
943
944 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800945 for (const ConfiguredPackage& package_impl : package_group.packages_) {
946 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -0800947 if (package_name != package->GetPackageName()) {
948 // All packages in the same group are expected to have the same package name.
949 break;
950 }
951
952 uint32_t resid = package->FindEntryByName(type16, entry16);
953 if (resid == 0u && kAttr16 == type16) {
954 // Private attributes in libraries (such as the framework) are sometimes encoded
955 // under the type '^attr-private' in order to leave the ID space of public 'attr'
956 // free for future additions. Check '^attr-private' for the same name.
957 resid = package->FindEntryByName(kAttrPrivate16, entry16);
958 }
959
960 if (resid != 0u) {
961 return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId);
962 }
963 }
964 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800965 return 0u;
966}
967
Mårten Kongstad668ec5b2018-06-11 14:11:33 +0200968void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800969 for (PackageGroup& group : package_groups_) {
970 for (ConfiguredPackage& impl : group.packages_) {
971 // Destroy it.
972 impl.filtered_configs_.~ByteBucketArray();
973
974 // Re-create it.
975 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
976
977 // Create the filters here.
978 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) {
979 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index);
980 const auto iter_end = spec->types + spec->type_count;
981 for (auto iter = spec->types; iter != iter_end; ++iter) {
982 ResTable_config this_config;
983 this_config.copyFromDtoH((*iter)->config);
Mårten Kongstad668ec5b2018-06-11 14:11:33 +0200984 if (!filter_incompatible_configs || this_config.match(configuration_)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800985 group.configurations.push_back(this_config);
986 group.types.push_back(*iter);
987 }
988 }
989 });
990 }
991 }
992}
993
Adam Lesinski7ad11102016-10-28 16:39:15 -0700994void AssetManager2::InvalidateCaches(uint32_t diff) {
995 if (diff == 0xffffffffu) {
996 // Everything must go.
997 cached_bags_.clear();
998 return;
999 }
1000
1001 // Be more conservative with what gets purged. Only if the bag has other possible
1002 // variations with respect to what changed (diff) should we remove it.
1003 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1004 if (diff & iter->second->type_spec_flags) {
1005 iter = cached_bags_.erase(iter);
1006 } else {
1007 ++iter;
1008 }
1009 }
1010}
1011
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001012uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) {
1013 for (auto& package_group : package_groups_) {
1014 for (auto& package2 : package_group.packages_) {
1015 if (package2.loaded_package_ == package) {
1016 return package_group.dynamic_ref_table.mAssignedPackageId;
1017 }
1018 }
1019 }
1020 return 0;
1021}
1022
Adam Lesinski30080e22017-10-16 16:18:09 -07001023std::unique_ptr<Theme> AssetManager2::NewTheme() {
1024 return std::unique_ptr<Theme>(new Theme(this));
1025}
1026
1027Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1028}
1029
1030Theme::~Theme() = default;
1031
1032namespace {
1033
1034struct ThemeEntry {
1035 ApkAssetsCookie cookie;
1036 uint32_t type_spec_flags;
1037 Res_value value;
1038};
1039
1040struct ThemeType {
1041 int entry_count;
1042 ThemeEntry entries[0];
1043};
1044
1045constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
1046
1047} // namespace
1048
1049struct Theme::Package {
1050 // Each element of Type will be a dynamically sized object
1051 // allocated to have the entries stored contiguously with the Type.
1052 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
1053};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001054
1055bool Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001056 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001057
1058 const ResolvedBag* bag = asset_manager_->GetBag(resid);
1059 if (bag == nullptr) {
1060 return false;
1061 }
1062
1063 // Merge the flags from this style.
1064 type_spec_flags_ |= bag->type_spec_flags;
1065
Adam Lesinski30080e22017-10-16 16:18:09 -07001066 int last_type_idx = -1;
1067 int last_package_idx = -1;
1068 Package* last_package = nullptr;
1069 ThemeType* last_type = nullptr;
1070
1071 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
1072 // need to perform one resize per type.
1073 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
1074 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
1075 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001076 const uint32_t attr_resid = bag_iter->key;
1077
Adam Lesinski30080e22017-10-16 16:18:09 -07001078 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1079 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -08001080 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001081 return false;
1082 }
1083
Adam Lesinski30080e22017-10-16 16:18:09 -07001084 // We don't use the 0-based index for the type so that we can avoid doing ID validation
1085 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
1086 // the construction of this type is guarded with a resource ID check, it will never be
1087 // populated, and querying type ID 0 will always fail.
1088 const int package_idx = get_package_id(attr_resid);
1089 const int type_idx = get_type_id(attr_resid);
1090 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001091
Adam Lesinski30080e22017-10-16 16:18:09 -07001092 if (last_package_idx != package_idx) {
1093 std::unique_ptr<Package>& package = packages_[package_idx];
1094 if (package == nullptr) {
1095 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001096 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001097 last_package_idx = package_idx;
1098 last_package = package.get();
1099 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001100 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001101
1102 if (last_type_idx != type_idx) {
1103 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
1104 if (type == nullptr) {
1105 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
1106 // a sorted list of attributes, this shouldn't be resized again during this method call.
1107 type.reset(reinterpret_cast<ThemeType*>(
1108 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
1109 type->entry_count = entry_idx + 1;
1110 } else if (entry_idx >= type->entry_count) {
1111 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
1112 // a sorted list of attributes, this shouldn't be resized again during this method call.
1113 const int new_count = entry_idx + 1;
1114 type.reset(reinterpret_cast<ThemeType*>(
1115 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
1116
1117 // Clear out the newly allocated space (which isn't zeroed).
1118 memset(type->entries + type->entry_count, 0,
1119 (new_count - type->entry_count) * sizeof(ThemeEntry));
1120 type->entry_count = new_count;
1121 }
1122 last_type_idx = type_idx;
1123 last_type = type.get();
1124 }
1125
1126 ThemeEntry& entry = last_type->entries[entry_idx];
1127 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
1128 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001129 entry.cookie = bag_iter->cookie;
1130 entry.type_spec_flags |= bag->type_spec_flags;
1131 entry.value = bag_iter->value;
1132 }
1133 }
1134 return true;
1135}
1136
1137ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
1138 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -07001139 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001140
1141 uint32_t type_spec_flags = 0u;
1142
Adam Lesinski30080e22017-10-16 16:18:09 -07001143 do {
1144 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001145 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -07001146 if (package != nullptr) {
1147 // The themes are constructed with a 1-based type ID, so no need to decrement here.
1148 const int type_idx = get_type_id(resid);
1149 const ThemeType* type = package->types[type_idx].get();
1150 if (type != nullptr) {
1151 const int entry_idx = get_entry_id(resid);
1152 if (entry_idx < type->entry_count) {
1153 const ThemeEntry& entry = type->entries[entry_idx];
1154 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001155
Adam Lesinski30080e22017-10-16 16:18:09 -07001156 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
1157 if (cnt > 0) {
1158 cnt--;
1159 resid = entry.value.data;
1160 continue;
1161 }
1162 return kInvalidCookie;
1163 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001164
Adam Lesinski30080e22017-10-16 16:18:09 -07001165 // @null is different than @empty.
1166 if (entry.value.dataType == Res_value::TYPE_NULL &&
1167 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1168 return kInvalidCookie;
1169 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001170
Adam Lesinski30080e22017-10-16 16:18:09 -07001171 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -05001172 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -07001173 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001174 }
Adam Lesinskida431a22016-12-29 16:08:16 -05001175 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001176 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001177 break;
1178 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001179 return kInvalidCookie;
1180}
1181
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001182ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
1183 ResTable_config* in_out_selected_config,
1184 uint32_t* in_out_type_spec_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001185 uint32_t* out_last_ref) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001186 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
1187 uint32_t new_flags;
1188 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
1189 if (cookie == kInvalidCookie) {
1190 return kInvalidCookie;
1191 }
1192
1193 if (in_out_type_spec_flags != nullptr) {
1194 *in_out_type_spec_flags |= new_flags;
1195 }
1196 }
1197 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
1198 in_out_type_spec_flags, out_last_ref);
1199}
1200
Adam Lesinski7ad11102016-10-28 16:39:15 -07001201void Theme::Clear() {
1202 type_spec_flags_ = 0u;
1203 for (std::unique_ptr<Package>& package : packages_) {
1204 package.reset();
1205 }
1206}
1207
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001208void Theme::SetTo(const Theme& o) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001209 if (this == &o) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001210 return;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001211 }
1212
Adam Lesinski7ad11102016-10-28 16:39:15 -07001213 type_spec_flags_ = o.type_spec_flags_;
1214
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001215 if (asset_manager_ == o.asset_manager_) {
1216 // The theme comes from the same asset manager so all theme data can be copied exactly
1217 for (size_t p = 0; p < packages_.size(); p++) {
1218 const Package *package = o.packages_[p].get();
1219 if (package == nullptr) {
1220 // The other theme doesn't have this package, clear ours.
1221 packages_[p].reset();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001222 continue;
1223 }
1224
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001225 if (packages_[p] == nullptr) {
1226 // The other theme has this package, but we don't. Make one.
1227 packages_[p].reset(new Package());
1228 }
1229
1230 for (size_t t = 0; t < package->types.size(); t++) {
1231 const ThemeType *type = package->types[t].get();
1232 if (type == nullptr) {
1233 // The other theme doesn't have this type, clear ours.
1234 packages_[p]->types[t].reset();
1235 continue;
1236 }
1237
1238 // Create a new type and update it to theirs.
1239 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
1240 void *copied_data = malloc(type_alloc_size);
1241 memcpy(copied_data, type, type_alloc_size);
1242 packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data));
1243 }
1244 }
1245 } else {
1246 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1247 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1248 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1249
1250 // Determine which ApkAssets are loaded in both theme AssetManagers
1251 std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets();
1252 for (size_t i = 0; i < src_assets.size(); i++) {
1253 const ApkAssets* src_asset = src_assets[i];
1254
1255 std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets();
1256 for (size_t j = 0; j < dest_assets.size(); j++) {
1257 const ApkAssets* dest_asset = dest_assets[j];
1258
1259 // Map the runtime package of the source apk asset to the destination apk asset
1260 if (src_asset->GetPath() == dest_asset->GetPath()) {
1261 const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages =
1262 src_asset->GetLoadedArsc()->GetPackages();
1263 const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages =
1264 dest_asset->GetLoadedArsc()->GetPackages();
1265
1266 SourceToDestinationRuntimePackageMap package_map;
1267
1268 // The source and destination package should have the same number of packages loaded in
1269 // the same order.
1270 const size_t N = src_packages.size();
1271 CHECK(N == dest_packages.size())
1272 << " LoadedArsc " << src_asset->GetPath() << " differs number of packages.";
1273 for (size_t p = 0; p < N; p++) {
1274 auto& src_package = src_packages[p];
1275 auto& dest_package = dest_packages[p];
1276 CHECK(src_package->GetPackageName() == dest_package->GetPackageName())
1277 << " Package " << src_package->GetPackageName() << " differs in load order.";
1278
1279 int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get());
1280 int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get());
1281 package_map[src_package_id] = dest_package_id;
1282 }
1283
1284 src_to_dest_asset_cookies.insert(std::pair<ApkAssetsCookie, ApkAssetsCookie>(i, j));
1285 src_asset_cookie_id_map.insert(
1286 std::pair<ApkAssetsCookie, SourceToDestinationRuntimePackageMap>(i, package_map));
1287 break;
1288 }
1289 }
1290 }
1291
1292 // Reset the data in the destination theme
1293 for (size_t p = 0; p < packages_.size(); p++) {
1294 if (packages_[p] != nullptr) {
1295 packages_[p].reset();
1296 }
1297 }
1298
1299 for (size_t p = 0; p < packages_.size(); p++) {
1300 const Package *package = o.packages_[p].get();
1301 if (package == nullptr) {
1302 continue;
1303 }
1304
1305 for (size_t t = 0; t < package->types.size(); t++) {
1306 const ThemeType *type = package->types[t].get();
1307 if (type == nullptr) {
1308 continue;
1309 }
1310
1311 for (size_t e = 0; e < type->entry_count; e++) {
1312 const ThemeEntry &entry = type->entries[e];
1313 if (entry.value.dataType == Res_value::TYPE_NULL &&
1314 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1315 continue;
1316 }
1317
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001318 // If the attribute value represents an attribute or reference, the package id of the
1319 // value needs to be rewritten to the package id of the value in the destination
1320 uint32_t attribue_data = entry.value.data;
1321 if ((entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1322 || entry.value.dataType == Res_value::TYPE_REFERENCE
1323 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1324 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1325 && attribue_data != 0x0) {
1326
1327 // Determine the package id of the reference in the destination AssetManager
1328 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1329 if (value_package_map == src_asset_cookie_id_map.end()) {
1330 continue;
1331 }
1332
1333 auto value_dest_package = value_package_map->second.find(
1334 get_package_id(entry.value.data));
1335 if (value_dest_package == value_package_map->second.end()) {
1336 continue;
1337 }
1338
1339 attribue_data = fix_package_id(entry.value.data, value_dest_package->second);
1340 }
1341
1342 // The package id of the attribute needs to be rewritten to the package id of the
1343 // attribute in the destination
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001344 int attribute_dest_package_id = p;
1345 if (attribute_dest_package_id != 0x01) {
1346 // Find the cookie of the attribute resource id
1347 FindEntryResult attribute_entry_result;
1348 ApkAssetsCookie attribute_cookie =
1349 o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ , false,
1350 &attribute_entry_result);
1351
1352 // Determine the package id of the attribute in the destination AssetManager
1353 auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie);
1354 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1355 continue;
1356 }
1357 auto attribute_dest_package = attribute_package_map->second.find(
1358 attribute_dest_package_id);
1359 if (attribute_dest_package == attribute_package_map->second.end()) {
1360 continue;
1361 }
1362 attribute_dest_package_id = attribute_dest_package->second;
1363 }
1364
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001365 // Lazily instantiate the destination package
1366 std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id];
1367 if (dest_package == nullptr) {
1368 dest_package.reset(new Package());
1369 }
1370
1371 // Lazily instantiate and resize the destination type
1372 util::unique_cptr<ThemeType>& dest_type = dest_package->types[t];
1373 if (dest_type == nullptr || dest_type->entry_count < type->entry_count) {
1374 const size_t type_alloc_size = sizeof(ThemeType)
1375 + (type->entry_count * sizeof(ThemeEntry));
1376 void* dest_data = malloc(type_alloc_size);
1377 memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry));
1378
1379 // Copy the existing destination type values if the type is resized
1380 if (dest_type != nullptr) {
1381 memcpy(dest_data, type, sizeof(ThemeType)
1382 + (dest_type->entry_count * sizeof(ThemeEntry)));
1383 }
1384
1385 dest_type.reset(reinterpret_cast<ThemeType *>(dest_data));
1386 dest_type->entry_count = type->entry_count;
1387 }
1388
1389 // Find the cookie of the value in the destination
1390 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1391 if (value_dest_cookie == src_to_dest_asset_cookies.end()) {
1392 continue;
1393 }
1394
1395 dest_type->entries[e].cookie = value_dest_cookie->second;
1396 dest_type->entries[e].value.dataType = entry.value.dataType;
1397 dest_type->entries[e].value.data = attribue_data;
1398 dest_type->entries[e].type_spec_flags = entry.type_spec_flags;
1399 }
1400 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001401 }
1402 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001403}
1404
1405void Theme::Dump() const {
1406 base::ScopedLogSeverity _log(base::INFO);
1407 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
1408
1409 for (int p = 0; p < packages_.size(); p++) {
1410 auto& package = packages_[p];
1411 if (package == nullptr) {
1412 continue;
1413 }
1414
1415 for (int t = 0; t < package->types.size(); t++) {
1416 auto& type = package->types[t];
1417 if (type == nullptr) {
1418 continue;
1419 }
1420
1421 for (int e = 0; e < type->entry_count; e++) {
1422 auto& entry = type->entries[e];
1423 if (entry.value.dataType == Res_value::TYPE_NULL &&
1424 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1425 continue;
1426 }
1427
1428 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1429 make_resid(p, t, e), entry.value.data,
1430 entry.value.dataType, entry.cookie);
1431 }
1432 }
1433 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001434}
1435
1436} // namespace android