blob: eaf452b5fa7110ce9ab94b977f4dbccb60a8508f [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>
Adam Lesinski0c405242017-01-13 20:47:26 -080025
Adam Lesinski7ad11102016-10-28 16:39:15 -070026#include "android-base/logging.h"
27#include "android-base/stringprintf.h"
Ryan Mitchell8a891d82019-07-01 09:48:23 -070028#include "androidfw/ResourceUtils.h"
Ryan Mitchell31b11052019-06-13 13:47:26 -070029#include "androidfw/Util.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070030#include "utils/ByteOrder.h"
31#include "utils/Trace.h"
32
33#ifdef _WIN32
34#ifdef ERROR
35#undef ERROR
36#endif
37#endif
38
39namespace android {
40
Adam Lesinskibebfcc42018-02-12 14:27:46 -080041struct FindEntryResult {
42 // A pointer to the resource table entry for this resource.
43 // If the size of the entry is > sizeof(ResTable_entry), it can be cast to
44 // a ResTable_map_entry and processed as a bag/map.
Ryan Mitchell8a891d82019-07-01 09:48:23 -070045 ResTable_entry_handle entry;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080046
47 // The configuration for which the resulting entry was defined. This is already swapped to host
48 // endianness.
49 ResTable_config config;
50
51 // The bitmask of configuration axis with which the resource value varies.
52 uint32_t type_flags;
53
54 // The dynamic package ID map for the package from which this resource came from.
55 const DynamicRefTable* dynamic_ref_table;
56
Ryan Mitchell8a891d82019-07-01 09:48:23 -070057 // The package name of the resource.
58 const std::string* package_name;
59
Adam Lesinskibebfcc42018-02-12 14:27:46 -080060 // The string pool reference to the type's name. This uses a different string pool than
61 // the global string pool, but this is hidden from the caller.
62 StringPoolRef type_string_ref;
63
64 // The string pool reference to the entry's name. This uses a different string pool than
65 // the global string pool, but this is hidden from the caller.
66 StringPoolRef entry_string_ref;
67};
68
Ryan Mitchellb894c272020-02-12 10:31:44 -080069AssetManager2::AssetManager2() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070070 memset(&configuration_, 0, sizeof(configuration_));
71}
Adam Lesinski7ad11102016-10-28 16:39:15 -070072
73bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets,
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020074 bool invalidate_caches, bool filter_incompatible_configs) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070075 apk_assets_ = apk_assets;
Adam Lesinskida431a22016-12-29 16:08:16 -050076 BuildDynamicRefTable();
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020077 RebuildFilterList(filter_incompatible_configs);
Adam Lesinski7ad11102016-10-28 16:39:15 -070078 if (invalidate_caches) {
79 InvalidateCaches(static_cast<uint32_t>(-1));
80 }
81 return true;
82}
83
Adam Lesinskida431a22016-12-29 16:08:16 -050084void AssetManager2::BuildDynamicRefTable() {
85 package_groups_.clear();
86 package_ids_.fill(0xff);
87
Ryan Mitchellb894c272020-02-12 10:31:44 -080088 // A mapping from apk assets path to the runtime package id of its first loaded package.
Ryan Mitchell8a891d82019-07-01 09:48:23 -070089 std::unordered_map<std::string, uint8_t> apk_assets_package_ids;
90
Ryan Mitchell824cc492020-02-12 10:48:14 -080091 // Overlay resources are not directly referenced by an application so their resource ids
92 // can change throughout the application's lifetime. Assign overlay package ids last.
93 std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
94 std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) {
95 return !a->IsOverlay();
96 });
97
98 // The assets cookie must map to the position of the apk assets in the unsorted apk assets list.
99 std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies;
100 apk_assets_cookies.reserve(apk_assets_.size());
101 for (size_t i = 0, n = apk_assets_.size(); i < n; i++) {
102 apk_assets_cookies[apk_assets_[i]] = static_cast<ApkAssetsCookie>(i);
103 }
104
Ryan Mitchellb894c272020-02-12 10:31:44 -0800105 // 0x01 is reserved for the android package.
106 int next_package_id = 0x02;
Ryan Mitchell824cc492020-02-12 10:48:14 -0800107 for (const ApkAssets* apk_assets : sorted_apk_assets) {
Ryan Mitchellb894c272020-02-12 10:31:44 -0800108 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
Ryan Mitchellb894c272020-02-12 10:31:44 -0800109 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
110 // Get the package ID or assign one if a shared library.
111 int package_id;
112 if (package->IsDynamic()) {
113 package_id = next_package_id++;
114 } else {
115 package_id = package->GetPackageId();
Adam Lesinskida431a22016-12-29 16:08:16 -0500116 }
117
118 // Add the mapping for package ID to index if not present.
119 uint8_t idx = package_ids_[package_id];
120 if (idx == 0xff) {
121 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
122 package_groups_.push_back({});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700123
124 if (apk_assets->IsOverlay()) {
125 // The target package must precede the overlay package in the apk assets paths in order
126 // to take effect.
127 const auto& loaded_idmap = apk_assets->GetLoadedIdmap();
128 auto target_package_iter = apk_assets_package_ids.find(loaded_idmap->TargetApkPath());
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700129 if (target_package_iter == apk_assets_package_ids.end()) {
130 LOG(INFO) << "failed to find target package for overlay "
131 << loaded_idmap->OverlayApkPath();
132 } else {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700133 const uint8_t target_package_id = target_package_iter->second;
134 const uint8_t target_idx = package_ids_[target_package_id];
135 CHECK(target_idx != 0xff) << "overlay added to apk_assets_package_ids but does not"
136 << " have an assigned package group";
137
138 PackageGroup& target_package_group = package_groups_[target_idx];
139
Ryan Mitchell824cc492020-02-12 10:48:14 -0800140 // Create a special dynamic reference table for the overlay to rewrite references to
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700141 // overlay resources as references to the target resources they overlay.
142 auto overlay_table = std::make_shared<OverlayDynamicRefTable>(
143 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
144 package_groups_.back().dynamic_ref_table = overlay_table;
145
146 // Add the overlay resource map to the target package's set of overlays.
147 target_package_group.overlays_.push_back(
148 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
149 overlay_table.get()),
Ryan Mitchell824cc492020-02-12 10:48:14 -0800150 apk_assets_cookies[apk_assets]});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700151 }
152 }
153
154 DynamicRefTable* ref_table = package_groups_.back().dynamic_ref_table.get();
155 ref_table->mAssignedPackageId = package_id;
156 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500157 }
158 PackageGroup* package_group = &package_groups_[idx];
159
160 // Add the package and to the set of packages with the same ID.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800161 package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
Ryan Mitchell824cc492020-02-12 10:48:14 -0800162 package_group->cookies_.push_back(apk_assets_cookies[apk_assets]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500163
164 // Add the package name -> build time ID mappings.
165 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
166 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700167 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500168 package_name, static_cast<uint8_t>(entry.package_id));
169 }
Ryan Mitchellb894c272020-02-12 10:31:44 -0800170
171 apk_assets_package_ids.insert(std::make_pair(apk_assets->GetPath(), package_id));
Adam Lesinskida431a22016-12-29 16:08:16 -0500172 }
173 }
174
175 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
176 const auto package_groups_end = package_groups_.end();
177 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800178 const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500179 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700180 iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()),
181 iter->dynamic_ref_table->mAssignedPackageId);
Adam Lesinskida431a22016-12-29 16:08:16 -0500182 }
183 }
184}
185
186void AssetManager2::DumpToLog() const {
187 base::ScopedLogSeverity _log(base::INFO);
188
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800189 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
190
Adam Lesinskida431a22016-12-29 16:08:16 -0500191 std::string list;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800192 for (const auto& apk_assets : apk_assets_) {
193 base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str());
194 }
195 LOG(INFO) << "ApkAssets: " << list;
196
197 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500198 for (size_t i = 0; i < package_ids_.size(); i++) {
199 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800200 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500201 }
202 }
203 LOG(INFO) << "Package ID map: " << list;
204
Adam Lesinski0dd36992018-01-25 15:38:38 -0800205 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800206 list = "";
207 for (const auto& package : package_group.packages_) {
208 const LoadedPackage* loaded_package = package.loaded_package_;
209 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
210 loaded_package->GetPackageId(),
211 (loaded_package->IsDynamic() ? " dynamic" : ""));
212 }
213 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700214 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800215 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800216
217 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700218 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800219 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700220 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800221 }
222 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500223 }
224}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700225
226const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
227 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
228 return nullptr;
229 }
230 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
231}
232
Adam Lesinskida431a22016-12-29 16:08:16 -0500233const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
234 if (package_id >= package_ids_.size()) {
235 return nullptr;
236 }
237
238 const size_t idx = package_ids_[package_id];
239 if (idx == 0xff) {
240 return nullptr;
241 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700242 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500243}
244
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700245std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
246 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800247 for (const PackageGroup& package_group : package_groups_) {
248 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
249 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700250 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800251 }
252 }
253 }
254 return nullptr;
255}
256
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100257const std::unordered_map<std::string, std::string>*
258 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
259
260 if (package_id >= package_ids_.size()) {
261 return nullptr;
262 }
263
264 const size_t idx = package_ids_[package_id];
265 if (idx == 0xff) {
266 return nullptr;
267 }
268
269 const PackageGroup& package_group = package_groups_[idx];
270 if (package_group.packages_.size() == 0) {
271 return nullptr;
272 }
273
274 const auto loaded_package = package_group.packages_[0].loaded_package_;
275 return &loaded_package->GetOverlayableMap();
276}
277
Ryan Mitchell2e394222019-08-28 12:10:51 -0700278bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name,
279 std::string* out) const {
280 uint8_t package_id = 0U;
281 for (const auto& apk_assets : apk_assets_) {
282 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
283 if (loaded_arsc == nullptr) {
284 continue;
285 }
286
287 const auto& loaded_packages = loaded_arsc->GetPackages();
288 if (loaded_packages.empty()) {
289 continue;
290 }
291
292 const auto& loaded_package = loaded_packages[0];
293 if (loaded_package->GetPackageName() == package_name) {
294 package_id = GetAssignedPackageId(loaded_package.get());
295 break;
296 }
297 }
298
299 if (package_id == 0U) {
300 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
301 return false;
302 }
303
304 const size_t idx = package_ids_[package_id];
305 if (idx == 0xff) {
306 return false;
307 }
308
309 std::string output;
310 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
311 const LoadedPackage* loaded_package = package.loaded_package_;
312 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
313 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
314 if (info != nullptr) {
315 ResourceName res_name;
316 if (!GetResourceName(*it, &res_name)) {
317 ANDROID_LOG(ERROR) << base::StringPrintf(
318 "Unable to retrieve name of overlayable resource 0x%08x", *it);
319 return false;
320 }
321
322 const std::string name = ToFormattedResourceString(&res_name);
323 output.append(base::StringPrintf(
324 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
325 name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags));
326 }
327 }
328 }
329
330 *out = std::move(output);
331 return true;
332}
333
Adam Lesinski7ad11102016-10-28 16:39:15 -0700334void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
335 const int diff = configuration_.diff(configuration);
336 configuration_ = configuration;
337
338 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800339 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700340 InvalidateCaches(static_cast<uint32_t>(diff));
341 }
342}
343
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700344std::set<std::string> AssetManager2::GetNonSystemOverlayPaths() const {
345 std::set<std::string> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800346 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800347 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800348 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700349 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800350 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700351 break;
352 }
353 }
354
355 if (!found_system_package) {
356 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
357 non_system_overlays.insert(apk_assets_[overlay.cookie]->GetPath());
358 }
359 }
360 }
361
362 return non_system_overlays;
363}
364
365std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system,
366 bool exclude_mipmap) const {
367 ATRACE_NAME("AssetManager::GetResourceConfigurations");
368 const auto non_system_overlays =
369 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
370
371 std::set<ResTable_config> configurations;
372 for (const PackageGroup& package_group : package_groups_) {
373 for (size_t i = 0; i < package_group.packages_.size(); i++) {
374 const ConfiguredPackage& package = package_group.packages_[i];
375 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800376 continue;
377 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800378
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700379 auto apk_assets = apk_assets_[package_group.cookies_[i]];
380 if (exclude_system && apk_assets->IsOverlay()
381 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
382 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800383 continue;
384 }
385
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800386 package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
Adam Lesinski0c405242017-01-13 20:47:26 -0800387 }
388 }
389 return configurations;
390}
391
392std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800393 bool merge_equivalent_languages) const {
394 ATRACE_NAME("AssetManager::GetResourceLocales");
Adam Lesinski0c405242017-01-13 20:47:26 -0800395 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700396 const auto non_system_overlays =
397 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
398
Adam Lesinski0c405242017-01-13 20:47:26 -0800399 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700400 for (size_t i = 0; i < package_group.packages_.size(); i++) {
401 const ConfiguredPackage& package = package_group.packages_[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800402 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800403 continue;
404 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800405
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700406 auto apk_assets = apk_assets_[package_group.cookies_[i]];
407 if (exclude_system && apk_assets->IsOverlay()
408 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
409 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800410 continue;
411 }
412
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800413 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800414 }
415 }
416 return locales;
417}
418
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800419std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
420 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700421 const std::string new_path = "assets/" + filename;
422 return OpenNonAsset(new_path, mode);
423}
424
425std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800426 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700427 const std::string new_path = "assets/" + filename;
428 return OpenNonAsset(new_path, cookie, mode);
429}
430
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800431std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
432 ATRACE_NAME("AssetManager::OpenDir");
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800433
434 std::string full_path = "assets/" + dirname;
435 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
436 util::make_unique<SortedVector<AssetDir::FileInfo>>();
437
438 // Start from the back.
439 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
440 const ApkAssets* apk_assets = *iter;
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100441 if (apk_assets->IsOverlay()) {
442 continue;
443 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800444
445 auto func = [&](const StringPiece& name, FileType type) {
446 AssetDir::FileInfo info;
447 info.setFileName(String8(name.data(), name.size()));
448 info.setFileType(type);
449 info.setSourceName(String8(apk_assets->GetPath().c_str()));
450 files->add(info);
451 };
452
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700453 if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800454 return {};
455 }
456 }
457
458 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
459 asset_dir->setFileList(files.release());
460 return asset_dir;
461}
462
Adam Lesinski7ad11102016-10-28 16:39:15 -0700463// Search in reverse because that's how we used to do it and we need to preserve behaviour.
464// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
465// is inconsistent for split APKs.
466std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
467 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800468 ApkAssetsCookie* out_cookie) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700469 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100470 // Prevent RRO from modifying assets and other entries accessed by file
471 // path. Explicitly asking for a path in a given package (denoted by a
472 // cookie) is still OK.
473 if (apk_assets_[i]->IsOverlay()) {
474 continue;
475 }
476
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700477 std::unique_ptr<Asset> asset = apk_assets_[i]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700478 if (asset) {
479 if (out_cookie != nullptr) {
480 *out_cookie = i;
481 }
482 return asset;
483 }
484 }
485
486 if (out_cookie != nullptr) {
487 *out_cookie = kInvalidCookie;
488 }
489 return {};
490}
491
492std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800493 ApkAssetsCookie cookie,
494 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700495 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
496 return {};
497 }
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700498 return apk_assets_[cookie]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700499}
500
501ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800502 bool /*stop_at_first_match*/,
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800503 bool ignore_configuration,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800504 FindEntryResult* out_entry) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700505 if (resource_resolution_logging_enabled_) {
506 // Clear the last logged resource resolution.
507 ResetResourceResolution();
508 last_resolution_.resid = resid;
509 }
510
Adam Lesinski7ad11102016-10-28 16:39:15 -0700511 // Might use this if density_override != 0.
512 ResTable_config density_override_config;
513
514 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800515 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700516 if (density_override != 0 && density_override != configuration_.density) {
517 density_override_config = configuration_;
518 density_override_config.density = density_override;
519 desired_config = &density_override_config;
520 }
521
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700522 // Retrieve the package group from the package id of the resource id.
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800523 if (!is_valid_resid(resid)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500524 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
525 return kInvalidCookie;
526 }
527
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800528 const uint32_t package_id = get_package_id(resid);
529 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800530 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700531 uint8_t package_idx = package_ids_[package_id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800532 if (package_idx == 0xff) {
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800533 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.",
534 package_id, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500535 return kInvalidCookie;
536 }
537
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800538 const PackageGroup& package_group = package_groups_[package_idx];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700539 ApkAssetsCookie cookie = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
540 false /* stop_at_first_match */,
541 ignore_configuration, out_entry);
542 if (UNLIKELY(cookie == kInvalidCookie)) {
543 return kInvalidCookie;
544 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800545
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700546 if (!apk_assets_[cookie]->IsLoader()) {
547 for (const auto& id_map : package_group.overlays_) {
548 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
549 if (!overlay_entry) {
550 // No id map entry exists for this target resource.
551 continue;
552 }
553
554 if (overlay_entry.IsTableEntry()) {
555 // The target resource is overlaid by an inline value not represented by a resource.
556 out_entry->entry = overlay_entry.GetTableEntry();
557 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
558 cookie = id_map.cookie;
559 continue;
560 }
561
562 FindEntryResult overlay_result;
563 ApkAssetsCookie overlay_cookie = FindEntry(overlay_entry.GetResourceId(), density_override,
564 false /* stop_at_first_match */,
565 ignore_configuration, &overlay_result);
566 if (UNLIKELY(overlay_cookie == kInvalidCookie)) {
567 continue;
568 }
569
570 if (!overlay_result.config.isBetterThan(out_entry->config, desired_config)
571 && overlay_result.config.compare(out_entry->config) != 0) {
572 // The configuration of the entry for the overlay must be equal to or better than the target
573 // configuration to be chosen as the better value.
574 continue;
575 }
576
577 cookie = overlay_cookie;
578 out_entry->entry = std::move(overlay_result.entry);
579 out_entry->config = overlay_result.config;
580 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
581 if (resource_resolution_logging_enabled_) {
582 last_resolution_.steps.push_back(
583 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result.config.toString(),
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700584 overlay_result.package_name});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700585 }
586 }
587 }
588
589 if (resource_resolution_logging_enabled_) {
590 last_resolution_.cookie = cookie;
591 last_resolution_.type_string_ref = out_entry->type_string_ref;
592 last_resolution_.entry_string_ref = out_entry->entry_string_ref;
593 }
594
595 return cookie;
596}
597
598ApkAssetsCookie AssetManager2::FindEntryInternal(const PackageGroup& package_group,
599 uint8_t type_idx, uint16_t entry_idx,
600 const ResTable_config& desired_config,
601 bool /*stop_at_first_match*/,
602 bool ignore_configuration,
603 FindEntryResult* out_entry) const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800604 ApkAssetsCookie best_cookie = kInvalidCookie;
605 const LoadedPackage* best_package = nullptr;
606 const ResTable_type* best_type = nullptr;
607 const ResTable_config* best_config = nullptr;
608 ResTable_config best_config_copy;
609 uint32_t best_offset = 0u;
610 uint32_t type_flags = 0u;
611
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700612 Resolution::Step::Type resolution_type = Resolution::Step::Type::NO_ENTRY;
Winson2f3669b2019-01-11 11:28:34 -0800613 std::vector<Resolution::Step> resolution_steps;
614
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800615 // If desired_config is the same as the set configuration, then we can use our filtered list
616 // and we don't need to match the configurations, since they already matched.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700617 const bool use_fast_path = !ignore_configuration && &desired_config == &configuration_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800618
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700619 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800620 for (size_t pi = 0; pi < package_count; pi++) {
621 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
622 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
623 ApkAssetsCookie cookie = package_group.cookies_[pi];
624
625 // If the type IDs are offset in this package, we need to take that into account when searching
626 // for a type.
627 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
628 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700629 continue;
630 }
631
Winson9947f1e2019-08-16 10:20:39 -0700632 // If the package is an overlay or custom loader,
633 // then even configurations that are the same MUST be chosen.
Winson9947f1e2019-08-16 10:20:39 -0700634 const bool package_is_loader = loaded_package->IsCustomLoader();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700635 type_flags |= type_spec->GetFlagsForEntryIndex(entry_idx);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800636
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800637 if (use_fast_path) {
Winson2f3669b2019-01-11 11:28:34 -0800638 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800639 const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations;
640 const size_t type_count = candidate_configs.size();
641 for (uint32_t i = 0; i < type_count; i++) {
642 const ResTable_config& this_config = candidate_configs[i];
643
644 // We can skip calling ResTable_config::match() because we know that all candidate
645 // configurations that do NOT match have been filtered-out.
Winson2f3669b2019-01-11 11:28:34 -0800646 if (best_config == nullptr) {
647 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700648 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
649 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
650 : Resolution::Step::Type::BETTER_MATCH;
651 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
652 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Winson2f3669b2019-01-11 11:28:34 -0800653 } else {
Winson9947f1e2019-08-16 10:20:39 -0700654 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700655 resolution_type = (package_is_loader) ? Resolution::Step::Type::SKIPPED_LOADER
656 : Resolution::Step::Type::SKIPPED;
Winson9947f1e2019-08-16 10:20:39 -0700657 resolution_steps.push_back(Resolution::Step{resolution_type,
658 this_config.toString(),
659 &loaded_package->GetPackageName()});
660 }
Winson2f3669b2019-01-11 11:28:34 -0800661 continue;
662 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800663
Winson2f3669b2019-01-11 11:28:34 -0800664 // The configuration matches and is better than the previous selection.
665 // Find the entry value if it exists for this configuration.
666 const ResTable_type* type = filtered_group.types[i];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700667 const uint32_t offset = LoadedPackage::GetEntryOffset(type, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800668 if (offset == ResTable_type::NO_ENTRY) {
Winson9947f1e2019-08-16 10:20:39 -0700669 if (resource_resolution_logging_enabled_) {
670 if (package_is_loader) {
671 resolution_type = Resolution::Step::Type::NO_ENTRY_LOADER;
672 } else {
673 resolution_type = Resolution::Step::Type::NO_ENTRY;
674 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700675 resolution_steps.push_back(Resolution::Step{resolution_type,
Winson9947f1e2019-08-16 10:20:39 -0700676 this_config.toString(),
677 &loaded_package->GetPackageName()});
678 }
Winson2f3669b2019-01-11 11:28:34 -0800679 continue;
680 }
681
682 best_cookie = cookie;
683 best_package = loaded_package;
684 best_type = type;
685 best_config = &this_config;
686 best_offset = offset;
687
688 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700689 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
690 this_config.toString(),
691 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800692 }
693 }
694 } else {
695 // This is the slower path, which doesn't use the filtered list of configurations.
696 // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness
697 // and fill in any new fields that did not exist when the APK was compiled.
698 // Furthermore when selecting configurations we can't just record the pointer to the
699 // ResTable_config, we must copy it.
700 const auto iter_end = type_spec->types + type_spec->type_count;
701 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800702 ResTable_config this_config{};
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800703
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800704 if (!ignore_configuration) {
705 this_config.copyFromDtoH((*iter)->config);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700706 if (!this_config.match(desired_config)) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800707 continue;
708 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800709
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800710 if (best_config == nullptr) {
711 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700712 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
713 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
714 : Resolution::Step::Type::BETTER_MATCH;
715 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
716 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800717 } else {
718 continue;
719 }
Winson2f3669b2019-01-11 11:28:34 -0800720 }
721
722 // The configuration matches and is better than the previous selection.
723 // Find the entry value if it exists for this configuration.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700724 const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800725 if (offset == ResTable_type::NO_ENTRY) {
726 continue;
727 }
728
729 best_cookie = cookie;
730 best_package = loaded_package;
731 best_type = *iter;
732 best_config_copy = this_config;
733 best_config = &best_config_copy;
734 best_offset = offset;
735
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800736 if (ignore_configuration) {
737 // Any configuration will suffice, so break.
738 break;
739 }
740
Winson2f3669b2019-01-11 11:28:34 -0800741 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700742 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
743 this_config.toString(),
744 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800745 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700746 }
747 }
748 }
749
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800750 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700751 return kInvalidCookie;
752 }
753
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800754 const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
755 if (UNLIKELY(best_entry == nullptr)) {
756 return kInvalidCookie;
757 }
758
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700759 out_entry->entry = ResTable_entry_handle::unmanaged(best_entry);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800760 out_entry->config = *best_config;
761 out_entry->type_flags = type_flags;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700762 out_entry->package_name = &best_package->GetPackageName();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800763 out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1);
764 out_entry->entry_string_ref =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700765 StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index);
766 out_entry->dynamic_ref_table = package_group.dynamic_ref_table.get();
Winson2f3669b2019-01-11 11:28:34 -0800767
Adam Lesinskida431a22016-12-29 16:08:16 -0500768 return best_cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700769}
770
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700771void AssetManager2::ResetResourceResolution() const {
772 last_resolution_.cookie = kInvalidCookie;
773 last_resolution_.resid = 0;
774 last_resolution_.steps.clear();
775 last_resolution_.type_string_ref = StringPoolRef();
776 last_resolution_.entry_string_ref = StringPoolRef();
777}
778
Winson2f3669b2019-01-11 11:28:34 -0800779void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
780 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800781 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700782 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800783 }
784}
785
786std::string AssetManager2::GetLastResourceResolution() const {
787 if (!resource_resolution_logging_enabled_) {
788 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
789 return std::string();
790 }
791
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700792 auto cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800793 if (cookie == kInvalidCookie) {
794 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
795 return std::string();
796 }
797
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700798 uint32_t resid = last_resolution_.resid;
799 std::vector<Resolution::Step>& steps = last_resolution_.steps;
Winson2f3669b2019-01-11 11:28:34 -0800800
801 ResourceName resource_name;
802 std::string resource_name_string;
803
804 const LoadedPackage* package =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700805 apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
Winson2f3669b2019-01-11 11:28:34 -0800806
807 if (package != nullptr) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700808 ToResourceName(last_resolution_.type_string_ref,
809 last_resolution_.entry_string_ref,
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800810 package->GetPackageName(),
Winson2f3669b2019-01-11 11:28:34 -0800811 &resource_name);
812 resource_name_string = ToFormattedResourceString(&resource_name);
813 }
814
815 std::stringstream log_stream;
816 log_stream << base::StringPrintf("Resolution for 0x%08x ", resid)
817 << resource_name_string
818 << "\n\tFor config -"
819 << configuration_.toString();
820
821 std::string prefix;
822 for (Resolution::Step step : steps) {
823 switch (step.type) {
824 case Resolution::Step::Type::INITIAL:
825 prefix = "Found initial";
826 break;
827 case Resolution::Step::Type::BETTER_MATCH:
828 prefix = "Found better";
829 break;
Winson9947f1e2019-08-16 10:20:39 -0700830 case Resolution::Step::Type::BETTER_MATCH_LOADER:
831 prefix = "Found better in loader";
832 break;
Winson2f3669b2019-01-11 11:28:34 -0800833 case Resolution::Step::Type::OVERLAID:
834 prefix = "Overlaid";
835 break;
Winson9947f1e2019-08-16 10:20:39 -0700836 case Resolution::Step::Type::OVERLAID_LOADER:
837 prefix = "Overlaid by loader";
838 break;
839 case Resolution::Step::Type::SKIPPED:
840 prefix = "Skipped";
841 break;
842 case Resolution::Step::Type::SKIPPED_LOADER:
843 prefix = "Skipped loader";
844 break;
845 case Resolution::Step::Type::NO_ENTRY:
846 prefix = "No entry";
847 break;
848 case Resolution::Step::Type::NO_ENTRY_LOADER:
849 prefix = "No entry for loader";
850 break;
Winson2f3669b2019-01-11 11:28:34 -0800851 }
852
853 if (!prefix.empty()) {
854 log_stream << "\n\t" << prefix << ": " << *step.package_name;
855
856 if (!step.config_name.isEmpty()) {
857 log_stream << " -" << step.config_name;
858 }
859 }
860 }
861
862 return log_stream.str();
863}
864
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800865bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700866 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800867 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
868 true /* stop_at_first_match */,
869 true /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700870 if (cookie == kInvalidCookie) {
871 return false;
872 }
873
Winson2f3669b2019-01-11 11:28:34 -0800874 return ToResourceName(entry.type_string_ref,
875 entry.entry_string_ref,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700876 *entry.package_name,
Winson2f3669b2019-01-11 11:28:34 -0800877 out_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700878}
879
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800880bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700881 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800882 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
883 false /* stop_at_first_match */,
884 true /* ignore_configuration */, &entry);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700885 if (cookie != kInvalidCookie) {
886 *out_flags = entry.type_flags;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800887 return true;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700888 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800889 return false;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700890}
891
892ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
893 uint16_t density_override, Res_value* out_value,
894 ResTable_config* out_selected_config,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800895 uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700896 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800897 ApkAssetsCookie cookie = FindEntry(resid, density_override, false /* stop_at_first_match */,
898 false /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700899 if (cookie == kInvalidCookie) {
900 return kInvalidCookie;
901 }
902
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700903 const ResTable_entry* table_entry = *entry.entry;
904 if (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700905 if (!may_be_bag) {
906 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800907 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700908 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800909
910 // Create a reference since we can't represent this complex type as a Res_value.
911 out_value->dataType = Res_value::TYPE_REFERENCE;
912 out_value->data = resid;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800913 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700914 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800915 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700916 }
917
918 const Res_value* device_value = reinterpret_cast<const Res_value*>(
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700919 reinterpret_cast<const uint8_t*>(table_entry) + dtohs(table_entry->size));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700920 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500921
922 // Convert the package ID to the runtime assigned package ID.
923 entry.dynamic_ref_table->lookupResourceValue(out_value);
924
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800925 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700926 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700927 return cookie;
928}
929
Adam Lesinski0c405242017-01-13 20:47:26 -0800930ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
931 ResTable_config* in_out_selected_config,
932 uint32_t* in_out_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800933 uint32_t* out_last_reference) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800934 constexpr const int kMaxIterations = 20;
935
Adam Lesinski0c405242017-01-13 20:47:26 -0800936 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
937 in_out_value->data != 0u && iteration < kMaxIterations;
938 iteration++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800939 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800940 uint32_t new_flags = 0u;
941 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
942 in_out_value, in_out_selected_config, &new_flags);
943 if (cookie == kInvalidCookie) {
944 return kInvalidCookie;
945 }
946 if (in_out_flags != nullptr) {
947 *in_out_flags |= new_flags;
948 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800949 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800950 // This reference can't be resolved, so exit now and let the caller deal with it.
951 return cookie;
952 }
953 }
954 return cookie;
955}
956
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800957const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) {
958 auto cached_iter = cached_bag_resid_stacks_.find(resid);
959 if (cached_iter != cached_bag_resid_stacks_.end()) {
960 return cached_iter->second;
961 } else {
962 auto found_resids = std::vector<uint32_t>();
963 GetBag(resid, found_resids);
964 // Cache style stacks if they are not already cached.
965 cached_bag_resid_stacks_[resid] = found_resids;
966 return found_resids;
967 }
968}
969
Adam Lesinski7ad11102016-10-28 16:39:15 -0700970const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
y57cd1952018-04-12 14:26:23 -0700971 auto found_resids = std::vector<uint32_t>();
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800972 auto bag = GetBag(resid, found_resids);
973
974 // Cache style stacks if they are not already cached.
975 auto cached_iter = cached_bag_resid_stacks_.find(resid);
976 if (cached_iter == cached_bag_resid_stacks_.end()) {
977 cached_bag_resid_stacks_[resid] = found_resids;
978 }
979 return bag;
y57cd1952018-04-12 14:26:23 -0700980}
981
Ryan Mitchell155d5392020-02-10 13:35:24 -0800982static bool compare_bag_entries(const ResolvedBag::Entry& entry1,
983 const ResolvedBag::Entry& entry2) {
984 return entry1.key < entry2.key;
985}
986
y57cd1952018-04-12 14:26:23 -0700987const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700988 auto cached_iter = cached_bags_.find(resid);
989 if (cached_iter != cached_bags_.end()) {
990 return cached_iter->second.get();
991 }
992
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700993 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800994 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
995 false /* stop_at_first_match */,
996 false /* ignore_configuration */,
997 &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700998 if (cookie == kInvalidCookie) {
999 return nullptr;
1000 }
1001
1002 // Check that the size of the entry header is at least as big as
1003 // the desired ResTable_map_entry. Also verify that the entry
1004 // was intended to be a map.
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001005 const ResTable_entry* table_entry = *entry.entry;
1006 if (dtohs(table_entry->size) < sizeof(ResTable_map_entry) ||
1007 (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001008 // Not a bag, nothing to do.
1009 return nullptr;
1010 }
1011
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001012 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(table_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001013 const ResTable_map* map_entry =
1014 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
1015 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
1016
y57cd1952018-04-12 14:26:23 -07001017 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
1018 // dependencies between bags
1019 child_resids.push_back(resid);
1020
Adam Lesinskida431a22016-12-29 16:08:16 -05001021 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell155d5392020-02-10 13:35:24 -08001022 if (parent_resid == 0U || std::find(child_resids.begin(), child_resids.end(), parent_resid)
y57cd1952018-04-12 14:26:23 -07001023 != child_resids.end()) {
Ryan Mitchell155d5392020-02-10 13:35:24 -08001024 // There is no parent or a circular dependency exist, meaning there is nothing to inherit and
1025 // we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001026 const size_t entry_count = map_entry_end - map_entry;
1027 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1028 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001029
1030 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001031 ResolvedBag::Entry* new_entry = new_bag->entries;
1032 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001033 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001034 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001035 // Attributes, arrays, etc don't have a resource id as the name. They specify
1036 // other data, which would be wrong to change via a lookup.
1037 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001038 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1039 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001040 return nullptr;
1041 }
1042 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001043 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001044 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001045 new_entry->key_pool = nullptr;
1046 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001047 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001048 new_entry->value.copyFrom_dtoh(map_entry->value);
1049 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1050 if (err != NO_ERROR) {
1051 LOG(ERROR) << base::StringPrintf(
1052 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1053 new_entry->value.data, new_key);
1054 return nullptr;
1055 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001056 sort_entries = sort_entries ||
1057 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001058 ++new_entry;
1059 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001060
1061 if (sort_entries) {
1062 std::sort(new_bag->entries, new_bag->entries + entry_count, compare_bag_entries);
1063 }
1064
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001065 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001066 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1067 ResolvedBag* result = new_bag.get();
1068 cached_bags_[resid] = std::move(new_bag);
1069 return result;
1070 }
1071
Adam Lesinskida431a22016-12-29 16:08:16 -05001072 // In case the parent is a dynamic reference, resolve it.
1073 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
1074
Adam Lesinski7ad11102016-10-28 16:39:15 -07001075 // Get the parent and do a merge of the keys.
y57cd1952018-04-12 14:26:23 -07001076 const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001077 if (parent_bag == nullptr) {
1078 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001079 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1080 resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001081 return nullptr;
1082 }
1083
Adam Lesinski7ad11102016-10-28 16:39:15 -07001084 // Create the max possible entries we can make. Once we construct the bag,
1085 // we will realloc to fit to size.
1086 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001087 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1088 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001089 ResolvedBag::Entry* new_entry = new_bag->entries;
1090
1091 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
1092 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
1093
1094 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001095 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001096 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001097 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001098 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001099 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001100 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1101 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001102 return nullptr;
1103 }
1104 }
1105
Adam Lesinski7ad11102016-10-28 16:39:15 -07001106 if (child_key <= parent_entry->key) {
1107 // Use the child key if it comes before the parent
1108 // or is equal to the parent (overrides).
1109 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001110 new_entry->key = child_key;
1111 new_entry->key_pool = nullptr;
1112 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001113 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001114 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001115 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1116 if (err != NO_ERROR) {
1117 LOG(ERROR) << base::StringPrintf(
1118 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1119 new_entry->value.data, child_key);
1120 return nullptr;
1121 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001122 ++map_entry;
1123 } else {
1124 // Take the parent entry as-is.
1125 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1126 }
1127
Ryan Mitchell155d5392020-02-10 13:35:24 -08001128 sort_entries = sort_entries ||
1129 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001130 if (child_key >= parent_entry->key) {
1131 // Move to the next parent entry if we used it or it was overridden.
1132 ++parent_entry;
1133 }
1134 // Increment to the next entry to fill.
1135 ++new_entry;
1136 }
1137
1138 // Finish the child entries if they exist.
1139 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001140 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001141 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001142 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001143 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1144 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001145 return nullptr;
1146 }
1147 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001148 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001149 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001150 new_entry->key_pool = nullptr;
1151 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001152 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001153 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001154 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1155 if (err != NO_ERROR) {
1156 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1157 new_entry->value.dataType, new_entry->value.data, new_key);
1158 return nullptr;
1159 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001160 sort_entries = sort_entries ||
1161 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001162 ++map_entry;
1163 ++new_entry;
1164 }
1165
1166 // Finish the parent entries if they exist.
1167 if (parent_entry != parent_entry_end) {
1168 // Take the rest of the parent entries as-is.
1169 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1170 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1171 new_entry += num_entries_to_copy;
1172 }
1173
1174 // Resize the resulting array to fit.
1175 const size_t actual_count = new_entry - new_bag->entries;
1176 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001177 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1178 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001179 }
1180
Ryan Mitchell155d5392020-02-10 13:35:24 -08001181 if (sort_entries) {
1182 std::sort(new_bag->entries, new_bag->entries + actual_count, compare_bag_entries);
1183 }
1184
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001185 // Combine flags from the parent and our own bag.
1186 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001187 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1188 ResolvedBag* result = new_bag.get();
1189 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001190 return result;
1191}
1192
Adam Lesinski929d6512017-01-16 19:11:19 -08001193static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
1194 ssize_t len =
1195 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1196 if (len < 0) {
1197 return false;
1198 }
1199 out->resize(static_cast<size_t>(len));
1200 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1201 static_cast<size_t>(len + 1));
1202 return true;
1203}
1204
Adam Lesinski0c405242017-01-13 20:47:26 -08001205uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
1206 const std::string& fallback_type,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001207 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001208 StringPiece package_name, type, entry;
1209 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
1210 return 0u;
1211 }
1212
1213 if (entry.empty()) {
1214 return 0u;
1215 }
1216
1217 if (package_name.empty()) {
1218 package_name = fallback_package;
1219 }
1220
1221 if (type.empty()) {
1222 type = fallback_type;
1223 }
1224
1225 std::u16string type16;
1226 if (!Utf8ToUtf16(type, &type16)) {
1227 return 0u;
1228 }
1229
1230 std::u16string entry16;
1231 if (!Utf8ToUtf16(entry, &entry16)) {
1232 return 0u;
1233 }
1234
1235 const StringPiece16 kAttr16 = u"attr";
1236 const static std::u16string kAttrPrivate16 = u"^attr-private";
1237
1238 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001239 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1240 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001241 if (package_name != package->GetPackageName()) {
1242 // All packages in the same group are expected to have the same package name.
1243 break;
1244 }
1245
1246 uint32_t resid = package->FindEntryByName(type16, entry16);
1247 if (resid == 0u && kAttr16 == type16) {
1248 // Private attributes in libraries (such as the framework) are sometimes encoded
1249 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1250 // free for future additions. Check '^attr-private' for the same name.
1251 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1252 }
1253
1254 if (resid != 0u) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001255 return fix_package_id(resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001256 }
1257 }
1258 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001259 return 0u;
1260}
1261
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001262void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001263 for (PackageGroup& group : package_groups_) {
1264 for (ConfiguredPackage& impl : group.packages_) {
1265 // Destroy it.
1266 impl.filtered_configs_.~ByteBucketArray();
1267
1268 // Re-create it.
1269 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
1270
1271 // Create the filters here.
1272 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) {
1273 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index);
1274 const auto iter_end = spec->types + spec->type_count;
1275 for (auto iter = spec->types; iter != iter_end; ++iter) {
1276 ResTable_config this_config;
1277 this_config.copyFromDtoH((*iter)->config);
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001278 if (!filter_incompatible_configs || this_config.match(configuration_)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001279 group.configurations.push_back(this_config);
1280 group.types.push_back(*iter);
1281 }
1282 }
1283 });
1284 }
1285 }
1286}
1287
Adam Lesinski7ad11102016-10-28 16:39:15 -07001288void AssetManager2::InvalidateCaches(uint32_t diff) {
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001289 cached_bag_resid_stacks_.clear();
1290
Adam Lesinski7ad11102016-10-28 16:39:15 -07001291 if (diff == 0xffffffffu) {
1292 // Everything must go.
1293 cached_bags_.clear();
1294 return;
1295 }
1296
1297 // Be more conservative with what gets purged. Only if the bag has other possible
1298 // variations with respect to what changed (diff) should we remove it.
1299 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1300 if (diff & iter->second->type_spec_flags) {
1301 iter = cached_bags_.erase(iter);
1302 } else {
1303 ++iter;
1304 }
1305 }
1306}
1307
Ryan Mitchell2e394222019-08-28 12:10:51 -07001308uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001309 for (auto& package_group : package_groups_) {
1310 for (auto& package2 : package_group.packages_) {
1311 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001312 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001313 }
1314 }
1315 }
1316 return 0;
1317}
1318
Adam Lesinski30080e22017-10-16 16:18:09 -07001319std::unique_ptr<Theme> AssetManager2::NewTheme() {
1320 return std::unique_ptr<Theme>(new Theme(this));
1321}
1322
1323Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1324}
1325
1326Theme::~Theme() = default;
1327
1328namespace {
1329
1330struct ThemeEntry {
1331 ApkAssetsCookie cookie;
1332 uint32_t type_spec_flags;
1333 Res_value value;
1334};
1335
1336struct ThemeType {
1337 int entry_count;
1338 ThemeEntry entries[0];
1339};
1340
1341constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
1342
1343} // namespace
1344
1345struct Theme::Package {
1346 // Each element of Type will be a dynamically sized object
1347 // allocated to have the entries stored contiguously with the Type.
1348 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
1349};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001350
1351bool Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001352 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001353
1354 const ResolvedBag* bag = asset_manager_->GetBag(resid);
1355 if (bag == nullptr) {
1356 return false;
1357 }
1358
1359 // Merge the flags from this style.
1360 type_spec_flags_ |= bag->type_spec_flags;
1361
Adam Lesinski30080e22017-10-16 16:18:09 -07001362 int last_type_idx = -1;
1363 int last_package_idx = -1;
1364 Package* last_package = nullptr;
1365 ThemeType* last_type = nullptr;
1366
1367 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
1368 // need to perform one resize per type.
1369 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
1370 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
1371 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001372 const uint32_t attr_resid = bag_iter->key;
1373
Adam Lesinski30080e22017-10-16 16:18:09 -07001374 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1375 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -08001376 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001377 return false;
1378 }
1379
Adam Lesinski30080e22017-10-16 16:18:09 -07001380 // We don't use the 0-based index for the type so that we can avoid doing ID validation
1381 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
1382 // the construction of this type is guarded with a resource ID check, it will never be
1383 // populated, and querying type ID 0 will always fail.
1384 const int package_idx = get_package_id(attr_resid);
1385 const int type_idx = get_type_id(attr_resid);
1386 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001387
Adam Lesinski30080e22017-10-16 16:18:09 -07001388 if (last_package_idx != package_idx) {
1389 std::unique_ptr<Package>& package = packages_[package_idx];
1390 if (package == nullptr) {
1391 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001392 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001393 last_package_idx = package_idx;
1394 last_package = package.get();
1395 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001396 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001397
1398 if (last_type_idx != type_idx) {
1399 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
1400 if (type == nullptr) {
1401 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
1402 // a sorted list of attributes, this shouldn't be resized again during this method call.
1403 type.reset(reinterpret_cast<ThemeType*>(
1404 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
1405 type->entry_count = entry_idx + 1;
1406 } else if (entry_idx >= type->entry_count) {
1407 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
1408 // a sorted list of attributes, this shouldn't be resized again during this method call.
1409 const int new_count = entry_idx + 1;
1410 type.reset(reinterpret_cast<ThemeType*>(
1411 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
1412
1413 // Clear out the newly allocated space (which isn't zeroed).
1414 memset(type->entries + type->entry_count, 0,
1415 (new_count - type->entry_count) * sizeof(ThemeEntry));
1416 type->entry_count = new_count;
1417 }
1418 last_type_idx = type_idx;
1419 last_type = type.get();
1420 }
1421
1422 ThemeEntry& entry = last_type->entries[entry_idx];
1423 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
1424 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001425 entry.cookie = bag_iter->cookie;
1426 entry.type_spec_flags |= bag->type_spec_flags;
1427 entry.value = bag_iter->value;
1428 }
1429 }
1430 return true;
1431}
1432
1433ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
1434 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -07001435 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001436
1437 uint32_t type_spec_flags = 0u;
1438
Adam Lesinski30080e22017-10-16 16:18:09 -07001439 do {
1440 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001441 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -07001442 if (package != nullptr) {
1443 // The themes are constructed with a 1-based type ID, so no need to decrement here.
1444 const int type_idx = get_type_id(resid);
1445 const ThemeType* type = package->types[type_idx].get();
1446 if (type != nullptr) {
1447 const int entry_idx = get_entry_id(resid);
1448 if (entry_idx < type->entry_count) {
1449 const ThemeEntry& entry = type->entries[entry_idx];
1450 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001451
Adam Lesinski30080e22017-10-16 16:18:09 -07001452 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
1453 if (cnt > 0) {
1454 cnt--;
1455 resid = entry.value.data;
1456 continue;
1457 }
1458 return kInvalidCookie;
1459 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001460
Adam Lesinski30080e22017-10-16 16:18:09 -07001461 // @null is different than @empty.
1462 if (entry.value.dataType == Res_value::TYPE_NULL &&
1463 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1464 return kInvalidCookie;
1465 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001466
Adam Lesinski30080e22017-10-16 16:18:09 -07001467 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -05001468 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -07001469 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001470 }
Adam Lesinskida431a22016-12-29 16:08:16 -05001471 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001472 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001473 break;
1474 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001475 return kInvalidCookie;
1476}
1477
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001478ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
1479 ResTable_config* in_out_selected_config,
1480 uint32_t* in_out_type_spec_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001481 uint32_t* out_last_ref) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001482 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
1483 uint32_t new_flags;
1484 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
1485 if (cookie == kInvalidCookie) {
1486 return kInvalidCookie;
1487 }
1488
1489 if (in_out_type_spec_flags != nullptr) {
1490 *in_out_type_spec_flags |= new_flags;
1491 }
1492 }
1493 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
1494 in_out_type_spec_flags, out_last_ref);
1495}
1496
Adam Lesinski7ad11102016-10-28 16:39:15 -07001497void Theme::Clear() {
1498 type_spec_flags_ = 0u;
1499 for (std::unique_ptr<Package>& package : packages_) {
1500 package.reset();
1501 }
1502}
1503
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001504void Theme::SetTo(const Theme& o) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001505 if (this == &o) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001506 return;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001507 }
1508
Adam Lesinski7ad11102016-10-28 16:39:15 -07001509 type_spec_flags_ = o.type_spec_flags_;
1510
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001511 if (asset_manager_ == o.asset_manager_) {
1512 // The theme comes from the same asset manager so all theme data can be copied exactly
1513 for (size_t p = 0; p < packages_.size(); p++) {
1514 const Package *package = o.packages_[p].get();
1515 if (package == nullptr) {
1516 // The other theme doesn't have this package, clear ours.
1517 packages_[p].reset();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001518 continue;
1519 }
1520
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001521 if (packages_[p] == nullptr) {
1522 // The other theme has this package, but we don't. Make one.
1523 packages_[p].reset(new Package());
1524 }
1525
1526 for (size_t t = 0; t < package->types.size(); t++) {
1527 const ThemeType *type = package->types[t].get();
1528 if (type == nullptr) {
1529 // The other theme doesn't have this type, clear ours.
1530 packages_[p]->types[t].reset();
1531 continue;
1532 }
1533
1534 // Create a new type and update it to theirs.
1535 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
1536 void *copied_data = malloc(type_alloc_size);
1537 memcpy(copied_data, type, type_alloc_size);
1538 packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data));
1539 }
1540 }
1541 } else {
1542 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1543 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1544 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1545
Ryan Mitchell93bca972019-03-08 17:26:28 -08001546 // Determine which ApkAssets are loaded in both theme AssetManagers.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001547 std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets();
1548 for (size_t i = 0; i < src_assets.size(); i++) {
1549 const ApkAssets* src_asset = src_assets[i];
1550
1551 std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets();
1552 for (size_t j = 0; j < dest_assets.size(); j++) {
1553 const ApkAssets* dest_asset = dest_assets[j];
1554
Ryan Mitchell93bca972019-03-08 17:26:28 -08001555 // Map the runtime package of the source apk asset to the destination apk asset.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001556 if (src_asset->GetPath() == dest_asset->GetPath()) {
1557 const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages =
1558 src_asset->GetLoadedArsc()->GetPackages();
1559 const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages =
1560 dest_asset->GetLoadedArsc()->GetPackages();
1561
1562 SourceToDestinationRuntimePackageMap package_map;
1563
1564 // The source and destination package should have the same number of packages loaded in
1565 // the same order.
1566 const size_t N = src_packages.size();
1567 CHECK(N == dest_packages.size())
1568 << " LoadedArsc " << src_asset->GetPath() << " differs number of packages.";
1569 for (size_t p = 0; p < N; p++) {
1570 auto& src_package = src_packages[p];
1571 auto& dest_package = dest_packages[p];
1572 CHECK(src_package->GetPackageName() == dest_package->GetPackageName())
1573 << " Package " << src_package->GetPackageName() << " differs in load order.";
1574
1575 int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get());
1576 int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get());
1577 package_map[src_package_id] = dest_package_id;
1578 }
1579
Ryan Mitchell93bca972019-03-08 17:26:28 -08001580 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
1581 src_asset_cookie_id_map.insert(std::make_pair(i, package_map));
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001582 break;
1583 }
1584 }
1585 }
1586
Ryan Mitchell93bca972019-03-08 17:26:28 -08001587 // Reset the data in the destination theme.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001588 for (size_t p = 0; p < packages_.size(); p++) {
1589 if (packages_[p] != nullptr) {
1590 packages_[p].reset();
1591 }
1592 }
1593
1594 for (size_t p = 0; p < packages_.size(); p++) {
1595 const Package *package = o.packages_[p].get();
1596 if (package == nullptr) {
1597 continue;
1598 }
1599
1600 for (size_t t = 0; t < package->types.size(); t++) {
1601 const ThemeType *type = package->types[t].get();
1602 if (type == nullptr) {
1603 continue;
1604 }
1605
1606 for (size_t e = 0; e < type->entry_count; e++) {
1607 const ThemeEntry &entry = type->entries[e];
1608 if (entry.value.dataType == Res_value::TYPE_NULL &&
1609 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1610 continue;
1611 }
1612
Ryan Mitchell93bca972019-03-08 17:26:28 -08001613 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1614 || entry.value.dataType == Res_value::TYPE_REFERENCE
1615 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1616 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1617 && entry.value.data != 0x0;
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001618
Ryan Mitchell93bca972019-03-08 17:26:28 -08001619 // If the attribute value represents an attribute or reference, the package id of the
1620 // value needs to be rewritten to the package id of the value in the destination.
1621 uint32_t attribute_data = entry.value.data;
1622 if (is_reference) {
1623 // Determine the package id of the reference in the destination AssetManager.
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001624 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1625 if (value_package_map == src_asset_cookie_id_map.end()) {
1626 continue;
1627 }
1628
1629 auto value_dest_package = value_package_map->second.find(
1630 get_package_id(entry.value.data));
1631 if (value_dest_package == value_package_map->second.end()) {
1632 continue;
1633 }
1634
Ryan Mitchell93bca972019-03-08 17:26:28 -08001635 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1636 }
1637
1638 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1639 // destination, only copy resources that do not reference resources in the source.
1640 ApkAssetsCookie data_dest_cookie;
1641 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1642 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1643 data_dest_cookie = value_dest_cookie->second;
1644 } else {
1645 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1646 continue;
1647 } else {
1648 data_dest_cookie = 0x0;
1649 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001650 }
1651
1652 // The package id of the attribute needs to be rewritten to the package id of the
Ryan Mitchell93bca972019-03-08 17:26:28 -08001653 // attribute in the destination.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001654 int attribute_dest_package_id = p;
1655 if (attribute_dest_package_id != 0x01) {
Ryan Mitchell93bca972019-03-08 17:26:28 -08001656 // Find the cookie of the attribute resource id in the source AssetManager
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001657 FindEntryResult attribute_entry_result;
1658 ApkAssetsCookie attribute_cookie =
Ryan Mitchella55dc2e2019-01-24 10:58:23 -08001659 o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ ,
1660 true /* stop_at_first_match */,
1661 true /* ignore_configuration */,
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001662 &attribute_entry_result);
1663
Ryan Mitchell93bca972019-03-08 17:26:28 -08001664 // Determine the package id of the attribute in the destination AssetManager.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001665 auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie);
1666 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1667 continue;
1668 }
1669 auto attribute_dest_package = attribute_package_map->second.find(
1670 attribute_dest_package_id);
1671 if (attribute_dest_package == attribute_package_map->second.end()) {
1672 continue;
1673 }
1674 attribute_dest_package_id = attribute_dest_package->second;
1675 }
1676
Ryan Mitchell93bca972019-03-08 17:26:28 -08001677 // Lazily instantiate the destination package.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001678 std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id];
1679 if (dest_package == nullptr) {
1680 dest_package.reset(new Package());
1681 }
1682
Ryan Mitchell93bca972019-03-08 17:26:28 -08001683 // Lazily instantiate and resize the destination type.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001684 util::unique_cptr<ThemeType>& dest_type = dest_package->types[t];
1685 if (dest_type == nullptr || dest_type->entry_count < type->entry_count) {
1686 const size_t type_alloc_size = sizeof(ThemeType)
1687 + (type->entry_count * sizeof(ThemeEntry));
1688 void* dest_data = malloc(type_alloc_size);
1689 memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry));
1690
Ryan Mitchell93bca972019-03-08 17:26:28 -08001691 // Copy the existing destination type values if the type is resized.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001692 if (dest_type != nullptr) {
1693 memcpy(dest_data, type, sizeof(ThemeType)
1694 + (dest_type->entry_count * sizeof(ThemeEntry)));
1695 }
1696
1697 dest_type.reset(reinterpret_cast<ThemeType *>(dest_data));
1698 dest_type->entry_count = type->entry_count;
1699 }
1700
Ryan Mitchell93bca972019-03-08 17:26:28 -08001701 dest_type->entries[e].cookie = data_dest_cookie;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001702 dest_type->entries[e].value.dataType = entry.value.dataType;
Ryan Mitchell93bca972019-03-08 17:26:28 -08001703 dest_type->entries[e].value.data = attribute_data;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001704 dest_type->entries[e].type_spec_flags = entry.type_spec_flags;
1705 }
1706 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001707 }
1708 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001709}
1710
1711void Theme::Dump() const {
1712 base::ScopedLogSeverity _log(base::INFO);
1713 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
1714
1715 for (int p = 0; p < packages_.size(); p++) {
1716 auto& package = packages_[p];
1717 if (package == nullptr) {
1718 continue;
1719 }
1720
1721 for (int t = 0; t < package->types.size(); t++) {
1722 auto& type = package->types[t];
1723 if (type == nullptr) {
1724 continue;
1725 }
1726
1727 for (int e = 0; e < type->entry_count; e++) {
1728 auto& entry = type->entries[e];
1729 if (entry.value.dataType == Res_value::TYPE_NULL &&
1730 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1731 continue;
1732 }
1733
1734 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1735 make_resid(p, t, e), entry.value.data,
1736 entry.value.dataType, entry.cookie);
1737 }
1738 }
1739 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001740}
1741
1742} // namespace android