blob: 61ba09b6a3c9a5e8a142986ee2fb07a4f0695c47 [file] [log] [blame]
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "DumpManifest.h"
18
Dianne Hackborn813d7502018-10-02 16:59:46 -070019#include <algorithm>
20
Ryan Mitchellfc225b22018-08-21 14:52:51 -070021#include "LoadedApk.h"
22#include "SdkConstants.h"
23#include "ValueVisitor.h"
24#include "io/File.h"
25#include "io/FileStream.h"
26#include "process/IResourceTableConsumer.h"
27#include "xml/XmlDom.h"
28
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020029#include "androidfw/ConfigDescription.h"
30
Ryan Mitchellfc225b22018-08-21 14:52:51 -070031using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070033
34namespace aapt {
35
36/**
37 * These are attribute resource constants for the platform, as found in android.R.attr.
38 */
39enum {
40 LABEL_ATTR = 0x01010001,
41 ICON_ATTR = 0x01010002,
42 NAME_ATTR = 0x01010003,
43 PERMISSION_ATTR = 0x01010006,
44 EXPORTED_ATTR = 0x01010010,
45 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000046 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070047 RESOURCE_ATTR = 0x01010025,
48 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000049 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070050 VALUE_ATTR = 0x01010024,
51 VERSION_CODE_ATTR = 0x0101021b,
52 VERSION_NAME_ATTR = 0x0101021c,
53 SCREEN_ORIENTATION_ATTR = 0x0101001e,
54 MIN_SDK_VERSION_ATTR = 0x0101020c,
55 MAX_SDK_VERSION_ATTR = 0x01010271,
56 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
57 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
58 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
59 REQ_NAVIGATION_ATTR = 0x0101022a,
60 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
61 TARGET_SDK_VERSION_ATTR = 0x01010270,
62 TEST_ONLY_ATTR = 0x01010272,
63 ANY_DENSITY_ATTR = 0x0101026c,
64 GL_ES_VERSION_ATTR = 0x01010281,
65 SMALL_SCREEN_ATTR = 0x01010284,
66 NORMAL_SCREEN_ATTR = 0x01010285,
67 LARGE_SCREEN_ATTR = 0x01010286,
68 XLARGE_SCREEN_ATTR = 0x010102bf,
69 REQUIRED_ATTR = 0x0101028e,
70 INSTALL_LOCATION_ATTR = 0x010102b7,
71 SCREEN_SIZE_ATTR = 0x010102ca,
72 SCREEN_DENSITY_ATTR = 0x010102cb,
73 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
74 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
75 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
76 PUBLIC_KEY_ATTR = 0x010103a6,
77 CATEGORY_ATTR = 0x010103e8,
78 BANNER_ATTR = 0x10103f2,
79 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070080 VERSION_ATTR = 0x01010519,
81 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000082 REQUIRED_FEATURE_ATTR = 0x01010554,
83 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000084 IS_STATIC_ATTR = 0x0101055a,
85 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
86 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070087 COMPILE_SDK_VERSION_ATTR = 0x01010572,
88 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070089 VERSION_MAJOR_ATTR = 0x01010577,
90 PACKAGE_TYPE_ATTR = 0x01010587,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070091};
92
93const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Ryan Mitchell95f02422020-04-30 10:25:53 -070094constexpr int kCurrentDevelopmentVersion = 10000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070095
96/** Retrieves the attribute of the element with the specified attribute resource id. */
97static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
98 for (auto& a : el->attributes) {
99 if (a.compiled_attribute && a.compiled_attribute.value().id) {
100 if (a.compiled_attribute.value().id.value() == resd_id) {
101 return std::move(&a);
102 }
103 }
104 }
105 return nullptr;
106}
107
108/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
109static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
110 const std::string &name) {
111 return el->FindAttribute(package, name);
112}
113
114class CommonFeatureGroup;
115
116class ManifestExtractor {
117 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700118
Ryan Mitchell214846d2018-09-19 16:57:01 -0700119 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700120 : apk_(apk), options_(options) { }
121
122 class Element {
123 public:
124 Element() = default;
125 virtual ~Element() = default;
126
127 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
128
129 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700130 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700131
132 /** Adds an element to the list of children of the element. */
133 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
134
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700135 template <typename Predicate>
136 void Filter(Predicate&& func) {
137 children_.erase(std::remove_if(children_.begin(), children_.end(),
138 [&](const auto& e) { return func(e.get()); }));
139 }
140
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700141 /** Retrieves the list of children of the element. */
142 const std::vector<std::unique_ptr<Element>>& children() const {
143 return children_;
144 }
145
146 /** Retrieves the extracted xml element tag. */
147 const std::string tag() const {
148 return tag_;
149 }
150
151 protected:
152 ManifestExtractor* extractor() const {
153 return extractor_;
154 }
155
156 /** Retrieves and stores the information extracted from the xml element. */
157 virtual void Extract(xml::Element* el) { }
158
159 /*
160 * Retrieves a configuration value of the resource entry that best matches the specified
161 * configuration.
162 */
163 static Value* BestConfigValue(ResourceEntry* entry,
164 const ConfigDescription& match) {
165 if (!entry) {
166 return nullptr;
167 }
168
169 // Determine the config that best matches the desired config
170 ResourceConfigValue* best_value = nullptr;
171 for (auto& value : entry->values) {
172 if (!value->config.match(match)) {
173 continue;
174 }
175
176 if (best_value != nullptr) {
177 if (!value->config.isBetterThan(best_value->config, &match)) {
178 if (value->config.compare(best_value->config) != 0) {
179 continue;
180 }
181 }
182 }
183
184 best_value = value.get();
185 }
186
187 // The entry has no values
188 if (!best_value) {
189 return nullptr;
190 }
191
192 return best_value->value.get();
193 }
194
195 /** Retrieves the resource assigned to the specified resource id if one exists. */
196 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700197 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700198 if (table) {
199 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700200 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700201 for (auto& entry : type->entries) {
202 if (entry->id && entry->id.value() == res_id.id) {
203 if (auto value = BestConfigValue(entry.get(), config)) {
204 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700205 }
206 }
207 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700208 }
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;
1072
1073 void Extract(xml::Element* element) override {
1074 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001075 std::string feature =
1076 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1077 if (!feature.empty()) {
1078 requiredFeatures.push_back(feature);
1079 }
1080 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1081 if (!feature.empty()) {
1082 requiredNotFeatures.push_back(feature);
1083 }
1084
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001085 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1086 maxSdkVersion = GetAttributeIntegerDefault(
1087 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1088
1089 if (!name.empty()) {
1090 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1091 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1092 }
1093 }
1094
Ryan Mitchell214846d2018-09-19 16:57:01 -07001095 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001096 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001097 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001098 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001099 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001100 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001101 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001102 for (const std::string& requiredFeature : requiredFeatures) {
1103 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1104 }
1105 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1106 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1107 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001108 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001109 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001110 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001111 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001112 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001113 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001114 }
1115 }
1116 }
1117
Ryan Mitchell214846d2018-09-19 16:57:01 -07001118 void PrintImplied(text::Printer* printer, const std::string& reason) {
1119 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001120 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001121 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001122 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001123 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001124 }
1125};
1126
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001127/** Represents <required-feature> elements. **/
1128class RequiredFeature : public ManifestExtractor::Element {
1129 public:
1130 RequiredFeature() = default;
1131 std::string name;
1132
1133 void Extract(xml::Element* element) override {
1134 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1135 auto parent_stack = extractor()->parent_stack();
1136 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1137 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1138 uses_permission->requiredFeatures.push_back(name);
1139 }
1140 }
1141};
1142
1143/** Represents <required-not-feature> elements. **/
1144class RequiredNotFeature : public ManifestExtractor::Element {
1145 public:
1146 RequiredNotFeature() = default;
1147 std::string name;
1148
1149 void Extract(xml::Element* element) override {
1150 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1151 auto parent_stack = extractor()->parent_stack();
1152 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1153 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1154 uses_permission->requiredNotFeatures.push_back(name);
1155 }
1156 }
1157};
1158
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001159/** Represents <uses-permission-sdk-23> elements. **/
1160class UsesPermissionSdk23 : public ManifestExtractor::Element {
1161 public:
1162 UsesPermissionSdk23() = default;
1163 const std::string* name = nullptr;
1164 const int32_t* maxSdkVersion = nullptr;
1165
1166 void Extract(xml::Element* element) override {
1167 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1168 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1169
1170 if (name) {
1171 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1172 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1173 }
1174 }
1175
Ryan Mitchell214846d2018-09-19 16:57:01 -07001176 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001177 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001178 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001179 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001180 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001181 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001182 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001183 }
1184 }
1185};
1186
1187/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1188class Permission : public ManifestExtractor::Element {
1189 public:
1190 Permission() = default;
1191 std::string name;
1192
1193 void Extract(xml::Element* element) override {
1194 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1195 }
1196
Ryan Mitchell214846d2018-09-19 16:57:01 -07001197 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001198 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001199 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001200 }
1201 }
1202};
1203
1204/** Represents <activity> elements. **/
1205class Activity : public ManifestExtractor::Element {
1206 public:
1207 Activity() = default;
1208 std::string name;
1209 std::string icon;
1210 std::string label;
1211 std::string banner;
1212
1213 bool has_component_ = false;
1214 bool has_launcher_category = false;
1215 bool has_leanback_launcher_category = false;
1216 bool has_main_action = false;
1217
1218 void Extract(xml::Element* element) override {
1219 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1220 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1221 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1222 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1223
1224 // Retrieve the package name from the manifest
1225 std::string package;
1226 for (auto& parent : extractor()->parent_stack()) {
1227 if (auto manifest = ElementCast<Manifest>(parent)) {
1228 package = manifest->package;
1229 break;
1230 }
1231 }
1232
1233 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001234 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001235 if (idx == 0) {
1236 name = package + name;
1237 } else if (idx < 0) {
1238 name = package + "." + name;
1239 }
1240
1241 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1242 if (orientation) {
1243 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1244 int orien = *orientation;
1245 if (orien == 0 || orien == 6 || orien == 8) {
1246 // Requests landscape, sensorLandscape, or reverseLandscape.
1247 common->addImpliedFeature("android.hardware.screen.landscape",
1248 "one or more activities have specified a landscape orientation",
1249 false);
1250 } else if (orien == 1 || orien == 7 || orien == 9) {
1251 // Requests portrait, sensorPortrait, or reversePortrait.
1252 common->addImpliedFeature("android.hardware.screen.portrait",
1253 "one or more activities have specified a portrait orientation",
1254 false);
1255 }
1256 }
1257 }
1258
Ryan Mitchell214846d2018-09-19 16:57:01 -07001259 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001260 // Print whether the activity has the HOME category and a the MAIN action
1261 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001262 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001263 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001264 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001265 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001266 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001267 android::ResTable::normalizeForOutput(label.data()).c_str(),
1268 icon.data()));
1269 }
1270
1271 // Print wether the activity has the HOME category and a the MAIN action
1272 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001273 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001274 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001275 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001276 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001277 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001278 android::ResTable::normalizeForOutput(label.data()).c_str(),
1279 icon.data(), banner.data()));
1280 }
1281 }
1282};
1283
1284/** Represents <intent-filter> elements. */
1285class IntentFilter : public ManifestExtractor::Element {
1286 public:
1287 IntentFilter() = default;
1288};
1289
1290/** Represents <category> elements. */
1291class Category : public ManifestExtractor::Element {
1292 public:
1293 Category() = default;
1294 std::string component = "";
1295
1296 void Extract(xml::Element* element) override {
1297 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1298
1299 auto parent_stack = extractor()->parent_stack();
1300 if (category && ElementCast<IntentFilter>(parent_stack[0])
1301 && ElementCast<Activity>(parent_stack[1])) {
1302 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1303
1304 if (*category == "android.intent.category.LAUNCHER") {
1305 activity->has_launcher_category = true;
1306 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1307 activity->has_leanback_launcher_category = true;
1308 } else if (*category == "android.intent.category.HOME") {
1309 component = "launcher";
1310 }
1311 }
1312 }
1313};
1314
1315/**
1316 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1317 * elements nested within.
1318 **/
1319class Provider : public ManifestExtractor::Element {
1320 public:
1321 Provider() = default;
1322 bool has_required_saf_attributes = false;
1323
1324 void Extract(xml::Element* element) override {
1325 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1326 const int32_t* grant_uri_permissions = GetAttributeInteger(
1327 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1328 const std::string* permission = GetAttributeString(
1329 FindAttribute(element, PERMISSION_ATTR));
1330
1331 has_required_saf_attributes = ((exported && *exported != 0)
1332 && (grant_uri_permissions && *grant_uri_permissions != 0)
1333 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1334 }
1335};
1336
1337/** Represents <receiver> elements. **/
1338class Receiver : public ManifestExtractor::Element {
1339 public:
1340 Receiver() = default;
1341 const std::string* permission = nullptr;
1342 bool has_component = false;
1343
1344 void Extract(xml::Element* element) override {
1345 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1346 }
1347};
1348
1349/**Represents <service> elements. **/
1350class Service : public ManifestExtractor::Element {
1351 public:
1352 Service() = 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 <uses-library> elements. **/
1362class UsesLibrary : public ManifestExtractor::Element {
1363 public:
1364 UsesLibrary() = default;
1365 std::string name;
1366 int required;
1367
1368 void Extract(xml::Element* element) override {
1369 auto parent_stack = extractor()->parent_stack();
1370 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1371 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1372 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1373 }
1374 }
1375
Ryan Mitchell214846d2018-09-19 16:57:01 -07001376 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001377 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001378 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001379 (required == 0) ? "-not-required" : "", name.data()));
1380 }
1381 }
1382};
1383
Dianne Hackborn813d7502018-10-02 16:59:46 -07001384/** Represents <static-library> elements. **/
1385class StaticLibrary : public ManifestExtractor::Element {
1386 public:
1387 StaticLibrary() = default;
1388 std::string name;
1389 int version;
1390 int versionMajor;
1391
1392 void Extract(xml::Element* element) override {
1393 auto parent_stack = extractor()->parent_stack();
1394 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1395 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1396 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1397 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1398 }
1399 }
1400
Ryan Mitchell214846d2018-09-19 16:57:01 -07001401 void Print(text::Printer* printer) override {
1402 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001403 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1404 name.data(), version, versionMajor));
1405 }
1406};
1407
1408/** Represents <uses-static-library> elements. **/
1409class UsesStaticLibrary : public ManifestExtractor::Element {
1410 public:
1411 UsesStaticLibrary() = default;
1412 std::string name;
1413 int version;
1414 int versionMajor;
1415 std::vector<std::string> certDigests;
1416
1417 void Extract(xml::Element* element) override {
1418 auto parent_stack = extractor()->parent_stack();
1419 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1420 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1421 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1422 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1423 AddCertDigest(element);
1424 }
1425 }
1426
1427 void AddCertDigest(xml::Element* element) {
1428 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1429 // We allow ":" delimiters in the SHA declaration as this is the format
1430 // emitted by the certtool making it easy for developers to copy/paste.
1431 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1432 if (!digest.empty()) {
1433 certDigests.push_back(digest);
1434 }
1435 }
1436
Ryan Mitchell214846d2018-09-19 16:57:01 -07001437 void Print(text::Printer* printer) override {
1438 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001439 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1440 name.data(), version, versionMajor));
1441 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001442 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001443 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001444 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001445 }
1446};
1447
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001448/** Represents <uses-native-library> elements. **/
1449class UsesNativeLibrary : public ManifestExtractor::Element {
1450 public:
1451 UsesNativeLibrary() = default;
1452 std::string name;
1453 int required;
1454
1455 void Extract(xml::Element* element) override {
1456 auto parent_stack = extractor()->parent_stack();
1457 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1458 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1459 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1460 }
1461 }
1462
1463 void Print(text::Printer* printer) override {
1464 if (!name.empty()) {
1465 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1466 (required == 0) ? "-not-required" : "", name.data()));
1467 }
1468 }
1469};
1470
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001471/**
1472 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1473 * explicitly enable meta data printing.
1474 **/
1475class MetaData : public ManifestExtractor::Element {
1476 public:
1477 MetaData() = default;
1478 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001479 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001480 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001481 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001482 const int* resource_int;
1483
1484 void Extract(xml::Element* element) override {
1485 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001486 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001487 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001488 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001489 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1490 }
1491
Ryan Mitchell214846d2018-09-19 16:57:01 -07001492 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001493 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001494 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001495 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001496 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001497 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001498 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001499 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001500 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001501 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001502 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001503 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001504 }
1505 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001506 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001507 }
1508 }
1509};
1510
1511/**
1512 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1513 * service components.
1514 **/
1515class Action : public ManifestExtractor::Element {
1516 public:
1517 Action() = default;
1518 std::string component = "";
1519
1520 void Extract(xml::Element* element) override {
1521 auto parent_stack = extractor()->parent_stack();
1522 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1523
1524 if (ElementCast<IntentFilter>(parent_stack[0])) {
1525 if (ElementCast<Activity>(parent_stack[1])) {
1526 // Detects the presence of a particular type of activity.
1527 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1528 auto map = std::map<std::string, std::string>({
1529 { "android.intent.action.MAIN" , "main" },
1530 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1531 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1532 });
1533
1534 auto entry = map.find(action);
1535 if (entry != map.end()) {
1536 component = entry->second;
1537 activity->has_component_ = true;
1538 }
1539
1540 if (action == "android.intent.action.MAIN") {
1541 activity->has_main_action = true;
1542 }
1543
1544 } else if (ElementCast<Receiver>(parent_stack[1])) {
1545 // Detects the presence of a particular type of receiver. If the action requires a
1546 // permission, then the receiver element is checked for the permission.
1547 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1548 auto map = std::map<std::string, std::string>({
1549 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1550 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1551 });
1552
1553 auto permissions = std::map<std::string, std::string>({
1554 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1555 });
1556
1557 auto entry = map.find(action);
1558 auto permission = permissions.find(action);
1559 if (entry != map.end() && (permission == permissions.end()
1560 || (receiver->permission && permission->second == *receiver->permission))) {
1561 receiver->has_component = true;
1562 component = entry->second;
1563 }
1564
1565 } else if (ElementCast<Service>(parent_stack[1])) {
1566 // Detects the presence of a particular type of service. If the action requires a
1567 // permission, then the service element is checked for the permission.
1568 Service* service = ElementCast<Service>(parent_stack[1]);
1569 auto map = std::map<std::string, std::string>({
1570 { "android.view.InputMethod" , "ime" },
1571 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1572 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1573 { "android.printservice.PrintService" , "print-service" },
1574 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1575 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1576 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1577 { "android.service.dreams.DreamService" , "dream" },
1578 });
1579
1580 auto permissions = std::map<std::string, std::string>({
1581 { "android.accessibilityservice.AccessibilityService" ,
1582 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1583 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1584 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1585 "android.permission.BIND_NFC_SERVICE" },
1586 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1587 "android.permission.BIND_NFC_SERVICE" },
1588 { "android.service.notification.NotificationListenerService" ,
1589 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1590 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1591 });
1592
1593 auto entry = map.find(action);
1594 auto permission = permissions.find(action);
1595 if (entry != map.end() && (permission == permissions.end()
1596 || (service->permission && permission->second == *service->permission))) {
1597 service->has_component= true;
1598 component = entry->second;
1599 }
1600
1601 } else if (ElementCast<Provider>(parent_stack[1])) {
1602 // Detects the presence of a particular type of receiver. If the provider requires a
1603 // permission, then the provider element is checked for the permission.
1604 // Detect whether this action
1605 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1606 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1607 && provider->has_required_saf_attributes) {
1608 component = "document-provider";
1609 }
1610 }
1611 }
1612
1613 // Represents a searchable interface
1614 if (action == "android.intent.action.SEARCH") {
1615 component = "search";
1616 }
1617 }
1618};
1619
1620/**
1621 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1622 **/
1623class SupportsInput : public ManifestExtractor::Element {
1624 public:
1625 SupportsInput() = default;
1626 std::vector<std::string> inputs;
1627
Ryan Mitchell214846d2018-09-19 16:57:01 -07001628 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001629 const size_t size = inputs.size();
1630 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001631 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001632 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001633 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001634 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001635 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001636 }
1637 }
1638};
1639
1640/** Represents <input-type> elements. **/
1641class InputType : public ManifestExtractor::Element {
1642 public:
1643 InputType() = default;
1644 void Extract(xml::Element* element) override {
1645 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1646 auto parent_stack = extractor()->parent_stack();
1647
1648 // Add the input to the set of supported inputs
1649 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1650 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1651 supports->inputs.push_back(*name);
1652 }
1653 }
1654};
1655
1656/** Represents <original-package> elements. **/
1657class OriginalPackage : public ManifestExtractor::Element {
1658 public:
1659 OriginalPackage() = default;
1660 const std::string* name = nullptr;
1661
1662 void Extract(xml::Element* element) override {
1663 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1664 }
1665
Ryan Mitchell214846d2018-09-19 16:57:01 -07001666 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001667 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001668 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001669 }
1670 }
1671};
1672
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001673
1674/** Represents <overlay> elements. **/
1675class Overlay : public ManifestExtractor::Element {
1676 public:
1677 Overlay() = default;
1678 const std::string* target_package = nullptr;
1679 int priority;
1680 bool is_static;
1681 const std::string* required_property_name = nullptr;
1682 const std::string* required_property_value = nullptr;
1683
1684 void Extract(xml::Element* element) override {
1685 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1686 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1687 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1688 required_property_name = GetAttributeString(
1689 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1690 required_property_value = GetAttributeString(
1691 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1692 }
1693
1694 void Print(text::Printer* printer) override {
1695 printer->Print(StringPrintf("overlay:"));
1696 if (target_package) {
1697 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1698 }
1699 printer->Print(StringPrintf(" priority='%d'", priority));
1700 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1701 if (required_property_name) {
1702 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1703 }
1704 if (required_property_value) {
1705 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1706 }
1707 printer->Print("\n");
1708 }
1709};
1710
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001711/** * Represents <package-verifier> elements. **/
1712class PackageVerifier : public ManifestExtractor::Element {
1713 public:
1714 PackageVerifier() = default;
1715 const std::string* name = nullptr;
1716 const std::string* public_key = nullptr;
1717
1718 void Extract(xml::Element* element) override {
1719 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1720 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1721 }
1722
Ryan Mitchell214846d2018-09-19 16:57:01 -07001723 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001724 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001725 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001726 name->data(), public_key->data()));
1727 }
1728 }
1729};
1730
1731/** Represents <uses-package> elements. **/
1732class UsesPackage : public ManifestExtractor::Element {
1733 public:
1734 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001735 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001736 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001737 int version;
1738 int versionMajor;
1739 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001740
1741 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001742 auto parent_stack = extractor()->parent_stack();
1743 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1744 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1745 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1746 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1747 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1748 AddCertDigest(element);
1749 }
1750 }
1751
1752 void AddCertDigest(xml::Element* element) {
1753 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1754 // We allow ":" delimiters in the SHA declaration as this is the format
1755 // emitted by the certtool making it easy for developers to copy/paste.
1756 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1757 if (!digest.empty()) {
1758 certDigests.push_back(digest);
1759 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001760 }
1761
Ryan Mitchell214846d2018-09-19 16:57:01 -07001762 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001763 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001764 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001765 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001766 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1767 packageType->data(), name->data(), version, versionMajor));
1768 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001769 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001770 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001771 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001772 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001773 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001774 }
1775 }
1776 }
1777};
1778
1779/** Represents <additional-certificate> elements. **/
1780class AdditionalCertificate : public ManifestExtractor::Element {
1781 public:
1782 AdditionalCertificate() = default;
1783
1784 void Extract(xml::Element* element) override {
1785 auto parent_stack = extractor()->parent_stack();
1786 if (parent_stack.size() > 0) {
1787 if (ElementCast<UsesPackage>(parent_stack[0])) {
1788 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1789 uses->AddCertDigest(element);
1790 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1791 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1792 uses->AddCertDigest(element);
1793 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001794 }
1795 }
1796};
1797
1798/** Represents <screen> elements found in <compatible-screens> elements. */
1799class Screen : public ManifestExtractor::Element {
1800 public:
1801 Screen() = default;
1802 const int32_t* size = nullptr;
1803 const int32_t* density = nullptr;
1804
1805 void Extract(xml::Element* element) override {
1806 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1807 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1808 }
1809};
1810
1811/**
1812 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1813 * that each denote a supported screen size and screen density.
1814 **/
1815class CompatibleScreens : public ManifestExtractor::Element {
1816 public:
1817 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001818 void Print(text::Printer* printer) override {
1819 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001820
1821 bool first = true;
1822 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1823 if (auto screen = ElementCast<Screen>(el)) {
1824 if (first) {
1825 first = false;
1826 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001827 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001828 }
1829
1830 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001831 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001832 }
1833 }
1834 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001835 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001836 }
1837};
1838
1839/** Represents <supports-gl-texture> elements. **/
1840class SupportsGlTexture : public ManifestExtractor::Element {
1841 public:
1842 SupportsGlTexture() = default;
1843 const std::string* name = nullptr;
1844
1845 void Extract(xml::Element* element) override {
1846 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1847 }
1848
Ryan Mitchell214846d2018-09-19 16:57:01 -07001849 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001850 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001851 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001852 }
1853 }
1854};
1855
Todd Kennedyce3e1292020-10-29 17:14:24 -07001856/** Represents <property> elements. **/
1857class Property : public ManifestExtractor::Element {
1858 public:
1859 Property() = default;
1860 std::string name;
1861 std::string value;
1862 const int* value_int;
1863 std::string resource;
1864 const int* resource_int;
1865
1866 void Extract(xml::Element* element) override {
1867 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1868 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
1869 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
1870 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
1871 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1872 }
1873
1874 void Print(text::Printer* printer) override {
1875 printer->Print(StringPrintf("property: name='%s' ", name.data()));
1876 if (!value.empty()) {
1877 printer->Print(StringPrintf("value='%s' ", value.data()));
1878 } else if (value_int) {
1879 printer->Print(StringPrintf("value='%d' ", *value_int));
1880 } else {
1881 if (!resource.empty()) {
1882 printer->Print(StringPrintf("resource='%s' ", resource.data()));
1883 } else if (resource_int) {
1884 printer->Print(StringPrintf("resource='%d' ", *resource_int));
1885 }
1886 }
1887 printer->Print("\n");
1888 }
1889};
1890
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001891/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001892static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001893 el->Print(printer);
1894 for (auto &child : el->children()) {
1895 Print(child.get(), printer);
1896 }
1897}
1898
Ryan Mitchell214846d2018-09-19 16:57:01 -07001899bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001900 // Load the manifest
1901 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1902 if (doc == nullptr) {
1903 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1904 return false;
1905 }
1906
1907 xml::Element* element = doc->root.get();
1908 if (element->name != "manifest") {
1909 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1910 return false;
1911 }
1912
1913 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1914 // printing only permission elements is requested
1915 if (options_.only_permissions) {
1916 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1917 ManifestExtractor::Element::Inflate(this, element);
1918
1919 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1920 for (xml::Element* child : element->GetChildElements()) {
1921 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1922 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001923 // Inflate the element and its descendants
1924 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001925 manifest->AddChild(permission_element);
1926 }
1927 }
1928
Ryan Mitchell214846d2018-09-19 16:57:01 -07001929 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001930 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1931 el->Print(printer);
1932 });
1933
1934 return true;
1935 }
1936
1937 return false;
1938 }
1939
1940 // Collect information about the resource configurations
1941 if (apk_->GetResourceTable()) {
1942 for (auto &package : apk_->GetResourceTable()->packages) {
1943 for (auto &type : package->types) {
1944 for (auto &entry : type->entries) {
1945 for (auto &value : entry->values) {
1946 std::string locale_str = value->config.GetBcp47LanguageTag();
1947
1948 // Collect all the unique locales of the apk
1949 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001950 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001951 config.setBcp47Locale(locale_str.data());
1952 locales_.insert(std::make_pair(locale_str, config));
1953 }
1954
1955 // Collect all the unique density of the apk
1956 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1957 : value->config.density;
1958 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001959 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001960 config.density = density;
1961 densities_.insert(std::make_pair(density, config));
1962 }
1963 }
1964 }
1965 }
1966 }
1967 }
1968
1969 // Extract badging information
1970 auto root = Visit(element);
1971
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07001972 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
1973 // attribute values from the last defined tag.
1974 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
1975 for (const auto& child : root->children()) {
1976 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
1977 filtered_uses_sdk_tags.emplace_back(uses_sdk);
1978 }
1979 }
1980 filtered_uses_sdk_tags.pop_back();
1981
1982 root->Filter([&](const ManifestExtractor::Element* e) {
1983 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
1984 filtered_uses_sdk_tags.end();
1985 });
1986
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001987 // Print the elements in order seen
1988 Print(root.get(), printer);
1989
1990 /** Recursively checks the extracted elements for the specified permission. **/
1991 auto FindPermission = [&](ManifestExtractor::Element* root,
1992 const std::string& name) -> ManifestExtractor::Element* {
1993 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1994 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1995 return permission->name == name;
1996 }
1997 return false;
1998 });
1999 };
2000
2001 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
2002 int32_t max_sdk_version) -> void {
2003 auto permission = util::make_unique<UsesPermission>();
2004 permission->name = name;
2005 permission->maxSdkVersion = max_sdk_version;
2006 permission->Print(printer);
2007 permission->PrintImplied(printer, reason);
2008 };
2009
2010 // Implied permissions
2011 // Pre-1.6 implicitly granted permission compatibility logic
2012 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
2013 bool insert_write_external = false;
2014 auto write_external_permission = ElementCast<UsesPermission>(
2015 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
2016
2017 if (target_sdk() < 4) {
2018 if (!write_external_permission) {
2019 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
2020 insert_write_external = true;
2021 }
2022
2023 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
2024 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
2025 }
2026 }
2027
2028 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2029 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2030 // do this (regardless of target API version) because we can't have
2031 // an app with write permission but not read permission.
2032 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
2033 if (!read_external && (insert_write_external || write_external_permission)) {
2034 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
2035 "requested WRITE_EXTERNAL_STORAGE",
2036 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
2037 }
2038
2039 // Pre-JellyBean call log permission compatibility.
2040 if (target_sdk() < 16) {
2041 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
2042 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
2043 PrintPermission("android.permission.READ_CALL_LOG",
2044 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
2045 }
2046
2047 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
2048 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
2049 PrintPermission("android.permission.WRITE_CALL_LOG",
2050 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
2051 }
2052 }
2053
2054 // If the app hasn't declared the touchscreen as a feature requirement (either
2055 // directly or implied, required or not), then the faketouch feature is implied.
2056 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
2057 common_feature_group->addImpliedFeature("android.hardware.faketouch",
2058 "default feature for all apps", false);
2059 }
2060
2061 // Only print the common feature group if no feature group is defined
2062 std::vector<FeatureGroup*> feature_groups;
2063 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
2064 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2065 feature_groups.push_back(feature_group);
2066 }
2067 });
2068
2069 if (feature_groups.empty()) {
2070 common_feature_group->PrintGroup(printer);
2071 } else {
2072 // Merge the common feature group into the feature group
2073 for (auto& feature_group : feature_groups) {
2074 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
2075 common_feature_group->open_gles_version);
2076 feature_group->Merge(common_feature_group);
2077 feature_group->PrintGroup(printer);
2078 }
2079 };
2080
2081 // Collect the component types of the application
2082 std::set<std::string> components;
2083 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
2084 if (ElementCast<Action>(el)) {
2085 auto action = ElementCast<Action>(el);
2086 if (!action->component.empty()) {
2087 components.insert(action->component);
2088 return;
2089 }
2090 }
2091
2092 if (ElementCast<Category>(el)) {
2093 auto category = ElementCast<Category>(el);
2094 if (!category->component.empty()) {
2095 components.insert(category->component);
2096 return;
2097 }
2098 }
2099 });
2100
2101 // Check for the payment component
2102 auto apk = apk_;
2103 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2104 if (auto service = ElementCast<Service>(el)) {
2105 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2106 [&](ManifestExtractor::Element* el) -> bool {
2107 if (auto action = ElementCast<Action>(el)) {
2108 return (action->component == "host-apdu");
2109 }
2110 return false;
2111 }));
2112
2113 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2114 [&](ManifestExtractor::Element* el) -> bool {
2115 if (auto action = ElementCast<Action>(el)) {
2116 return (action->component == "offhost-apdu");
2117 }
2118 return false;
2119 }));
2120
2121 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2122 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2123 if (auto meta_data = ElementCast<MetaData>(el)) {
2124 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2125 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2126 && offhost_apdu_action)) {
2127
2128 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002129 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002130 return;
2131 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002132 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002133 if (!resource) {
2134 return;
2135 }
2136
2137 // Look for the payment category on an <aid-group> element
2138 auto& root = resource.get()->root;
2139 if ((host_apdu_action && root->name == "host-apdu-service")
2140 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2141
2142 for (auto& child : root->GetChildElements()) {
2143 if (child->name == "aid-group") {
2144 auto category = FindAttribute(child, CATEGORY_ATTR);
2145 if (category && category->value == "payment") {
2146 components.insert("payment");
2147 return;
2148 }
2149 }
2150 }
2151 }
2152 }
2153 }
2154 });
2155 }
2156 });
2157
2158 // Print the components types if they are present
2159 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2160 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002161 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002162 }
2163 };
2164
2165 PrintComponent("app-widget");
2166 PrintComponent("device-admin");
2167 PrintComponent("ime");
2168 PrintComponent("wallpaper");
2169 PrintComponent("accessibility");
2170 PrintComponent("print-service");
2171 PrintComponent("payment");
2172 PrintComponent("search");
2173 PrintComponent("document-provider");
2174 PrintComponent("launcher");
2175 PrintComponent("notification-listener");
2176 PrintComponent("dream");
2177 PrintComponent("camera");
2178 PrintComponent("camera-secure");
2179
2180 // Print presence of main activity
2181 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002182 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002183 }
2184
2185 // Print presence of activities, recivers, and services with no special components
2186 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2187 if (auto activity = ElementCast<Activity>(el)) {
2188 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002189 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002190 return true;
2191 }
2192 }
2193 return false;
2194 });
2195
2196 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2197 if (auto receiver = ElementCast<Receiver>(el)) {
2198 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002199 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002200 return true;
2201 }
2202 }
2203 return false;
2204 });
2205
2206 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2207 if (auto service = ElementCast<Service>(el)) {
2208 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002209 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002210 return true;
2211 }
2212 }
2213 return false;
2214 });
2215
2216 // Print the supported screens
2217 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2218 [&](ManifestExtractor::Element* el) -> bool {
2219 return ElementCast<SupportsScreen>(el) != nullptr;
2220 }));
2221
2222 if (screen) {
2223 screen->PrintScreens(printer, target_sdk_);
2224 } else {
2225 // Print the default supported screens
2226 SupportsScreen default_screens;
2227 default_screens.PrintScreens(printer, target_sdk_);
2228 }
2229
2230 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002231 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002232 for (auto& config : locales_) {
2233 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002234 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002235 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002236 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002237 }
2238 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002239 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002240
2241 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002242 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002243 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002244 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002245 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002246 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002247
2248 // Print the supported architectures of the app
2249 std::set<std::string> architectures;
2250 auto it = apk_->GetFileCollection()->Iterator();
2251 while (it->HasNext()) {
2252 auto file_path = it->Next()->GetSource().path;
2253
2254
2255 size_t pos = file_path.find("lib/");
2256 if (pos != std::string::npos) {
2257 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002258 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002259 if (pos != std::string::npos) {
2260 file_path = file_path.substr(0, pos);
2261 }
2262
2263 architectures.insert(file_path);
2264 }
2265 }
2266
2267 // Determine if the application has multiArch supports
2268 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2269 if (auto application = ElementCast<Application>(el)) {
2270 return application->has_multi_arch;
2271 }
2272 return false;
2273 });
2274
2275 bool output_alt_native_code = false;
2276 // A multiArch package is one that contains 64-bit and
2277 // 32-bit versions of native code and expects 3rd-party
2278 // apps to load these native code libraries. Since most
2279 // 64-bit systems also support 32-bit apps, the apps
2280 // loading this multiArch package's code may be either
2281 if (has_multi_arch) {
2282 // If this is a multiArch package, report the 64-bit
2283 // version only. Then as a separate entry, report the
2284 // rest.
2285 //
2286 // If we report the 32-bit architecture, this APK will
2287 // be installed on a 32-bit device, causing a large waste
2288 // of bandwidth and disk space. This assumes that
2289 // the developer of the multiArch package has also
2290 // made a version that is 32-bit only.
2291 const std::string kIntel64 = "x86_64";
2292 const std::string kArm64 = "arm64-v8a";
2293
2294 auto arch = architectures.find(kIntel64);
2295 if (arch == architectures.end()) {
2296 arch = architectures.find(kArm64);
2297 }
2298
2299 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002300 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002301 architectures.erase(arch);
2302 output_alt_native_code = true;
2303 }
2304 }
2305
2306 if (architectures.size() > 0) {
2307 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002308 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002309 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002310 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002311 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002312 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002313 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002314 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002315 }
2316
2317 return true;
2318}
2319
2320/**
2321 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2322 * pointer.
2323 **/
2324template<typename T>
2325T* ElementCast(ManifestExtractor::Element* element) {
2326 if (element == nullptr) {
2327 return nullptr;
2328 }
2329
2330 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002331 {"action", std::is_base_of<Action, T>::value},
2332 {"activity", std::is_base_of<Activity, T>::value},
2333 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2334 {"application", std::is_base_of<Application, T>::value},
2335 {"category", std::is_base_of<Category, T>::value},
2336 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2337 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2338 {"input-type", std::is_base_of<InputType, T>::value},
2339 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2340 {"meta-data", std::is_base_of<MetaData, T>::value},
2341 {"manifest", std::is_base_of<Manifest, T>::value},
2342 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2343 {"overlay", std::is_base_of<Overlay, T>::value},
2344 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2345 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002346 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002347 {"provider", std::is_base_of<Provider, T>::value},
2348 {"receiver", std::is_base_of<Receiver, T>::value},
2349 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2350 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2351 {"screen", std::is_base_of<Screen, T>::value},
2352 {"service", std::is_base_of<Service, T>::value},
2353 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2354 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2355 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2356 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2357 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2358 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2359 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2360 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2361 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2362 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2363 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2364 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2365 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002366 };
2367
2368 auto check = kTagCheck.find(element->tag());
2369 if (check != kTagCheck.end() && check->second) {
2370 return static_cast<T*>(element);
2371 }
2372 return nullptr;
2373}
2374
2375template<typename T>
2376std::unique_ptr<T> CreateType() {
2377 return std::move(util::make_unique<T>());
2378}
2379
2380std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2381 ManifestExtractor* extractor, xml::Element* el) {
2382 const std::unordered_map<std::string,
2383 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2384 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002385 {"action", &CreateType<Action>},
2386 {"activity", &CreateType<Activity>},
2387 {"additional-certificate", &CreateType<AdditionalCertificate>},
2388 {"application", &CreateType<Application>},
2389 {"category", &CreateType<Category>},
2390 {"compatible-screens", &CreateType<CompatibleScreens>},
2391 {"feature-group", &CreateType<FeatureGroup>},
2392 {"input-type", &CreateType<InputType>},
2393 {"intent-filter", &CreateType<IntentFilter>},
2394 {"manifest", &CreateType<Manifest>},
2395 {"meta-data", &CreateType<MetaData>},
2396 {"original-package", &CreateType<OriginalPackage>},
2397 {"overlay", &CreateType<Overlay>},
2398 {"package-verifier", &CreateType<PackageVerifier>},
2399 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002400 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002401 {"provider", &CreateType<Provider>},
2402 {"receiver", &CreateType<Receiver>},
2403 {"required-feature", &CreateType<RequiredFeature>},
2404 {"required-not-feature", &CreateType<RequiredNotFeature>},
2405 {"screen", &CreateType<Screen>},
2406 {"service", &CreateType<Service>},
2407 {"static-library", &CreateType<StaticLibrary>},
2408 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2409 {"supports-input", &CreateType<SupportsInput>},
2410 {"supports-screens", &CreateType<SupportsScreen>},
2411 {"uses-configuration", &CreateType<UsesConfiguarion>},
2412 {"uses-feature", &CreateType<UsesFeature>},
2413 {"uses-library", &CreateType<UsesLibrary>},
2414 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2415 {"uses-package", &CreateType<UsesPackage>},
2416 {"uses-permission", &CreateType<UsesPermission>},
2417 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2418 {"uses-sdk", &CreateType<UsesSdkBadging>},
2419 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2420 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002421
2422 // Attempt to map the xml tag to a element inflater
2423 std::unique_ptr<ManifestExtractor::Element> element;
2424 auto check = kTagCheck.find(el->name);
2425 if (check != kTagCheck.end()) {
2426 element = check->second();
2427 } else {
2428 element = util::make_unique<ManifestExtractor::Element>();
2429 }
2430
2431 element->extractor_ = extractor;
2432 element->tag_ = el->name;
2433 element->Extract(el);
2434 return element;
2435}
2436
2437std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2438 auto element = ManifestExtractor::Element::Inflate(this, el);
2439 parent_stack_.insert(parent_stack_.begin(), element.get());
2440
2441 // Process the element and recursively visit the children
2442 for (xml::Element* child : el->GetChildElements()) {
2443 auto v = Visit(child);
2444 element->AddChild(v);
2445 }
2446
2447 parent_stack_.erase(parent_stack_.begin());
2448 return element;
2449}
2450
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002451
Ryan Mitchell214846d2018-09-19 16:57:01 -07002452int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2453 IDiagnostics* diag) {
2454 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002455 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002456}
2457
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002458} // namespace aapt