blob: f29c9185cafa7f68d5149c6f3ddd9d2c6f6a22a5 [file] [log] [blame]
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001/*
2 * Copyright (C) 2018 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#include "DumpManifest.h"
18
Dianne Hackborn813d7502018-10-02 16:59:46 -070019#include <algorithm>
20
Ryan Mitchellfc225b22018-08-21 14:52:51 -070021#include "LoadedApk.h"
22#include "SdkConstants.h"
23#include "ValueVisitor.h"
24#include "io/File.h"
25#include "io/FileStream.h"
26#include "process/IResourceTableConsumer.h"
27#include "xml/XmlDom.h"
28
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020029#include "androidfw/ConfigDescription.h"
30
Ryan Mitchellfc225b22018-08-21 14:52:51 -070031using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070033
34namespace aapt {
35
36/**
37 * These are attribute resource constants for the platform, as found in android.R.attr.
38 */
39enum {
40 LABEL_ATTR = 0x01010001,
41 ICON_ATTR = 0x01010002,
42 NAME_ATTR = 0x01010003,
43 PERMISSION_ATTR = 0x01010006,
44 EXPORTED_ATTR = 0x01010010,
45 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000046 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070047 RESOURCE_ATTR = 0x01010025,
48 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000049 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070050 VALUE_ATTR = 0x01010024,
51 VERSION_CODE_ATTR = 0x0101021b,
52 VERSION_NAME_ATTR = 0x0101021c,
53 SCREEN_ORIENTATION_ATTR = 0x0101001e,
54 MIN_SDK_VERSION_ATTR = 0x0101020c,
55 MAX_SDK_VERSION_ATTR = 0x01010271,
56 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
57 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
58 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
59 REQ_NAVIGATION_ATTR = 0x0101022a,
60 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
61 TARGET_SDK_VERSION_ATTR = 0x01010270,
62 TEST_ONLY_ATTR = 0x01010272,
63 ANY_DENSITY_ATTR = 0x0101026c,
64 GL_ES_VERSION_ATTR = 0x01010281,
65 SMALL_SCREEN_ATTR = 0x01010284,
66 NORMAL_SCREEN_ATTR = 0x01010285,
67 LARGE_SCREEN_ATTR = 0x01010286,
68 XLARGE_SCREEN_ATTR = 0x010102bf,
69 REQUIRED_ATTR = 0x0101028e,
70 INSTALL_LOCATION_ATTR = 0x010102b7,
71 SCREEN_SIZE_ATTR = 0x010102ca,
72 SCREEN_DENSITY_ATTR = 0x010102cb,
73 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
74 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
75 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
76 PUBLIC_KEY_ATTR = 0x010103a6,
77 CATEGORY_ATTR = 0x010103e8,
78 BANNER_ATTR = 0x10103f2,
79 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070080 VERSION_ATTR = 0x01010519,
81 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000082 REQUIRED_FEATURE_ATTR = 0x01010554,
83 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000084 IS_STATIC_ATTR = 0x0101055a,
85 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
86 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070087 COMPILE_SDK_VERSION_ATTR = 0x01010572,
88 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070089 VERSION_MAJOR_ATTR = 0x01010577,
90 PACKAGE_TYPE_ATTR = 0x01010587,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070091};
92
93const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Ryan Mitchell95f02422020-04-30 10:25:53 -070094constexpr int kCurrentDevelopmentVersion = 10000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070095
96/** Retrieves the attribute of the element with the specified attribute resource id. */
97static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
98 for (auto& a : el->attributes) {
99 if (a.compiled_attribute && a.compiled_attribute.value().id) {
100 if (a.compiled_attribute.value().id.value() == resd_id) {
101 return std::move(&a);
102 }
103 }
104 }
105 return nullptr;
106}
107
108/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
109static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
110 const std::string &name) {
111 return el->FindAttribute(package, name);
112}
113
114class CommonFeatureGroup;
115
116class ManifestExtractor {
117 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700118
Ryan Mitchell214846d2018-09-19 16:57:01 -0700119 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700120 : apk_(apk), options_(options) { }
121
122 class Element {
123 public:
124 Element() = default;
125 virtual ~Element() = default;
126
127 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
128
129 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700130 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700131
132 /** Adds an element to the list of children of the element. */
133 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
134
135 /** Retrieves the list of children of the element. */
136 const std::vector<std::unique_ptr<Element>>& children() const {
137 return children_;
138 }
139
140 /** Retrieves the extracted xml element tag. */
141 const std::string tag() const {
142 return tag_;
143 }
144
145 protected:
146 ManifestExtractor* extractor() const {
147 return extractor_;
148 }
149
150 /** Retrieves and stores the information extracted from the xml element. */
151 virtual void Extract(xml::Element* el) { }
152
153 /*
154 * Retrieves a configuration value of the resource entry that best matches the specified
155 * configuration.
156 */
157 static Value* BestConfigValue(ResourceEntry* entry,
158 const ConfigDescription& match) {
159 if (!entry) {
160 return nullptr;
161 }
162
163 // Determine the config that best matches the desired config
164 ResourceConfigValue* best_value = nullptr;
165 for (auto& value : entry->values) {
166 if (!value->config.match(match)) {
167 continue;
168 }
169
170 if (best_value != nullptr) {
171 if (!value->config.isBetterThan(best_value->config, &match)) {
172 if (value->config.compare(best_value->config) != 0) {
173 continue;
174 }
175 }
176 }
177
178 best_value = value.get();
179 }
180
181 // The entry has no values
182 if (!best_value) {
183 return nullptr;
184 }
185
186 return best_value->value.get();
187 }
188
189 /** Retrieves the resource assigned to the specified resource id if one exists. */
190 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700191 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700192 if (table) {
193 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700194 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700195 for (auto& entry : type->entries) {
196 if (entry->id && entry->id.value() == res_id.id) {
197 if (auto value = BestConfigValue(entry.get(), config)) {
198 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700199 }
200 }
201 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700202 }
203 }
204 }
205 return nullptr;
206 }
207
208 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700209 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700210 const int kMaxIterations = 40;
211 int i = 0;
212 while (ref && ref->id && i++ < kMaxIterations) {
213 auto table = extractor_->apk_->GetResourceTable();
214 if (auto value = FindValueById(table, ref->id.value(), config)) {
215 if (ValueCast<Reference>(value)) {
216 ref = ValueCast<Reference>(value);
217 } else {
218 return value;
219 }
220 }
221 }
222 return nullptr;
223 }
224
225 /**
226 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
227 * this will attempt to resolve the reference to an integer value.
228 **/
229 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700230 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700231 if (attr != nullptr) {
232 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700233 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700234 Value* value = attr->compiled_value.get();
235 if (ValueCast<Reference>(value)) {
236 value = ResolveReference(ValueCast<Reference>(value), config);
237 } else {
238 value = attr->compiled_value.get();
239 }
240 // Retrieve the integer data if possible
241 if (value != nullptr) {
242 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
243 return (int32_t*) &intValue->value.data;
244 }
245 }
246 }
247 }
248 return nullptr;
249 }
250
251 /**
252 * A version of GetAttributeInteger that returns a default integer if the attribute does not
253 * exist or cannot be resolved to an integer value.
254 **/
255 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700256 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700257 auto value = GetAttributeInteger(attr, config);
258 if (value) {
259 return *value;
260 }
261 return def;
262 }
263
264 /**
265 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
266 * this will attempt to resolve the reference to a string value.
267 **/
268 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700269 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700270 if (attr != nullptr) {
271 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700272 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700273 Value* value = attr->compiled_value.get();
274 if (ValueCast<Reference>(value)) {
275 value = ResolveReference(ValueCast<Reference>(value), config);
276 } else {
277 value = attr->compiled_value.get();
278 }
279
280 // Retrieve the string data of the value if possible
281 if (value != nullptr) {
282 if (String* intValue = ValueCast<String>(value)) {
283 return &(*intValue->value);
284 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
285 return &(*rawValue->value);
286 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
287 return &(*strValue->path);
288 }
289 }
290 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700291
292 if (!attr->value.empty()) {
293 return &attr->value;
294 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700295 }
296 return nullptr;
297 }
298
299 /**
300 * A version of GetAttributeString that returns a default string if the attribute does not
301 * exist or cannot be resolved to an string value.
302 **/
303 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700304 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700305 auto value = GetAttributeString(attr, config);
306 if (value) {
307 return *value;
308 }
309 return def;
310 }
311
312 private:
313 ManifestExtractor* extractor_;
314 std::vector<std::unique_ptr<Element>> children_;
315 std::string tag_;
316 };
317
318 friend Element;
319
320 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700321 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700322 ConfigDescription config;
323 config.orientation = android::ResTable_config::ORIENTATION_PORT;
324 config.density = android::ResTable_config::DENSITY_MEDIUM;
Ryan Mitchell95f02422020-04-30 10:25:53 -0700325 config.sdkVersion = kCurrentDevelopmentVersion; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700326 config.screenWidthDp = 320;
327 config.screenHeightDp = 480;
328 config.smallestScreenWidthDp = 320;
329 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
330 return config;
331 }
332
Ryan Mitchell214846d2018-09-19 16:57:01 -0700333 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700334
335 /** Recursively visit the xml element tree and return a processed badging element tree. */
336 std::unique_ptr<Element> Visit(xml::Element* element);
337
338 /** Raises the target sdk value if the min target is greater than the current target. */
339 void RaiseTargetSdk(int32_t min_target) {
340 if (min_target > target_sdk_) {
341 target_sdk_ = min_target;
342 }
343 }
344
345 /**
346 * Retrieves the default feature group that features are added into when <uses-feature>
347 * are not in a <feature-group> element.
348 **/
349 CommonFeatureGroup* GetCommonFeatureGroup() {
350 return commonFeatureGroup_.get();
351 }
352
353 /**
354 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
355 * used for that density setting.
356 **/
357 const std::map<uint16_t, ConfigDescription> densities() const {
358 return densities_;
359 }
360
361 /**
362 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
363 * would be used for that locale setting.
364 **/
365 const std::map<std::string, ConfigDescription> locales() const {
366 return locales_;
367 }
368
369 /** Retrieves the current stack of parent during data extraction. */
370 const std::vector<Element*> parent_stack() const {
371 return parent_stack_;
372 }
373
374 int32_t target_sdk() const {
375 return target_sdk_;
376 }
377
378 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700379 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700380
381 private:
382 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
383 std::map<std::string, ConfigDescription> locales_;
384 std::map<uint16_t, ConfigDescription> densities_;
385 std::vector<Element*> parent_stack_;
386 int32_t target_sdk_ = 0;
387};
388
389template<typename T> T* ElementCast(ManifestExtractor::Element* element);
390
391/** Recurs through the children of the specified root in depth-first order. */
392static void ForEachChild(ManifestExtractor::Element* root,
393 std::function<void(ManifestExtractor::Element*)> f) {
394 for (auto& child : root->children()) {
395 f(child.get());
396 ForEachChild(child.get(), f);
397 }
398}
399
400/**
401 * Checks the element and its recursive children for an element that makes the specified
402 * conditional function return true. Returns the first element that makes the conditional function
403 * return true.
404 **/
405static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
406 std::function<bool(ManifestExtractor::Element*)> f) {
407 if (f(root)) {
408 return root;
409 }
410 for (auto& child : root->children()) {
411 if (auto b2 = FindElement(child.get(), f)) {
412 return b2;
413 }
414 }
415 return nullptr;
416}
417
418/** Represents the <manifest> elements **/
419class Manifest : public ManifestExtractor::Element {
420 public:
421 Manifest() = default;
422 std::string package;
423 int32_t versionCode;
424 std::string versionName;
425 const std::string* split = nullptr;
426 const std::string* platformVersionName = nullptr;
427 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700428 const int32_t* platformVersionNameInt = nullptr;
429 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700430 const int32_t* compilesdkVersion = nullptr;
431 const std::string* compilesdkVersionCodename = nullptr;
432 const int32_t* installLocation = nullptr;
433
434 void Extract(xml::Element* manifest) override {
435 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
436 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
437 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
438 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
439
440 // Extract the platform build info
441 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
442 "platformBuildVersionName"));
443 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
444 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700445 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
446 "platformBuildVersionName"));
447 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
448 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700449
450 // Extract the compile sdk info
451 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
452 compilesdkVersionCodename = GetAttributeString(
453 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
454 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
455 }
456
Ryan Mitchell214846d2018-09-19 16:57:01 -0700457 void Print(text::Printer* printer) override {
458 printer->Print(StringPrintf("package: name='%s' ", package.data()));
459 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700460 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700461 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700462
463 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700464 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700465 }
466 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700467 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700468 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700469 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
470 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700471 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700472 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700473 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700474 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
475 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700476 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700477 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700478 }
479 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700480 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700481 compilesdkVersionCodename->data()));
482 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700483 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700484
485 if (installLocation) {
486 switch (*installLocation) {
487 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700488 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700489 break;
490 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700491 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700492 break;
493 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700494 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700495 break;
496 default:
497 break;
498 }
499 }
500 }
501};
502
503/** Represents <application> elements. **/
504class Application : public ManifestExtractor::Element {
505 public:
506 Application() = default;
507 std::string label;
508 std::string icon;
509 std::string banner;
510 int32_t is_game;
511 int32_t debuggable;
512 int32_t test_only;
513 bool has_multi_arch;
514
515 /** Mapping from locales to app names. */
516 std::map<std::string, std::string> locale_labels;
517
518 /** Mapping from densities to app icons. */
519 std::map<uint16_t, std::string> density_icons;
520
521 void Extract(xml::Element* element) override {
522 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
523 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
524 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
525 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
526 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
527 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
528
529 // We must search by name because the multiArch flag hasn't been API
530 // frozen yet.
531 has_multi_arch = (GetAttributeIntegerDefault(
532 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
533
534 // Retrieve the app names for every locale the app supports
535 auto attr = FindAttribute(element, LABEL_ATTR);
536 for (auto& config : extractor()->locales()) {
537 if (auto label = GetAttributeString(attr, config.second)) {
538 if (label) {
539 locale_labels.insert(std::make_pair(config.first, *label));
540 }
541 }
542 }
543
544 // Retrieve the icons for the densities the app supports
545 attr = FindAttribute(element, ICON_ATTR);
546 for (auto& config : extractor()->densities()) {
547 if (auto resource = GetAttributeString(attr, config.second)) {
548 if (resource) {
549 density_icons.insert(std::make_pair(config.first, *resource));
550 }
551 }
552 }
553 }
554
Ryan Mitchell214846d2018-09-19 16:57:01 -0700555 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700556 // Print the labels for every locale
557 for (auto p : locale_labels) {
558 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700559 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700560 android::ResTable::normalizeForOutput(p.second.data())
561 .c_str()));
562 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700563 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700564 android::ResTable::normalizeForOutput(p.second.data())
565 .c_str()));
566 }
567 }
568
569 // Print the icon paths for every density
570 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700571 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700572 }
573
574 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700575 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700576 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700577 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700578 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700579 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700580 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700581 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700582
583 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700584 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700585 }
586 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700587 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700588 }
589 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700590 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700591 }
592 }
593};
594
595/** Represents <uses-sdk> elements. **/
596class UsesSdkBadging : public ManifestExtractor::Element {
597 public:
598 UsesSdkBadging() = default;
599 const int32_t* min_sdk = nullptr;
600 const std::string* min_sdk_name = nullptr;
601 const int32_t* max_sdk = nullptr;
602 const int32_t* target_sdk = nullptr;
603 const std::string* target_sdk_name = nullptr;
604
605 void Extract(xml::Element* element) override {
606 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
607 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
608 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
609 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
610 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
611
612 // Detect the target sdk of the element
613 if ((min_sdk_name && *min_sdk_name == "Donut")
614 || (target_sdk_name && *target_sdk_name == "Donut")) {
615 extractor()->RaiseTargetSdk(4);
616 }
617 if (min_sdk) {
618 extractor()->RaiseTargetSdk(*min_sdk);
619 }
620 if (target_sdk) {
621 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700622 } else if (target_sdk_name) {
623 extractor()->RaiseTargetSdk(kCurrentDevelopmentVersion);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700624 }
625 }
626
Ryan Mitchell214846d2018-09-19 16:57:01 -0700627 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700628 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700629 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700630 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700631 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700632 }
633 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700634 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700635 }
636 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700637 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700638 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700639 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700640 }
641 }
642};
643
644/** Represents <uses-configuration> elements. **/
645class UsesConfiguarion : public ManifestExtractor::Element {
646 public:
647 UsesConfiguarion() = default;
648 int32_t req_touch_screen = 0;
649 int32_t req_keyboard_type = 0;
650 int32_t req_hard_keyboard = 0;
651 int32_t req_navigation = 0;
652 int32_t req_five_way_nav = 0;
653
654 void Extract(xml::Element* element) override {
655 req_touch_screen = GetAttributeIntegerDefault(
656 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
657 req_keyboard_type = GetAttributeIntegerDefault(
658 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
659 req_hard_keyboard = GetAttributeIntegerDefault(
660 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
661 req_navigation = GetAttributeIntegerDefault(
662 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
663 req_five_way_nav = GetAttributeIntegerDefault(
664 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
665 }
666
Ryan Mitchell214846d2018-09-19 16:57:01 -0700667 void Print(text::Printer* printer) override {
668 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700669 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700670 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700671 }
672 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700673 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700674 }
675 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700676 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700677 }
678 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700679 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700680 }
681 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700682 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700683 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700684 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700685 }
686};
687
688/** Represents <supports-screen> elements. **/
689class SupportsScreen : public ManifestExtractor::Element {
690 public:
691 SupportsScreen() = default;
692 int32_t small_screen = 1;
693 int32_t normal_screen = 1;
694 int32_t large_screen = 1;
695 int32_t xlarge_screen = 1;
696 int32_t any_density = 1;
697 int32_t requires_smallest_width_dp = 0;
698 int32_t compatible_width_limit_dp = 0;
699 int32_t largest_width_limit_dp = 0;
700
701 void Extract(xml::Element* element) override {
702 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
703 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
704 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
705 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
706 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
707
708 requires_smallest_width_dp = GetAttributeIntegerDefault(
709 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
710 compatible_width_limit_dp = GetAttributeIntegerDefault(
711 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
712 largest_width_limit_dp = GetAttributeIntegerDefault(
713 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
714
715 // For modern apps, if screen size buckets haven't been specified
716 // but the new width ranges have, then infer the buckets from them.
717 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
718 && requires_smallest_width_dp > 0) {
719 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
720 : requires_smallest_width_dp;
721 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
722 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
723 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
724 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
725 }
726 }
727
Ryan Mitchell214846d2018-09-19 16:57:01 -0700728 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700729 int32_t small_screen_temp = small_screen;
730 int32_t normal_screen_temp = normal_screen;
731 int32_t large_screen_temp = large_screen;
732 int32_t xlarge_screen_temp = xlarge_screen;
733 int32_t any_density_temp = any_density;
734
735 // Determine default values for any unspecified screen sizes,
736 // based on the target SDK of the package. As of 4 (donut)
737 // the screen size support was introduced, so all default to
738 // enabled.
739 if (small_screen_temp > 0) {
740 small_screen_temp = target_sdk >= 4 ? -1 : 0;
741 }
742 if (normal_screen_temp > 0) {
743 normal_screen_temp = -1;
744 }
745 if (large_screen_temp > 0) {
746 large_screen_temp = target_sdk >= 4 ? -1 : 0;
747 }
748 if (xlarge_screen_temp > 0) {
749 // Introduced in Gingerbread.
750 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
751 }
752 if (any_density_temp > 0) {
753 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
754 || compatible_width_limit_dp > 0) ? -1 : 0;
755 }
756
757 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700758 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700759 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700760 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700761 }
762 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700763 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700764 }
765 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700766 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700767 }
768 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700769 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700770 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700771 printer->Print("\n");
772 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700773 (any_density_temp ) ? "true" : "false"));
774 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700775 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700776 }
777 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700778 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700779 }
780 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700781 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700782 }
783 }
784};
785
786/** Represents <feature-group> elements. **/
787class FeatureGroup : public ManifestExtractor::Element {
788 public:
789 FeatureGroup() = default;
790 std::string label;
791 int32_t open_gles_version = 0;
792
793 void Extract(xml::Element* element) override {
794 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
795 }
796
Ryan Mitchell214846d2018-09-19 16:57:01 -0700797 virtual void PrintGroup(text::Printer* printer) {
798 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700799 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700800 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700801 }
802
803 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700804 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700805 (feature.second.required ? "" : "-not-required"),
806 feature.first.data()));
807 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700808 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700809 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700810 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700811 }
812 }
813
814 /** Adds a feature to the feature group. */
815 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
816 features_.insert(std::make_pair(name, Feature{ required, version }));
817 if (required) {
818 if (name == "android.hardware.camera.autofocus" ||
819 name == "android.hardware.camera.flash") {
820 AddFeature("android.hardware.camera", true);
821 } else if (name == "android.hardware.location.gps" ||
822 name == "android.hardware.location.network") {
823 AddFeature("android.hardware.location", true);
824 } else if (name == "android.hardware.faketouch.multitouch") {
825 AddFeature("android.hardware.faketouch", true);
826 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
827 name == "android.hardware.faketouch.multitouch.jazzhands") {
828 AddFeature("android.hardware.faketouch.multitouch", true);
829 AddFeature("android.hardware.faketouch", true);
830 } else if (name == "android.hardware.touchscreen.multitouch") {
831 AddFeature("android.hardware.touchscreen", true);
832 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
833 name == "android.hardware.touchscreen.multitouch.jazzhands") {
834 AddFeature("android.hardware.touchscreen.multitouch", true);
835 AddFeature("android.hardware.touchscreen", true);
836 } else if (name == "android.hardware.opengles.aep") {
837 const int kOpenGLESVersion31 = 0x00030001;
838 if (kOpenGLESVersion31 > open_gles_version) {
839 open_gles_version = kOpenGLESVersion31;
840 }
841 }
842 }
843 }
844
845 /** Returns true if the feature group has the given feature. */
846 virtual bool HasFeature(const std::string& name) {
847 return features_.find(name) != features_.end();
848 }
849
850 /** Merges the features of another feature group into this group. */
851 void Merge(FeatureGroup* group) {
852 open_gles_version = std::max(open_gles_version, group->open_gles_version);
853 for (auto& feature : group->features_) {
854 features_.insert(feature);
855 }
856 }
857
858 protected:
859 struct Feature {
860 public:
861 bool required = false;
862 int32_t version = -1;
863 };
864
865 /* Mapping of feature names to their properties. */
866 std::map<std::string, Feature> features_;
867};
868
869/**
870 * Represents the default feature group for the application if no <feature-group> elements are
871 * present in the manifest.
872 **/
873class CommonFeatureGroup : public FeatureGroup {
874 public:
875 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700876 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700877 FeatureGroup::PrintGroup(printer);
878
879 // Also print the implied features
880 for (auto feature : implied_features_) {
881 if (features_.find(feature.first) == features_.end()) {
882 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700883 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
884 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700885 feature.first.data()));
886
887 // Print the reasons as a sentence
888 size_t count = 0;
889 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700890 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700891 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700892 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700893 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700894 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700895 }
896 count++;
897 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 }
900 }
901 }
902
903 /** Returns true if the feature group has the given feature. */
904 bool HasFeature(const std::string& name) override {
905 return FeatureGroup::HasFeature(name)
906 || implied_features_.find(name) != implied_features_.end();
907 }
908
909 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
910 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
911 auto entry = implied_features_.find(name);
912 if (entry == implied_features_.end()) {
913 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
914 entry = implied_features_.find(name);
915 }
916
917 // A non-sdk 23 implied feature takes precedence.
918 if (entry->second.implied_from_sdk_k23 && !sdk23) {
919 entry->second.implied_from_sdk_k23 = false;
920 }
921
922 entry->second.reasons.insert(reason);
923 }
924
925 /**
926 * Adds a feature to a set of implied features for all features that are implied by the presence
927 * of the permission.
928 **/
929 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
930 if (name == "android.permission.CAMERA") {
931 addImpliedFeature("android.hardware.camera",
932 StringPrintf("requested %s permission", name.data()),
933 sdk23);
934
935 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
936 if (targetSdk < SDK_LOLLIPOP) {
937 addImpliedFeature("android.hardware.location.gps",
938 StringPrintf("requested %s permission", name.data()),
939 sdk23);
940 addImpliedFeature("android.hardware.location.gps",
941 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
942 sdk23);
943 }
944 addImpliedFeature("android.hardware.location",
945 StringPrintf("requested %s permission", name.data()),
946 sdk23);
947
948 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
949 if (targetSdk < SDK_LOLLIPOP) {
950 addImpliedFeature("android.hardware.location.network",
951 StringPrintf("requested %s permission", name.data()),
952 sdk23);
953 addImpliedFeature("android.hardware.location.network",
954 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
955 sdk23);
956 }
957 addImpliedFeature("android.hardware.location",
958 StringPrintf("requested %s permission", name.data()),
959 sdk23);
960
961 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
962 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
963 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
964 addImpliedFeature("android.hardware.location",
965 StringPrintf("requested %s permission", name.data()),
966 sdk23);
967
968 } else if (name == "android.permission.BLUETOOTH" ||
969 name == "android.permission.BLUETOOTH_ADMIN") {
970 if (targetSdk > SDK_DONUT) {
971 addImpliedFeature("android.hardware.bluetooth",
972 StringPrintf("requested %s permission", name.data()),
973 sdk23);
974 addImpliedFeature("android.hardware.bluetooth",
975 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
976 sdk23);
977 }
978
979 } else if (name == "android.permission.RECORD_AUDIO") {
980 addImpliedFeature("android.hardware.microphone",
981 StringPrintf("requested %s permission", name.data()),
982 sdk23);
983
984 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
985 name == "android.permission.CHANGE_WIFI_STATE" ||
986 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
987 addImpliedFeature("android.hardware.wifi",
988 StringPrintf("requested %s permission", name.data()),
989 sdk23);
990
991 } else if (name == "android.permission.CALL_PHONE" ||
992 name == "android.permission.CALL_PRIVILEGED" ||
993 name == "android.permission.MODIFY_PHONE_STATE" ||
994 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
995 name == "android.permission.READ_SMS" ||
996 name == "android.permission.RECEIVE_SMS" ||
997 name == "android.permission.RECEIVE_MMS" ||
998 name == "android.permission.RECEIVE_WAP_PUSH" ||
999 name == "android.permission.SEND_SMS" ||
1000 name == "android.permission.WRITE_APN_SETTINGS" ||
1001 name == "android.permission.WRITE_SMS") {
1002 addImpliedFeature("android.hardware.telephony",
1003 "requested a telephony permission",
1004 sdk23);
1005 }
1006 }
1007
1008 private:
1009 /**
1010 * Represents a feature that has been automatically added due to a pre-requisite or for some
1011 * other reason.
1012 */
1013 struct ImpliedFeature {
1014 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1015
1016 /** List of human-readable reasons for why this feature was implied. */
1017 std::set<std::string> reasons;
1018
1019 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1020 bool implied_from_sdk_k23;
1021 };
1022
1023 /* Mapping of implied feature names to their properties. */
1024 std::map<std::string, ImpliedFeature> implied_features_;
1025};
1026
1027/** Represents <uses-feature> elements. **/
1028class UsesFeature : public ManifestExtractor::Element {
1029 public:
1030 UsesFeature() = default;
1031 void Extract(xml::Element* element) override {
1032 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1033 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1034 bool required = GetAttributeIntegerDefault(
1035 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1036 int32_t version = GetAttributeIntegerDefault(
1037 FindAttribute(element, kAndroidNamespace, "version"), 0);
1038
1039 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1040 // common feature group
1041 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1042 if (!feature_group) {
1043 feature_group = extractor()->GetCommonFeatureGroup();
1044 } else {
1045 // All features in side of <feature-group> elements are required.
1046 required = true;
1047 }
1048
1049 if (name) {
1050 feature_group->AddFeature(*name, required, version);
1051 } else if (gl) {
1052 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1053 }
1054 }
1055};
1056
1057/** Represents <uses-permission> elements. **/
1058class UsesPermission : public ManifestExtractor::Element {
1059 public:
1060 UsesPermission() = default;
1061 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001062 std::vector<std::string> requiredFeatures;
1063 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001064 int32_t required = true;
1065 int32_t maxSdkVersion = -1;
1066
1067 void Extract(xml::Element* element) override {
1068 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001069 std::string feature =
1070 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1071 if (!feature.empty()) {
1072 requiredFeatures.push_back(feature);
1073 }
1074 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1075 if (!feature.empty()) {
1076 requiredNotFeatures.push_back(feature);
1077 }
1078
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001079 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1080 maxSdkVersion = GetAttributeIntegerDefault(
1081 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1082
1083 if (!name.empty()) {
1084 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1085 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1086 }
1087 }
1088
Ryan Mitchell214846d2018-09-19 16:57:01 -07001089 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001090 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001091 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001092 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001093 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001094 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001095 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001096 for (const std::string& requiredFeature : requiredFeatures) {
1097 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1098 }
1099 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1100 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1101 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001102 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001103 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001104 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001105 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001106 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001107 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001108 }
1109 }
1110 }
1111
Ryan Mitchell214846d2018-09-19 16:57:01 -07001112 void PrintImplied(text::Printer* printer, const std::string& reason) {
1113 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001114 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001115 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001116 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001117 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001118 }
1119};
1120
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001121/** Represents <required-feature> elements. **/
1122class RequiredFeature : public ManifestExtractor::Element {
1123 public:
1124 RequiredFeature() = default;
1125 std::string name;
1126
1127 void Extract(xml::Element* element) override {
1128 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1129 auto parent_stack = extractor()->parent_stack();
1130 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1131 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1132 uses_permission->requiredFeatures.push_back(name);
1133 }
1134 }
1135};
1136
1137/** Represents <required-not-feature> elements. **/
1138class RequiredNotFeature : public ManifestExtractor::Element {
1139 public:
1140 RequiredNotFeature() = default;
1141 std::string name;
1142
1143 void Extract(xml::Element* element) override {
1144 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1145 auto parent_stack = extractor()->parent_stack();
1146 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1147 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1148 uses_permission->requiredNotFeatures.push_back(name);
1149 }
1150 }
1151};
1152
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001153/** Represents <uses-permission-sdk-23> elements. **/
1154class UsesPermissionSdk23 : public ManifestExtractor::Element {
1155 public:
1156 UsesPermissionSdk23() = default;
1157 const std::string* name = nullptr;
1158 const int32_t* maxSdkVersion = nullptr;
1159
1160 void Extract(xml::Element* element) override {
1161 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1162 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1163
1164 if (name) {
1165 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1166 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1167 }
1168 }
1169
Ryan Mitchell214846d2018-09-19 16:57:01 -07001170 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001171 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001172 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001173 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001174 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001175 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001176 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001177 }
1178 }
1179};
1180
1181/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1182class Permission : public ManifestExtractor::Element {
1183 public:
1184 Permission() = default;
1185 std::string name;
1186
1187 void Extract(xml::Element* element) override {
1188 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1189 }
1190
Ryan Mitchell214846d2018-09-19 16:57:01 -07001191 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001192 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001193 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001194 }
1195 }
1196};
1197
1198/** Represents <activity> elements. **/
1199class Activity : public ManifestExtractor::Element {
1200 public:
1201 Activity() = default;
1202 std::string name;
1203 std::string icon;
1204 std::string label;
1205 std::string banner;
1206
1207 bool has_component_ = false;
1208 bool has_launcher_category = false;
1209 bool has_leanback_launcher_category = false;
1210 bool has_main_action = false;
1211
1212 void Extract(xml::Element* element) override {
1213 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1214 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1215 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1216 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1217
1218 // Retrieve the package name from the manifest
1219 std::string package;
1220 for (auto& parent : extractor()->parent_stack()) {
1221 if (auto manifest = ElementCast<Manifest>(parent)) {
1222 package = manifest->package;
1223 break;
1224 }
1225 }
1226
1227 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001228 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001229 if (idx == 0) {
1230 name = package + name;
1231 } else if (idx < 0) {
1232 name = package + "." + name;
1233 }
1234
1235 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1236 if (orientation) {
1237 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1238 int orien = *orientation;
1239 if (orien == 0 || orien == 6 || orien == 8) {
1240 // Requests landscape, sensorLandscape, or reverseLandscape.
1241 common->addImpliedFeature("android.hardware.screen.landscape",
1242 "one or more activities have specified a landscape orientation",
1243 false);
1244 } else if (orien == 1 || orien == 7 || orien == 9) {
1245 // Requests portrait, sensorPortrait, or reversePortrait.
1246 common->addImpliedFeature("android.hardware.screen.portrait",
1247 "one or more activities have specified a portrait orientation",
1248 false);
1249 }
1250 }
1251 }
1252
Ryan Mitchell214846d2018-09-19 16:57:01 -07001253 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001254 // Print whether the activity has the HOME category and a the MAIN action
1255 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001256 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001257 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001258 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001259 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001260 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001261 android::ResTable::normalizeForOutput(label.data()).c_str(),
1262 icon.data()));
1263 }
1264
1265 // Print wether the activity has the HOME category and a the MAIN action
1266 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001267 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001268 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001269 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001270 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001271 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001272 android::ResTable::normalizeForOutput(label.data()).c_str(),
1273 icon.data(), banner.data()));
1274 }
1275 }
1276};
1277
1278/** Represents <intent-filter> elements. */
1279class IntentFilter : public ManifestExtractor::Element {
1280 public:
1281 IntentFilter() = default;
1282};
1283
1284/** Represents <category> elements. */
1285class Category : public ManifestExtractor::Element {
1286 public:
1287 Category() = default;
1288 std::string component = "";
1289
1290 void Extract(xml::Element* element) override {
1291 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1292
1293 auto parent_stack = extractor()->parent_stack();
1294 if (category && ElementCast<IntentFilter>(parent_stack[0])
1295 && ElementCast<Activity>(parent_stack[1])) {
1296 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1297
1298 if (*category == "android.intent.category.LAUNCHER") {
1299 activity->has_launcher_category = true;
1300 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1301 activity->has_leanback_launcher_category = true;
1302 } else if (*category == "android.intent.category.HOME") {
1303 component = "launcher";
1304 }
1305 }
1306 }
1307};
1308
1309/**
1310 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1311 * elements nested within.
1312 **/
1313class Provider : public ManifestExtractor::Element {
1314 public:
1315 Provider() = default;
1316 bool has_required_saf_attributes = false;
1317
1318 void Extract(xml::Element* element) override {
1319 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1320 const int32_t* grant_uri_permissions = GetAttributeInteger(
1321 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1322 const std::string* permission = GetAttributeString(
1323 FindAttribute(element, PERMISSION_ATTR));
1324
1325 has_required_saf_attributes = ((exported && *exported != 0)
1326 && (grant_uri_permissions && *grant_uri_permissions != 0)
1327 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1328 }
1329};
1330
1331/** Represents <receiver> elements. **/
1332class Receiver : public ManifestExtractor::Element {
1333 public:
1334 Receiver() = default;
1335 const std::string* permission = nullptr;
1336 bool has_component = false;
1337
1338 void Extract(xml::Element* element) override {
1339 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1340 }
1341};
1342
1343/**Represents <service> elements. **/
1344class Service : public ManifestExtractor::Element {
1345 public:
1346 Service() = default;
1347 const std::string* permission = nullptr;
1348 bool has_component = false;
1349
1350 void Extract(xml::Element* element) override {
1351 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1352 }
1353};
1354
1355/** Represents <uses-library> elements. **/
1356class UsesLibrary : public ManifestExtractor::Element {
1357 public:
1358 UsesLibrary() = default;
1359 std::string name;
1360 int required;
1361
1362 void Extract(xml::Element* element) override {
1363 auto parent_stack = extractor()->parent_stack();
1364 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1365 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1366 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1367 }
1368 }
1369
Ryan Mitchell214846d2018-09-19 16:57:01 -07001370 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001371 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001372 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001373 (required == 0) ? "-not-required" : "", name.data()));
1374 }
1375 }
1376};
1377
Dianne Hackborn813d7502018-10-02 16:59:46 -07001378/** Represents <static-library> elements. **/
1379class StaticLibrary : public ManifestExtractor::Element {
1380 public:
1381 StaticLibrary() = default;
1382 std::string name;
1383 int version;
1384 int versionMajor;
1385
1386 void Extract(xml::Element* element) override {
1387 auto parent_stack = extractor()->parent_stack();
1388 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1389 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1390 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1391 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1392 }
1393 }
1394
Ryan Mitchell214846d2018-09-19 16:57:01 -07001395 void Print(text::Printer* printer) override {
1396 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001397 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1398 name.data(), version, versionMajor));
1399 }
1400};
1401
1402/** Represents <uses-static-library> elements. **/
1403class UsesStaticLibrary : public ManifestExtractor::Element {
1404 public:
1405 UsesStaticLibrary() = default;
1406 std::string name;
1407 int version;
1408 int versionMajor;
1409 std::vector<std::string> certDigests;
1410
1411 void Extract(xml::Element* element) override {
1412 auto parent_stack = extractor()->parent_stack();
1413 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1414 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1415 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1416 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1417 AddCertDigest(element);
1418 }
1419 }
1420
1421 void AddCertDigest(xml::Element* element) {
1422 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1423 // We allow ":" delimiters in the SHA declaration as this is the format
1424 // emitted by the certtool making it easy for developers to copy/paste.
1425 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1426 if (!digest.empty()) {
1427 certDigests.push_back(digest);
1428 }
1429 }
1430
Ryan Mitchell214846d2018-09-19 16:57:01 -07001431 void Print(text::Printer* printer) override {
1432 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001433 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1434 name.data(), version, versionMajor));
1435 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001436 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001437 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001438 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001439 }
1440};
1441
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001442/** Represents <uses-native-library> elements. **/
1443class UsesNativeLibrary : public ManifestExtractor::Element {
1444 public:
1445 UsesNativeLibrary() = default;
1446 std::string name;
1447 int required;
1448
1449 void Extract(xml::Element* element) override {
1450 auto parent_stack = extractor()->parent_stack();
1451 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1452 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1453 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1454 }
1455 }
1456
1457 void Print(text::Printer* printer) override {
1458 if (!name.empty()) {
1459 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1460 (required == 0) ? "-not-required" : "", name.data()));
1461 }
1462 }
1463};
1464
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001465/**
1466 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1467 * explicitly enable meta data printing.
1468 **/
1469class MetaData : public ManifestExtractor::Element {
1470 public:
1471 MetaData() = default;
1472 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001473 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001474 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001475 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001476 const int* resource_int;
1477
1478 void Extract(xml::Element* element) override {
1479 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001480 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001481 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001482 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001483 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1484 }
1485
Ryan Mitchell214846d2018-09-19 16:57:01 -07001486 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001487 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001488 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001489 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001490 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001491 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001492 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001493 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001494 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001495 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001496 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001497 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001498 }
1499 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001500 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001501 }
1502 }
1503};
1504
1505/**
1506 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1507 * service components.
1508 **/
1509class Action : public ManifestExtractor::Element {
1510 public:
1511 Action() = default;
1512 std::string component = "";
1513
1514 void Extract(xml::Element* element) override {
1515 auto parent_stack = extractor()->parent_stack();
1516 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1517
1518 if (ElementCast<IntentFilter>(parent_stack[0])) {
1519 if (ElementCast<Activity>(parent_stack[1])) {
1520 // Detects the presence of a particular type of activity.
1521 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1522 auto map = std::map<std::string, std::string>({
1523 { "android.intent.action.MAIN" , "main" },
1524 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1525 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1526 });
1527
1528 auto entry = map.find(action);
1529 if (entry != map.end()) {
1530 component = entry->second;
1531 activity->has_component_ = true;
1532 }
1533
1534 if (action == "android.intent.action.MAIN") {
1535 activity->has_main_action = true;
1536 }
1537
1538 } else if (ElementCast<Receiver>(parent_stack[1])) {
1539 // Detects the presence of a particular type of receiver. If the action requires a
1540 // permission, then the receiver element is checked for the permission.
1541 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1542 auto map = std::map<std::string, std::string>({
1543 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1544 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1545 });
1546
1547 auto permissions = std::map<std::string, std::string>({
1548 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1549 });
1550
1551 auto entry = map.find(action);
1552 auto permission = permissions.find(action);
1553 if (entry != map.end() && (permission == permissions.end()
1554 || (receiver->permission && permission->second == *receiver->permission))) {
1555 receiver->has_component = true;
1556 component = entry->second;
1557 }
1558
1559 } else if (ElementCast<Service>(parent_stack[1])) {
1560 // Detects the presence of a particular type of service. If the action requires a
1561 // permission, then the service element is checked for the permission.
1562 Service* service = ElementCast<Service>(parent_stack[1]);
1563 auto map = std::map<std::string, std::string>({
1564 { "android.view.InputMethod" , "ime" },
1565 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1566 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1567 { "android.printservice.PrintService" , "print-service" },
1568 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1569 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1570 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1571 { "android.service.dreams.DreamService" , "dream" },
1572 });
1573
1574 auto permissions = std::map<std::string, std::string>({
1575 { "android.accessibilityservice.AccessibilityService" ,
1576 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1577 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1578 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1579 "android.permission.BIND_NFC_SERVICE" },
1580 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1581 "android.permission.BIND_NFC_SERVICE" },
1582 { "android.service.notification.NotificationListenerService" ,
1583 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1584 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1585 });
1586
1587 auto entry = map.find(action);
1588 auto permission = permissions.find(action);
1589 if (entry != map.end() && (permission == permissions.end()
1590 || (service->permission && permission->second == *service->permission))) {
1591 service->has_component= true;
1592 component = entry->second;
1593 }
1594
1595 } else if (ElementCast<Provider>(parent_stack[1])) {
1596 // Detects the presence of a particular type of receiver. If the provider requires a
1597 // permission, then the provider element is checked for the permission.
1598 // Detect whether this action
1599 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1600 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1601 && provider->has_required_saf_attributes) {
1602 component = "document-provider";
1603 }
1604 }
1605 }
1606
1607 // Represents a searchable interface
1608 if (action == "android.intent.action.SEARCH") {
1609 component = "search";
1610 }
1611 }
1612};
1613
1614/**
1615 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1616 **/
1617class SupportsInput : public ManifestExtractor::Element {
1618 public:
1619 SupportsInput() = default;
1620 std::vector<std::string> inputs;
1621
Ryan Mitchell214846d2018-09-19 16:57:01 -07001622 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001623 const size_t size = inputs.size();
1624 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001625 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001626 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001627 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001628 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001629 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001630 }
1631 }
1632};
1633
1634/** Represents <input-type> elements. **/
1635class InputType : public ManifestExtractor::Element {
1636 public:
1637 InputType() = default;
1638 void Extract(xml::Element* element) override {
1639 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1640 auto parent_stack = extractor()->parent_stack();
1641
1642 // Add the input to the set of supported inputs
1643 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1644 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1645 supports->inputs.push_back(*name);
1646 }
1647 }
1648};
1649
1650/** Represents <original-package> elements. **/
1651class OriginalPackage : public ManifestExtractor::Element {
1652 public:
1653 OriginalPackage() = default;
1654 const std::string* name = nullptr;
1655
1656 void Extract(xml::Element* element) override {
1657 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1658 }
1659
Ryan Mitchell214846d2018-09-19 16:57:01 -07001660 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001661 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001662 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001663 }
1664 }
1665};
1666
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001667
1668/** Represents <overlay> elements. **/
1669class Overlay : public ManifestExtractor::Element {
1670 public:
1671 Overlay() = default;
1672 const std::string* target_package = nullptr;
1673 int priority;
1674 bool is_static;
1675 const std::string* required_property_name = nullptr;
1676 const std::string* required_property_value = nullptr;
1677
1678 void Extract(xml::Element* element) override {
1679 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1680 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1681 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1682 required_property_name = GetAttributeString(
1683 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1684 required_property_value = GetAttributeString(
1685 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1686 }
1687
1688 void Print(text::Printer* printer) override {
1689 printer->Print(StringPrintf("overlay:"));
1690 if (target_package) {
1691 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1692 }
1693 printer->Print(StringPrintf(" priority='%d'", priority));
1694 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1695 if (required_property_name) {
1696 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1697 }
1698 if (required_property_value) {
1699 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1700 }
1701 printer->Print("\n");
1702 }
1703};
1704
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001705/** * Represents <package-verifier> elements. **/
1706class PackageVerifier : public ManifestExtractor::Element {
1707 public:
1708 PackageVerifier() = default;
1709 const std::string* name = nullptr;
1710 const std::string* public_key = nullptr;
1711
1712 void Extract(xml::Element* element) override {
1713 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1714 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1715 }
1716
Ryan Mitchell214846d2018-09-19 16:57:01 -07001717 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001718 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001719 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001720 name->data(), public_key->data()));
1721 }
1722 }
1723};
1724
1725/** Represents <uses-package> elements. **/
1726class UsesPackage : public ManifestExtractor::Element {
1727 public:
1728 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001729 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001730 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001731 int version;
1732 int versionMajor;
1733 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001734
1735 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001736 auto parent_stack = extractor()->parent_stack();
1737 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1738 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1739 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1740 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1741 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1742 AddCertDigest(element);
1743 }
1744 }
1745
1746 void AddCertDigest(xml::Element* element) {
1747 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1748 // We allow ":" delimiters in the SHA declaration as this is the format
1749 // emitted by the certtool making it easy for developers to copy/paste.
1750 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1751 if (!digest.empty()) {
1752 certDigests.push_back(digest);
1753 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001754 }
1755
Ryan Mitchell214846d2018-09-19 16:57:01 -07001756 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001757 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001758 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001759 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001760 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1761 packageType->data(), name->data(), version, versionMajor));
1762 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001763 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001764 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001765 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001766 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001767 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001768 }
1769 }
1770 }
1771};
1772
1773/** Represents <additional-certificate> elements. **/
1774class AdditionalCertificate : public ManifestExtractor::Element {
1775 public:
1776 AdditionalCertificate() = default;
1777
1778 void Extract(xml::Element* element) override {
1779 auto parent_stack = extractor()->parent_stack();
1780 if (parent_stack.size() > 0) {
1781 if (ElementCast<UsesPackage>(parent_stack[0])) {
1782 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1783 uses->AddCertDigest(element);
1784 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1785 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1786 uses->AddCertDigest(element);
1787 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001788 }
1789 }
1790};
1791
1792/** Represents <screen> elements found in <compatible-screens> elements. */
1793class Screen : public ManifestExtractor::Element {
1794 public:
1795 Screen() = default;
1796 const int32_t* size = nullptr;
1797 const int32_t* density = nullptr;
1798
1799 void Extract(xml::Element* element) override {
1800 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1801 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1802 }
1803};
1804
1805/**
1806 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1807 * that each denote a supported screen size and screen density.
1808 **/
1809class CompatibleScreens : public ManifestExtractor::Element {
1810 public:
1811 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001812 void Print(text::Printer* printer) override {
1813 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001814
1815 bool first = true;
1816 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1817 if (auto screen = ElementCast<Screen>(el)) {
1818 if (first) {
1819 first = false;
1820 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001821 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001822 }
1823
1824 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001825 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001826 }
1827 }
1828 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001829 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001830 }
1831};
1832
1833/** Represents <supports-gl-texture> elements. **/
1834class SupportsGlTexture : public ManifestExtractor::Element {
1835 public:
1836 SupportsGlTexture() = default;
1837 const std::string* name = nullptr;
1838
1839 void Extract(xml::Element* element) override {
1840 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1841 }
1842
Ryan Mitchell214846d2018-09-19 16:57:01 -07001843 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001844 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001845 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001846 }
1847 }
1848};
1849
Todd Kennedyce3e1292020-10-29 17:14:24 -07001850/** Represents <property> elements. **/
1851class Property : public ManifestExtractor::Element {
1852 public:
1853 Property() = default;
1854 std::string name;
1855 std::string value;
1856 const int* value_int;
1857 std::string resource;
1858 const int* resource_int;
1859
1860 void Extract(xml::Element* element) override {
1861 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1862 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
1863 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
1864 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
1865 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1866 }
1867
1868 void Print(text::Printer* printer) override {
1869 printer->Print(StringPrintf("property: name='%s' ", name.data()));
1870 if (!value.empty()) {
1871 printer->Print(StringPrintf("value='%s' ", value.data()));
1872 } else if (value_int) {
1873 printer->Print(StringPrintf("value='%d' ", *value_int));
1874 } else {
1875 if (!resource.empty()) {
1876 printer->Print(StringPrintf("resource='%s' ", resource.data()));
1877 } else if (resource_int) {
1878 printer->Print(StringPrintf("resource='%d' ", *resource_int));
1879 }
1880 }
1881 printer->Print("\n");
1882 }
1883};
1884
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001885/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001886static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001887 el->Print(printer);
1888 for (auto &child : el->children()) {
1889 Print(child.get(), printer);
1890 }
1891}
1892
Ryan Mitchell214846d2018-09-19 16:57:01 -07001893bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001894 // Load the manifest
1895 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1896 if (doc == nullptr) {
1897 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1898 return false;
1899 }
1900
1901 xml::Element* element = doc->root.get();
1902 if (element->name != "manifest") {
1903 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1904 return false;
1905 }
1906
1907 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1908 // printing only permission elements is requested
1909 if (options_.only_permissions) {
1910 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1911 ManifestExtractor::Element::Inflate(this, element);
1912
1913 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1914 for (xml::Element* child : element->GetChildElements()) {
1915 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1916 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001917 // Inflate the element and its descendants
1918 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001919 manifest->AddChild(permission_element);
1920 }
1921 }
1922
Ryan Mitchell214846d2018-09-19 16:57:01 -07001923 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001924 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1925 el->Print(printer);
1926 });
1927
1928 return true;
1929 }
1930
1931 return false;
1932 }
1933
1934 // Collect information about the resource configurations
1935 if (apk_->GetResourceTable()) {
1936 for (auto &package : apk_->GetResourceTable()->packages) {
1937 for (auto &type : package->types) {
1938 for (auto &entry : type->entries) {
1939 for (auto &value : entry->values) {
1940 std::string locale_str = value->config.GetBcp47LanguageTag();
1941
1942 // Collect all the unique locales of the apk
1943 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001944 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001945 config.setBcp47Locale(locale_str.data());
1946 locales_.insert(std::make_pair(locale_str, config));
1947 }
1948
1949 // Collect all the unique density of the apk
1950 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1951 : value->config.density;
1952 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001953 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001954 config.density = density;
1955 densities_.insert(std::make_pair(density, config));
1956 }
1957 }
1958 }
1959 }
1960 }
1961 }
1962
1963 // Extract badging information
1964 auto root = Visit(element);
1965
1966 // Print the elements in order seen
1967 Print(root.get(), printer);
1968
1969 /** Recursively checks the extracted elements for the specified permission. **/
1970 auto FindPermission = [&](ManifestExtractor::Element* root,
1971 const std::string& name) -> ManifestExtractor::Element* {
1972 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1973 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1974 return permission->name == name;
1975 }
1976 return false;
1977 });
1978 };
1979
1980 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1981 int32_t max_sdk_version) -> void {
1982 auto permission = util::make_unique<UsesPermission>();
1983 permission->name = name;
1984 permission->maxSdkVersion = max_sdk_version;
1985 permission->Print(printer);
1986 permission->PrintImplied(printer, reason);
1987 };
1988
1989 // Implied permissions
1990 // Pre-1.6 implicitly granted permission compatibility logic
1991 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
1992 bool insert_write_external = false;
1993 auto write_external_permission = ElementCast<UsesPermission>(
1994 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
1995
1996 if (target_sdk() < 4) {
1997 if (!write_external_permission) {
1998 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
1999 insert_write_external = true;
2000 }
2001
2002 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
2003 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
2004 }
2005 }
2006
2007 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2008 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2009 // do this (regardless of target API version) because we can't have
2010 // an app with write permission but not read permission.
2011 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
2012 if (!read_external && (insert_write_external || write_external_permission)) {
2013 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
2014 "requested WRITE_EXTERNAL_STORAGE",
2015 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
2016 }
2017
2018 // Pre-JellyBean call log permission compatibility.
2019 if (target_sdk() < 16) {
2020 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
2021 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
2022 PrintPermission("android.permission.READ_CALL_LOG",
2023 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
2024 }
2025
2026 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
2027 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
2028 PrintPermission("android.permission.WRITE_CALL_LOG",
2029 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
2030 }
2031 }
2032
2033 // If the app hasn't declared the touchscreen as a feature requirement (either
2034 // directly or implied, required or not), then the faketouch feature is implied.
2035 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
2036 common_feature_group->addImpliedFeature("android.hardware.faketouch",
2037 "default feature for all apps", false);
2038 }
2039
2040 // Only print the common feature group if no feature group is defined
2041 std::vector<FeatureGroup*> feature_groups;
2042 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
2043 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2044 feature_groups.push_back(feature_group);
2045 }
2046 });
2047
2048 if (feature_groups.empty()) {
2049 common_feature_group->PrintGroup(printer);
2050 } else {
2051 // Merge the common feature group into the feature group
2052 for (auto& feature_group : feature_groups) {
2053 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
2054 common_feature_group->open_gles_version);
2055 feature_group->Merge(common_feature_group);
2056 feature_group->PrintGroup(printer);
2057 }
2058 };
2059
2060 // Collect the component types of the application
2061 std::set<std::string> components;
2062 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
2063 if (ElementCast<Action>(el)) {
2064 auto action = ElementCast<Action>(el);
2065 if (!action->component.empty()) {
2066 components.insert(action->component);
2067 return;
2068 }
2069 }
2070
2071 if (ElementCast<Category>(el)) {
2072 auto category = ElementCast<Category>(el);
2073 if (!category->component.empty()) {
2074 components.insert(category->component);
2075 return;
2076 }
2077 }
2078 });
2079
2080 // Check for the payment component
2081 auto apk = apk_;
2082 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2083 if (auto service = ElementCast<Service>(el)) {
2084 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2085 [&](ManifestExtractor::Element* el) -> bool {
2086 if (auto action = ElementCast<Action>(el)) {
2087 return (action->component == "host-apdu");
2088 }
2089 return false;
2090 }));
2091
2092 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2093 [&](ManifestExtractor::Element* el) -> bool {
2094 if (auto action = ElementCast<Action>(el)) {
2095 return (action->component == "offhost-apdu");
2096 }
2097 return false;
2098 }));
2099
2100 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2101 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2102 if (auto meta_data = ElementCast<MetaData>(el)) {
2103 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2104 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2105 && offhost_apdu_action)) {
2106
2107 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002108 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002109 return;
2110 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002111 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002112 if (!resource) {
2113 return;
2114 }
2115
2116 // Look for the payment category on an <aid-group> element
2117 auto& root = resource.get()->root;
2118 if ((host_apdu_action && root->name == "host-apdu-service")
2119 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2120
2121 for (auto& child : root->GetChildElements()) {
2122 if (child->name == "aid-group") {
2123 auto category = FindAttribute(child, CATEGORY_ATTR);
2124 if (category && category->value == "payment") {
2125 components.insert("payment");
2126 return;
2127 }
2128 }
2129 }
2130 }
2131 }
2132 }
2133 });
2134 }
2135 });
2136
2137 // Print the components types if they are present
2138 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2139 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002140 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002141 }
2142 };
2143
2144 PrintComponent("app-widget");
2145 PrintComponent("device-admin");
2146 PrintComponent("ime");
2147 PrintComponent("wallpaper");
2148 PrintComponent("accessibility");
2149 PrintComponent("print-service");
2150 PrintComponent("payment");
2151 PrintComponent("search");
2152 PrintComponent("document-provider");
2153 PrintComponent("launcher");
2154 PrintComponent("notification-listener");
2155 PrintComponent("dream");
2156 PrintComponent("camera");
2157 PrintComponent("camera-secure");
2158
2159 // Print presence of main activity
2160 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002161 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002162 }
2163
2164 // Print presence of activities, recivers, and services with no special components
2165 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2166 if (auto activity = ElementCast<Activity>(el)) {
2167 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002168 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002169 return true;
2170 }
2171 }
2172 return false;
2173 });
2174
2175 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2176 if (auto receiver = ElementCast<Receiver>(el)) {
2177 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002178 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002179 return true;
2180 }
2181 }
2182 return false;
2183 });
2184
2185 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2186 if (auto service = ElementCast<Service>(el)) {
2187 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002188 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002189 return true;
2190 }
2191 }
2192 return false;
2193 });
2194
2195 // Print the supported screens
2196 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2197 [&](ManifestExtractor::Element* el) -> bool {
2198 return ElementCast<SupportsScreen>(el) != nullptr;
2199 }));
2200
2201 if (screen) {
2202 screen->PrintScreens(printer, target_sdk_);
2203 } else {
2204 // Print the default supported screens
2205 SupportsScreen default_screens;
2206 default_screens.PrintScreens(printer, target_sdk_);
2207 }
2208
2209 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002210 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002211 for (auto& config : locales_) {
2212 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002213 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002214 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002215 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002216 }
2217 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002218 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002219
2220 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002221 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002222 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002223 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002224 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002225 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002226
2227 // Print the supported architectures of the app
2228 std::set<std::string> architectures;
2229 auto it = apk_->GetFileCollection()->Iterator();
2230 while (it->HasNext()) {
2231 auto file_path = it->Next()->GetSource().path;
2232
2233
2234 size_t pos = file_path.find("lib/");
2235 if (pos != std::string::npos) {
2236 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002237 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002238 if (pos != std::string::npos) {
2239 file_path = file_path.substr(0, pos);
2240 }
2241
2242 architectures.insert(file_path);
2243 }
2244 }
2245
2246 // Determine if the application has multiArch supports
2247 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2248 if (auto application = ElementCast<Application>(el)) {
2249 return application->has_multi_arch;
2250 }
2251 return false;
2252 });
2253
2254 bool output_alt_native_code = false;
2255 // A multiArch package is one that contains 64-bit and
2256 // 32-bit versions of native code and expects 3rd-party
2257 // apps to load these native code libraries. Since most
2258 // 64-bit systems also support 32-bit apps, the apps
2259 // loading this multiArch package's code may be either
2260 if (has_multi_arch) {
2261 // If this is a multiArch package, report the 64-bit
2262 // version only. Then as a separate entry, report the
2263 // rest.
2264 //
2265 // If we report the 32-bit architecture, this APK will
2266 // be installed on a 32-bit device, causing a large waste
2267 // of bandwidth and disk space. This assumes that
2268 // the developer of the multiArch package has also
2269 // made a version that is 32-bit only.
2270 const std::string kIntel64 = "x86_64";
2271 const std::string kArm64 = "arm64-v8a";
2272
2273 auto arch = architectures.find(kIntel64);
2274 if (arch == architectures.end()) {
2275 arch = architectures.find(kArm64);
2276 }
2277
2278 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002279 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002280 architectures.erase(arch);
2281 output_alt_native_code = true;
2282 }
2283 }
2284
2285 if (architectures.size() > 0) {
2286 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002287 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002288 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002289 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002290 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002291 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002292 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002293 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002294 }
2295
2296 return true;
2297}
2298
2299/**
2300 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2301 * pointer.
2302 **/
2303template<typename T>
2304T* ElementCast(ManifestExtractor::Element* element) {
2305 if (element == nullptr) {
2306 return nullptr;
2307 }
2308
2309 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002310 {"action", std::is_base_of<Action, T>::value},
2311 {"activity", std::is_base_of<Activity, T>::value},
2312 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2313 {"application", std::is_base_of<Application, T>::value},
2314 {"category", std::is_base_of<Category, T>::value},
2315 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2316 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2317 {"input-type", std::is_base_of<InputType, T>::value},
2318 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2319 {"meta-data", std::is_base_of<MetaData, T>::value},
2320 {"manifest", std::is_base_of<Manifest, T>::value},
2321 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2322 {"overlay", std::is_base_of<Overlay, T>::value},
2323 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2324 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002325 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002326 {"provider", std::is_base_of<Provider, T>::value},
2327 {"receiver", std::is_base_of<Receiver, T>::value},
2328 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2329 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2330 {"screen", std::is_base_of<Screen, T>::value},
2331 {"service", std::is_base_of<Service, T>::value},
2332 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2333 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2334 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2335 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2336 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2337 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2338 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2339 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2340 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2341 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2342 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2343 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2344 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002345 };
2346
2347 auto check = kTagCheck.find(element->tag());
2348 if (check != kTagCheck.end() && check->second) {
2349 return static_cast<T*>(element);
2350 }
2351 return nullptr;
2352}
2353
2354template<typename T>
2355std::unique_ptr<T> CreateType() {
2356 return std::move(util::make_unique<T>());
2357}
2358
2359std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2360 ManifestExtractor* extractor, xml::Element* el) {
2361 const std::unordered_map<std::string,
2362 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2363 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002364 {"action", &CreateType<Action>},
2365 {"activity", &CreateType<Activity>},
2366 {"additional-certificate", &CreateType<AdditionalCertificate>},
2367 {"application", &CreateType<Application>},
2368 {"category", &CreateType<Category>},
2369 {"compatible-screens", &CreateType<CompatibleScreens>},
2370 {"feature-group", &CreateType<FeatureGroup>},
2371 {"input-type", &CreateType<InputType>},
2372 {"intent-filter", &CreateType<IntentFilter>},
2373 {"manifest", &CreateType<Manifest>},
2374 {"meta-data", &CreateType<MetaData>},
2375 {"original-package", &CreateType<OriginalPackage>},
2376 {"overlay", &CreateType<Overlay>},
2377 {"package-verifier", &CreateType<PackageVerifier>},
2378 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002379 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002380 {"provider", &CreateType<Provider>},
2381 {"receiver", &CreateType<Receiver>},
2382 {"required-feature", &CreateType<RequiredFeature>},
2383 {"required-not-feature", &CreateType<RequiredNotFeature>},
2384 {"screen", &CreateType<Screen>},
2385 {"service", &CreateType<Service>},
2386 {"static-library", &CreateType<StaticLibrary>},
2387 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2388 {"supports-input", &CreateType<SupportsInput>},
2389 {"supports-screens", &CreateType<SupportsScreen>},
2390 {"uses-configuration", &CreateType<UsesConfiguarion>},
2391 {"uses-feature", &CreateType<UsesFeature>},
2392 {"uses-library", &CreateType<UsesLibrary>},
2393 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2394 {"uses-package", &CreateType<UsesPackage>},
2395 {"uses-permission", &CreateType<UsesPermission>},
2396 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2397 {"uses-sdk", &CreateType<UsesSdkBadging>},
2398 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2399 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002400
2401 // Attempt to map the xml tag to a element inflater
2402 std::unique_ptr<ManifestExtractor::Element> element;
2403 auto check = kTagCheck.find(el->name);
2404 if (check != kTagCheck.end()) {
2405 element = check->second();
2406 } else {
2407 element = util::make_unique<ManifestExtractor::Element>();
2408 }
2409
2410 element->extractor_ = extractor;
2411 element->tag_ = el->name;
2412 element->Extract(el);
2413 return element;
2414}
2415
2416std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2417 auto element = ManifestExtractor::Element::Inflate(this, el);
2418 parent_stack_.insert(parent_stack_.begin(), element.get());
2419
2420 // Process the element and recursively visit the children
2421 for (xml::Element* child : el->GetChildElements()) {
2422 auto v = Visit(child);
2423 element->AddChild(v);
2424 }
2425
2426 parent_stack_.erase(parent_stack_.begin());
2427 return element;
2428}
2429
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002430
Ryan Mitchell214846d2018-09-19 16:57:01 -07002431int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2432 IDiagnostics* diag) {
2433 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002434 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002435}
2436
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002437} // namespace aapt