blob: fb7c596e7497a1e0a87880e97dc994c91614e502 [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,
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060091 USES_PERMISSION_FLAGS_ATTR = 0x01010644,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070092};
93
94const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Ryan Mitchell95f02422020-04-30 10:25:53 -070095constexpr int kCurrentDevelopmentVersion = 10000;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060096constexpr int kNeverForLocation = 0x00010000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070097
98/** Retrieves the attribute of the element with the specified attribute resource id. */
99static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
100 for (auto& a : el->attributes) {
101 if (a.compiled_attribute && a.compiled_attribute.value().id) {
102 if (a.compiled_attribute.value().id.value() == resd_id) {
103 return std::move(&a);
104 }
105 }
106 }
107 return nullptr;
108}
109
110/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
111static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
112 const std::string &name) {
113 return el->FindAttribute(package, name);
114}
115
116class CommonFeatureGroup;
117
118class ManifestExtractor {
119 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700120
Ryan Mitchell214846d2018-09-19 16:57:01 -0700121 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700122 : apk_(apk), options_(options) { }
123
124 class Element {
125 public:
126 Element() = default;
127 virtual ~Element() = default;
128
129 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
130
131 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700132 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700133
134 /** Adds an element to the list of children of the element. */
135 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
136
137 /** Retrieves the list of children of the element. */
138 const std::vector<std::unique_ptr<Element>>& children() const {
139 return children_;
140 }
141
142 /** Retrieves the extracted xml element tag. */
143 const std::string tag() const {
144 return tag_;
145 }
146
147 protected:
148 ManifestExtractor* extractor() const {
149 return extractor_;
150 }
151
152 /** Retrieves and stores the information extracted from the xml element. */
153 virtual void Extract(xml::Element* el) { }
154
155 /*
156 * Retrieves a configuration value of the resource entry that best matches the specified
157 * configuration.
158 */
159 static Value* BestConfigValue(ResourceEntry* entry,
160 const ConfigDescription& match) {
161 if (!entry) {
162 return nullptr;
163 }
164
165 // Determine the config that best matches the desired config
166 ResourceConfigValue* best_value = nullptr;
167 for (auto& value : entry->values) {
168 if (!value->config.match(match)) {
169 continue;
170 }
171
172 if (best_value != nullptr) {
173 if (!value->config.isBetterThan(best_value->config, &match)) {
174 if (value->config.compare(best_value->config) != 0) {
175 continue;
176 }
177 }
178 }
179
180 best_value = value.get();
181 }
182
183 // The entry has no values
184 if (!best_value) {
185 return nullptr;
186 }
187
188 return best_value->value.get();
189 }
190
191 /** Retrieves the resource assigned to the specified resource id if one exists. */
192 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700193 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700194 if (table) {
195 for (auto& package : table->packages) {
196 if (package->id && package->id.value() == res_id.package_id()) {
197 for (auto& type : package->types) {
198 if (type->id && type->id.value() == res_id.type_id()) {
199 for (auto& entry : type->entries) {
200 if (entry->id && entry->id.value() == res_id.entry_id()) {
201 if (auto value = BestConfigValue(entry.get(), config)) {
202 return value;
203 }
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211 return nullptr;
212 }
213
214 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700215 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700216 const int kMaxIterations = 40;
217 int i = 0;
218 while (ref && ref->id && i++ < kMaxIterations) {
219 auto table = extractor_->apk_->GetResourceTable();
220 if (auto value = FindValueById(table, ref->id.value(), config)) {
221 if (ValueCast<Reference>(value)) {
222 ref = ValueCast<Reference>(value);
223 } else {
224 return value;
225 }
226 }
227 }
228 return nullptr;
229 }
230
231 /**
232 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
233 * this will attempt to resolve the reference to an integer value.
234 **/
235 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700236 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700237 if (attr != nullptr) {
238 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700239 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700240 Value* value = attr->compiled_value.get();
241 if (ValueCast<Reference>(value)) {
242 value = ResolveReference(ValueCast<Reference>(value), config);
243 } else {
244 value = attr->compiled_value.get();
245 }
246 // Retrieve the integer data if possible
247 if (value != nullptr) {
248 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
249 return (int32_t*) &intValue->value.data;
250 }
251 }
252 }
253 }
254 return nullptr;
255 }
256
257 /**
258 * A version of GetAttributeInteger that returns a default integer if the attribute does not
259 * exist or cannot be resolved to an integer value.
260 **/
261 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700262 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700263 auto value = GetAttributeInteger(attr, config);
264 if (value) {
265 return *value;
266 }
267 return def;
268 }
269
270 /**
271 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
272 * this will attempt to resolve the reference to a string value.
273 **/
274 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700275 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700276 if (attr != nullptr) {
277 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700278 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700279 Value* value = attr->compiled_value.get();
280 if (ValueCast<Reference>(value)) {
281 value = ResolveReference(ValueCast<Reference>(value), config);
282 } else {
283 value = attr->compiled_value.get();
284 }
285
286 // Retrieve the string data of the value if possible
287 if (value != nullptr) {
288 if (String* intValue = ValueCast<String>(value)) {
289 return &(*intValue->value);
290 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
291 return &(*rawValue->value);
292 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
293 return &(*strValue->path);
294 }
295 }
296 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700297
298 if (!attr->value.empty()) {
299 return &attr->value;
300 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700301 }
302 return nullptr;
303 }
304
305 /**
306 * A version of GetAttributeString that returns a default string if the attribute does not
307 * exist or cannot be resolved to an string value.
308 **/
309 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700310 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700311 auto value = GetAttributeString(attr, config);
312 if (value) {
313 return *value;
314 }
315 return def;
316 }
317
318 private:
319 ManifestExtractor* extractor_;
320 std::vector<std::unique_ptr<Element>> children_;
321 std::string tag_;
322 };
323
324 friend Element;
325
326 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700327 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700328 ConfigDescription config;
329 config.orientation = android::ResTable_config::ORIENTATION_PORT;
330 config.density = android::ResTable_config::DENSITY_MEDIUM;
Ryan Mitchell95f02422020-04-30 10:25:53 -0700331 config.sdkVersion = kCurrentDevelopmentVersion; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700332 config.screenWidthDp = 320;
333 config.screenHeightDp = 480;
334 config.smallestScreenWidthDp = 320;
335 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
336 return config;
337 }
338
Ryan Mitchell214846d2018-09-19 16:57:01 -0700339 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700340
341 /** Recursively visit the xml element tree and return a processed badging element tree. */
342 std::unique_ptr<Element> Visit(xml::Element* element);
343
344 /** Raises the target sdk value if the min target is greater than the current target. */
345 void RaiseTargetSdk(int32_t min_target) {
346 if (min_target > target_sdk_) {
347 target_sdk_ = min_target;
348 }
349 }
350
351 /**
352 * Retrieves the default feature group that features are added into when <uses-feature>
353 * are not in a <feature-group> element.
354 **/
355 CommonFeatureGroup* GetCommonFeatureGroup() {
356 return commonFeatureGroup_.get();
357 }
358
359 /**
360 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
361 * used for that density setting.
362 **/
363 const std::map<uint16_t, ConfigDescription> densities() const {
364 return densities_;
365 }
366
367 /**
368 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
369 * would be used for that locale setting.
370 **/
371 const std::map<std::string, ConfigDescription> locales() const {
372 return locales_;
373 }
374
375 /** Retrieves the current stack of parent during data extraction. */
376 const std::vector<Element*> parent_stack() const {
377 return parent_stack_;
378 }
379
380 int32_t target_sdk() const {
381 return target_sdk_;
382 }
383
384 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700385 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700386
387 private:
388 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
389 std::map<std::string, ConfigDescription> locales_;
390 std::map<uint16_t, ConfigDescription> densities_;
391 std::vector<Element*> parent_stack_;
392 int32_t target_sdk_ = 0;
393};
394
395template<typename T> T* ElementCast(ManifestExtractor::Element* element);
396
397/** Recurs through the children of the specified root in depth-first order. */
398static void ForEachChild(ManifestExtractor::Element* root,
399 std::function<void(ManifestExtractor::Element*)> f) {
400 for (auto& child : root->children()) {
401 f(child.get());
402 ForEachChild(child.get(), f);
403 }
404}
405
406/**
407 * Checks the element and its recursive children for an element that makes the specified
408 * conditional function return true. Returns the first element that makes the conditional function
409 * return true.
410 **/
411static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
412 std::function<bool(ManifestExtractor::Element*)> f) {
413 if (f(root)) {
414 return root;
415 }
416 for (auto& child : root->children()) {
417 if (auto b2 = FindElement(child.get(), f)) {
418 return b2;
419 }
420 }
421 return nullptr;
422}
423
424/** Represents the <manifest> elements **/
425class Manifest : public ManifestExtractor::Element {
426 public:
427 Manifest() = default;
428 std::string package;
429 int32_t versionCode;
430 std::string versionName;
431 const std::string* split = nullptr;
432 const std::string* platformVersionName = nullptr;
433 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700434 const int32_t* platformVersionNameInt = nullptr;
435 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700436 const int32_t* compilesdkVersion = nullptr;
437 const std::string* compilesdkVersionCodename = nullptr;
438 const int32_t* installLocation = nullptr;
439
440 void Extract(xml::Element* manifest) override {
441 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
442 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
443 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
444 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
445
446 // Extract the platform build info
447 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
448 "platformBuildVersionName"));
449 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
450 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700451 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
452 "platformBuildVersionName"));
453 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
454 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700455
456 // Extract the compile sdk info
457 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
458 compilesdkVersionCodename = GetAttributeString(
459 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
460 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
461 }
462
Ryan Mitchell214846d2018-09-19 16:57:01 -0700463 void Print(text::Printer* printer) override {
464 printer->Print(StringPrintf("package: name='%s' ", package.data()));
465 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700466 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700467 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700468
469 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700470 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700471 }
472 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700473 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700474 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700475 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
476 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700477 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700478 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700479 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700480 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
481 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700482 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700483 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700484 }
485 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700486 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700487 compilesdkVersionCodename->data()));
488 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700489 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700490
491 if (installLocation) {
492 switch (*installLocation) {
493 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700494 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700495 break;
496 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700497 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700498 break;
499 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700500 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700501 break;
502 default:
503 break;
504 }
505 }
506 }
507};
508
509/** Represents <application> elements. **/
510class Application : public ManifestExtractor::Element {
511 public:
512 Application() = default;
513 std::string label;
514 std::string icon;
515 std::string banner;
516 int32_t is_game;
517 int32_t debuggable;
518 int32_t test_only;
519 bool has_multi_arch;
520
521 /** Mapping from locales to app names. */
522 std::map<std::string, std::string> locale_labels;
523
524 /** Mapping from densities to app icons. */
525 std::map<uint16_t, std::string> density_icons;
526
527 void Extract(xml::Element* element) override {
528 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
529 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
530 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
531 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
532 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
533 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
534
535 // We must search by name because the multiArch flag hasn't been API
536 // frozen yet.
537 has_multi_arch = (GetAttributeIntegerDefault(
538 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
539
540 // Retrieve the app names for every locale the app supports
541 auto attr = FindAttribute(element, LABEL_ATTR);
542 for (auto& config : extractor()->locales()) {
543 if (auto label = GetAttributeString(attr, config.second)) {
544 if (label) {
545 locale_labels.insert(std::make_pair(config.first, *label));
546 }
547 }
548 }
549
550 // Retrieve the icons for the densities the app supports
551 attr = FindAttribute(element, ICON_ATTR);
552 for (auto& config : extractor()->densities()) {
553 if (auto resource = GetAttributeString(attr, config.second)) {
554 if (resource) {
555 density_icons.insert(std::make_pair(config.first, *resource));
556 }
557 }
558 }
559 }
560
Ryan Mitchell214846d2018-09-19 16:57:01 -0700561 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700562 // Print the labels for every locale
563 for (auto p : locale_labels) {
564 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700565 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700566 android::ResTable::normalizeForOutput(p.second.data())
567 .c_str()));
568 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700569 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700570 android::ResTable::normalizeForOutput(p.second.data())
571 .c_str()));
572 }
573 }
574
575 // Print the icon paths for every density
576 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700577 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700578 }
579
580 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700581 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700582 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700583 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700584 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700585 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700586 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700587 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700588
589 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700590 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700591 }
592 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700593 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700594 }
595 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700596 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700597 }
598 }
599};
600
601/** Represents <uses-sdk> elements. **/
602class UsesSdkBadging : public ManifestExtractor::Element {
603 public:
604 UsesSdkBadging() = default;
605 const int32_t* min_sdk = nullptr;
606 const std::string* min_sdk_name = nullptr;
607 const int32_t* max_sdk = nullptr;
608 const int32_t* target_sdk = nullptr;
609 const std::string* target_sdk_name = nullptr;
610
611 void Extract(xml::Element* element) override {
612 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
613 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
614 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
615 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
616 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
617
618 // Detect the target sdk of the element
619 if ((min_sdk_name && *min_sdk_name == "Donut")
620 || (target_sdk_name && *target_sdk_name == "Donut")) {
621 extractor()->RaiseTargetSdk(4);
622 }
623 if (min_sdk) {
624 extractor()->RaiseTargetSdk(*min_sdk);
625 }
626 if (target_sdk) {
627 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700628 } else if (target_sdk_name) {
629 extractor()->RaiseTargetSdk(kCurrentDevelopmentVersion);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700630 }
631 }
632
Ryan Mitchell214846d2018-09-19 16:57:01 -0700633 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700634 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700636 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700637 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700638 }
639 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700640 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700641 }
642 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700643 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700644 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700645 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700646 }
647 }
648};
649
650/** Represents <uses-configuration> elements. **/
651class UsesConfiguarion : public ManifestExtractor::Element {
652 public:
653 UsesConfiguarion() = default;
654 int32_t req_touch_screen = 0;
655 int32_t req_keyboard_type = 0;
656 int32_t req_hard_keyboard = 0;
657 int32_t req_navigation = 0;
658 int32_t req_five_way_nav = 0;
659
660 void Extract(xml::Element* element) override {
661 req_touch_screen = GetAttributeIntegerDefault(
662 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
663 req_keyboard_type = GetAttributeIntegerDefault(
664 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
665 req_hard_keyboard = GetAttributeIntegerDefault(
666 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
667 req_navigation = GetAttributeIntegerDefault(
668 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
669 req_five_way_nav = GetAttributeIntegerDefault(
670 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
671 }
672
Ryan Mitchell214846d2018-09-19 16:57:01 -0700673 void Print(text::Printer* printer) override {
674 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700675 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700676 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700677 }
678 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700679 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700680 }
681 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700682 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700683 }
684 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700685 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700686 }
687 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700688 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700689 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700690 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700691 }
692};
693
694/** Represents <supports-screen> elements. **/
695class SupportsScreen : public ManifestExtractor::Element {
696 public:
697 SupportsScreen() = default;
698 int32_t small_screen = 1;
699 int32_t normal_screen = 1;
700 int32_t large_screen = 1;
701 int32_t xlarge_screen = 1;
702 int32_t any_density = 1;
703 int32_t requires_smallest_width_dp = 0;
704 int32_t compatible_width_limit_dp = 0;
705 int32_t largest_width_limit_dp = 0;
706
707 void Extract(xml::Element* element) override {
708 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
709 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
710 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
711 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
712 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
713
714 requires_smallest_width_dp = GetAttributeIntegerDefault(
715 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
716 compatible_width_limit_dp = GetAttributeIntegerDefault(
717 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
718 largest_width_limit_dp = GetAttributeIntegerDefault(
719 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
720
721 // For modern apps, if screen size buckets haven't been specified
722 // but the new width ranges have, then infer the buckets from them.
723 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
724 && requires_smallest_width_dp > 0) {
725 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
726 : requires_smallest_width_dp;
727 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
728 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
729 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
730 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
731 }
732 }
733
Ryan Mitchell214846d2018-09-19 16:57:01 -0700734 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700735 int32_t small_screen_temp = small_screen;
736 int32_t normal_screen_temp = normal_screen;
737 int32_t large_screen_temp = large_screen;
738 int32_t xlarge_screen_temp = xlarge_screen;
739 int32_t any_density_temp = any_density;
740
741 // Determine default values for any unspecified screen sizes,
742 // based on the target SDK of the package. As of 4 (donut)
743 // the screen size support was introduced, so all default to
744 // enabled.
745 if (small_screen_temp > 0) {
746 small_screen_temp = target_sdk >= 4 ? -1 : 0;
747 }
748 if (normal_screen_temp > 0) {
749 normal_screen_temp = -1;
750 }
751 if (large_screen_temp > 0) {
752 large_screen_temp = target_sdk >= 4 ? -1 : 0;
753 }
754 if (xlarge_screen_temp > 0) {
755 // Introduced in Gingerbread.
756 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
757 }
758 if (any_density_temp > 0) {
759 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
760 || compatible_width_limit_dp > 0) ? -1 : 0;
761 }
762
763 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700764 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700765 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700766 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700767 }
768 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700769 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700770 }
771 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700772 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700773 }
774 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700775 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700776 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700777 printer->Print("\n");
778 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700779 (any_density_temp ) ? "true" : "false"));
780 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700781 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700782 }
783 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700784 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700785 }
786 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700787 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700788 }
789 }
790};
791
792/** Represents <feature-group> elements. **/
793class FeatureGroup : public ManifestExtractor::Element {
794 public:
795 FeatureGroup() = default;
796 std::string label;
797 int32_t open_gles_version = 0;
798
799 void Extract(xml::Element* element) override {
800 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
801 }
802
Ryan Mitchell214846d2018-09-19 16:57:01 -0700803 virtual void PrintGroup(text::Printer* printer) {
804 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700805 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700806 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700807 }
808
809 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700810 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700811 (feature.second.required ? "" : "-not-required"),
812 feature.first.data()));
813 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700814 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700815 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700816 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700817 }
818 }
819
820 /** Adds a feature to the feature group. */
821 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
822 features_.insert(std::make_pair(name, Feature{ required, version }));
823 if (required) {
824 if (name == "android.hardware.camera.autofocus" ||
825 name == "android.hardware.camera.flash") {
826 AddFeature("android.hardware.camera", true);
827 } else if (name == "android.hardware.location.gps" ||
828 name == "android.hardware.location.network") {
829 AddFeature("android.hardware.location", true);
830 } else if (name == "android.hardware.faketouch.multitouch") {
831 AddFeature("android.hardware.faketouch", true);
832 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
833 name == "android.hardware.faketouch.multitouch.jazzhands") {
834 AddFeature("android.hardware.faketouch.multitouch", true);
835 AddFeature("android.hardware.faketouch", true);
836 } else if (name == "android.hardware.touchscreen.multitouch") {
837 AddFeature("android.hardware.touchscreen", true);
838 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
839 name == "android.hardware.touchscreen.multitouch.jazzhands") {
840 AddFeature("android.hardware.touchscreen.multitouch", true);
841 AddFeature("android.hardware.touchscreen", true);
842 } else if (name == "android.hardware.opengles.aep") {
843 const int kOpenGLESVersion31 = 0x00030001;
844 if (kOpenGLESVersion31 > open_gles_version) {
845 open_gles_version = kOpenGLESVersion31;
846 }
847 }
848 }
849 }
850
851 /** Returns true if the feature group has the given feature. */
852 virtual bool HasFeature(const std::string& name) {
853 return features_.find(name) != features_.end();
854 }
855
856 /** Merges the features of another feature group into this group. */
857 void Merge(FeatureGroup* group) {
858 open_gles_version = std::max(open_gles_version, group->open_gles_version);
859 for (auto& feature : group->features_) {
860 features_.insert(feature);
861 }
862 }
863
864 protected:
865 struct Feature {
866 public:
867 bool required = false;
868 int32_t version = -1;
869 };
870
871 /* Mapping of feature names to their properties. */
872 std::map<std::string, Feature> features_;
873};
874
875/**
876 * Represents the default feature group for the application if no <feature-group> elements are
877 * present in the manifest.
878 **/
879class CommonFeatureGroup : public FeatureGroup {
880 public:
881 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700882 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700883 FeatureGroup::PrintGroup(printer);
884
885 // Also print the implied features
886 for (auto feature : implied_features_) {
887 if (features_.find(feature.first) == features_.end()) {
888 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700889 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
890 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700891 feature.first.data()));
892
893 // Print the reasons as a sentence
894 size_t count = 0;
895 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700896 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700897 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700900 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700901 }
902 count++;
903 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700904 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700905 }
906 }
907 }
908
909 /** Returns true if the feature group has the given feature. */
910 bool HasFeature(const std::string& name) override {
911 return FeatureGroup::HasFeature(name)
912 || implied_features_.find(name) != implied_features_.end();
913 }
914
915 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
916 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
917 auto entry = implied_features_.find(name);
918 if (entry == implied_features_.end()) {
919 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
920 entry = implied_features_.find(name);
921 }
922
923 // A non-sdk 23 implied feature takes precedence.
924 if (entry->second.implied_from_sdk_k23 && !sdk23) {
925 entry->second.implied_from_sdk_k23 = false;
926 }
927
928 entry->second.reasons.insert(reason);
929 }
930
931 /**
932 * Adds a feature to a set of implied features for all features that are implied by the presence
933 * of the permission.
934 **/
935 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
936 if (name == "android.permission.CAMERA") {
937 addImpliedFeature("android.hardware.camera",
938 StringPrintf("requested %s permission", name.data()),
939 sdk23);
940
941 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
942 if (targetSdk < SDK_LOLLIPOP) {
943 addImpliedFeature("android.hardware.location.gps",
944 StringPrintf("requested %s permission", name.data()),
945 sdk23);
946 addImpliedFeature("android.hardware.location.gps",
947 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
948 sdk23);
949 }
950 addImpliedFeature("android.hardware.location",
951 StringPrintf("requested %s permission", name.data()),
952 sdk23);
953
954 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
955 if (targetSdk < SDK_LOLLIPOP) {
956 addImpliedFeature("android.hardware.location.network",
957 StringPrintf("requested %s permission", name.data()),
958 sdk23);
959 addImpliedFeature("android.hardware.location.network",
960 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
961 sdk23);
962 }
963 addImpliedFeature("android.hardware.location",
964 StringPrintf("requested %s permission", name.data()),
965 sdk23);
966
967 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
968 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
969 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
970 addImpliedFeature("android.hardware.location",
971 StringPrintf("requested %s permission", name.data()),
972 sdk23);
973
974 } else if (name == "android.permission.BLUETOOTH" ||
975 name == "android.permission.BLUETOOTH_ADMIN") {
976 if (targetSdk > SDK_DONUT) {
977 addImpliedFeature("android.hardware.bluetooth",
978 StringPrintf("requested %s permission", name.data()),
979 sdk23);
980 addImpliedFeature("android.hardware.bluetooth",
981 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
982 sdk23);
983 }
984
985 } else if (name == "android.permission.RECORD_AUDIO") {
986 addImpliedFeature("android.hardware.microphone",
987 StringPrintf("requested %s permission", name.data()),
988 sdk23);
989
990 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
991 name == "android.permission.CHANGE_WIFI_STATE" ||
992 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
993 addImpliedFeature("android.hardware.wifi",
994 StringPrintf("requested %s permission", name.data()),
995 sdk23);
996
997 } else if (name == "android.permission.CALL_PHONE" ||
998 name == "android.permission.CALL_PRIVILEGED" ||
999 name == "android.permission.MODIFY_PHONE_STATE" ||
1000 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1001 name == "android.permission.READ_SMS" ||
1002 name == "android.permission.RECEIVE_SMS" ||
1003 name == "android.permission.RECEIVE_MMS" ||
1004 name == "android.permission.RECEIVE_WAP_PUSH" ||
1005 name == "android.permission.SEND_SMS" ||
1006 name == "android.permission.WRITE_APN_SETTINGS" ||
1007 name == "android.permission.WRITE_SMS") {
1008 addImpliedFeature("android.hardware.telephony",
1009 "requested a telephony permission",
1010 sdk23);
1011 }
1012 }
1013
1014 private:
1015 /**
1016 * Represents a feature that has been automatically added due to a pre-requisite or for some
1017 * other reason.
1018 */
1019 struct ImpliedFeature {
1020 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1021
1022 /** List of human-readable reasons for why this feature was implied. */
1023 std::set<std::string> reasons;
1024
1025 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1026 bool implied_from_sdk_k23;
1027 };
1028
1029 /* Mapping of implied feature names to their properties. */
1030 std::map<std::string, ImpliedFeature> implied_features_;
1031};
1032
1033/** Represents <uses-feature> elements. **/
1034class UsesFeature : public ManifestExtractor::Element {
1035 public:
1036 UsesFeature() = default;
1037 void Extract(xml::Element* element) override {
1038 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1039 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1040 bool required = GetAttributeIntegerDefault(
1041 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1042 int32_t version = GetAttributeIntegerDefault(
1043 FindAttribute(element, kAndroidNamespace, "version"), 0);
1044
1045 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1046 // common feature group
1047 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1048 if (!feature_group) {
1049 feature_group = extractor()->GetCommonFeatureGroup();
1050 } else {
1051 // All features in side of <feature-group> elements are required.
1052 required = true;
1053 }
1054
1055 if (name) {
1056 feature_group->AddFeature(*name, required, version);
1057 } else if (gl) {
1058 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1059 }
1060 }
1061};
1062
1063/** Represents <uses-permission> elements. **/
1064class UsesPermission : public ManifestExtractor::Element {
1065 public:
1066 UsesPermission() = default;
1067 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001068 std::vector<std::string> requiredFeatures;
1069 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001070 int32_t required = true;
1071 int32_t maxSdkVersion = -1;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001072 int32_t usesPermissionFlags = 0;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001073
1074 void Extract(xml::Element* element) override {
1075 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001076 std::string feature =
1077 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1078 if (!feature.empty()) {
1079 requiredFeatures.push_back(feature);
1080 }
1081 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1082 if (!feature.empty()) {
1083 requiredNotFeatures.push_back(feature);
1084 }
1085
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001086 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1087 maxSdkVersion = GetAttributeIntegerDefault(
1088 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001089 usesPermissionFlags = GetAttributeIntegerDefault(
1090 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001091
1092 if (!name.empty()) {
1093 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1094 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1095 }
1096 }
1097
Ryan Mitchell214846d2018-09-19 16:57:01 -07001098 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001099 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001100 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001101 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001102 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001103 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001104 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1105 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1106 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001107 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001108 for (const std::string& requiredFeature : requiredFeatures) {
1109 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1110 }
1111 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1112 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1113 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001114 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001115 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001116 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001117 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001118 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001119 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1120 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1121 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001122 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001123 }
1124 }
1125 }
1126
Ryan Mitchell214846d2018-09-19 16:57:01 -07001127 void PrintImplied(text::Printer* printer, const std::string& reason) {
1128 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001129 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001130 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001131 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001132 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1133 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1134 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001135 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001136 }
1137};
1138
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001139/** Represents <required-feature> elements. **/
1140class RequiredFeature : public ManifestExtractor::Element {
1141 public:
1142 RequiredFeature() = default;
1143 std::string name;
1144
1145 void Extract(xml::Element* element) override {
1146 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1147 auto parent_stack = extractor()->parent_stack();
1148 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1149 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1150 uses_permission->requiredFeatures.push_back(name);
1151 }
1152 }
1153};
1154
1155/** Represents <required-not-feature> elements. **/
1156class RequiredNotFeature : public ManifestExtractor::Element {
1157 public:
1158 RequiredNotFeature() = default;
1159 std::string name;
1160
1161 void Extract(xml::Element* element) override {
1162 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1163 auto parent_stack = extractor()->parent_stack();
1164 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1165 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1166 uses_permission->requiredNotFeatures.push_back(name);
1167 }
1168 }
1169};
1170
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001171/** Represents <uses-permission-sdk-23> elements. **/
1172class UsesPermissionSdk23 : public ManifestExtractor::Element {
1173 public:
1174 UsesPermissionSdk23() = default;
1175 const std::string* name = nullptr;
1176 const int32_t* maxSdkVersion = nullptr;
1177
1178 void Extract(xml::Element* element) override {
1179 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1180 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1181
1182 if (name) {
1183 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1184 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1185 }
1186 }
1187
Ryan Mitchell214846d2018-09-19 16:57:01 -07001188 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001189 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001190 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001191 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001192 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001193 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001194 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001195 }
1196 }
1197};
1198
1199/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1200class Permission : public ManifestExtractor::Element {
1201 public:
1202 Permission() = default;
1203 std::string name;
1204
1205 void Extract(xml::Element* element) override {
1206 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1207 }
1208
Ryan Mitchell214846d2018-09-19 16:57:01 -07001209 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001210 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001211 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001212 }
1213 }
1214};
1215
1216/** Represents <activity> elements. **/
1217class Activity : public ManifestExtractor::Element {
1218 public:
1219 Activity() = default;
1220 std::string name;
1221 std::string icon;
1222 std::string label;
1223 std::string banner;
1224
1225 bool has_component_ = false;
1226 bool has_launcher_category = false;
1227 bool has_leanback_launcher_category = false;
1228 bool has_main_action = false;
1229
1230 void Extract(xml::Element* element) override {
1231 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1232 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1233 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1234 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1235
1236 // Retrieve the package name from the manifest
1237 std::string package;
1238 for (auto& parent : extractor()->parent_stack()) {
1239 if (auto manifest = ElementCast<Manifest>(parent)) {
1240 package = manifest->package;
1241 break;
1242 }
1243 }
1244
1245 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001246 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001247 if (idx == 0) {
1248 name = package + name;
1249 } else if (idx < 0) {
1250 name = package + "." + name;
1251 }
1252
1253 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1254 if (orientation) {
1255 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1256 int orien = *orientation;
1257 if (orien == 0 || orien == 6 || orien == 8) {
1258 // Requests landscape, sensorLandscape, or reverseLandscape.
1259 common->addImpliedFeature("android.hardware.screen.landscape",
1260 "one or more activities have specified a landscape orientation",
1261 false);
1262 } else if (orien == 1 || orien == 7 || orien == 9) {
1263 // Requests portrait, sensorPortrait, or reversePortrait.
1264 common->addImpliedFeature("android.hardware.screen.portrait",
1265 "one or more activities have specified a portrait orientation",
1266 false);
1267 }
1268 }
1269 }
1270
Ryan Mitchell214846d2018-09-19 16:57:01 -07001271 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001272 // Print whether the activity has the HOME category and a the MAIN action
1273 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001274 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001275 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001276 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001277 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001278 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001279 android::ResTable::normalizeForOutput(label.data()).c_str(),
1280 icon.data()));
1281 }
1282
1283 // Print wether the activity has the HOME category and a the MAIN action
1284 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001285 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001286 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001287 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001288 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001289 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001290 android::ResTable::normalizeForOutput(label.data()).c_str(),
1291 icon.data(), banner.data()));
1292 }
1293 }
1294};
1295
1296/** Represents <intent-filter> elements. */
1297class IntentFilter : public ManifestExtractor::Element {
1298 public:
1299 IntentFilter() = default;
1300};
1301
1302/** Represents <category> elements. */
1303class Category : public ManifestExtractor::Element {
1304 public:
1305 Category() = default;
1306 std::string component = "";
1307
1308 void Extract(xml::Element* element) override {
1309 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1310
1311 auto parent_stack = extractor()->parent_stack();
1312 if (category && ElementCast<IntentFilter>(parent_stack[0])
1313 && ElementCast<Activity>(parent_stack[1])) {
1314 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1315
1316 if (*category == "android.intent.category.LAUNCHER") {
1317 activity->has_launcher_category = true;
1318 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1319 activity->has_leanback_launcher_category = true;
1320 } else if (*category == "android.intent.category.HOME") {
1321 component = "launcher";
1322 }
1323 }
1324 }
1325};
1326
1327/**
1328 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1329 * elements nested within.
1330 **/
1331class Provider : public ManifestExtractor::Element {
1332 public:
1333 Provider() = default;
1334 bool has_required_saf_attributes = false;
1335
1336 void Extract(xml::Element* element) override {
1337 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1338 const int32_t* grant_uri_permissions = GetAttributeInteger(
1339 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1340 const std::string* permission = GetAttributeString(
1341 FindAttribute(element, PERMISSION_ATTR));
1342
1343 has_required_saf_attributes = ((exported && *exported != 0)
1344 && (grant_uri_permissions && *grant_uri_permissions != 0)
1345 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1346 }
1347};
1348
1349/** Represents <receiver> elements. **/
1350class Receiver : public ManifestExtractor::Element {
1351 public:
1352 Receiver() = default;
1353 const std::string* permission = nullptr;
1354 bool has_component = false;
1355
1356 void Extract(xml::Element* element) override {
1357 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1358 }
1359};
1360
1361/**Represents <service> elements. **/
1362class Service : public ManifestExtractor::Element {
1363 public:
1364 Service() = default;
1365 const std::string* permission = nullptr;
1366 bool has_component = false;
1367
1368 void Extract(xml::Element* element) override {
1369 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1370 }
1371};
1372
1373/** Represents <uses-library> elements. **/
1374class UsesLibrary : public ManifestExtractor::Element {
1375 public:
1376 UsesLibrary() = default;
1377 std::string name;
1378 int required;
1379
1380 void Extract(xml::Element* element) override {
1381 auto parent_stack = extractor()->parent_stack();
1382 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1383 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1384 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1385 }
1386 }
1387
Ryan Mitchell214846d2018-09-19 16:57:01 -07001388 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001389 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001390 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001391 (required == 0) ? "-not-required" : "", name.data()));
1392 }
1393 }
1394};
1395
Dianne Hackborn813d7502018-10-02 16:59:46 -07001396/** Represents <static-library> elements. **/
1397class StaticLibrary : public ManifestExtractor::Element {
1398 public:
1399 StaticLibrary() = default;
1400 std::string name;
1401 int version;
1402 int versionMajor;
1403
1404 void Extract(xml::Element* element) override {
1405 auto parent_stack = extractor()->parent_stack();
1406 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1407 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1408 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1409 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1410 }
1411 }
1412
Ryan Mitchell214846d2018-09-19 16:57:01 -07001413 void Print(text::Printer* printer) override {
1414 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001415 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1416 name.data(), version, versionMajor));
1417 }
1418};
1419
1420/** Represents <uses-static-library> elements. **/
1421class UsesStaticLibrary : public ManifestExtractor::Element {
1422 public:
1423 UsesStaticLibrary() = default;
1424 std::string name;
1425 int version;
1426 int versionMajor;
1427 std::vector<std::string> certDigests;
1428
1429 void Extract(xml::Element* element) override {
1430 auto parent_stack = extractor()->parent_stack();
1431 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1432 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1433 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1434 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1435 AddCertDigest(element);
1436 }
1437 }
1438
1439 void AddCertDigest(xml::Element* element) {
1440 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1441 // We allow ":" delimiters in the SHA declaration as this is the format
1442 // emitted by the certtool making it easy for developers to copy/paste.
1443 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1444 if (!digest.empty()) {
1445 certDigests.push_back(digest);
1446 }
1447 }
1448
Ryan Mitchell214846d2018-09-19 16:57:01 -07001449 void Print(text::Printer* printer) override {
1450 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001451 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1452 name.data(), version, versionMajor));
1453 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001454 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001455 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001456 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001457 }
1458};
1459
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001460/** Represents <uses-native-library> elements. **/
1461class UsesNativeLibrary : public ManifestExtractor::Element {
1462 public:
1463 UsesNativeLibrary() = default;
1464 std::string name;
1465 int required;
1466
1467 void Extract(xml::Element* element) override {
1468 auto parent_stack = extractor()->parent_stack();
1469 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1470 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1471 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1472 }
1473 }
1474
1475 void Print(text::Printer* printer) override {
1476 if (!name.empty()) {
1477 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1478 (required == 0) ? "-not-required" : "", name.data()));
1479 }
1480 }
1481};
1482
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001483/**
1484 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1485 * explicitly enable meta data printing.
1486 **/
1487class MetaData : public ManifestExtractor::Element {
1488 public:
1489 MetaData() = default;
1490 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001491 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001492 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001493 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001494 const int* resource_int;
1495
1496 void Extract(xml::Element* element) override {
1497 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001498 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001499 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001500 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001501 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1502 }
1503
Ryan Mitchell214846d2018-09-19 16:57:01 -07001504 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001505 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001506 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001507 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001508 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001509 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001510 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001511 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001512 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001513 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001514 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001515 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001516 }
1517 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001518 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001519 }
1520 }
1521};
1522
1523/**
1524 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1525 * service components.
1526 **/
1527class Action : public ManifestExtractor::Element {
1528 public:
1529 Action() = default;
1530 std::string component = "";
1531
1532 void Extract(xml::Element* element) override {
1533 auto parent_stack = extractor()->parent_stack();
1534 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1535
1536 if (ElementCast<IntentFilter>(parent_stack[0])) {
1537 if (ElementCast<Activity>(parent_stack[1])) {
1538 // Detects the presence of a particular type of activity.
1539 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1540 auto map = std::map<std::string, std::string>({
1541 { "android.intent.action.MAIN" , "main" },
1542 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1543 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1544 });
1545
1546 auto entry = map.find(action);
1547 if (entry != map.end()) {
1548 component = entry->second;
1549 activity->has_component_ = true;
1550 }
1551
1552 if (action == "android.intent.action.MAIN") {
1553 activity->has_main_action = true;
1554 }
1555
1556 } else if (ElementCast<Receiver>(parent_stack[1])) {
1557 // Detects the presence of a particular type of receiver. If the action requires a
1558 // permission, then the receiver element is checked for the permission.
1559 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1560 auto map = std::map<std::string, std::string>({
1561 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1562 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1563 });
1564
1565 auto permissions = std::map<std::string, std::string>({
1566 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1567 });
1568
1569 auto entry = map.find(action);
1570 auto permission = permissions.find(action);
1571 if (entry != map.end() && (permission == permissions.end()
1572 || (receiver->permission && permission->second == *receiver->permission))) {
1573 receiver->has_component = true;
1574 component = entry->second;
1575 }
1576
1577 } else if (ElementCast<Service>(parent_stack[1])) {
1578 // Detects the presence of a particular type of service. If the action requires a
1579 // permission, then the service element is checked for the permission.
1580 Service* service = ElementCast<Service>(parent_stack[1]);
1581 auto map = std::map<std::string, std::string>({
1582 { "android.view.InputMethod" , "ime" },
1583 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1584 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1585 { "android.printservice.PrintService" , "print-service" },
1586 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1587 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1588 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1589 { "android.service.dreams.DreamService" , "dream" },
1590 });
1591
1592 auto permissions = std::map<std::string, std::string>({
1593 { "android.accessibilityservice.AccessibilityService" ,
1594 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1595 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1596 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1597 "android.permission.BIND_NFC_SERVICE" },
1598 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1599 "android.permission.BIND_NFC_SERVICE" },
1600 { "android.service.notification.NotificationListenerService" ,
1601 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1602 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1603 });
1604
1605 auto entry = map.find(action);
1606 auto permission = permissions.find(action);
1607 if (entry != map.end() && (permission == permissions.end()
1608 || (service->permission && permission->second == *service->permission))) {
1609 service->has_component= true;
1610 component = entry->second;
1611 }
1612
1613 } else if (ElementCast<Provider>(parent_stack[1])) {
1614 // Detects the presence of a particular type of receiver. If the provider requires a
1615 // permission, then the provider element is checked for the permission.
1616 // Detect whether this action
1617 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1618 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1619 && provider->has_required_saf_attributes) {
1620 component = "document-provider";
1621 }
1622 }
1623 }
1624
1625 // Represents a searchable interface
1626 if (action == "android.intent.action.SEARCH") {
1627 component = "search";
1628 }
1629 }
1630};
1631
1632/**
1633 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1634 **/
1635class SupportsInput : public ManifestExtractor::Element {
1636 public:
1637 SupportsInput() = default;
1638 std::vector<std::string> inputs;
1639
Ryan Mitchell214846d2018-09-19 16:57:01 -07001640 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001641 const size_t size = inputs.size();
1642 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001643 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001644 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001645 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001646 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001647 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001648 }
1649 }
1650};
1651
1652/** Represents <input-type> elements. **/
1653class InputType : public ManifestExtractor::Element {
1654 public:
1655 InputType() = default;
1656 void Extract(xml::Element* element) override {
1657 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1658 auto parent_stack = extractor()->parent_stack();
1659
1660 // Add the input to the set of supported inputs
1661 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1662 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1663 supports->inputs.push_back(*name);
1664 }
1665 }
1666};
1667
1668/** Represents <original-package> elements. **/
1669class OriginalPackage : public ManifestExtractor::Element {
1670 public:
1671 OriginalPackage() = default;
1672 const std::string* name = nullptr;
1673
1674 void Extract(xml::Element* element) override {
1675 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1676 }
1677
Ryan Mitchell214846d2018-09-19 16:57:01 -07001678 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001679 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001680 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001681 }
1682 }
1683};
1684
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001685
1686/** Represents <overlay> elements. **/
1687class Overlay : public ManifestExtractor::Element {
1688 public:
1689 Overlay() = default;
1690 const std::string* target_package = nullptr;
1691 int priority;
1692 bool is_static;
1693 const std::string* required_property_name = nullptr;
1694 const std::string* required_property_value = nullptr;
1695
1696 void Extract(xml::Element* element) override {
1697 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1698 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1699 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1700 required_property_name = GetAttributeString(
1701 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1702 required_property_value = GetAttributeString(
1703 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1704 }
1705
1706 void Print(text::Printer* printer) override {
1707 printer->Print(StringPrintf("overlay:"));
1708 if (target_package) {
1709 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1710 }
1711 printer->Print(StringPrintf(" priority='%d'", priority));
1712 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1713 if (required_property_name) {
1714 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1715 }
1716 if (required_property_value) {
1717 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1718 }
1719 printer->Print("\n");
1720 }
1721};
1722
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001723/** * Represents <package-verifier> elements. **/
1724class PackageVerifier : public ManifestExtractor::Element {
1725 public:
1726 PackageVerifier() = default;
1727 const std::string* name = nullptr;
1728 const std::string* public_key = nullptr;
1729
1730 void Extract(xml::Element* element) override {
1731 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1732 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1733 }
1734
Ryan Mitchell214846d2018-09-19 16:57:01 -07001735 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001736 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001737 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001738 name->data(), public_key->data()));
1739 }
1740 }
1741};
1742
1743/** Represents <uses-package> elements. **/
1744class UsesPackage : public ManifestExtractor::Element {
1745 public:
1746 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001747 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001748 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001749 int version;
1750 int versionMajor;
1751 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001752
1753 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001754 auto parent_stack = extractor()->parent_stack();
1755 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1756 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1757 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1758 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1759 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1760 AddCertDigest(element);
1761 }
1762 }
1763
1764 void AddCertDigest(xml::Element* element) {
1765 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1766 // We allow ":" delimiters in the SHA declaration as this is the format
1767 // emitted by the certtool making it easy for developers to copy/paste.
1768 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1769 if (!digest.empty()) {
1770 certDigests.push_back(digest);
1771 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001772 }
1773
Ryan Mitchell214846d2018-09-19 16:57:01 -07001774 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001775 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001776 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001777 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001778 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1779 packageType->data(), name->data(), version, versionMajor));
1780 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001781 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001782 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001783 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001784 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001785 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001786 }
1787 }
1788 }
1789};
1790
1791/** Represents <additional-certificate> elements. **/
1792class AdditionalCertificate : public ManifestExtractor::Element {
1793 public:
1794 AdditionalCertificate() = default;
1795
1796 void Extract(xml::Element* element) override {
1797 auto parent_stack = extractor()->parent_stack();
1798 if (parent_stack.size() > 0) {
1799 if (ElementCast<UsesPackage>(parent_stack[0])) {
1800 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1801 uses->AddCertDigest(element);
1802 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1803 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1804 uses->AddCertDigest(element);
1805 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001806 }
1807 }
1808};
1809
1810/** Represents <screen> elements found in <compatible-screens> elements. */
1811class Screen : public ManifestExtractor::Element {
1812 public:
1813 Screen() = default;
1814 const int32_t* size = nullptr;
1815 const int32_t* density = nullptr;
1816
1817 void Extract(xml::Element* element) override {
1818 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1819 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1820 }
1821};
1822
1823/**
1824 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1825 * that each denote a supported screen size and screen density.
1826 **/
1827class CompatibleScreens : public ManifestExtractor::Element {
1828 public:
1829 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001830 void Print(text::Printer* printer) override {
1831 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001832
1833 bool first = true;
1834 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1835 if (auto screen = ElementCast<Screen>(el)) {
1836 if (first) {
1837 first = false;
1838 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001839 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001840 }
1841
1842 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001843 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001844 }
1845 }
1846 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001847 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001848 }
1849};
1850
1851/** Represents <supports-gl-texture> elements. **/
1852class SupportsGlTexture : public ManifestExtractor::Element {
1853 public:
1854 SupportsGlTexture() = default;
1855 const std::string* name = nullptr;
1856
1857 void Extract(xml::Element* element) override {
1858 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1859 }
1860
Ryan Mitchell214846d2018-09-19 16:57:01 -07001861 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001862 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001863 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001864 }
1865 }
1866};
1867
Todd Kennedyce3e1292020-10-29 17:14:24 -07001868/** Represents <property> elements. **/
1869class Property : public ManifestExtractor::Element {
1870 public:
1871 Property() = default;
1872 std::string name;
1873 std::string value;
1874 const int* value_int;
1875 std::string resource;
1876 const int* resource_int;
1877
1878 void Extract(xml::Element* element) override {
1879 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1880 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
1881 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
1882 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
1883 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1884 }
1885
1886 void Print(text::Printer* printer) override {
1887 printer->Print(StringPrintf("property: name='%s' ", name.data()));
1888 if (!value.empty()) {
1889 printer->Print(StringPrintf("value='%s' ", value.data()));
1890 } else if (value_int) {
1891 printer->Print(StringPrintf("value='%d' ", *value_int));
1892 } else {
1893 if (!resource.empty()) {
1894 printer->Print(StringPrintf("resource='%s' ", resource.data()));
1895 } else if (resource_int) {
1896 printer->Print(StringPrintf("resource='%d' ", *resource_int));
1897 }
1898 }
1899 printer->Print("\n");
1900 }
1901};
1902
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001903/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001904static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001905 el->Print(printer);
1906 for (auto &child : el->children()) {
1907 Print(child.get(), printer);
1908 }
1909}
1910
Ryan Mitchell214846d2018-09-19 16:57:01 -07001911bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001912 // Load the manifest
1913 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1914 if (doc == nullptr) {
1915 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1916 return false;
1917 }
1918
1919 xml::Element* element = doc->root.get();
1920 if (element->name != "manifest") {
1921 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1922 return false;
1923 }
1924
1925 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1926 // printing only permission elements is requested
1927 if (options_.only_permissions) {
1928 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1929 ManifestExtractor::Element::Inflate(this, element);
1930
1931 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1932 for (xml::Element* child : element->GetChildElements()) {
1933 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1934 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001935 // Inflate the element and its descendants
1936 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001937 manifest->AddChild(permission_element);
1938 }
1939 }
1940
Ryan Mitchell214846d2018-09-19 16:57:01 -07001941 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001942 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1943 el->Print(printer);
1944 });
1945
1946 return true;
1947 }
1948
1949 return false;
1950 }
1951
1952 // Collect information about the resource configurations
1953 if (apk_->GetResourceTable()) {
1954 for (auto &package : apk_->GetResourceTable()->packages) {
1955 for (auto &type : package->types) {
1956 for (auto &entry : type->entries) {
1957 for (auto &value : entry->values) {
1958 std::string locale_str = value->config.GetBcp47LanguageTag();
1959
1960 // Collect all the unique locales of the apk
1961 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001962 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001963 config.setBcp47Locale(locale_str.data());
1964 locales_.insert(std::make_pair(locale_str, config));
1965 }
1966
1967 // Collect all the unique density of the apk
1968 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1969 : value->config.density;
1970 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001971 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001972 config.density = density;
1973 densities_.insert(std::make_pair(density, config));
1974 }
1975 }
1976 }
1977 }
1978 }
1979 }
1980
1981 // Extract badging information
1982 auto root = Visit(element);
1983
1984 // Print the elements in order seen
1985 Print(root.get(), printer);
1986
1987 /** Recursively checks the extracted elements for the specified permission. **/
1988 auto FindPermission = [&](ManifestExtractor::Element* root,
1989 const std::string& name) -> ManifestExtractor::Element* {
1990 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1991 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1992 return permission->name == name;
1993 }
1994 return false;
1995 });
1996 };
1997
1998 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1999 int32_t max_sdk_version) -> void {
2000 auto permission = util::make_unique<UsesPermission>();
2001 permission->name = name;
2002 permission->maxSdkVersion = max_sdk_version;
2003 permission->Print(printer);
2004 permission->PrintImplied(printer, reason);
2005 };
2006
2007 // Implied permissions
2008 // Pre-1.6 implicitly granted permission compatibility logic
2009 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
2010 bool insert_write_external = false;
2011 auto write_external_permission = ElementCast<UsesPermission>(
2012 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
2013
2014 if (target_sdk() < 4) {
2015 if (!write_external_permission) {
2016 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
2017 insert_write_external = true;
2018 }
2019
2020 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
2021 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
2022 }
2023 }
2024
2025 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2026 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2027 // do this (regardless of target API version) because we can't have
2028 // an app with write permission but not read permission.
2029 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
2030 if (!read_external && (insert_write_external || write_external_permission)) {
2031 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
2032 "requested WRITE_EXTERNAL_STORAGE",
2033 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
2034 }
2035
2036 // Pre-JellyBean call log permission compatibility.
2037 if (target_sdk() < 16) {
2038 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
2039 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
2040 PrintPermission("android.permission.READ_CALL_LOG",
2041 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
2042 }
2043
2044 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
2045 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
2046 PrintPermission("android.permission.WRITE_CALL_LOG",
2047 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
2048 }
2049 }
2050
2051 // If the app hasn't declared the touchscreen as a feature requirement (either
2052 // directly or implied, required or not), then the faketouch feature is implied.
2053 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
2054 common_feature_group->addImpliedFeature("android.hardware.faketouch",
2055 "default feature for all apps", false);
2056 }
2057
2058 // Only print the common feature group if no feature group is defined
2059 std::vector<FeatureGroup*> feature_groups;
2060 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
2061 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2062 feature_groups.push_back(feature_group);
2063 }
2064 });
2065
2066 if (feature_groups.empty()) {
2067 common_feature_group->PrintGroup(printer);
2068 } else {
2069 // Merge the common feature group into the feature group
2070 for (auto& feature_group : feature_groups) {
2071 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
2072 common_feature_group->open_gles_version);
2073 feature_group->Merge(common_feature_group);
2074 feature_group->PrintGroup(printer);
2075 }
2076 };
2077
2078 // Collect the component types of the application
2079 std::set<std::string> components;
2080 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
2081 if (ElementCast<Action>(el)) {
2082 auto action = ElementCast<Action>(el);
2083 if (!action->component.empty()) {
2084 components.insert(action->component);
2085 return;
2086 }
2087 }
2088
2089 if (ElementCast<Category>(el)) {
2090 auto category = ElementCast<Category>(el);
2091 if (!category->component.empty()) {
2092 components.insert(category->component);
2093 return;
2094 }
2095 }
2096 });
2097
2098 // Check for the payment component
2099 auto apk = apk_;
2100 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2101 if (auto service = ElementCast<Service>(el)) {
2102 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2103 [&](ManifestExtractor::Element* el) -> bool {
2104 if (auto action = ElementCast<Action>(el)) {
2105 return (action->component == "host-apdu");
2106 }
2107 return false;
2108 }));
2109
2110 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2111 [&](ManifestExtractor::Element* el) -> bool {
2112 if (auto action = ElementCast<Action>(el)) {
2113 return (action->component == "offhost-apdu");
2114 }
2115 return false;
2116 }));
2117
2118 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2119 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2120 if (auto meta_data = ElementCast<MetaData>(el)) {
2121 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2122 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2123 && offhost_apdu_action)) {
2124
2125 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002126 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002127 return;
2128 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002129 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002130 if (!resource) {
2131 return;
2132 }
2133
2134 // Look for the payment category on an <aid-group> element
2135 auto& root = resource.get()->root;
2136 if ((host_apdu_action && root->name == "host-apdu-service")
2137 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2138
2139 for (auto& child : root->GetChildElements()) {
2140 if (child->name == "aid-group") {
2141 auto category = FindAttribute(child, CATEGORY_ATTR);
2142 if (category && category->value == "payment") {
2143 components.insert("payment");
2144 return;
2145 }
2146 }
2147 }
2148 }
2149 }
2150 }
2151 });
2152 }
2153 });
2154
2155 // Print the components types if they are present
2156 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2157 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002158 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002159 }
2160 };
2161
2162 PrintComponent("app-widget");
2163 PrintComponent("device-admin");
2164 PrintComponent("ime");
2165 PrintComponent("wallpaper");
2166 PrintComponent("accessibility");
2167 PrintComponent("print-service");
2168 PrintComponent("payment");
2169 PrintComponent("search");
2170 PrintComponent("document-provider");
2171 PrintComponent("launcher");
2172 PrintComponent("notification-listener");
2173 PrintComponent("dream");
2174 PrintComponent("camera");
2175 PrintComponent("camera-secure");
2176
2177 // Print presence of main activity
2178 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002179 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002180 }
2181
2182 // Print presence of activities, recivers, and services with no special components
2183 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2184 if (auto activity = ElementCast<Activity>(el)) {
2185 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002186 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002187 return true;
2188 }
2189 }
2190 return false;
2191 });
2192
2193 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2194 if (auto receiver = ElementCast<Receiver>(el)) {
2195 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002196 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002197 return true;
2198 }
2199 }
2200 return false;
2201 });
2202
2203 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2204 if (auto service = ElementCast<Service>(el)) {
2205 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002206 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002207 return true;
2208 }
2209 }
2210 return false;
2211 });
2212
2213 // Print the supported screens
2214 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2215 [&](ManifestExtractor::Element* el) -> bool {
2216 return ElementCast<SupportsScreen>(el) != nullptr;
2217 }));
2218
2219 if (screen) {
2220 screen->PrintScreens(printer, target_sdk_);
2221 } else {
2222 // Print the default supported screens
2223 SupportsScreen default_screens;
2224 default_screens.PrintScreens(printer, target_sdk_);
2225 }
2226
2227 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002228 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002229 for (auto& config : locales_) {
2230 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002231 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002232 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002233 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002234 }
2235 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002236 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002237
2238 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002239 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002240 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002241 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002242 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002243 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002244
2245 // Print the supported architectures of the app
2246 std::set<std::string> architectures;
2247 auto it = apk_->GetFileCollection()->Iterator();
2248 while (it->HasNext()) {
2249 auto file_path = it->Next()->GetSource().path;
2250
2251
2252 size_t pos = file_path.find("lib/");
2253 if (pos != std::string::npos) {
2254 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002255 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002256 if (pos != std::string::npos) {
2257 file_path = file_path.substr(0, pos);
2258 }
2259
2260 architectures.insert(file_path);
2261 }
2262 }
2263
2264 // Determine if the application has multiArch supports
2265 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2266 if (auto application = ElementCast<Application>(el)) {
2267 return application->has_multi_arch;
2268 }
2269 return false;
2270 });
2271
2272 bool output_alt_native_code = false;
2273 // A multiArch package is one that contains 64-bit and
2274 // 32-bit versions of native code and expects 3rd-party
2275 // apps to load these native code libraries. Since most
2276 // 64-bit systems also support 32-bit apps, the apps
2277 // loading this multiArch package's code may be either
2278 if (has_multi_arch) {
2279 // If this is a multiArch package, report the 64-bit
2280 // version only. Then as a separate entry, report the
2281 // rest.
2282 //
2283 // If we report the 32-bit architecture, this APK will
2284 // be installed on a 32-bit device, causing a large waste
2285 // of bandwidth and disk space. This assumes that
2286 // the developer of the multiArch package has also
2287 // made a version that is 32-bit only.
2288 const std::string kIntel64 = "x86_64";
2289 const std::string kArm64 = "arm64-v8a";
2290
2291 auto arch = architectures.find(kIntel64);
2292 if (arch == architectures.end()) {
2293 arch = architectures.find(kArm64);
2294 }
2295
2296 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002297 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002298 architectures.erase(arch);
2299 output_alt_native_code = true;
2300 }
2301 }
2302
2303 if (architectures.size() > 0) {
2304 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002305 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002306 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002307 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002308 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002309 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002310 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002311 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002312 }
2313
2314 return true;
2315}
2316
2317/**
2318 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2319 * pointer.
2320 **/
2321template<typename T>
2322T* ElementCast(ManifestExtractor::Element* element) {
2323 if (element == nullptr) {
2324 return nullptr;
2325 }
2326
2327 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002328 {"action", std::is_base_of<Action, T>::value},
2329 {"activity", std::is_base_of<Activity, T>::value},
2330 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2331 {"application", std::is_base_of<Application, T>::value},
2332 {"category", std::is_base_of<Category, T>::value},
2333 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2334 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2335 {"input-type", std::is_base_of<InputType, T>::value},
2336 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2337 {"meta-data", std::is_base_of<MetaData, T>::value},
2338 {"manifest", std::is_base_of<Manifest, T>::value},
2339 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2340 {"overlay", std::is_base_of<Overlay, T>::value},
2341 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2342 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002343 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002344 {"provider", std::is_base_of<Provider, T>::value},
2345 {"receiver", std::is_base_of<Receiver, T>::value},
2346 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2347 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2348 {"screen", std::is_base_of<Screen, T>::value},
2349 {"service", std::is_base_of<Service, T>::value},
2350 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2351 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2352 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2353 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2354 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2355 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2356 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2357 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2358 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2359 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2360 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2361 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2362 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002363 };
2364
2365 auto check = kTagCheck.find(element->tag());
2366 if (check != kTagCheck.end() && check->second) {
2367 return static_cast<T*>(element);
2368 }
2369 return nullptr;
2370}
2371
2372template<typename T>
2373std::unique_ptr<T> CreateType() {
2374 return std::move(util::make_unique<T>());
2375}
2376
2377std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2378 ManifestExtractor* extractor, xml::Element* el) {
2379 const std::unordered_map<std::string,
2380 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2381 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002382 {"action", &CreateType<Action>},
2383 {"activity", &CreateType<Activity>},
2384 {"additional-certificate", &CreateType<AdditionalCertificate>},
2385 {"application", &CreateType<Application>},
2386 {"category", &CreateType<Category>},
2387 {"compatible-screens", &CreateType<CompatibleScreens>},
2388 {"feature-group", &CreateType<FeatureGroup>},
2389 {"input-type", &CreateType<InputType>},
2390 {"intent-filter", &CreateType<IntentFilter>},
2391 {"manifest", &CreateType<Manifest>},
2392 {"meta-data", &CreateType<MetaData>},
2393 {"original-package", &CreateType<OriginalPackage>},
2394 {"overlay", &CreateType<Overlay>},
2395 {"package-verifier", &CreateType<PackageVerifier>},
2396 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002397 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002398 {"provider", &CreateType<Provider>},
2399 {"receiver", &CreateType<Receiver>},
2400 {"required-feature", &CreateType<RequiredFeature>},
2401 {"required-not-feature", &CreateType<RequiredNotFeature>},
2402 {"screen", &CreateType<Screen>},
2403 {"service", &CreateType<Service>},
2404 {"static-library", &CreateType<StaticLibrary>},
2405 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2406 {"supports-input", &CreateType<SupportsInput>},
2407 {"supports-screens", &CreateType<SupportsScreen>},
2408 {"uses-configuration", &CreateType<UsesConfiguarion>},
2409 {"uses-feature", &CreateType<UsesFeature>},
2410 {"uses-library", &CreateType<UsesLibrary>},
2411 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2412 {"uses-package", &CreateType<UsesPackage>},
2413 {"uses-permission", &CreateType<UsesPermission>},
2414 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2415 {"uses-sdk", &CreateType<UsesSdkBadging>},
2416 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2417 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002418
2419 // Attempt to map the xml tag to a element inflater
2420 std::unique_ptr<ManifestExtractor::Element> element;
2421 auto check = kTagCheck.find(el->name);
2422 if (check != kTagCheck.end()) {
2423 element = check->second();
2424 } else {
2425 element = util::make_unique<ManifestExtractor::Element>();
2426 }
2427
2428 element->extractor_ = extractor;
2429 element->tag_ = el->name;
2430 element->Extract(el);
2431 return element;
2432}
2433
2434std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2435 auto element = ManifestExtractor::Element::Inflate(this, el);
2436 parent_stack_.insert(parent_stack_.begin(), element.get());
2437
2438 // Process the element and recursively visit the children
2439 for (xml::Element* child : el->GetChildElements()) {
2440 auto v = Visit(child);
2441 element->AddChild(v);
2442 }
2443
2444 parent_stack_.erase(parent_stack_.begin());
2445 return element;
2446}
2447
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002448
Ryan Mitchell214846d2018-09-19 16:57:01 -07002449int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2450 IDiagnostics* diag) {
2451 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002452 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002453}
2454
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002455} // namespace aapt