blob: 803828fc16c574f55405f966e78aa5a8bd897fdf [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 Mitchellb894c272020-02-12 10:31:44 -080091 // 0x01 is reserved for the android package.
92 int next_package_id = 0x02;
93 const size_t apk_assets_count = apk_assets_.size();
94 for (size_t i = 0; i < apk_assets_count; i++) {
95 const ApkAssets* apk_assets = apk_assets_[i];
96 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
97
98 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
99 // Get the package ID or assign one if a shared library.
100 int package_id;
101 if (package->IsDynamic()) {
102 package_id = next_package_id++;
103 } else {
104 package_id = package->GetPackageId();
Adam Lesinskida431a22016-12-29 16:08:16 -0500105 }
106
107 // Add the mapping for package ID to index if not present.
108 uint8_t idx = package_ids_[package_id];
109 if (idx == 0xff) {
110 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
111 package_groups_.push_back({});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700112
113 if (apk_assets->IsOverlay()) {
114 // The target package must precede the overlay package in the apk assets paths in order
115 // to take effect.
116 const auto& loaded_idmap = apk_assets->GetLoadedIdmap();
117 auto target_package_iter = apk_assets_package_ids.find(loaded_idmap->TargetApkPath());
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700118 if (target_package_iter == apk_assets_package_ids.end()) {
119 LOG(INFO) << "failed to find target package for overlay "
120 << loaded_idmap->OverlayApkPath();
121 } else {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700122 const uint8_t target_package_id = target_package_iter->second;
123 const uint8_t target_idx = package_ids_[target_package_id];
124 CHECK(target_idx != 0xff) << "overlay added to apk_assets_package_ids but does not"
125 << " have an assigned package group";
126
127 PackageGroup& target_package_group = package_groups_[target_idx];
128
Ryan Mitchellb894c272020-02-12 10:31:44 -0800129 // Create a special dynamic reference table for the overlay to rewite references to
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700130 // overlay resources as references to the target resources they overlay.
131 auto overlay_table = std::make_shared<OverlayDynamicRefTable>(
132 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
133 package_groups_.back().dynamic_ref_table = overlay_table;
134
135 // Add the overlay resource map to the target package's set of overlays.
136 target_package_group.overlays_.push_back(
137 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
138 overlay_table.get()),
139 static_cast<ApkAssetsCookie>(i)});
140 }
141 }
142
143 DynamicRefTable* ref_table = package_groups_.back().dynamic_ref_table.get();
144 ref_table->mAssignedPackageId = package_id;
145 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500146 }
147 PackageGroup* package_group = &package_groups_[idx];
148
149 // Add the package and to the set of packages with the same ID.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800150 package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
Adam Lesinskida431a22016-12-29 16:08:16 -0500151 package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i));
152
153 // Add the package name -> build time ID mappings.
154 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
155 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700156 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500157 package_name, static_cast<uint8_t>(entry.package_id));
158 }
Ryan Mitchellb894c272020-02-12 10:31:44 -0800159
160 apk_assets_package_ids.insert(std::make_pair(apk_assets->GetPath(), package_id));
Adam Lesinskida431a22016-12-29 16:08:16 -0500161 }
162 }
163
164 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
165 const auto package_groups_end = package_groups_.end();
166 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800167 const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500168 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700169 iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()),
170 iter->dynamic_ref_table->mAssignedPackageId);
Adam Lesinskida431a22016-12-29 16:08:16 -0500171 }
172 }
173}
174
175void AssetManager2::DumpToLog() const {
176 base::ScopedLogSeverity _log(base::INFO);
177
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800178 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
179
Adam Lesinskida431a22016-12-29 16:08:16 -0500180 std::string list;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800181 for (const auto& apk_assets : apk_assets_) {
182 base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str());
183 }
184 LOG(INFO) << "ApkAssets: " << list;
185
186 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500187 for (size_t i = 0; i < package_ids_.size(); i++) {
188 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800189 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500190 }
191 }
192 LOG(INFO) << "Package ID map: " << list;
193
Adam Lesinski0dd36992018-01-25 15:38:38 -0800194 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800195 list = "";
196 for (const auto& package : package_group.packages_) {
197 const LoadedPackage* loaded_package = package.loaded_package_;
198 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
199 loaded_package->GetPackageId(),
200 (loaded_package->IsDynamic() ? " dynamic" : ""));
201 }
202 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700203 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800204 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800205
206 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700207 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800208 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700209 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800210 }
211 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500212 }
213}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700214
215const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
216 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
217 return nullptr;
218 }
219 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
220}
221
Adam Lesinskida431a22016-12-29 16:08:16 -0500222const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
223 if (package_id >= package_ids_.size()) {
224 return nullptr;
225 }
226
227 const size_t idx = package_ids_[package_id];
228 if (idx == 0xff) {
229 return nullptr;
230 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700231 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500232}
233
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700234std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
235 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800236 for (const PackageGroup& package_group : package_groups_) {
237 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
238 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700239 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800240 }
241 }
242 }
243 return nullptr;
244}
245
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100246const std::unordered_map<std::string, std::string>*
247 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
248
249 if (package_id >= package_ids_.size()) {
250 return nullptr;
251 }
252
253 const size_t idx = package_ids_[package_id];
254 if (idx == 0xff) {
255 return nullptr;
256 }
257
258 const PackageGroup& package_group = package_groups_[idx];
259 if (package_group.packages_.size() == 0) {
260 return nullptr;
261 }
262
263 const auto loaded_package = package_group.packages_[0].loaded_package_;
264 return &loaded_package->GetOverlayableMap();
265}
266
Ryan Mitchell2e394222019-08-28 12:10:51 -0700267bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name,
268 std::string* out) const {
269 uint8_t package_id = 0U;
270 for (const auto& apk_assets : apk_assets_) {
271 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
272 if (loaded_arsc == nullptr) {
273 continue;
274 }
275
276 const auto& loaded_packages = loaded_arsc->GetPackages();
277 if (loaded_packages.empty()) {
278 continue;
279 }
280
281 const auto& loaded_package = loaded_packages[0];
282 if (loaded_package->GetPackageName() == package_name) {
283 package_id = GetAssignedPackageId(loaded_package.get());
284 break;
285 }
286 }
287
288 if (package_id == 0U) {
289 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
290 return false;
291 }
292
293 const size_t idx = package_ids_[package_id];
294 if (idx == 0xff) {
295 return false;
296 }
297
298 std::string output;
299 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
300 const LoadedPackage* loaded_package = package.loaded_package_;
301 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
302 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
303 if (info != nullptr) {
304 ResourceName res_name;
305 if (!GetResourceName(*it, &res_name)) {
306 ANDROID_LOG(ERROR) << base::StringPrintf(
307 "Unable to retrieve name of overlayable resource 0x%08x", *it);
308 return false;
309 }
310
311 const std::string name = ToFormattedResourceString(&res_name);
312 output.append(base::StringPrintf(
313 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
314 name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags));
315 }
316 }
317 }
318
319 *out = std::move(output);
320 return true;
321}
322
Adam Lesinski7ad11102016-10-28 16:39:15 -0700323void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
324 const int diff = configuration_.diff(configuration);
325 configuration_ = configuration;
326
327 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800328 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700329 InvalidateCaches(static_cast<uint32_t>(diff));
330 }
331}
332
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700333std::set<std::string> AssetManager2::GetNonSystemOverlayPaths() const {
334 std::set<std::string> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800335 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800336 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800337 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700338 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800339 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700340 break;
341 }
342 }
343
344 if (!found_system_package) {
345 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
346 non_system_overlays.insert(apk_assets_[overlay.cookie]->GetPath());
347 }
348 }
349 }
350
351 return non_system_overlays;
352}
353
354std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system,
355 bool exclude_mipmap) const {
356 ATRACE_NAME("AssetManager::GetResourceConfigurations");
357 const auto non_system_overlays =
358 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
359
360 std::set<ResTable_config> configurations;
361 for (const PackageGroup& package_group : package_groups_) {
362 for (size_t i = 0; i < package_group.packages_.size(); i++) {
363 const ConfiguredPackage& package = package_group.packages_[i];
364 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800365 continue;
366 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800367
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700368 auto apk_assets = apk_assets_[package_group.cookies_[i]];
369 if (exclude_system && apk_assets->IsOverlay()
370 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
371 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800372 continue;
373 }
374
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800375 package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
Adam Lesinski0c405242017-01-13 20:47:26 -0800376 }
377 }
378 return configurations;
379}
380
381std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800382 bool merge_equivalent_languages) const {
383 ATRACE_NAME("AssetManager::GetResourceLocales");
Adam Lesinski0c405242017-01-13 20:47:26 -0800384 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700385 const auto non_system_overlays =
386 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
387
Adam Lesinski0c405242017-01-13 20:47:26 -0800388 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700389 for (size_t i = 0; i < package_group.packages_.size(); i++) {
390 const ConfiguredPackage& package = package_group.packages_[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800391 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800392 continue;
393 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800394
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700395 auto apk_assets = apk_assets_[package_group.cookies_[i]];
396 if (exclude_system && apk_assets->IsOverlay()
397 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
398 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800399 continue;
400 }
401
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800402 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800403 }
404 }
405 return locales;
406}
407
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800408std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
409 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700410 const std::string new_path = "assets/" + filename;
411 return OpenNonAsset(new_path, mode);
412}
413
414std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800415 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700416 const std::string new_path = "assets/" + filename;
417 return OpenNonAsset(new_path, cookie, mode);
418}
419
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800420std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
421 ATRACE_NAME("AssetManager::OpenDir");
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800422
423 std::string full_path = "assets/" + dirname;
424 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
425 util::make_unique<SortedVector<AssetDir::FileInfo>>();
426
427 // Start from the back.
428 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
429 const ApkAssets* apk_assets = *iter;
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100430 if (apk_assets->IsOverlay()) {
431 continue;
432 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800433
434 auto func = [&](const StringPiece& name, FileType type) {
435 AssetDir::FileInfo info;
436 info.setFileName(String8(name.data(), name.size()));
437 info.setFileType(type);
438 info.setSourceName(String8(apk_assets->GetPath().c_str()));
439 files->add(info);
440 };
441
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700442 if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800443 return {};
444 }
445 }
446
447 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
448 asset_dir->setFileList(files.release());
449 return asset_dir;
450}
451
Adam Lesinski7ad11102016-10-28 16:39:15 -0700452// Search in reverse because that's how we used to do it and we need to preserve behaviour.
453// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
454// is inconsistent for split APKs.
455std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
456 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800457 ApkAssetsCookie* out_cookie) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700458 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100459 // Prevent RRO from modifying assets and other entries accessed by file
460 // path. Explicitly asking for a path in a given package (denoted by a
461 // cookie) is still OK.
462 if (apk_assets_[i]->IsOverlay()) {
463 continue;
464 }
465
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700466 std::unique_ptr<Asset> asset = apk_assets_[i]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700467 if (asset) {
468 if (out_cookie != nullptr) {
469 *out_cookie = i;
470 }
471 return asset;
472 }
473 }
474
475 if (out_cookie != nullptr) {
476 *out_cookie = kInvalidCookie;
477 }
478 return {};
479}
480
481std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800482 ApkAssetsCookie cookie,
483 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700484 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
485 return {};
486 }
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700487 return apk_assets_[cookie]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700488}
489
490ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800491 bool /*stop_at_first_match*/,
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800492 bool ignore_configuration,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800493 FindEntryResult* out_entry) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700494 if (resource_resolution_logging_enabled_) {
495 // Clear the last logged resource resolution.
496 ResetResourceResolution();
497 last_resolution_.resid = resid;
498 }
499
Adam Lesinski7ad11102016-10-28 16:39:15 -0700500 // Might use this if density_override != 0.
501 ResTable_config density_override_config;
502
503 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800504 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700505 if (density_override != 0 && density_override != configuration_.density) {
506 density_override_config = configuration_;
507 density_override_config.density = density_override;
508 desired_config = &density_override_config;
509 }
510
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700511 // Retrieve the package group from the package id of the resource id.
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800512 if (!is_valid_resid(resid)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500513 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
514 return kInvalidCookie;
515 }
516
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800517 const uint32_t package_id = get_package_id(resid);
518 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800519 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700520 uint8_t package_idx = package_ids_[package_id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800521 if (package_idx == 0xff) {
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800522 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.",
523 package_id, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500524 return kInvalidCookie;
525 }
526
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800527 const PackageGroup& package_group = package_groups_[package_idx];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700528 ApkAssetsCookie cookie = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
529 false /* stop_at_first_match */,
530 ignore_configuration, out_entry);
531 if (UNLIKELY(cookie == kInvalidCookie)) {
532 return kInvalidCookie;
533 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800534
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700535 if (!apk_assets_[cookie]->IsLoader()) {
536 for (const auto& id_map : package_group.overlays_) {
537 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
538 if (!overlay_entry) {
539 // No id map entry exists for this target resource.
540 continue;
541 }
542
543 if (overlay_entry.IsTableEntry()) {
544 // The target resource is overlaid by an inline value not represented by a resource.
545 out_entry->entry = overlay_entry.GetTableEntry();
546 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
547 cookie = id_map.cookie;
548 continue;
549 }
550
551 FindEntryResult overlay_result;
552 ApkAssetsCookie overlay_cookie = FindEntry(overlay_entry.GetResourceId(), density_override,
553 false /* stop_at_first_match */,
554 ignore_configuration, &overlay_result);
555 if (UNLIKELY(overlay_cookie == kInvalidCookie)) {
556 continue;
557 }
558
559 if (!overlay_result.config.isBetterThan(out_entry->config, desired_config)
560 && overlay_result.config.compare(out_entry->config) != 0) {
561 // The configuration of the entry for the overlay must be equal to or better than the target
562 // configuration to be chosen as the better value.
563 continue;
564 }
565
566 cookie = overlay_cookie;
567 out_entry->entry = std::move(overlay_result.entry);
568 out_entry->config = overlay_result.config;
569 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
570 if (resource_resolution_logging_enabled_) {
571 last_resolution_.steps.push_back(
572 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result.config.toString(),
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700573 overlay_result.package_name});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700574 }
575 }
576 }
577
578 if (resource_resolution_logging_enabled_) {
579 last_resolution_.cookie = cookie;
580 last_resolution_.type_string_ref = out_entry->type_string_ref;
581 last_resolution_.entry_string_ref = out_entry->entry_string_ref;
582 }
583
584 return cookie;
585}
586
587ApkAssetsCookie AssetManager2::FindEntryInternal(const PackageGroup& package_group,
588 uint8_t type_idx, uint16_t entry_idx,
589 const ResTable_config& desired_config,
590 bool /*stop_at_first_match*/,
591 bool ignore_configuration,
592 FindEntryResult* out_entry) const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800593 ApkAssetsCookie best_cookie = kInvalidCookie;
594 const LoadedPackage* best_package = nullptr;
595 const ResTable_type* best_type = nullptr;
596 const ResTable_config* best_config = nullptr;
597 ResTable_config best_config_copy;
598 uint32_t best_offset = 0u;
599 uint32_t type_flags = 0u;
600
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700601 Resolution::Step::Type resolution_type = Resolution::Step::Type::NO_ENTRY;
Winson2f3669b2019-01-11 11:28:34 -0800602 std::vector<Resolution::Step> resolution_steps;
603
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800604 // If desired_config is the same as the set configuration, then we can use our filtered list
605 // and we don't need to match the configurations, since they already matched.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700606 const bool use_fast_path = !ignore_configuration && &desired_config == &configuration_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800607
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700608 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800609 for (size_t pi = 0; pi < package_count; pi++) {
610 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
611 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
612 ApkAssetsCookie cookie = package_group.cookies_[pi];
613
614 // If the type IDs are offset in this package, we need to take that into account when searching
615 // for a type.
616 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
617 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700618 continue;
619 }
620
Winson9947f1e2019-08-16 10:20:39 -0700621 // If the package is an overlay or custom loader,
622 // then even configurations that are the same MUST be chosen.
Winson9947f1e2019-08-16 10:20:39 -0700623 const bool package_is_loader = loaded_package->IsCustomLoader();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700624 type_flags |= type_spec->GetFlagsForEntryIndex(entry_idx);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800625
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800626 if (use_fast_path) {
Winson2f3669b2019-01-11 11:28:34 -0800627 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800628 const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations;
629 const size_t type_count = candidate_configs.size();
630 for (uint32_t i = 0; i < type_count; i++) {
631 const ResTable_config& this_config = candidate_configs[i];
632
633 // We can skip calling ResTable_config::match() because we know that all candidate
634 // configurations that do NOT match have been filtered-out.
Winson2f3669b2019-01-11 11:28:34 -0800635 if (best_config == nullptr) {
636 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700637 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
638 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
639 : Resolution::Step::Type::BETTER_MATCH;
640 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
641 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Winson2f3669b2019-01-11 11:28:34 -0800642 } else {
Winson9947f1e2019-08-16 10:20:39 -0700643 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700644 resolution_type = (package_is_loader) ? Resolution::Step::Type::SKIPPED_LOADER
645 : Resolution::Step::Type::SKIPPED;
Winson9947f1e2019-08-16 10:20:39 -0700646 resolution_steps.push_back(Resolution::Step{resolution_type,
647 this_config.toString(),
648 &loaded_package->GetPackageName()});
649 }
Winson2f3669b2019-01-11 11:28:34 -0800650 continue;
651 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800652
Winson2f3669b2019-01-11 11:28:34 -0800653 // The configuration matches and is better than the previous selection.
654 // Find the entry value if it exists for this configuration.
655 const ResTable_type* type = filtered_group.types[i];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700656 const uint32_t offset = LoadedPackage::GetEntryOffset(type, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800657 if (offset == ResTable_type::NO_ENTRY) {
Winson9947f1e2019-08-16 10:20:39 -0700658 if (resource_resolution_logging_enabled_) {
659 if (package_is_loader) {
660 resolution_type = Resolution::Step::Type::NO_ENTRY_LOADER;
661 } else {
662 resolution_type = Resolution::Step::Type::NO_ENTRY;
663 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700664 resolution_steps.push_back(Resolution::Step{resolution_type,
Winson9947f1e2019-08-16 10:20:39 -0700665 this_config.toString(),
666 &loaded_package->GetPackageName()});
667 }
Winson2f3669b2019-01-11 11:28:34 -0800668 continue;
669 }
670
671 best_cookie = cookie;
672 best_package = loaded_package;
673 best_type = type;
674 best_config = &this_config;
675 best_offset = offset;
676
677 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700678 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
679 this_config.toString(),
680 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800681 }
682 }
683 } else {
684 // This is the slower path, which doesn't use the filtered list of configurations.
685 // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness
686 // and fill in any new fields that did not exist when the APK was compiled.
687 // Furthermore when selecting configurations we can't just record the pointer to the
688 // ResTable_config, we must copy it.
689 const auto iter_end = type_spec->types + type_spec->type_count;
690 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800691 ResTable_config this_config{};
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800692
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800693 if (!ignore_configuration) {
694 this_config.copyFromDtoH((*iter)->config);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700695 if (!this_config.match(desired_config)) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800696 continue;
697 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800698
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800699 if (best_config == nullptr) {
700 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700701 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
702 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
703 : Resolution::Step::Type::BETTER_MATCH;
704 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
705 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800706 } else {
707 continue;
708 }
Winson2f3669b2019-01-11 11:28:34 -0800709 }
710
711 // The configuration matches and is better than the previous selection.
712 // Find the entry value if it exists for this configuration.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700713 const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800714 if (offset == ResTable_type::NO_ENTRY) {
715 continue;
716 }
717
718 best_cookie = cookie;
719 best_package = loaded_package;
720 best_type = *iter;
721 best_config_copy = this_config;
722 best_config = &best_config_copy;
723 best_offset = offset;
724
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800725 if (ignore_configuration) {
726 // Any configuration will suffice, so break.
727 break;
728 }
729
Winson2f3669b2019-01-11 11:28:34 -0800730 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700731 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
732 this_config.toString(),
733 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800734 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700735 }
736 }
737 }
738
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800739 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700740 return kInvalidCookie;
741 }
742
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800743 const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
744 if (UNLIKELY(best_entry == nullptr)) {
745 return kInvalidCookie;
746 }
747
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700748 out_entry->entry = ResTable_entry_handle::unmanaged(best_entry);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800749 out_entry->config = *best_config;
750 out_entry->type_flags = type_flags;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700751 out_entry->package_name = &best_package->GetPackageName();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800752 out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1);
753 out_entry->entry_string_ref =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700754 StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index);
755 out_entry->dynamic_ref_table = package_group.dynamic_ref_table.get();
Winson2f3669b2019-01-11 11:28:34 -0800756
Adam Lesinskida431a22016-12-29 16:08:16 -0500757 return best_cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700758}
759
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700760void AssetManager2::ResetResourceResolution() const {
761 last_resolution_.cookie = kInvalidCookie;
762 last_resolution_.resid = 0;
763 last_resolution_.steps.clear();
764 last_resolution_.type_string_ref = StringPoolRef();
765 last_resolution_.entry_string_ref = StringPoolRef();
766}
767
Winson2f3669b2019-01-11 11:28:34 -0800768void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
769 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800770 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700771 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800772 }
773}
774
775std::string AssetManager2::GetLastResourceResolution() const {
776 if (!resource_resolution_logging_enabled_) {
777 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
778 return std::string();
779 }
780
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700781 auto cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800782 if (cookie == kInvalidCookie) {
783 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
784 return std::string();
785 }
786
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700787 uint32_t resid = last_resolution_.resid;
788 std::vector<Resolution::Step>& steps = last_resolution_.steps;
Winson2f3669b2019-01-11 11:28:34 -0800789
790 ResourceName resource_name;
791 std::string resource_name_string;
792
793 const LoadedPackage* package =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700794 apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
Winson2f3669b2019-01-11 11:28:34 -0800795
796 if (package != nullptr) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700797 ToResourceName(last_resolution_.type_string_ref,
798 last_resolution_.entry_string_ref,
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800799 package->GetPackageName(),
Winson2f3669b2019-01-11 11:28:34 -0800800 &resource_name);
801 resource_name_string = ToFormattedResourceString(&resource_name);
802 }
803
804 std::stringstream log_stream;
805 log_stream << base::StringPrintf("Resolution for 0x%08x ", resid)
806 << resource_name_string
807 << "\n\tFor config -"
808 << configuration_.toString();
809
810 std::string prefix;
811 for (Resolution::Step step : steps) {
812 switch (step.type) {
813 case Resolution::Step::Type::INITIAL:
814 prefix = "Found initial";
815 break;
816 case Resolution::Step::Type::BETTER_MATCH:
817 prefix = "Found better";
818 break;
Winson9947f1e2019-08-16 10:20:39 -0700819 case Resolution::Step::Type::BETTER_MATCH_LOADER:
820 prefix = "Found better in loader";
821 break;
Winson2f3669b2019-01-11 11:28:34 -0800822 case Resolution::Step::Type::OVERLAID:
823 prefix = "Overlaid";
824 break;
Winson9947f1e2019-08-16 10:20:39 -0700825 case Resolution::Step::Type::OVERLAID_LOADER:
826 prefix = "Overlaid by loader";
827 break;
828 case Resolution::Step::Type::SKIPPED:
829 prefix = "Skipped";
830 break;
831 case Resolution::Step::Type::SKIPPED_LOADER:
832 prefix = "Skipped loader";
833 break;
834 case Resolution::Step::Type::NO_ENTRY:
835 prefix = "No entry";
836 break;
837 case Resolution::Step::Type::NO_ENTRY_LOADER:
838 prefix = "No entry for loader";
839 break;
Winson2f3669b2019-01-11 11:28:34 -0800840 }
841
842 if (!prefix.empty()) {
843 log_stream << "\n\t" << prefix << ": " << *step.package_name;
844
845 if (!step.config_name.isEmpty()) {
846 log_stream << " -" << step.config_name;
847 }
848 }
849 }
850
851 return log_stream.str();
852}
853
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800854bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700855 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800856 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
857 true /* stop_at_first_match */,
858 true /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700859 if (cookie == kInvalidCookie) {
860 return false;
861 }
862
Winson2f3669b2019-01-11 11:28:34 -0800863 return ToResourceName(entry.type_string_ref,
864 entry.entry_string_ref,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700865 *entry.package_name,
Winson2f3669b2019-01-11 11:28:34 -0800866 out_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700867}
868
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800869bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700870 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800871 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
872 false /* stop_at_first_match */,
873 true /* ignore_configuration */, &entry);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700874 if (cookie != kInvalidCookie) {
875 *out_flags = entry.type_flags;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800876 return true;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700877 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800878 return false;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700879}
880
881ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
882 uint16_t density_override, Res_value* out_value,
883 ResTable_config* out_selected_config,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800884 uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700885 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800886 ApkAssetsCookie cookie = FindEntry(resid, density_override, false /* stop_at_first_match */,
887 false /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700888 if (cookie == kInvalidCookie) {
889 return kInvalidCookie;
890 }
891
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700892 const ResTable_entry* table_entry = *entry.entry;
893 if (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700894 if (!may_be_bag) {
895 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800896 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700897 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800898
899 // Create a reference since we can't represent this complex type as a Res_value.
900 out_value->dataType = Res_value::TYPE_REFERENCE;
901 out_value->data = resid;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800902 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700903 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800904 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700905 }
906
907 const Res_value* device_value = reinterpret_cast<const Res_value*>(
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700908 reinterpret_cast<const uint8_t*>(table_entry) + dtohs(table_entry->size));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700909 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500910
911 // Convert the package ID to the runtime assigned package ID.
912 entry.dynamic_ref_table->lookupResourceValue(out_value);
913
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800914 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700915 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700916 return cookie;
917}
918
Adam Lesinski0c405242017-01-13 20:47:26 -0800919ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
920 ResTable_config* in_out_selected_config,
921 uint32_t* in_out_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800922 uint32_t* out_last_reference) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800923 constexpr const int kMaxIterations = 20;
924
Adam Lesinski0c405242017-01-13 20:47:26 -0800925 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
926 in_out_value->data != 0u && iteration < kMaxIterations;
927 iteration++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800928 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800929 uint32_t new_flags = 0u;
930 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
931 in_out_value, in_out_selected_config, &new_flags);
932 if (cookie == kInvalidCookie) {
933 return kInvalidCookie;
934 }
935 if (in_out_flags != nullptr) {
936 *in_out_flags |= new_flags;
937 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800938 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800939 // This reference can't be resolved, so exit now and let the caller deal with it.
940 return cookie;
941 }
942 }
943 return cookie;
944}
945
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800946const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) {
947 auto cached_iter = cached_bag_resid_stacks_.find(resid);
948 if (cached_iter != cached_bag_resid_stacks_.end()) {
949 return cached_iter->second;
950 } else {
951 auto found_resids = std::vector<uint32_t>();
952 GetBag(resid, found_resids);
953 // Cache style stacks if they are not already cached.
954 cached_bag_resid_stacks_[resid] = found_resids;
955 return found_resids;
956 }
957}
958
Adam Lesinski7ad11102016-10-28 16:39:15 -0700959const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
y57cd1952018-04-12 14:26:23 -0700960 auto found_resids = std::vector<uint32_t>();
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800961 auto bag = GetBag(resid, found_resids);
962
963 // Cache style stacks if they are not already cached.
964 auto cached_iter = cached_bag_resid_stacks_.find(resid);
965 if (cached_iter == cached_bag_resid_stacks_.end()) {
966 cached_bag_resid_stacks_[resid] = found_resids;
967 }
968 return bag;
y57cd1952018-04-12 14:26:23 -0700969}
970
Ryan Mitchell155d5392020-02-10 13:35:24 -0800971static bool compare_bag_entries(const ResolvedBag::Entry& entry1,
972 const ResolvedBag::Entry& entry2) {
973 return entry1.key < entry2.key;
974}
975
y57cd1952018-04-12 14:26:23 -0700976const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700977 auto cached_iter = cached_bags_.find(resid);
978 if (cached_iter != cached_bags_.end()) {
979 return cached_iter->second.get();
980 }
981
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700982 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800983 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
984 false /* stop_at_first_match */,
985 false /* ignore_configuration */,
986 &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700987 if (cookie == kInvalidCookie) {
988 return nullptr;
989 }
990
991 // Check that the size of the entry header is at least as big as
992 // the desired ResTable_map_entry. Also verify that the entry
993 // was intended to be a map.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700994 const ResTable_entry* table_entry = *entry.entry;
995 if (dtohs(table_entry->size) < sizeof(ResTable_map_entry) ||
996 (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700997 // Not a bag, nothing to do.
998 return nullptr;
999 }
1000
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001001 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(table_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001002 const ResTable_map* map_entry =
1003 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
1004 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
1005
y57cd1952018-04-12 14:26:23 -07001006 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
1007 // dependencies between bags
1008 child_resids.push_back(resid);
1009
Adam Lesinskida431a22016-12-29 16:08:16 -05001010 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell155d5392020-02-10 13:35:24 -08001011 if (parent_resid == 0U || std::find(child_resids.begin(), child_resids.end(), parent_resid)
y57cd1952018-04-12 14:26:23 -07001012 != child_resids.end()) {
Ryan Mitchell155d5392020-02-10 13:35:24 -08001013 // There is no parent or a circular dependency exist, meaning there is nothing to inherit and
1014 // we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001015 const size_t entry_count = map_entry_end - map_entry;
1016 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1017 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001018
1019 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001020 ResolvedBag::Entry* new_entry = new_bag->entries;
1021 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001022 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001023 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001024 // Attributes, arrays, etc don't have a resource id as the name. They specify
1025 // other data, which would be wrong to change via a lookup.
1026 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001027 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1028 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001029 return nullptr;
1030 }
1031 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001032 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001033 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001034 new_entry->key_pool = nullptr;
1035 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001036 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001037 new_entry->value.copyFrom_dtoh(map_entry->value);
1038 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1039 if (err != NO_ERROR) {
1040 LOG(ERROR) << base::StringPrintf(
1041 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1042 new_entry->value.data, new_key);
1043 return nullptr;
1044 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001045 sort_entries = sort_entries ||
1046 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001047 ++new_entry;
1048 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001049
1050 if (sort_entries) {
1051 std::sort(new_bag->entries, new_bag->entries + entry_count, compare_bag_entries);
1052 }
1053
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001054 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001055 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1056 ResolvedBag* result = new_bag.get();
1057 cached_bags_[resid] = std::move(new_bag);
1058 return result;
1059 }
1060
Adam Lesinskida431a22016-12-29 16:08:16 -05001061 // In case the parent is a dynamic reference, resolve it.
1062 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
1063
Adam Lesinski7ad11102016-10-28 16:39:15 -07001064 // Get the parent and do a merge of the keys.
y57cd1952018-04-12 14:26:23 -07001065 const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001066 if (parent_bag == nullptr) {
1067 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001068 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1069 resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001070 return nullptr;
1071 }
1072
Adam Lesinski7ad11102016-10-28 16:39:15 -07001073 // Create the max possible entries we can make. Once we construct the bag,
1074 // we will realloc to fit to size.
1075 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001076 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1077 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001078 ResolvedBag::Entry* new_entry = new_bag->entries;
1079
1080 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
1081 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
1082
1083 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001084 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001085 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001086 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001087 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001088 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001089 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1090 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001091 return nullptr;
1092 }
1093 }
1094
Adam Lesinski7ad11102016-10-28 16:39:15 -07001095 if (child_key <= parent_entry->key) {
1096 // Use the child key if it comes before the parent
1097 // or is equal to the parent (overrides).
1098 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001099 new_entry->key = child_key;
1100 new_entry->key_pool = nullptr;
1101 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001102 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001103 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001104 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1105 if (err != NO_ERROR) {
1106 LOG(ERROR) << base::StringPrintf(
1107 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1108 new_entry->value.data, child_key);
1109 return nullptr;
1110 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001111 ++map_entry;
1112 } else {
1113 // Take the parent entry as-is.
1114 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1115 }
1116
Ryan Mitchell155d5392020-02-10 13:35:24 -08001117 sort_entries = sort_entries ||
1118 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001119 if (child_key >= parent_entry->key) {
1120 // Move to the next parent entry if we used it or it was overridden.
1121 ++parent_entry;
1122 }
1123 // Increment to the next entry to fill.
1124 ++new_entry;
1125 }
1126
1127 // Finish the child entries if they exist.
1128 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001129 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001130 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001131 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001132 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1133 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001134 return nullptr;
1135 }
1136 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001137 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001138 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001139 new_entry->key_pool = nullptr;
1140 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001141 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001142 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001143 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1144 if (err != NO_ERROR) {
1145 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1146 new_entry->value.dataType, new_entry->value.data, new_key);
1147 return nullptr;
1148 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001149 sort_entries = sort_entries ||
1150 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001151 ++map_entry;
1152 ++new_entry;
1153 }
1154
1155 // Finish the parent entries if they exist.
1156 if (parent_entry != parent_entry_end) {
1157 // Take the rest of the parent entries as-is.
1158 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1159 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1160 new_entry += num_entries_to_copy;
1161 }
1162
1163 // Resize the resulting array to fit.
1164 const size_t actual_count = new_entry - new_bag->entries;
1165 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001166 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1167 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001168 }
1169
Ryan Mitchell155d5392020-02-10 13:35:24 -08001170 if (sort_entries) {
1171 std::sort(new_bag->entries, new_bag->entries + actual_count, compare_bag_entries);
1172 }
1173
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001174 // Combine flags from the parent and our own bag.
1175 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001176 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1177 ResolvedBag* result = new_bag.get();
1178 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001179 return result;
1180}
1181
Adam Lesinski929d6512017-01-16 19:11:19 -08001182static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
1183 ssize_t len =
1184 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1185 if (len < 0) {
1186 return false;
1187 }
1188 out->resize(static_cast<size_t>(len));
1189 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1190 static_cast<size_t>(len + 1));
1191 return true;
1192}
1193
Adam Lesinski0c405242017-01-13 20:47:26 -08001194uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
1195 const std::string& fallback_type,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001196 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001197 StringPiece package_name, type, entry;
1198 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
1199 return 0u;
1200 }
1201
1202 if (entry.empty()) {
1203 return 0u;
1204 }
1205
1206 if (package_name.empty()) {
1207 package_name = fallback_package;
1208 }
1209
1210 if (type.empty()) {
1211 type = fallback_type;
1212 }
1213
1214 std::u16string type16;
1215 if (!Utf8ToUtf16(type, &type16)) {
1216 return 0u;
1217 }
1218
1219 std::u16string entry16;
1220 if (!Utf8ToUtf16(entry, &entry16)) {
1221 return 0u;
1222 }
1223
1224 const StringPiece16 kAttr16 = u"attr";
1225 const static std::u16string kAttrPrivate16 = u"^attr-private";
1226
1227 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001228 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1229 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001230 if (package_name != package->GetPackageName()) {
1231 // All packages in the same group are expected to have the same package name.
1232 break;
1233 }
1234
1235 uint32_t resid = package->FindEntryByName(type16, entry16);
1236 if (resid == 0u && kAttr16 == type16) {
1237 // Private attributes in libraries (such as the framework) are sometimes encoded
1238 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1239 // free for future additions. Check '^attr-private' for the same name.
1240 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1241 }
1242
1243 if (resid != 0u) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001244 return fix_package_id(resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001245 }
1246 }
1247 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001248 return 0u;
1249}
1250
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001251void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001252 for (PackageGroup& group : package_groups_) {
1253 for (ConfiguredPackage& impl : group.packages_) {
1254 // Destroy it.
1255 impl.filtered_configs_.~ByteBucketArray();
1256
1257 // Re-create it.
1258 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
1259
1260 // Create the filters here.
1261 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) {
1262 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index);
1263 const auto iter_end = spec->types + spec->type_count;
1264 for (auto iter = spec->types; iter != iter_end; ++iter) {
1265 ResTable_config this_config;
1266 this_config.copyFromDtoH((*iter)->config);
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001267 if (!filter_incompatible_configs || this_config.match(configuration_)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001268 group.configurations.push_back(this_config);
1269 group.types.push_back(*iter);
1270 }
1271 }
1272 });
1273 }
1274 }
1275}
1276
Adam Lesinski7ad11102016-10-28 16:39:15 -07001277void AssetManager2::InvalidateCaches(uint32_t diff) {
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001278 cached_bag_resid_stacks_.clear();
1279
Adam Lesinski7ad11102016-10-28 16:39:15 -07001280 if (diff == 0xffffffffu) {
1281 // Everything must go.
1282 cached_bags_.clear();
1283 return;
1284 }
1285
1286 // Be more conservative with what gets purged. Only if the bag has other possible
1287 // variations with respect to what changed (diff) should we remove it.
1288 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1289 if (diff & iter->second->type_spec_flags) {
1290 iter = cached_bags_.erase(iter);
1291 } else {
1292 ++iter;
1293 }
1294 }
1295}
1296
Ryan Mitchell2e394222019-08-28 12:10:51 -07001297uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001298 for (auto& package_group : package_groups_) {
1299 for (auto& package2 : package_group.packages_) {
1300 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001301 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001302 }
1303 }
1304 }
1305 return 0;
1306}
1307
Adam Lesinski30080e22017-10-16 16:18:09 -07001308std::unique_ptr<Theme> AssetManager2::NewTheme() {
1309 return std::unique_ptr<Theme>(new Theme(this));
1310}
1311
1312Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1313}
1314
1315Theme::~Theme() = default;
1316
1317namespace {
1318
1319struct ThemeEntry {
1320 ApkAssetsCookie cookie;
1321 uint32_t type_spec_flags;
1322 Res_value value;
1323};
1324
1325struct ThemeType {
1326 int entry_count;
1327 ThemeEntry entries[0];
1328};
1329
1330constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
1331
1332} // namespace
1333
1334struct Theme::Package {
1335 // Each element of Type will be a dynamically sized object
1336 // allocated to have the entries stored contiguously with the Type.
1337 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
1338};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001339
1340bool Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001341 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001342
1343 const ResolvedBag* bag = asset_manager_->GetBag(resid);
1344 if (bag == nullptr) {
1345 return false;
1346 }
1347
1348 // Merge the flags from this style.
1349 type_spec_flags_ |= bag->type_spec_flags;
1350
Adam Lesinski30080e22017-10-16 16:18:09 -07001351 int last_type_idx = -1;
1352 int last_package_idx = -1;
1353 Package* last_package = nullptr;
1354 ThemeType* last_type = nullptr;
1355
1356 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
1357 // need to perform one resize per type.
1358 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
1359 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
1360 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001361 const uint32_t attr_resid = bag_iter->key;
1362
Adam Lesinski30080e22017-10-16 16:18:09 -07001363 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1364 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -08001365 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001366 return false;
1367 }
1368
Adam Lesinski30080e22017-10-16 16:18:09 -07001369 // We don't use the 0-based index for the type so that we can avoid doing ID validation
1370 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
1371 // the construction of this type is guarded with a resource ID check, it will never be
1372 // populated, and querying type ID 0 will always fail.
1373 const int package_idx = get_package_id(attr_resid);
1374 const int type_idx = get_type_id(attr_resid);
1375 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001376
Adam Lesinski30080e22017-10-16 16:18:09 -07001377 if (last_package_idx != package_idx) {
1378 std::unique_ptr<Package>& package = packages_[package_idx];
1379 if (package == nullptr) {
1380 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001381 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001382 last_package_idx = package_idx;
1383 last_package = package.get();
1384 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001385 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001386
1387 if (last_type_idx != type_idx) {
1388 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
1389 if (type == nullptr) {
1390 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
1391 // a sorted list of attributes, this shouldn't be resized again during this method call.
1392 type.reset(reinterpret_cast<ThemeType*>(
1393 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
1394 type->entry_count = entry_idx + 1;
1395 } else if (entry_idx >= type->entry_count) {
1396 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
1397 // a sorted list of attributes, this shouldn't be resized again during this method call.
1398 const int new_count = entry_idx + 1;
1399 type.reset(reinterpret_cast<ThemeType*>(
1400 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
1401
1402 // Clear out the newly allocated space (which isn't zeroed).
1403 memset(type->entries + type->entry_count, 0,
1404 (new_count - type->entry_count) * sizeof(ThemeEntry));
1405 type->entry_count = new_count;
1406 }
1407 last_type_idx = type_idx;
1408 last_type = type.get();
1409 }
1410
1411 ThemeEntry& entry = last_type->entries[entry_idx];
1412 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
1413 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001414 entry.cookie = bag_iter->cookie;
1415 entry.type_spec_flags |= bag->type_spec_flags;
1416 entry.value = bag_iter->value;
1417 }
1418 }
1419 return true;
1420}
1421
1422ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
1423 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -07001424 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001425
1426 uint32_t type_spec_flags = 0u;
1427
Adam Lesinski30080e22017-10-16 16:18:09 -07001428 do {
1429 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001430 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -07001431 if (package != nullptr) {
1432 // The themes are constructed with a 1-based type ID, so no need to decrement here.
1433 const int type_idx = get_type_id(resid);
1434 const ThemeType* type = package->types[type_idx].get();
1435 if (type != nullptr) {
1436 const int entry_idx = get_entry_id(resid);
1437 if (entry_idx < type->entry_count) {
1438 const ThemeEntry& entry = type->entries[entry_idx];
1439 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001440
Adam Lesinski30080e22017-10-16 16:18:09 -07001441 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
1442 if (cnt > 0) {
1443 cnt--;
1444 resid = entry.value.data;
1445 continue;
1446 }
1447 return kInvalidCookie;
1448 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001449
Adam Lesinski30080e22017-10-16 16:18:09 -07001450 // @null is different than @empty.
1451 if (entry.value.dataType == Res_value::TYPE_NULL &&
1452 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1453 return kInvalidCookie;
1454 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001455
Adam Lesinski30080e22017-10-16 16:18:09 -07001456 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -05001457 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -07001458 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001459 }
Adam Lesinskida431a22016-12-29 16:08:16 -05001460 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001461 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001462 break;
1463 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001464 return kInvalidCookie;
1465}
1466
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001467ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
1468 ResTable_config* in_out_selected_config,
1469 uint32_t* in_out_type_spec_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001470 uint32_t* out_last_ref) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001471 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
1472 uint32_t new_flags;
1473 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
1474 if (cookie == kInvalidCookie) {
1475 return kInvalidCookie;
1476 }
1477
1478 if (in_out_type_spec_flags != nullptr) {
1479 *in_out_type_spec_flags |= new_flags;
1480 }
1481 }
1482 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
1483 in_out_type_spec_flags, out_last_ref);
1484}
1485
Adam Lesinski7ad11102016-10-28 16:39:15 -07001486void Theme::Clear() {
1487 type_spec_flags_ = 0u;
1488 for (std::unique_ptr<Package>& package : packages_) {
1489 package.reset();
1490 }
1491}
1492
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001493void Theme::SetTo(const Theme& o) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001494 if (this == &o) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001495 return;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001496 }
1497
Adam Lesinski7ad11102016-10-28 16:39:15 -07001498 type_spec_flags_ = o.type_spec_flags_;
1499
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001500 if (asset_manager_ == o.asset_manager_) {
1501 // The theme comes from the same asset manager so all theme data can be copied exactly
1502 for (size_t p = 0; p < packages_.size(); p++) {
1503 const Package *package = o.packages_[p].get();
1504 if (package == nullptr) {
1505 // The other theme doesn't have this package, clear ours.
1506 packages_[p].reset();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001507 continue;
1508 }
1509
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001510 if (packages_[p] == nullptr) {
1511 // The other theme has this package, but we don't. Make one.
1512 packages_[p].reset(new Package());
1513 }
1514
1515 for (size_t t = 0; t < package->types.size(); t++) {
1516 const ThemeType *type = package->types[t].get();
1517 if (type == nullptr) {
1518 // The other theme doesn't have this type, clear ours.
1519 packages_[p]->types[t].reset();
1520 continue;
1521 }
1522
1523 // Create a new type and update it to theirs.
1524 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
1525 void *copied_data = malloc(type_alloc_size);
1526 memcpy(copied_data, type, type_alloc_size);
1527 packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data));
1528 }
1529 }
1530 } else {
1531 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1532 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1533 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1534
Ryan Mitchell93bca972019-03-08 17:26:28 -08001535 // Determine which ApkAssets are loaded in both theme AssetManagers.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001536 std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets();
1537 for (size_t i = 0; i < src_assets.size(); i++) {
1538 const ApkAssets* src_asset = src_assets[i];
1539
1540 std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets();
1541 for (size_t j = 0; j < dest_assets.size(); j++) {
1542 const ApkAssets* dest_asset = dest_assets[j];
1543
Ryan Mitchell93bca972019-03-08 17:26:28 -08001544 // Map the runtime package of the source apk asset to the destination apk asset.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001545 if (src_asset->GetPath() == dest_asset->GetPath()) {
1546 const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages =
1547 src_asset->GetLoadedArsc()->GetPackages();
1548 const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages =
1549 dest_asset->GetLoadedArsc()->GetPackages();
1550
1551 SourceToDestinationRuntimePackageMap package_map;
1552
1553 // The source and destination package should have the same number of packages loaded in
1554 // the same order.
1555 const size_t N = src_packages.size();
1556 CHECK(N == dest_packages.size())
1557 << " LoadedArsc " << src_asset->GetPath() << " differs number of packages.";
1558 for (size_t p = 0; p < N; p++) {
1559 auto& src_package = src_packages[p];
1560 auto& dest_package = dest_packages[p];
1561 CHECK(src_package->GetPackageName() == dest_package->GetPackageName())
1562 << " Package " << src_package->GetPackageName() << " differs in load order.";
1563
1564 int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get());
1565 int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get());
1566 package_map[src_package_id] = dest_package_id;
1567 }
1568
Ryan Mitchell93bca972019-03-08 17:26:28 -08001569 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
1570 src_asset_cookie_id_map.insert(std::make_pair(i, package_map));
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001571 break;
1572 }
1573 }
1574 }
1575
Ryan Mitchell93bca972019-03-08 17:26:28 -08001576 // Reset the data in the destination theme.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001577 for (size_t p = 0; p < packages_.size(); p++) {
1578 if (packages_[p] != nullptr) {
1579 packages_[p].reset();
1580 }
1581 }
1582
1583 for (size_t p = 0; p < packages_.size(); p++) {
1584 const Package *package = o.packages_[p].get();
1585 if (package == nullptr) {
1586 continue;
1587 }
1588
1589 for (size_t t = 0; t < package->types.size(); t++) {
1590 const ThemeType *type = package->types[t].get();
1591 if (type == nullptr) {
1592 continue;
1593 }
1594
1595 for (size_t e = 0; e < type->entry_count; e++) {
1596 const ThemeEntry &entry = type->entries[e];
1597 if (entry.value.dataType == Res_value::TYPE_NULL &&
1598 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1599 continue;
1600 }
1601
Ryan Mitchell93bca972019-03-08 17:26:28 -08001602 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1603 || entry.value.dataType == Res_value::TYPE_REFERENCE
1604 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1605 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1606 && entry.value.data != 0x0;
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001607
Ryan Mitchell93bca972019-03-08 17:26:28 -08001608 // If the attribute value represents an attribute or reference, the package id of the
1609 // value needs to be rewritten to the package id of the value in the destination.
1610 uint32_t attribute_data = entry.value.data;
1611 if (is_reference) {
1612 // Determine the package id of the reference in the destination AssetManager.
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001613 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1614 if (value_package_map == src_asset_cookie_id_map.end()) {
1615 continue;
1616 }
1617
1618 auto value_dest_package = value_package_map->second.find(
1619 get_package_id(entry.value.data));
1620 if (value_dest_package == value_package_map->second.end()) {
1621 continue;
1622 }
1623
Ryan Mitchell93bca972019-03-08 17:26:28 -08001624 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1625 }
1626
1627 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1628 // destination, only copy resources that do not reference resources in the source.
1629 ApkAssetsCookie data_dest_cookie;
1630 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1631 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1632 data_dest_cookie = value_dest_cookie->second;
1633 } else {
1634 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1635 continue;
1636 } else {
1637 data_dest_cookie = 0x0;
1638 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001639 }
1640
1641 // The package id of the attribute needs to be rewritten to the package id of the
Ryan Mitchell93bca972019-03-08 17:26:28 -08001642 // attribute in the destination.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001643 int attribute_dest_package_id = p;
1644 if (attribute_dest_package_id != 0x01) {
Ryan Mitchell93bca972019-03-08 17:26:28 -08001645 // Find the cookie of the attribute resource id in the source AssetManager
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001646 FindEntryResult attribute_entry_result;
1647 ApkAssetsCookie attribute_cookie =
Ryan Mitchella55dc2e2019-01-24 10:58:23 -08001648 o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ ,
1649 true /* stop_at_first_match */,
1650 true /* ignore_configuration */,
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001651 &attribute_entry_result);
1652
Ryan Mitchell93bca972019-03-08 17:26:28 -08001653 // Determine the package id of the attribute in the destination AssetManager.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001654 auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie);
1655 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1656 continue;
1657 }
1658 auto attribute_dest_package = attribute_package_map->second.find(
1659 attribute_dest_package_id);
1660 if (attribute_dest_package == attribute_package_map->second.end()) {
1661 continue;
1662 }
1663 attribute_dest_package_id = attribute_dest_package->second;
1664 }
1665
Ryan Mitchell93bca972019-03-08 17:26:28 -08001666 // Lazily instantiate the destination package.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001667 std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id];
1668 if (dest_package == nullptr) {
1669 dest_package.reset(new Package());
1670 }
1671
Ryan Mitchell93bca972019-03-08 17:26:28 -08001672 // Lazily instantiate and resize the destination type.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001673 util::unique_cptr<ThemeType>& dest_type = dest_package->types[t];
1674 if (dest_type == nullptr || dest_type->entry_count < type->entry_count) {
1675 const size_t type_alloc_size = sizeof(ThemeType)
1676 + (type->entry_count * sizeof(ThemeEntry));
1677 void* dest_data = malloc(type_alloc_size);
1678 memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry));
1679
Ryan Mitchell93bca972019-03-08 17:26:28 -08001680 // Copy the existing destination type values if the type is resized.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001681 if (dest_type != nullptr) {
1682 memcpy(dest_data, type, sizeof(ThemeType)
1683 + (dest_type->entry_count * sizeof(ThemeEntry)));
1684 }
1685
1686 dest_type.reset(reinterpret_cast<ThemeType *>(dest_data));
1687 dest_type->entry_count = type->entry_count;
1688 }
1689
Ryan Mitchell93bca972019-03-08 17:26:28 -08001690 dest_type->entries[e].cookie = data_dest_cookie;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001691 dest_type->entries[e].value.dataType = entry.value.dataType;
Ryan Mitchell93bca972019-03-08 17:26:28 -08001692 dest_type->entries[e].value.data = attribute_data;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001693 dest_type->entries[e].type_spec_flags = entry.type_spec_flags;
1694 }
1695 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001696 }
1697 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001698}
1699
1700void Theme::Dump() const {
1701 base::ScopedLogSeverity _log(base::INFO);
1702 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
1703
1704 for (int p = 0; p < packages_.size(); p++) {
1705 auto& package = packages_[p];
1706 if (package == nullptr) {
1707 continue;
1708 }
1709
1710 for (int t = 0; t < package->types.size(); t++) {
1711 auto& type = package->types[t];
1712 if (type == nullptr) {
1713 continue;
1714 }
1715
1716 for (int e = 0; e < type->entry_count; e++) {
1717 auto& entry = type->entries[e];
1718 if (entry.value.dataType == Res_value::TYPE_NULL &&
1719 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1720 continue;
1721 }
1722
1723 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1724 make_resid(p, t, e), entry.value.data,
1725 entry.value.dataType, entry.cookie);
1726 }
1727 }
1728 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001729}
1730
1731} // namespace android