blob: cc1cf7f2324623cfc879e86f1728b30b8766aeca [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) {
194 if (package->id && package->id.value() == res_id.package_id()) {
195 for (auto& type : package->types) {
196 if (type->id && type->id.value() == res_id.type_id()) {
197 for (auto& entry : type->entries) {
198 if (entry->id && entry->id.value() == res_id.entry_id()) {
199 if (auto value = BestConfigValue(entry.get(), config)) {
200 return value;
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 return nullptr;
210 }
211
212 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700213 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700214 const int kMaxIterations = 40;
215 int i = 0;
216 while (ref && ref->id && i++ < kMaxIterations) {
217 auto table = extractor_->apk_->GetResourceTable();
218 if (auto value = FindValueById(table, ref->id.value(), config)) {
219 if (ValueCast<Reference>(value)) {
220 ref = ValueCast<Reference>(value);
221 } else {
222 return value;
223 }
224 }
225 }
226 return nullptr;
227 }
228
229 /**
230 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
231 * this will attempt to resolve the reference to an integer value.
232 **/
233 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700234 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700235 if (attr != nullptr) {
236 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700237 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700238 Value* value = attr->compiled_value.get();
239 if (ValueCast<Reference>(value)) {
240 value = ResolveReference(ValueCast<Reference>(value), config);
241 } else {
242 value = attr->compiled_value.get();
243 }
244 // Retrieve the integer data if possible
245 if (value != nullptr) {
246 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
247 return (int32_t*) &intValue->value.data;
248 }
249 }
250 }
251 }
252 return nullptr;
253 }
254
255 /**
256 * A version of GetAttributeInteger that returns a default integer if the attribute does not
257 * exist or cannot be resolved to an integer value.
258 **/
259 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700260 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700261 auto value = GetAttributeInteger(attr, config);
262 if (value) {
263 return *value;
264 }
265 return def;
266 }
267
268 /**
269 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
270 * this will attempt to resolve the reference to a string value.
271 **/
272 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700273 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700274 if (attr != nullptr) {
275 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700276 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700277 Value* value = attr->compiled_value.get();
278 if (ValueCast<Reference>(value)) {
279 value = ResolveReference(ValueCast<Reference>(value), config);
280 } else {
281 value = attr->compiled_value.get();
282 }
283
284 // Retrieve the string data of the value if possible
285 if (value != nullptr) {
286 if (String* intValue = ValueCast<String>(value)) {
287 return &(*intValue->value);
288 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
289 return &(*rawValue->value);
290 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
291 return &(*strValue->path);
292 }
293 }
294 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700295
296 if (!attr->value.empty()) {
297 return &attr->value;
298 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700299 }
300 return nullptr;
301 }
302
303 /**
304 * A version of GetAttributeString that returns a default string if the attribute does not
305 * exist or cannot be resolved to an string value.
306 **/
307 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700308 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700309 auto value = GetAttributeString(attr, config);
310 if (value) {
311 return *value;
312 }
313 return def;
314 }
315
316 private:
317 ManifestExtractor* extractor_;
318 std::vector<std::unique_ptr<Element>> children_;
319 std::string tag_;
320 };
321
322 friend Element;
323
324 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700325 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700326 ConfigDescription config;
327 config.orientation = android::ResTable_config::ORIENTATION_PORT;
328 config.density = android::ResTable_config::DENSITY_MEDIUM;
Ryan Mitchell95f02422020-04-30 10:25:53 -0700329 config.sdkVersion = kCurrentDevelopmentVersion; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700330 config.screenWidthDp = 320;
331 config.screenHeightDp = 480;
332 config.smallestScreenWidthDp = 320;
333 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
334 return config;
335 }
336
Ryan Mitchell214846d2018-09-19 16:57:01 -0700337 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700338
339 /** Recursively visit the xml element tree and return a processed badging element tree. */
340 std::unique_ptr<Element> Visit(xml::Element* element);
341
342 /** Raises the target sdk value if the min target is greater than the current target. */
343 void RaiseTargetSdk(int32_t min_target) {
344 if (min_target > target_sdk_) {
345 target_sdk_ = min_target;
346 }
347 }
348
349 /**
350 * Retrieves the default feature group that features are added into when <uses-feature>
351 * are not in a <feature-group> element.
352 **/
353 CommonFeatureGroup* GetCommonFeatureGroup() {
354 return commonFeatureGroup_.get();
355 }
356
357 /**
358 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
359 * used for that density setting.
360 **/
361 const std::map<uint16_t, ConfigDescription> densities() const {
362 return densities_;
363 }
364
365 /**
366 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
367 * would be used for that locale setting.
368 **/
369 const std::map<std::string, ConfigDescription> locales() const {
370 return locales_;
371 }
372
373 /** Retrieves the current stack of parent during data extraction. */
374 const std::vector<Element*> parent_stack() const {
375 return parent_stack_;
376 }
377
378 int32_t target_sdk() const {
379 return target_sdk_;
380 }
381
382 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700383 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700384
385 private:
386 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
387 std::map<std::string, ConfigDescription> locales_;
388 std::map<uint16_t, ConfigDescription> densities_;
389 std::vector<Element*> parent_stack_;
390 int32_t target_sdk_ = 0;
391};
392
393template<typename T> T* ElementCast(ManifestExtractor::Element* element);
394
395/** Recurs through the children of the specified root in depth-first order. */
396static void ForEachChild(ManifestExtractor::Element* root,
397 std::function<void(ManifestExtractor::Element*)> f) {
398 for (auto& child : root->children()) {
399 f(child.get());
400 ForEachChild(child.get(), f);
401 }
402}
403
404/**
405 * Checks the element and its recursive children for an element that makes the specified
406 * conditional function return true. Returns the first element that makes the conditional function
407 * return true.
408 **/
409static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
410 std::function<bool(ManifestExtractor::Element*)> f) {
411 if (f(root)) {
412 return root;
413 }
414 for (auto& child : root->children()) {
415 if (auto b2 = FindElement(child.get(), f)) {
416 return b2;
417 }
418 }
419 return nullptr;
420}
421
422/** Represents the <manifest> elements **/
423class Manifest : public ManifestExtractor::Element {
424 public:
425 Manifest() = default;
426 std::string package;
427 int32_t versionCode;
428 std::string versionName;
429 const std::string* split = nullptr;
430 const std::string* platformVersionName = nullptr;
431 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700432 const int32_t* platformVersionNameInt = nullptr;
433 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700434 const int32_t* compilesdkVersion = nullptr;
435 const std::string* compilesdkVersionCodename = nullptr;
436 const int32_t* installLocation = nullptr;
437
438 void Extract(xml::Element* manifest) override {
439 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
440 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
441 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
442 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
443
444 // Extract the platform build info
445 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
446 "platformBuildVersionName"));
447 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
448 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700449 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
450 "platformBuildVersionName"));
451 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
452 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700453
454 // Extract the compile sdk info
455 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
456 compilesdkVersionCodename = GetAttributeString(
457 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
458 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
459 }
460
Ryan Mitchell214846d2018-09-19 16:57:01 -0700461 void Print(text::Printer* printer) override {
462 printer->Print(StringPrintf("package: name='%s' ", package.data()));
463 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700464 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700465 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700466
467 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700468 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700469 }
470 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700471 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700472 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700473 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
474 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700475 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700476 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700477 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700478 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
479 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700480 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700481 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700482 }
483 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700484 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700485 compilesdkVersionCodename->data()));
486 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700487 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700488
489 if (installLocation) {
490 switch (*installLocation) {
491 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700492 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700493 break;
494 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700495 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700496 break;
497 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700498 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700499 break;
500 default:
501 break;
502 }
503 }
504 }
505};
506
507/** Represents <application> elements. **/
508class Application : public ManifestExtractor::Element {
509 public:
510 Application() = default;
511 std::string label;
512 std::string icon;
513 std::string banner;
514 int32_t is_game;
515 int32_t debuggable;
516 int32_t test_only;
517 bool has_multi_arch;
518
519 /** Mapping from locales to app names. */
520 std::map<std::string, std::string> locale_labels;
521
522 /** Mapping from densities to app icons. */
523 std::map<uint16_t, std::string> density_icons;
524
525 void Extract(xml::Element* element) override {
526 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
527 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
528 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
529 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
530 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
531 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
532
533 // We must search by name because the multiArch flag hasn't been API
534 // frozen yet.
535 has_multi_arch = (GetAttributeIntegerDefault(
536 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
537
538 // Retrieve the app names for every locale the app supports
539 auto attr = FindAttribute(element, LABEL_ATTR);
540 for (auto& config : extractor()->locales()) {
541 if (auto label = GetAttributeString(attr, config.second)) {
542 if (label) {
543 locale_labels.insert(std::make_pair(config.first, *label));
544 }
545 }
546 }
547
548 // Retrieve the icons for the densities the app supports
549 attr = FindAttribute(element, ICON_ATTR);
550 for (auto& config : extractor()->densities()) {
551 if (auto resource = GetAttributeString(attr, config.second)) {
552 if (resource) {
553 density_icons.insert(std::make_pair(config.first, *resource));
554 }
555 }
556 }
557 }
558
Ryan Mitchell214846d2018-09-19 16:57:01 -0700559 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700560 // Print the labels for every locale
561 for (auto p : locale_labels) {
562 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700563 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700564 android::ResTable::normalizeForOutput(p.second.data())
565 .c_str()));
566 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700567 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700568 android::ResTable::normalizeForOutput(p.second.data())
569 .c_str()));
570 }
571 }
572
573 // Print the icon paths for every density
574 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700575 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700576 }
577
578 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700579 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700580 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700581 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700582 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700583 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700584 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700585 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700586
587 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700588 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700589 }
590 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700591 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700592 }
593 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700594 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700595 }
596 }
597};
598
599/** Represents <uses-sdk> elements. **/
600class UsesSdkBadging : public ManifestExtractor::Element {
601 public:
602 UsesSdkBadging() = default;
603 const int32_t* min_sdk = nullptr;
604 const std::string* min_sdk_name = nullptr;
605 const int32_t* max_sdk = nullptr;
606 const int32_t* target_sdk = nullptr;
607 const std::string* target_sdk_name = nullptr;
608
609 void Extract(xml::Element* element) override {
610 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
611 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
612 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
613 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
614 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
615
616 // Detect the target sdk of the element
617 if ((min_sdk_name && *min_sdk_name == "Donut")
618 || (target_sdk_name && *target_sdk_name == "Donut")) {
619 extractor()->RaiseTargetSdk(4);
620 }
621 if (min_sdk) {
622 extractor()->RaiseTargetSdk(*min_sdk);
623 }
624 if (target_sdk) {
625 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700626 } else if (target_sdk_name) {
627 extractor()->RaiseTargetSdk(kCurrentDevelopmentVersion);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700628 }
629 }
630
Ryan Mitchell214846d2018-09-19 16:57:01 -0700631 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700632 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700633 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700634 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700636 }
637 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700638 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700639 }
640 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700641 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700642 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700643 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700644 }
645 }
646};
647
648/** Represents <uses-configuration> elements. **/
649class UsesConfiguarion : public ManifestExtractor::Element {
650 public:
651 UsesConfiguarion() = default;
652 int32_t req_touch_screen = 0;
653 int32_t req_keyboard_type = 0;
654 int32_t req_hard_keyboard = 0;
655 int32_t req_navigation = 0;
656 int32_t req_five_way_nav = 0;
657
658 void Extract(xml::Element* element) override {
659 req_touch_screen = GetAttributeIntegerDefault(
660 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
661 req_keyboard_type = GetAttributeIntegerDefault(
662 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
663 req_hard_keyboard = GetAttributeIntegerDefault(
664 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
665 req_navigation = GetAttributeIntegerDefault(
666 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
667 req_five_way_nav = GetAttributeIntegerDefault(
668 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
669 }
670
Ryan Mitchell214846d2018-09-19 16:57:01 -0700671 void Print(text::Printer* printer) override {
672 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700673 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700674 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700675 }
676 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700677 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700678 }
679 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700680 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700681 }
682 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700683 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700684 }
685 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700686 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700687 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700688 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700689 }
690};
691
692/** Represents <supports-screen> elements. **/
693class SupportsScreen : public ManifestExtractor::Element {
694 public:
695 SupportsScreen() = default;
696 int32_t small_screen = 1;
697 int32_t normal_screen = 1;
698 int32_t large_screen = 1;
699 int32_t xlarge_screen = 1;
700 int32_t any_density = 1;
701 int32_t requires_smallest_width_dp = 0;
702 int32_t compatible_width_limit_dp = 0;
703 int32_t largest_width_limit_dp = 0;
704
705 void Extract(xml::Element* element) override {
706 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
707 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
708 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
709 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
710 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
711
712 requires_smallest_width_dp = GetAttributeIntegerDefault(
713 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
714 compatible_width_limit_dp = GetAttributeIntegerDefault(
715 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
716 largest_width_limit_dp = GetAttributeIntegerDefault(
717 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
718
719 // For modern apps, if screen size buckets haven't been specified
720 // but the new width ranges have, then infer the buckets from them.
721 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
722 && requires_smallest_width_dp > 0) {
723 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
724 : requires_smallest_width_dp;
725 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
726 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
727 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
728 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
729 }
730 }
731
Ryan Mitchell214846d2018-09-19 16:57:01 -0700732 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700733 int32_t small_screen_temp = small_screen;
734 int32_t normal_screen_temp = normal_screen;
735 int32_t large_screen_temp = large_screen;
736 int32_t xlarge_screen_temp = xlarge_screen;
737 int32_t any_density_temp = any_density;
738
739 // Determine default values for any unspecified screen sizes,
740 // based on the target SDK of the package. As of 4 (donut)
741 // the screen size support was introduced, so all default to
742 // enabled.
743 if (small_screen_temp > 0) {
744 small_screen_temp = target_sdk >= 4 ? -1 : 0;
745 }
746 if (normal_screen_temp > 0) {
747 normal_screen_temp = -1;
748 }
749 if (large_screen_temp > 0) {
750 large_screen_temp = target_sdk >= 4 ? -1 : 0;
751 }
752 if (xlarge_screen_temp > 0) {
753 // Introduced in Gingerbread.
754 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
755 }
756 if (any_density_temp > 0) {
757 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
758 || compatible_width_limit_dp > 0) ? -1 : 0;
759 }
760
761 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700762 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700763 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700764 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700765 }
766 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700767 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700768 }
769 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700770 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700771 }
772 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700773 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700774 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700775 printer->Print("\n");
776 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700777 (any_density_temp ) ? "true" : "false"));
778 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700779 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700780 }
781 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700782 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700783 }
784 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700785 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700786 }
787 }
788};
789
790/** Represents <feature-group> elements. **/
791class FeatureGroup : public ManifestExtractor::Element {
792 public:
793 FeatureGroup() = default;
794 std::string label;
795 int32_t open_gles_version = 0;
796
797 void Extract(xml::Element* element) override {
798 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
799 }
800
Ryan Mitchell214846d2018-09-19 16:57:01 -0700801 virtual void PrintGroup(text::Printer* printer) {
802 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700803 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700804 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700805 }
806
807 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700808 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700809 (feature.second.required ? "" : "-not-required"),
810 feature.first.data()));
811 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700812 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700813 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700814 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700815 }
816 }
817
818 /** Adds a feature to the feature group. */
819 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
820 features_.insert(std::make_pair(name, Feature{ required, version }));
821 if (required) {
822 if (name == "android.hardware.camera.autofocus" ||
823 name == "android.hardware.camera.flash") {
824 AddFeature("android.hardware.camera", true);
825 } else if (name == "android.hardware.location.gps" ||
826 name == "android.hardware.location.network") {
827 AddFeature("android.hardware.location", true);
828 } else if (name == "android.hardware.faketouch.multitouch") {
829 AddFeature("android.hardware.faketouch", true);
830 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
831 name == "android.hardware.faketouch.multitouch.jazzhands") {
832 AddFeature("android.hardware.faketouch.multitouch", true);
833 AddFeature("android.hardware.faketouch", true);
834 } else if (name == "android.hardware.touchscreen.multitouch") {
835 AddFeature("android.hardware.touchscreen", true);
836 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
837 name == "android.hardware.touchscreen.multitouch.jazzhands") {
838 AddFeature("android.hardware.touchscreen.multitouch", true);
839 AddFeature("android.hardware.touchscreen", true);
840 } else if (name == "android.hardware.opengles.aep") {
841 const int kOpenGLESVersion31 = 0x00030001;
842 if (kOpenGLESVersion31 > open_gles_version) {
843 open_gles_version = kOpenGLESVersion31;
844 }
845 }
846 }
847 }
848
849 /** Returns true if the feature group has the given feature. */
850 virtual bool HasFeature(const std::string& name) {
851 return features_.find(name) != features_.end();
852 }
853
854 /** Merges the features of another feature group into this group. */
855 void Merge(FeatureGroup* group) {
856 open_gles_version = std::max(open_gles_version, group->open_gles_version);
857 for (auto& feature : group->features_) {
858 features_.insert(feature);
859 }
860 }
861
862 protected:
863 struct Feature {
864 public:
865 bool required = false;
866 int32_t version = -1;
867 };
868
869 /* Mapping of feature names to their properties. */
870 std::map<std::string, Feature> features_;
871};
872
873/**
874 * Represents the default feature group for the application if no <feature-group> elements are
875 * present in the manifest.
876 **/
877class CommonFeatureGroup : public FeatureGroup {
878 public:
879 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700880 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700881 FeatureGroup::PrintGroup(printer);
882
883 // Also print the implied features
884 for (auto feature : implied_features_) {
885 if (features_.find(feature.first) == features_.end()) {
886 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700887 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
888 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700889 feature.first.data()));
890
891 // Print the reasons as a sentence
892 size_t count = 0;
893 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700894 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700895 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700896 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700897 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 }
900 count++;
901 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700902 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700903 }
904 }
905 }
906
907 /** Returns true if the feature group has the given feature. */
908 bool HasFeature(const std::string& name) override {
909 return FeatureGroup::HasFeature(name)
910 || implied_features_.find(name) != implied_features_.end();
911 }
912
913 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
914 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
915 auto entry = implied_features_.find(name);
916 if (entry == implied_features_.end()) {
917 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
918 entry = implied_features_.find(name);
919 }
920
921 // A non-sdk 23 implied feature takes precedence.
922 if (entry->second.implied_from_sdk_k23 && !sdk23) {
923 entry->second.implied_from_sdk_k23 = false;
924 }
925
926 entry->second.reasons.insert(reason);
927 }
928
929 /**
930 * Adds a feature to a set of implied features for all features that are implied by the presence
931 * of the permission.
932 **/
933 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
934 if (name == "android.permission.CAMERA") {
935 addImpliedFeature("android.hardware.camera",
936 StringPrintf("requested %s permission", name.data()),
937 sdk23);
938
939 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
940 if (targetSdk < SDK_LOLLIPOP) {
941 addImpliedFeature("android.hardware.location.gps",
942 StringPrintf("requested %s permission", name.data()),
943 sdk23);
944 addImpliedFeature("android.hardware.location.gps",
945 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
946 sdk23);
947 }
948 addImpliedFeature("android.hardware.location",
949 StringPrintf("requested %s permission", name.data()),
950 sdk23);
951
952 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
953 if (targetSdk < SDK_LOLLIPOP) {
954 addImpliedFeature("android.hardware.location.network",
955 StringPrintf("requested %s permission", name.data()),
956 sdk23);
957 addImpliedFeature("android.hardware.location.network",
958 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
959 sdk23);
960 }
961 addImpliedFeature("android.hardware.location",
962 StringPrintf("requested %s permission", name.data()),
963 sdk23);
964
965 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
966 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
967 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
968 addImpliedFeature("android.hardware.location",
969 StringPrintf("requested %s permission", name.data()),
970 sdk23);
971
972 } else if (name == "android.permission.BLUETOOTH" ||
973 name == "android.permission.BLUETOOTH_ADMIN") {
974 if (targetSdk > SDK_DONUT) {
975 addImpliedFeature("android.hardware.bluetooth",
976 StringPrintf("requested %s permission", name.data()),
977 sdk23);
978 addImpliedFeature("android.hardware.bluetooth",
979 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
980 sdk23);
981 }
982
983 } else if (name == "android.permission.RECORD_AUDIO") {
984 addImpliedFeature("android.hardware.microphone",
985 StringPrintf("requested %s permission", name.data()),
986 sdk23);
987
988 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
989 name == "android.permission.CHANGE_WIFI_STATE" ||
990 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
991 addImpliedFeature("android.hardware.wifi",
992 StringPrintf("requested %s permission", name.data()),
993 sdk23);
994
995 } else if (name == "android.permission.CALL_PHONE" ||
996 name == "android.permission.CALL_PRIVILEGED" ||
997 name == "android.permission.MODIFY_PHONE_STATE" ||
998 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
999 name == "android.permission.READ_SMS" ||
1000 name == "android.permission.RECEIVE_SMS" ||
1001 name == "android.permission.RECEIVE_MMS" ||
1002 name == "android.permission.RECEIVE_WAP_PUSH" ||
1003 name == "android.permission.SEND_SMS" ||
1004 name == "android.permission.WRITE_APN_SETTINGS" ||
1005 name == "android.permission.WRITE_SMS") {
1006 addImpliedFeature("android.hardware.telephony",
1007 "requested a telephony permission",
1008 sdk23);
1009 }
1010 }
1011
1012 private:
1013 /**
1014 * Represents a feature that has been automatically added due to a pre-requisite or for some
1015 * other reason.
1016 */
1017 struct ImpliedFeature {
1018 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1019
1020 /** List of human-readable reasons for why this feature was implied. */
1021 std::set<std::string> reasons;
1022
1023 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1024 bool implied_from_sdk_k23;
1025 };
1026
1027 /* Mapping of implied feature names to their properties. */
1028 std::map<std::string, ImpliedFeature> implied_features_;
1029};
1030
1031/** Represents <uses-feature> elements. **/
1032class UsesFeature : public ManifestExtractor::Element {
1033 public:
1034 UsesFeature() = default;
1035 void Extract(xml::Element* element) override {
1036 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1037 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1038 bool required = GetAttributeIntegerDefault(
1039 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1040 int32_t version = GetAttributeIntegerDefault(
1041 FindAttribute(element, kAndroidNamespace, "version"), 0);
1042
1043 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1044 // common feature group
1045 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1046 if (!feature_group) {
1047 feature_group = extractor()->GetCommonFeatureGroup();
1048 } else {
1049 // All features in side of <feature-group> elements are required.
1050 required = true;
1051 }
1052
1053 if (name) {
1054 feature_group->AddFeature(*name, required, version);
1055 } else if (gl) {
1056 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1057 }
1058 }
1059};
1060
1061/** Represents <uses-permission> elements. **/
1062class UsesPermission : public ManifestExtractor::Element {
1063 public:
1064 UsesPermission() = default;
1065 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001066 std::vector<std::string> requiredFeatures;
1067 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001068 int32_t required = true;
1069 int32_t maxSdkVersion = -1;
1070
1071 void Extract(xml::Element* element) override {
1072 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001073 std::string feature =
1074 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1075 if (!feature.empty()) {
1076 requiredFeatures.push_back(feature);
1077 }
1078 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1079 if (!feature.empty()) {
1080 requiredNotFeatures.push_back(feature);
1081 }
1082
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001083 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1084 maxSdkVersion = GetAttributeIntegerDefault(
1085 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1086
1087 if (!name.empty()) {
1088 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1089 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1090 }
1091 }
1092
Ryan Mitchell214846d2018-09-19 16:57:01 -07001093 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001094 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001095 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001096 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001097 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001098 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001099 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001100 for (const std::string& requiredFeature : requiredFeatures) {
1101 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1102 }
1103 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1104 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1105 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001106 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001107 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001108 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001109 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001110 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001111 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001112 }
1113 }
1114 }
1115
Ryan Mitchell214846d2018-09-19 16:57:01 -07001116 void PrintImplied(text::Printer* printer, const std::string& reason) {
1117 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001118 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001119 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001120 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001121 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001122 }
1123};
1124
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001125/** Represents <required-feature> elements. **/
1126class RequiredFeature : public ManifestExtractor::Element {
1127 public:
1128 RequiredFeature() = default;
1129 std::string name;
1130
1131 void Extract(xml::Element* element) override {
1132 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1133 auto parent_stack = extractor()->parent_stack();
1134 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1135 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1136 uses_permission->requiredFeatures.push_back(name);
1137 }
1138 }
1139};
1140
1141/** Represents <required-not-feature> elements. **/
1142class RequiredNotFeature : public ManifestExtractor::Element {
1143 public:
1144 RequiredNotFeature() = default;
1145 std::string name;
1146
1147 void Extract(xml::Element* element) override {
1148 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1149 auto parent_stack = extractor()->parent_stack();
1150 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1151 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1152 uses_permission->requiredNotFeatures.push_back(name);
1153 }
1154 }
1155};
1156
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001157/** Represents <uses-permission-sdk-23> elements. **/
1158class UsesPermissionSdk23 : public ManifestExtractor::Element {
1159 public:
1160 UsesPermissionSdk23() = default;
1161 const std::string* name = nullptr;
1162 const int32_t* maxSdkVersion = nullptr;
1163
1164 void Extract(xml::Element* element) override {
1165 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1166 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1167
1168 if (name) {
1169 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1170 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1171 }
1172 }
1173
Ryan Mitchell214846d2018-09-19 16:57:01 -07001174 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001175 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001176 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001177 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001178 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001179 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001180 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001181 }
1182 }
1183};
1184
1185/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1186class Permission : public ManifestExtractor::Element {
1187 public:
1188 Permission() = default;
1189 std::string name;
1190
1191 void Extract(xml::Element* element) override {
1192 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1193 }
1194
Ryan Mitchell214846d2018-09-19 16:57:01 -07001195 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001196 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001197 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001198 }
1199 }
1200};
1201
1202/** Represents <activity> elements. **/
1203class Activity : public ManifestExtractor::Element {
1204 public:
1205 Activity() = default;
1206 std::string name;
1207 std::string icon;
1208 std::string label;
1209 std::string banner;
1210
1211 bool has_component_ = false;
1212 bool has_launcher_category = false;
1213 bool has_leanback_launcher_category = false;
1214 bool has_main_action = false;
1215
1216 void Extract(xml::Element* element) override {
1217 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1218 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1219 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1220 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1221
1222 // Retrieve the package name from the manifest
1223 std::string package;
1224 for (auto& parent : extractor()->parent_stack()) {
1225 if (auto manifest = ElementCast<Manifest>(parent)) {
1226 package = manifest->package;
1227 break;
1228 }
1229 }
1230
1231 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001232 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001233 if (idx == 0) {
1234 name = package + name;
1235 } else if (idx < 0) {
1236 name = package + "." + name;
1237 }
1238
1239 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1240 if (orientation) {
1241 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1242 int orien = *orientation;
1243 if (orien == 0 || orien == 6 || orien == 8) {
1244 // Requests landscape, sensorLandscape, or reverseLandscape.
1245 common->addImpliedFeature("android.hardware.screen.landscape",
1246 "one or more activities have specified a landscape orientation",
1247 false);
1248 } else if (orien == 1 || orien == 7 || orien == 9) {
1249 // Requests portrait, sensorPortrait, or reversePortrait.
1250 common->addImpliedFeature("android.hardware.screen.portrait",
1251 "one or more activities have specified a portrait orientation",
1252 false);
1253 }
1254 }
1255 }
1256
Ryan Mitchell214846d2018-09-19 16:57:01 -07001257 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001258 // Print whether the activity has the HOME category and a the MAIN action
1259 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001260 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001261 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001262 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001263 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001264 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001265 android::ResTable::normalizeForOutput(label.data()).c_str(),
1266 icon.data()));
1267 }
1268
1269 // Print wether the activity has the HOME category and a the MAIN action
1270 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001271 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001272 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001273 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001274 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001275 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001276 android::ResTable::normalizeForOutput(label.data()).c_str(),
1277 icon.data(), banner.data()));
1278 }
1279 }
1280};
1281
1282/** Represents <intent-filter> elements. */
1283class IntentFilter : public ManifestExtractor::Element {
1284 public:
1285 IntentFilter() = default;
1286};
1287
1288/** Represents <category> elements. */
1289class Category : public ManifestExtractor::Element {
1290 public:
1291 Category() = default;
1292 std::string component = "";
1293
1294 void Extract(xml::Element* element) override {
1295 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1296
1297 auto parent_stack = extractor()->parent_stack();
1298 if (category && ElementCast<IntentFilter>(parent_stack[0])
1299 && ElementCast<Activity>(parent_stack[1])) {
1300 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1301
1302 if (*category == "android.intent.category.LAUNCHER") {
1303 activity->has_launcher_category = true;
1304 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1305 activity->has_leanback_launcher_category = true;
1306 } else if (*category == "android.intent.category.HOME") {
1307 component = "launcher";
1308 }
1309 }
1310 }
1311};
1312
1313/**
1314 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1315 * elements nested within.
1316 **/
1317class Provider : public ManifestExtractor::Element {
1318 public:
1319 Provider() = default;
1320 bool has_required_saf_attributes = false;
1321
1322 void Extract(xml::Element* element) override {
1323 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1324 const int32_t* grant_uri_permissions = GetAttributeInteger(
1325 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1326 const std::string* permission = GetAttributeString(
1327 FindAttribute(element, PERMISSION_ATTR));
1328
1329 has_required_saf_attributes = ((exported && *exported != 0)
1330 && (grant_uri_permissions && *grant_uri_permissions != 0)
1331 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1332 }
1333};
1334
1335/** Represents <receiver> elements. **/
1336class Receiver : public ManifestExtractor::Element {
1337 public:
1338 Receiver() = default;
1339 const std::string* permission = nullptr;
1340 bool has_component = false;
1341
1342 void Extract(xml::Element* element) override {
1343 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1344 }
1345};
1346
1347/**Represents <service> elements. **/
1348class Service : public ManifestExtractor::Element {
1349 public:
1350 Service() = default;
1351 const std::string* permission = nullptr;
1352 bool has_component = false;
1353
1354 void Extract(xml::Element* element) override {
1355 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1356 }
1357};
1358
1359/** Represents <uses-library> elements. **/
1360class UsesLibrary : public ManifestExtractor::Element {
1361 public:
1362 UsesLibrary() = default;
1363 std::string name;
1364 int required;
1365
1366 void Extract(xml::Element* element) override {
1367 auto parent_stack = extractor()->parent_stack();
1368 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1369 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1370 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1371 }
1372 }
1373
Ryan Mitchell214846d2018-09-19 16:57:01 -07001374 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001375 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001376 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001377 (required == 0) ? "-not-required" : "", name.data()));
1378 }
1379 }
1380};
1381
Dianne Hackborn813d7502018-10-02 16:59:46 -07001382/** Represents <static-library> elements. **/
1383class StaticLibrary : public ManifestExtractor::Element {
1384 public:
1385 StaticLibrary() = default;
1386 std::string name;
1387 int version;
1388 int versionMajor;
1389
1390 void Extract(xml::Element* element) override {
1391 auto parent_stack = extractor()->parent_stack();
1392 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1393 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1394 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1395 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1396 }
1397 }
1398
Ryan Mitchell214846d2018-09-19 16:57:01 -07001399 void Print(text::Printer* printer) override {
1400 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001401 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1402 name.data(), version, versionMajor));
1403 }
1404};
1405
1406/** Represents <uses-static-library> elements. **/
1407class UsesStaticLibrary : public ManifestExtractor::Element {
1408 public:
1409 UsesStaticLibrary() = default;
1410 std::string name;
1411 int version;
1412 int versionMajor;
1413 std::vector<std::string> certDigests;
1414
1415 void Extract(xml::Element* element) override {
1416 auto parent_stack = extractor()->parent_stack();
1417 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1418 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1419 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1420 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1421 AddCertDigest(element);
1422 }
1423 }
1424
1425 void AddCertDigest(xml::Element* element) {
1426 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1427 // We allow ":" delimiters in the SHA declaration as this is the format
1428 // emitted by the certtool making it easy for developers to copy/paste.
1429 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1430 if (!digest.empty()) {
1431 certDigests.push_back(digest);
1432 }
1433 }
1434
Ryan Mitchell214846d2018-09-19 16:57:01 -07001435 void Print(text::Printer* printer) override {
1436 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001437 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1438 name.data(), version, versionMajor));
1439 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001440 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001441 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001442 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001443 }
1444};
1445
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001446/** Represents <uses-native-library> elements. **/
1447class UsesNativeLibrary : public ManifestExtractor::Element {
1448 public:
1449 UsesNativeLibrary() = default;
1450 std::string name;
1451 int required;
1452
1453 void Extract(xml::Element* element) override {
1454 auto parent_stack = extractor()->parent_stack();
1455 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1456 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1457 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1458 }
1459 }
1460
1461 void Print(text::Printer* printer) override {
1462 if (!name.empty()) {
1463 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1464 (required == 0) ? "-not-required" : "", name.data()));
1465 }
1466 }
1467};
1468
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001469/**
1470 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1471 * explicitly enable meta data printing.
1472 **/
1473class MetaData : public ManifestExtractor::Element {
1474 public:
1475 MetaData() = default;
1476 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001477 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001478 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001479 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001480 const int* resource_int;
1481
1482 void Extract(xml::Element* element) override {
1483 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001484 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001485 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001486 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001487 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1488 }
1489
Ryan Mitchell214846d2018-09-19 16:57:01 -07001490 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001491 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001492 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001493 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001494 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001495 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001496 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001497 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001498 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001499 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001500 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001501 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001502 }
1503 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001504 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001505 }
1506 }
1507};
1508
1509/**
1510 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1511 * service components.
1512 **/
1513class Action : public ManifestExtractor::Element {
1514 public:
1515 Action() = default;
1516 std::string component = "";
1517
1518 void Extract(xml::Element* element) override {
1519 auto parent_stack = extractor()->parent_stack();
1520 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1521
1522 if (ElementCast<IntentFilter>(parent_stack[0])) {
1523 if (ElementCast<Activity>(parent_stack[1])) {
1524 // Detects the presence of a particular type of activity.
1525 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1526 auto map = std::map<std::string, std::string>({
1527 { "android.intent.action.MAIN" , "main" },
1528 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1529 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1530 });
1531
1532 auto entry = map.find(action);
1533 if (entry != map.end()) {
1534 component = entry->second;
1535 activity->has_component_ = true;
1536 }
1537
1538 if (action == "android.intent.action.MAIN") {
1539 activity->has_main_action = true;
1540 }
1541
1542 } else if (ElementCast<Receiver>(parent_stack[1])) {
1543 // Detects the presence of a particular type of receiver. If the action requires a
1544 // permission, then the receiver element is checked for the permission.
1545 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1546 auto map = std::map<std::string, std::string>({
1547 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1548 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1549 });
1550
1551 auto permissions = std::map<std::string, std::string>({
1552 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1553 });
1554
1555 auto entry = map.find(action);
1556 auto permission = permissions.find(action);
1557 if (entry != map.end() && (permission == permissions.end()
1558 || (receiver->permission && permission->second == *receiver->permission))) {
1559 receiver->has_component = true;
1560 component = entry->second;
1561 }
1562
1563 } else if (ElementCast<Service>(parent_stack[1])) {
1564 // Detects the presence of a particular type of service. If the action requires a
1565 // permission, then the service element is checked for the permission.
1566 Service* service = ElementCast<Service>(parent_stack[1]);
1567 auto map = std::map<std::string, std::string>({
1568 { "android.view.InputMethod" , "ime" },
1569 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1570 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1571 { "android.printservice.PrintService" , "print-service" },
1572 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1573 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1574 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1575 { "android.service.dreams.DreamService" , "dream" },
1576 });
1577
1578 auto permissions = std::map<std::string, std::string>({
1579 { "android.accessibilityservice.AccessibilityService" ,
1580 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1581 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1582 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1583 "android.permission.BIND_NFC_SERVICE" },
1584 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1585 "android.permission.BIND_NFC_SERVICE" },
1586 { "android.service.notification.NotificationListenerService" ,
1587 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1588 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1589 });
1590
1591 auto entry = map.find(action);
1592 auto permission = permissions.find(action);
1593 if (entry != map.end() && (permission == permissions.end()
1594 || (service->permission && permission->second == *service->permission))) {
1595 service->has_component= true;
1596 component = entry->second;
1597 }
1598
1599 } else if (ElementCast<Provider>(parent_stack[1])) {
1600 // Detects the presence of a particular type of receiver. If the provider requires a
1601 // permission, then the provider element is checked for the permission.
1602 // Detect whether this action
1603 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1604 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1605 && provider->has_required_saf_attributes) {
1606 component = "document-provider";
1607 }
1608 }
1609 }
1610
1611 // Represents a searchable interface
1612 if (action == "android.intent.action.SEARCH") {
1613 component = "search";
1614 }
1615 }
1616};
1617
1618/**
1619 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1620 **/
1621class SupportsInput : public ManifestExtractor::Element {
1622 public:
1623 SupportsInput() = default;
1624 std::vector<std::string> inputs;
1625
Ryan Mitchell214846d2018-09-19 16:57:01 -07001626 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001627 const size_t size = inputs.size();
1628 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001629 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001630 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001631 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001632 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001633 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001634 }
1635 }
1636};
1637
1638/** Represents <input-type> elements. **/
1639class InputType : public ManifestExtractor::Element {
1640 public:
1641 InputType() = default;
1642 void Extract(xml::Element* element) override {
1643 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1644 auto parent_stack = extractor()->parent_stack();
1645
1646 // Add the input to the set of supported inputs
1647 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1648 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1649 supports->inputs.push_back(*name);
1650 }
1651 }
1652};
1653
1654/** Represents <original-package> elements. **/
1655class OriginalPackage : public ManifestExtractor::Element {
1656 public:
1657 OriginalPackage() = default;
1658 const std::string* name = nullptr;
1659
1660 void Extract(xml::Element* element) override {
1661 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1662 }
1663
Ryan Mitchell214846d2018-09-19 16:57:01 -07001664 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001665 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001666 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001667 }
1668 }
1669};
1670
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001671
1672/** Represents <overlay> elements. **/
1673class Overlay : public ManifestExtractor::Element {
1674 public:
1675 Overlay() = default;
1676 const std::string* target_package = nullptr;
1677 int priority;
1678 bool is_static;
1679 const std::string* required_property_name = nullptr;
1680 const std::string* required_property_value = nullptr;
1681
1682 void Extract(xml::Element* element) override {
1683 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1684 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1685 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1686 required_property_name = GetAttributeString(
1687 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1688 required_property_value = GetAttributeString(
1689 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1690 }
1691
1692 void Print(text::Printer* printer) override {
1693 printer->Print(StringPrintf("overlay:"));
1694 if (target_package) {
1695 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1696 }
1697 printer->Print(StringPrintf(" priority='%d'", priority));
1698 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1699 if (required_property_name) {
1700 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1701 }
1702 if (required_property_value) {
1703 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1704 }
1705 printer->Print("\n");
1706 }
1707};
1708
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001709/** * Represents <package-verifier> elements. **/
1710class PackageVerifier : public ManifestExtractor::Element {
1711 public:
1712 PackageVerifier() = default;
1713 const std::string* name = nullptr;
1714 const std::string* public_key = nullptr;
1715
1716 void Extract(xml::Element* element) override {
1717 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1718 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1719 }
1720
Ryan Mitchell214846d2018-09-19 16:57:01 -07001721 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001722 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001723 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001724 name->data(), public_key->data()));
1725 }
1726 }
1727};
1728
1729/** Represents <uses-package> elements. **/
1730class UsesPackage : public ManifestExtractor::Element {
1731 public:
1732 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001733 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001734 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001735 int version;
1736 int versionMajor;
1737 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001738
1739 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001740 auto parent_stack = extractor()->parent_stack();
1741 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1742 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1743 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1744 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1745 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1746 AddCertDigest(element);
1747 }
1748 }
1749
1750 void AddCertDigest(xml::Element* element) {
1751 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1752 // We allow ":" delimiters in the SHA declaration as this is the format
1753 // emitted by the certtool making it easy for developers to copy/paste.
1754 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1755 if (!digest.empty()) {
1756 certDigests.push_back(digest);
1757 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001758 }
1759
Ryan Mitchell214846d2018-09-19 16:57:01 -07001760 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001761 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001762 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001763 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001764 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1765 packageType->data(), name->data(), version, versionMajor));
1766 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001767 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001768 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001769 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001770 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001771 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001772 }
1773 }
1774 }
1775};
1776
1777/** Represents <additional-certificate> elements. **/
1778class AdditionalCertificate : public ManifestExtractor::Element {
1779 public:
1780 AdditionalCertificate() = default;
1781
1782 void Extract(xml::Element* element) override {
1783 auto parent_stack = extractor()->parent_stack();
1784 if (parent_stack.size() > 0) {
1785 if (ElementCast<UsesPackage>(parent_stack[0])) {
1786 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1787 uses->AddCertDigest(element);
1788 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1789 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1790 uses->AddCertDigest(element);
1791 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001792 }
1793 }
1794};
1795
1796/** Represents <screen> elements found in <compatible-screens> elements. */
1797class Screen : public ManifestExtractor::Element {
1798 public:
1799 Screen() = default;
1800 const int32_t* size = nullptr;
1801 const int32_t* density = nullptr;
1802
1803 void Extract(xml::Element* element) override {
1804 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1805 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1806 }
1807};
1808
1809/**
1810 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1811 * that each denote a supported screen size and screen density.
1812 **/
1813class CompatibleScreens : public ManifestExtractor::Element {
1814 public:
1815 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001816 void Print(text::Printer* printer) override {
1817 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001818
1819 bool first = true;
1820 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1821 if (auto screen = ElementCast<Screen>(el)) {
1822 if (first) {
1823 first = false;
1824 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001825 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001826 }
1827
1828 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001829 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001830 }
1831 }
1832 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001833 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001834 }
1835};
1836
1837/** Represents <supports-gl-texture> elements. **/
1838class SupportsGlTexture : public ManifestExtractor::Element {
1839 public:
1840 SupportsGlTexture() = default;
1841 const std::string* name = nullptr;
1842
1843 void Extract(xml::Element* element) override {
1844 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1845 }
1846
Ryan Mitchell214846d2018-09-19 16:57:01 -07001847 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001848 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001849 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001850 }
1851 }
1852};
1853
Todd Kennedyce3e1292020-10-29 17:14:24 -07001854/** Represents <property> elements. **/
1855class Property : public ManifestExtractor::Element {
1856 public:
1857 Property() = default;
1858 std::string name;
1859 std::string value;
1860 const int* value_int;
1861 std::string resource;
1862 const int* resource_int;
1863
1864 void Extract(xml::Element* element) override {
1865 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1866 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
1867 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
1868 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
1869 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1870 }
1871
1872 void Print(text::Printer* printer) override {
1873 printer->Print(StringPrintf("property: name='%s' ", name.data()));
1874 if (!value.empty()) {
1875 printer->Print(StringPrintf("value='%s' ", value.data()));
1876 } else if (value_int) {
1877 printer->Print(StringPrintf("value='%d' ", *value_int));
1878 } else {
1879 if (!resource.empty()) {
1880 printer->Print(StringPrintf("resource='%s' ", resource.data()));
1881 } else if (resource_int) {
1882 printer->Print(StringPrintf("resource='%d' ", *resource_int));
1883 }
1884 }
1885 printer->Print("\n");
1886 }
1887};
1888
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001889/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001890static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001891 el->Print(printer);
1892 for (auto &child : el->children()) {
1893 Print(child.get(), printer);
1894 }
1895}
1896
Ryan Mitchell214846d2018-09-19 16:57:01 -07001897bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001898 // Load the manifest
1899 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1900 if (doc == nullptr) {
1901 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1902 return false;
1903 }
1904
1905 xml::Element* element = doc->root.get();
1906 if (element->name != "manifest") {
1907 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1908 return false;
1909 }
1910
1911 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1912 // printing only permission elements is requested
1913 if (options_.only_permissions) {
1914 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1915 ManifestExtractor::Element::Inflate(this, element);
1916
1917 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1918 for (xml::Element* child : element->GetChildElements()) {
1919 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1920 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001921 // Inflate the element and its descendants
1922 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001923 manifest->AddChild(permission_element);
1924 }
1925 }
1926
Ryan Mitchell214846d2018-09-19 16:57:01 -07001927 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001928 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1929 el->Print(printer);
1930 });
1931
1932 return true;
1933 }
1934
1935 return false;
1936 }
1937
1938 // Collect information about the resource configurations
1939 if (apk_->GetResourceTable()) {
1940 for (auto &package : apk_->GetResourceTable()->packages) {
1941 for (auto &type : package->types) {
1942 for (auto &entry : type->entries) {
1943 for (auto &value : entry->values) {
1944 std::string locale_str = value->config.GetBcp47LanguageTag();
1945
1946 // Collect all the unique locales of the apk
1947 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001948 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001949 config.setBcp47Locale(locale_str.data());
1950 locales_.insert(std::make_pair(locale_str, config));
1951 }
1952
1953 // Collect all the unique density of the apk
1954 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1955 : value->config.density;
1956 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001957 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001958 config.density = density;
1959 densities_.insert(std::make_pair(density, config));
1960 }
1961 }
1962 }
1963 }
1964 }
1965 }
1966
1967 // Extract badging information
1968 auto root = Visit(element);
1969
1970 // Print the elements in order seen
1971 Print(root.get(), printer);
1972
1973 /** Recursively checks the extracted elements for the specified permission. **/
1974 auto FindPermission = [&](ManifestExtractor::Element* root,
1975 const std::string& name) -> ManifestExtractor::Element* {
1976 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1977 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1978 return permission->name == name;
1979 }
1980 return false;
1981 });
1982 };
1983
1984 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1985 int32_t max_sdk_version) -> void {
1986 auto permission = util::make_unique<UsesPermission>();
1987 permission->name = name;
1988 permission->maxSdkVersion = max_sdk_version;
1989 permission->Print(printer);
1990 permission->PrintImplied(printer, reason);
1991 };
1992
1993 // Implied permissions
1994 // Pre-1.6 implicitly granted permission compatibility logic
1995 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
1996 bool insert_write_external = false;
1997 auto write_external_permission = ElementCast<UsesPermission>(
1998 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
1999
2000 if (target_sdk() < 4) {
2001 if (!write_external_permission) {
2002 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
2003 insert_write_external = true;
2004 }
2005
2006 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
2007 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
2008 }
2009 }
2010
2011 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2012 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2013 // do this (regardless of target API version) because we can't have
2014 // an app with write permission but not read permission.
2015 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
2016 if (!read_external && (insert_write_external || write_external_permission)) {
2017 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
2018 "requested WRITE_EXTERNAL_STORAGE",
2019 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
2020 }
2021
2022 // Pre-JellyBean call log permission compatibility.
2023 if (target_sdk() < 16) {
2024 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
2025 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
2026 PrintPermission("android.permission.READ_CALL_LOG",
2027 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
2028 }
2029
2030 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
2031 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
2032 PrintPermission("android.permission.WRITE_CALL_LOG",
2033 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
2034 }
2035 }
2036
2037 // If the app hasn't declared the touchscreen as a feature requirement (either
2038 // directly or implied, required or not), then the faketouch feature is implied.
2039 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
2040 common_feature_group->addImpliedFeature("android.hardware.faketouch",
2041 "default feature for all apps", false);
2042 }
2043
2044 // Only print the common feature group if no feature group is defined
2045 std::vector<FeatureGroup*> feature_groups;
2046 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
2047 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2048 feature_groups.push_back(feature_group);
2049 }
2050 });
2051
2052 if (feature_groups.empty()) {
2053 common_feature_group->PrintGroup(printer);
2054 } else {
2055 // Merge the common feature group into the feature group
2056 for (auto& feature_group : feature_groups) {
2057 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
2058 common_feature_group->open_gles_version);
2059 feature_group->Merge(common_feature_group);
2060 feature_group->PrintGroup(printer);
2061 }
2062 };
2063
2064 // Collect the component types of the application
2065 std::set<std::string> components;
2066 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
2067 if (ElementCast<Action>(el)) {
2068 auto action = ElementCast<Action>(el);
2069 if (!action->component.empty()) {
2070 components.insert(action->component);
2071 return;
2072 }
2073 }
2074
2075 if (ElementCast<Category>(el)) {
2076 auto category = ElementCast<Category>(el);
2077 if (!category->component.empty()) {
2078 components.insert(category->component);
2079 return;
2080 }
2081 }
2082 });
2083
2084 // Check for the payment component
2085 auto apk = apk_;
2086 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2087 if (auto service = ElementCast<Service>(el)) {
2088 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2089 [&](ManifestExtractor::Element* el) -> bool {
2090 if (auto action = ElementCast<Action>(el)) {
2091 return (action->component == "host-apdu");
2092 }
2093 return false;
2094 }));
2095
2096 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2097 [&](ManifestExtractor::Element* el) -> bool {
2098 if (auto action = ElementCast<Action>(el)) {
2099 return (action->component == "offhost-apdu");
2100 }
2101 return false;
2102 }));
2103
2104 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2105 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2106 if (auto meta_data = ElementCast<MetaData>(el)) {
2107 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2108 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2109 && offhost_apdu_action)) {
2110
2111 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002112 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002113 return;
2114 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002115 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002116 if (!resource) {
2117 return;
2118 }
2119
2120 // Look for the payment category on an <aid-group> element
2121 auto& root = resource.get()->root;
2122 if ((host_apdu_action && root->name == "host-apdu-service")
2123 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2124
2125 for (auto& child : root->GetChildElements()) {
2126 if (child->name == "aid-group") {
2127 auto category = FindAttribute(child, CATEGORY_ATTR);
2128 if (category && category->value == "payment") {
2129 components.insert("payment");
2130 return;
2131 }
2132 }
2133 }
2134 }
2135 }
2136 }
2137 });
2138 }
2139 });
2140
2141 // Print the components types if they are present
2142 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2143 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002144 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002145 }
2146 };
2147
2148 PrintComponent("app-widget");
2149 PrintComponent("device-admin");
2150 PrintComponent("ime");
2151 PrintComponent("wallpaper");
2152 PrintComponent("accessibility");
2153 PrintComponent("print-service");
2154 PrintComponent("payment");
2155 PrintComponent("search");
2156 PrintComponent("document-provider");
2157 PrintComponent("launcher");
2158 PrintComponent("notification-listener");
2159 PrintComponent("dream");
2160 PrintComponent("camera");
2161 PrintComponent("camera-secure");
2162
2163 // Print presence of main activity
2164 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002165 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002166 }
2167
2168 // Print presence of activities, recivers, and services with no special components
2169 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2170 if (auto activity = ElementCast<Activity>(el)) {
2171 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002172 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002173 return true;
2174 }
2175 }
2176 return false;
2177 });
2178
2179 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2180 if (auto receiver = ElementCast<Receiver>(el)) {
2181 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002182 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002183 return true;
2184 }
2185 }
2186 return false;
2187 });
2188
2189 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2190 if (auto service = ElementCast<Service>(el)) {
2191 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002192 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002193 return true;
2194 }
2195 }
2196 return false;
2197 });
2198
2199 // Print the supported screens
2200 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2201 [&](ManifestExtractor::Element* el) -> bool {
2202 return ElementCast<SupportsScreen>(el) != nullptr;
2203 }));
2204
2205 if (screen) {
2206 screen->PrintScreens(printer, target_sdk_);
2207 } else {
2208 // Print the default supported screens
2209 SupportsScreen default_screens;
2210 default_screens.PrintScreens(printer, target_sdk_);
2211 }
2212
2213 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002214 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002215 for (auto& config : locales_) {
2216 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002217 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002218 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002219 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002220 }
2221 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002222 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002223
2224 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002225 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002226 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002227 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002228 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002229 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002230
2231 // Print the supported architectures of the app
2232 std::set<std::string> architectures;
2233 auto it = apk_->GetFileCollection()->Iterator();
2234 while (it->HasNext()) {
2235 auto file_path = it->Next()->GetSource().path;
2236
2237
2238 size_t pos = file_path.find("lib/");
2239 if (pos != std::string::npos) {
2240 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002241 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002242 if (pos != std::string::npos) {
2243 file_path = file_path.substr(0, pos);
2244 }
2245
2246 architectures.insert(file_path);
2247 }
2248 }
2249
2250 // Determine if the application has multiArch supports
2251 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2252 if (auto application = ElementCast<Application>(el)) {
2253 return application->has_multi_arch;
2254 }
2255 return false;
2256 });
2257
2258 bool output_alt_native_code = false;
2259 // A multiArch package is one that contains 64-bit and
2260 // 32-bit versions of native code and expects 3rd-party
2261 // apps to load these native code libraries. Since most
2262 // 64-bit systems also support 32-bit apps, the apps
2263 // loading this multiArch package's code may be either
2264 if (has_multi_arch) {
2265 // If this is a multiArch package, report the 64-bit
2266 // version only. Then as a separate entry, report the
2267 // rest.
2268 //
2269 // If we report the 32-bit architecture, this APK will
2270 // be installed on a 32-bit device, causing a large waste
2271 // of bandwidth and disk space. This assumes that
2272 // the developer of the multiArch package has also
2273 // made a version that is 32-bit only.
2274 const std::string kIntel64 = "x86_64";
2275 const std::string kArm64 = "arm64-v8a";
2276
2277 auto arch = architectures.find(kIntel64);
2278 if (arch == architectures.end()) {
2279 arch = architectures.find(kArm64);
2280 }
2281
2282 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002283 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002284 architectures.erase(arch);
2285 output_alt_native_code = true;
2286 }
2287 }
2288
2289 if (architectures.size() > 0) {
2290 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002291 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002292 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002293 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002294 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002295 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002296 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002297 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002298 }
2299
2300 return true;
2301}
2302
2303/**
2304 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2305 * pointer.
2306 **/
2307template<typename T>
2308T* ElementCast(ManifestExtractor::Element* element) {
2309 if (element == nullptr) {
2310 return nullptr;
2311 }
2312
2313 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002314 {"action", std::is_base_of<Action, T>::value},
2315 {"activity", std::is_base_of<Activity, T>::value},
2316 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2317 {"application", std::is_base_of<Application, T>::value},
2318 {"category", std::is_base_of<Category, T>::value},
2319 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2320 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2321 {"input-type", std::is_base_of<InputType, T>::value},
2322 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2323 {"meta-data", std::is_base_of<MetaData, T>::value},
2324 {"manifest", std::is_base_of<Manifest, T>::value},
2325 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2326 {"overlay", std::is_base_of<Overlay, T>::value},
2327 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2328 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002329 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002330 {"provider", std::is_base_of<Provider, T>::value},
2331 {"receiver", std::is_base_of<Receiver, T>::value},
2332 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2333 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2334 {"screen", std::is_base_of<Screen, T>::value},
2335 {"service", std::is_base_of<Service, T>::value},
2336 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2337 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2338 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2339 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2340 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2341 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2342 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2343 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2344 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2345 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2346 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2347 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2348 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002349 };
2350
2351 auto check = kTagCheck.find(element->tag());
2352 if (check != kTagCheck.end() && check->second) {
2353 return static_cast<T*>(element);
2354 }
2355 return nullptr;
2356}
2357
2358template<typename T>
2359std::unique_ptr<T> CreateType() {
2360 return std::move(util::make_unique<T>());
2361}
2362
2363std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2364 ManifestExtractor* extractor, xml::Element* el) {
2365 const std::unordered_map<std::string,
2366 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2367 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002368 {"action", &CreateType<Action>},
2369 {"activity", &CreateType<Activity>},
2370 {"additional-certificate", &CreateType<AdditionalCertificate>},
2371 {"application", &CreateType<Application>},
2372 {"category", &CreateType<Category>},
2373 {"compatible-screens", &CreateType<CompatibleScreens>},
2374 {"feature-group", &CreateType<FeatureGroup>},
2375 {"input-type", &CreateType<InputType>},
2376 {"intent-filter", &CreateType<IntentFilter>},
2377 {"manifest", &CreateType<Manifest>},
2378 {"meta-data", &CreateType<MetaData>},
2379 {"original-package", &CreateType<OriginalPackage>},
2380 {"overlay", &CreateType<Overlay>},
2381 {"package-verifier", &CreateType<PackageVerifier>},
2382 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002383 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002384 {"provider", &CreateType<Provider>},
2385 {"receiver", &CreateType<Receiver>},
2386 {"required-feature", &CreateType<RequiredFeature>},
2387 {"required-not-feature", &CreateType<RequiredNotFeature>},
2388 {"screen", &CreateType<Screen>},
2389 {"service", &CreateType<Service>},
2390 {"static-library", &CreateType<StaticLibrary>},
2391 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2392 {"supports-input", &CreateType<SupportsInput>},
2393 {"supports-screens", &CreateType<SupportsScreen>},
2394 {"uses-configuration", &CreateType<UsesConfiguarion>},
2395 {"uses-feature", &CreateType<UsesFeature>},
2396 {"uses-library", &CreateType<UsesLibrary>},
2397 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2398 {"uses-package", &CreateType<UsesPackage>},
2399 {"uses-permission", &CreateType<UsesPermission>},
2400 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2401 {"uses-sdk", &CreateType<UsesSdkBadging>},
2402 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2403 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002404
2405 // Attempt to map the xml tag to a element inflater
2406 std::unique_ptr<ManifestExtractor::Element> element;
2407 auto check = kTagCheck.find(el->name);
2408 if (check != kTagCheck.end()) {
2409 element = check->second();
2410 } else {
2411 element = util::make_unique<ManifestExtractor::Element>();
2412 }
2413
2414 element->extractor_ = extractor;
2415 element->tag_ = el->name;
2416 element->Extract(el);
2417 return element;
2418}
2419
2420std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2421 auto element = ManifestExtractor::Element::Inflate(this, el);
2422 parent_stack_.insert(parent_stack_.begin(), element.get());
2423
2424 // Process the element and recursively visit the children
2425 for (xml::Element* child : el->GetChildElements()) {
2426 auto v = Visit(child);
2427 element->AddChild(v);
2428 }
2429
2430 parent_stack_.erase(parent_stack_.begin());
2431 return element;
2432}
2433
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002434
Ryan Mitchell214846d2018-09-19 16:57:01 -07002435int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2436 IDiagnostics* diag) {
2437 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002438 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002439}
2440
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002441} // namespace aapt