blob: e7a82034c77a817d956b76281a4d6f4c783e51f8 [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,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000082 REQUIRED_FEATURE_ATTR = 0x01010557,
83 REQUIRED_NOT_FEATURE_ATTR = 0x01010558,
84 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";
94
95/** Retrieves the attribute of the element with the specified attribute resource id. */
96static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
97 for (auto& a : el->attributes) {
98 if (a.compiled_attribute && a.compiled_attribute.value().id) {
99 if (a.compiled_attribute.value().id.value() == resd_id) {
100 return std::move(&a);
101 }
102 }
103 }
104 return nullptr;
105}
106
107/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
108static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
109 const std::string &name) {
110 return el->FindAttribute(package, name);
111}
112
113class CommonFeatureGroup;
114
115class ManifestExtractor {
116 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700117
Ryan Mitchell214846d2018-09-19 16:57:01 -0700118 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700119 : apk_(apk), options_(options) { }
120
121 class Element {
122 public:
123 Element() = default;
124 virtual ~Element() = default;
125
126 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
127
128 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700129 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700130
131 /** Adds an element to the list of children of the element. */
132 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
133
134 /** Retrieves the list of children of the element. */
135 const std::vector<std::unique_ptr<Element>>& children() const {
136 return children_;
137 }
138
139 /** Retrieves the extracted xml element tag. */
140 const std::string tag() const {
141 return tag_;
142 }
143
144 protected:
145 ManifestExtractor* extractor() const {
146 return extractor_;
147 }
148
149 /** Retrieves and stores the information extracted from the xml element. */
150 virtual void Extract(xml::Element* el) { }
151
152 /*
153 * Retrieves a configuration value of the resource entry that best matches the specified
154 * configuration.
155 */
156 static Value* BestConfigValue(ResourceEntry* entry,
157 const ConfigDescription& match) {
158 if (!entry) {
159 return nullptr;
160 }
161
162 // Determine the config that best matches the desired config
163 ResourceConfigValue* best_value = nullptr;
164 for (auto& value : entry->values) {
165 if (!value->config.match(match)) {
166 continue;
167 }
168
169 if (best_value != nullptr) {
170 if (!value->config.isBetterThan(best_value->config, &match)) {
171 if (value->config.compare(best_value->config) != 0) {
172 continue;
173 }
174 }
175 }
176
177 best_value = value.get();
178 }
179
180 // The entry has no values
181 if (!best_value) {
182 return nullptr;
183 }
184
185 return best_value->value.get();
186 }
187
188 /** Retrieves the resource assigned to the specified resource id if one exists. */
189 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
190 const ConfigDescription& config = DummyConfig()) {
191 if (table) {
192 for (auto& package : table->packages) {
193 if (package->id && package->id.value() == res_id.package_id()) {
194 for (auto& type : package->types) {
195 if (type->id && type->id.value() == res_id.type_id()) {
196 for (auto& entry : type->entries) {
197 if (entry->id && entry->id.value() == res_id.entry_id()) {
198 if (auto value = BestConfigValue(entry.get(), config)) {
199 return value;
200 }
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 return nullptr;
209 }
210
211 /** Attempts to resolve the reference to a non-reference value. */
212 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DummyConfig()) {
213 const int kMaxIterations = 40;
214 int i = 0;
215 while (ref && ref->id && i++ < kMaxIterations) {
216 auto table = extractor_->apk_->GetResourceTable();
217 if (auto value = FindValueById(table, ref->id.value(), config)) {
218 if (ValueCast<Reference>(value)) {
219 ref = ValueCast<Reference>(value);
220 } else {
221 return value;
222 }
223 }
224 }
225 return nullptr;
226 }
227
228 /**
229 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
230 * this will attempt to resolve the reference to an integer value.
231 **/
232 int32_t* GetAttributeInteger(xml::Attribute* attr,
233 const ConfigDescription& config = DummyConfig()) {
234 if (attr != nullptr) {
235 if (attr->compiled_value) {
236 // Resolve references using the dummy configuration
237 Value* value = attr->compiled_value.get();
238 if (ValueCast<Reference>(value)) {
239 value = ResolveReference(ValueCast<Reference>(value), config);
240 } else {
241 value = attr->compiled_value.get();
242 }
243 // Retrieve the integer data if possible
244 if (value != nullptr) {
245 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
246 return (int32_t*) &intValue->value.data;
247 }
248 }
249 }
250 }
251 return nullptr;
252 }
253
254 /**
255 * A version of GetAttributeInteger that returns a default integer if the attribute does not
256 * exist or cannot be resolved to an integer value.
257 **/
258 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
259 const ConfigDescription& config = DummyConfig()) {
260 auto value = GetAttributeInteger(attr, config);
261 if (value) {
262 return *value;
263 }
264 return def;
265 }
266
267 /**
268 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
269 * this will attempt to resolve the reference to a string value.
270 **/
271 const std::string* GetAttributeString(xml::Attribute* attr,
272 const ConfigDescription& config = DummyConfig()) {
273 if (attr != nullptr) {
274 if (attr->compiled_value) {
275 // Resolve references using the dummy configuration
276 Value* value = attr->compiled_value.get();
277 if (ValueCast<Reference>(value)) {
278 value = ResolveReference(ValueCast<Reference>(value), config);
279 } else {
280 value = attr->compiled_value.get();
281 }
282
283 // Retrieve the string data of the value if possible
284 if (value != nullptr) {
285 if (String* intValue = ValueCast<String>(value)) {
286 return &(*intValue->value);
287 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
288 return &(*rawValue->value);
289 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
290 return &(*strValue->path);
291 }
292 }
293 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700294
295 if (!attr->value.empty()) {
296 return &attr->value;
297 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700298 }
299 return nullptr;
300 }
301
302 /**
303 * A version of GetAttributeString that returns a default string if the attribute does not
304 * exist or cannot be resolved to an string value.
305 **/
306 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
307 const ConfigDescription& config = DummyConfig()) {
308 auto value = GetAttributeString(attr, config);
309 if (value) {
310 return *value;
311 }
312 return def;
313 }
314
315 private:
316 ManifestExtractor* extractor_;
317 std::vector<std::unique_ptr<Element>> children_;
318 std::string tag_;
319 };
320
321 friend Element;
322
323 /** Creates a default configuration used to retrieve resources. */
324 static ConfigDescription DummyConfig() {
325 ConfigDescription config;
326 config.orientation = android::ResTable_config::ORIENTATION_PORT;
327 config.density = android::ResTable_config::DENSITY_MEDIUM;
328 config.sdkVersion = 10000; // Very high.
329 config.screenWidthDp = 320;
330 config.screenHeightDp = 480;
331 config.smallestScreenWidthDp = 320;
332 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
333 return config;
334 }
335
Ryan Mitchell214846d2018-09-19 16:57:01 -0700336 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700337
338 /** Recursively visit the xml element tree and return a processed badging element tree. */
339 std::unique_ptr<Element> Visit(xml::Element* element);
340
341 /** Raises the target sdk value if the min target is greater than the current target. */
342 void RaiseTargetSdk(int32_t min_target) {
343 if (min_target > target_sdk_) {
344 target_sdk_ = min_target;
345 }
346 }
347
348 /**
349 * Retrieves the default feature group that features are added into when <uses-feature>
350 * are not in a <feature-group> element.
351 **/
352 CommonFeatureGroup* GetCommonFeatureGroup() {
353 return commonFeatureGroup_.get();
354 }
355
356 /**
357 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
358 * used for that density setting.
359 **/
360 const std::map<uint16_t, ConfigDescription> densities() const {
361 return densities_;
362 }
363
364 /**
365 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
366 * would be used for that locale setting.
367 **/
368 const std::map<std::string, ConfigDescription> locales() const {
369 return locales_;
370 }
371
372 /** Retrieves the current stack of parent during data extraction. */
373 const std::vector<Element*> parent_stack() const {
374 return parent_stack_;
375 }
376
377 int32_t target_sdk() const {
378 return target_sdk_;
379 }
380
381 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700382 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700383
384 private:
385 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
386 std::map<std::string, ConfigDescription> locales_;
387 std::map<uint16_t, ConfigDescription> densities_;
388 std::vector<Element*> parent_stack_;
389 int32_t target_sdk_ = 0;
390};
391
392template<typename T> T* ElementCast(ManifestExtractor::Element* element);
393
394/** Recurs through the children of the specified root in depth-first order. */
395static void ForEachChild(ManifestExtractor::Element* root,
396 std::function<void(ManifestExtractor::Element*)> f) {
397 for (auto& child : root->children()) {
398 f(child.get());
399 ForEachChild(child.get(), f);
400 }
401}
402
403/**
404 * Checks the element and its recursive children for an element that makes the specified
405 * conditional function return true. Returns the first element that makes the conditional function
406 * return true.
407 **/
408static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
409 std::function<bool(ManifestExtractor::Element*)> f) {
410 if (f(root)) {
411 return root;
412 }
413 for (auto& child : root->children()) {
414 if (auto b2 = FindElement(child.get(), f)) {
415 return b2;
416 }
417 }
418 return nullptr;
419}
420
421/** Represents the <manifest> elements **/
422class Manifest : public ManifestExtractor::Element {
423 public:
424 Manifest() = default;
425 std::string package;
426 int32_t versionCode;
427 std::string versionName;
428 const std::string* split = nullptr;
429 const std::string* platformVersionName = nullptr;
430 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700431 const int32_t* platformVersionNameInt = nullptr;
432 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700433 const int32_t* compilesdkVersion = nullptr;
434 const std::string* compilesdkVersionCodename = nullptr;
435 const int32_t* installLocation = nullptr;
436
437 void Extract(xml::Element* manifest) override {
438 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
439 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
440 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
441 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
442
443 // Extract the platform build info
444 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
445 "platformBuildVersionName"));
446 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
447 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700448 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
449 "platformBuildVersionName"));
450 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
451 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700452
453 // Extract the compile sdk info
454 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
455 compilesdkVersionCodename = GetAttributeString(
456 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
457 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
458 }
459
Ryan Mitchell214846d2018-09-19 16:57:01 -0700460 void Print(text::Printer* printer) override {
461 printer->Print(StringPrintf("package: name='%s' ", package.data()));
462 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700463 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700464 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700465
466 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700467 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700468 }
469 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700470 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700471 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700472 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
473 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700474 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700475 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700476 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700477 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
478 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700479 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700480 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700481 }
482 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700483 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700484 compilesdkVersionCodename->data()));
485 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700486 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700487
488 if (installLocation) {
489 switch (*installLocation) {
490 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700491 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700492 break;
493 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700494 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700495 break;
496 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700497 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700498 break;
499 default:
500 break;
501 }
502 }
503 }
504};
505
506/** Represents <application> elements. **/
507class Application : public ManifestExtractor::Element {
508 public:
509 Application() = default;
510 std::string label;
511 std::string icon;
512 std::string banner;
513 int32_t is_game;
514 int32_t debuggable;
515 int32_t test_only;
516 bool has_multi_arch;
517
518 /** Mapping from locales to app names. */
519 std::map<std::string, std::string> locale_labels;
520
521 /** Mapping from densities to app icons. */
522 std::map<uint16_t, std::string> density_icons;
523
524 void Extract(xml::Element* element) override {
525 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
526 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
527 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
528 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
529 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
530 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
531
532 // We must search by name because the multiArch flag hasn't been API
533 // frozen yet.
534 has_multi_arch = (GetAttributeIntegerDefault(
535 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
536
537 // Retrieve the app names for every locale the app supports
538 auto attr = FindAttribute(element, LABEL_ATTR);
539 for (auto& config : extractor()->locales()) {
540 if (auto label = GetAttributeString(attr, config.second)) {
541 if (label) {
542 locale_labels.insert(std::make_pair(config.first, *label));
543 }
544 }
545 }
546
547 // Retrieve the icons for the densities the app supports
548 attr = FindAttribute(element, ICON_ATTR);
549 for (auto& config : extractor()->densities()) {
550 if (auto resource = GetAttributeString(attr, config.second)) {
551 if (resource) {
552 density_icons.insert(std::make_pair(config.first, *resource));
553 }
554 }
555 }
556 }
557
Ryan Mitchell214846d2018-09-19 16:57:01 -0700558 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700559 // Print the labels for every locale
560 for (auto p : locale_labels) {
561 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700562 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700563 android::ResTable::normalizeForOutput(p.second.data())
564 .c_str()));
565 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700566 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700567 android::ResTable::normalizeForOutput(p.second.data())
568 .c_str()));
569 }
570 }
571
572 // Print the icon paths for every density
573 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700574 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700575 }
576
577 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700578 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700579 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700580 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700581 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700582 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700583 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700584 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700585
586 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700587 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700588 }
589 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700590 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700591 }
592 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700593 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700594 }
595 }
596};
597
598/** Represents <uses-sdk> elements. **/
599class UsesSdkBadging : public ManifestExtractor::Element {
600 public:
601 UsesSdkBadging() = default;
602 const int32_t* min_sdk = nullptr;
603 const std::string* min_sdk_name = nullptr;
604 const int32_t* max_sdk = nullptr;
605 const int32_t* target_sdk = nullptr;
606 const std::string* target_sdk_name = nullptr;
607
608 void Extract(xml::Element* element) override {
609 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
610 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
611 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
612 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
613 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
614
615 // Detect the target sdk of the element
616 if ((min_sdk_name && *min_sdk_name == "Donut")
617 || (target_sdk_name && *target_sdk_name == "Donut")) {
618 extractor()->RaiseTargetSdk(4);
619 }
620 if (min_sdk) {
621 extractor()->RaiseTargetSdk(*min_sdk);
622 }
623 if (target_sdk) {
624 extractor()->RaiseTargetSdk(*target_sdk);
625 }
626 }
627
Ryan Mitchell214846d2018-09-19 16:57:01 -0700628 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700629 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700630 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700631 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700632 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700633 }
634 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700636 }
637 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700638 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700639 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700640 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700641 }
642 }
643};
644
645/** Represents <uses-configuration> elements. **/
646class UsesConfiguarion : public ManifestExtractor::Element {
647 public:
648 UsesConfiguarion() = default;
649 int32_t req_touch_screen = 0;
650 int32_t req_keyboard_type = 0;
651 int32_t req_hard_keyboard = 0;
652 int32_t req_navigation = 0;
653 int32_t req_five_way_nav = 0;
654
655 void Extract(xml::Element* element) override {
656 req_touch_screen = GetAttributeIntegerDefault(
657 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
658 req_keyboard_type = GetAttributeIntegerDefault(
659 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
660 req_hard_keyboard = GetAttributeIntegerDefault(
661 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
662 req_navigation = GetAttributeIntegerDefault(
663 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
664 req_five_way_nav = GetAttributeIntegerDefault(
665 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
666 }
667
Ryan Mitchell214846d2018-09-19 16:57:01 -0700668 void Print(text::Printer* printer) override {
669 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700670 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700671 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700672 }
673 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700674 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700675 }
676 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700677 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700678 }
679 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700680 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700681 }
682 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700683 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700684 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700685 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700686 }
687};
688
689/** Represents <supports-screen> elements. **/
690class SupportsScreen : public ManifestExtractor::Element {
691 public:
692 SupportsScreen() = default;
693 int32_t small_screen = 1;
694 int32_t normal_screen = 1;
695 int32_t large_screen = 1;
696 int32_t xlarge_screen = 1;
697 int32_t any_density = 1;
698 int32_t requires_smallest_width_dp = 0;
699 int32_t compatible_width_limit_dp = 0;
700 int32_t largest_width_limit_dp = 0;
701
702 void Extract(xml::Element* element) override {
703 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
704 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
705 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
706 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
707 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
708
709 requires_smallest_width_dp = GetAttributeIntegerDefault(
710 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
711 compatible_width_limit_dp = GetAttributeIntegerDefault(
712 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
713 largest_width_limit_dp = GetAttributeIntegerDefault(
714 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
715
716 // For modern apps, if screen size buckets haven't been specified
717 // but the new width ranges have, then infer the buckets from them.
718 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
719 && requires_smallest_width_dp > 0) {
720 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
721 : requires_smallest_width_dp;
722 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
723 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
724 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
725 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
726 }
727 }
728
Ryan Mitchell214846d2018-09-19 16:57:01 -0700729 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700730 int32_t small_screen_temp = small_screen;
731 int32_t normal_screen_temp = normal_screen;
732 int32_t large_screen_temp = large_screen;
733 int32_t xlarge_screen_temp = xlarge_screen;
734 int32_t any_density_temp = any_density;
735
736 // Determine default values for any unspecified screen sizes,
737 // based on the target SDK of the package. As of 4 (donut)
738 // the screen size support was introduced, so all default to
739 // enabled.
740 if (small_screen_temp > 0) {
741 small_screen_temp = target_sdk >= 4 ? -1 : 0;
742 }
743 if (normal_screen_temp > 0) {
744 normal_screen_temp = -1;
745 }
746 if (large_screen_temp > 0) {
747 large_screen_temp = target_sdk >= 4 ? -1 : 0;
748 }
749 if (xlarge_screen_temp > 0) {
750 // Introduced in Gingerbread.
751 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
752 }
753 if (any_density_temp > 0) {
754 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
755 || compatible_width_limit_dp > 0) ? -1 : 0;
756 }
757
758 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700759 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700760 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700761 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700762 }
763 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700764 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700765 }
766 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700767 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700768 }
769 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700770 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700771 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700772 printer->Print("\n");
773 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700774 (any_density_temp ) ? "true" : "false"));
775 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700776 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700777 }
778 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700779 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700780 }
781 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700782 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700783 }
784 }
785};
786
787/** Represents <feature-group> elements. **/
788class FeatureGroup : public ManifestExtractor::Element {
789 public:
790 FeatureGroup() = default;
791 std::string label;
792 int32_t open_gles_version = 0;
793
794 void Extract(xml::Element* element) override {
795 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
796 }
797
Ryan Mitchell214846d2018-09-19 16:57:01 -0700798 virtual void PrintGroup(text::Printer* printer) {
799 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700800 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700801 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700802 }
803
804 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700805 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700806 (feature.second.required ? "" : "-not-required"),
807 feature.first.data()));
808 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700809 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700810 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700811 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700812 }
813 }
814
815 /** Adds a feature to the feature group. */
816 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
817 features_.insert(std::make_pair(name, Feature{ required, version }));
818 if (required) {
819 if (name == "android.hardware.camera.autofocus" ||
820 name == "android.hardware.camera.flash") {
821 AddFeature("android.hardware.camera", true);
822 } else if (name == "android.hardware.location.gps" ||
823 name == "android.hardware.location.network") {
824 AddFeature("android.hardware.location", true);
825 } else if (name == "android.hardware.faketouch.multitouch") {
826 AddFeature("android.hardware.faketouch", true);
827 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
828 name == "android.hardware.faketouch.multitouch.jazzhands") {
829 AddFeature("android.hardware.faketouch.multitouch", true);
830 AddFeature("android.hardware.faketouch", true);
831 } else if (name == "android.hardware.touchscreen.multitouch") {
832 AddFeature("android.hardware.touchscreen", true);
833 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
834 name == "android.hardware.touchscreen.multitouch.jazzhands") {
835 AddFeature("android.hardware.touchscreen.multitouch", true);
836 AddFeature("android.hardware.touchscreen", true);
837 } else if (name == "android.hardware.opengles.aep") {
838 const int kOpenGLESVersion31 = 0x00030001;
839 if (kOpenGLESVersion31 > open_gles_version) {
840 open_gles_version = kOpenGLESVersion31;
841 }
842 }
843 }
844 }
845
846 /** Returns true if the feature group has the given feature. */
847 virtual bool HasFeature(const std::string& name) {
848 return features_.find(name) != features_.end();
849 }
850
851 /** Merges the features of another feature group into this group. */
852 void Merge(FeatureGroup* group) {
853 open_gles_version = std::max(open_gles_version, group->open_gles_version);
854 for (auto& feature : group->features_) {
855 features_.insert(feature);
856 }
857 }
858
859 protected:
860 struct Feature {
861 public:
862 bool required = false;
863 int32_t version = -1;
864 };
865
866 /* Mapping of feature names to their properties. */
867 std::map<std::string, Feature> features_;
868};
869
870/**
871 * Represents the default feature group for the application if no <feature-group> elements are
872 * present in the manifest.
873 **/
874class CommonFeatureGroup : public FeatureGroup {
875 public:
876 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700877 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700878 FeatureGroup::PrintGroup(printer);
879
880 // Also print the implied features
881 for (auto feature : implied_features_) {
882 if (features_.find(feature.first) == features_.end()) {
883 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700884 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
885 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700886 feature.first.data()));
887
888 // Print the reasons as a sentence
889 size_t count = 0;
890 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700891 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700892 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700893 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700894 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700895 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700896 }
897 count++;
898 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700899 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700900 }
901 }
902 }
903
904 /** Returns true if the feature group has the given feature. */
905 bool HasFeature(const std::string& name) override {
906 return FeatureGroup::HasFeature(name)
907 || implied_features_.find(name) != implied_features_.end();
908 }
909
910 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
911 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
912 auto entry = implied_features_.find(name);
913 if (entry == implied_features_.end()) {
914 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
915 entry = implied_features_.find(name);
916 }
917
918 // A non-sdk 23 implied feature takes precedence.
919 if (entry->second.implied_from_sdk_k23 && !sdk23) {
920 entry->second.implied_from_sdk_k23 = false;
921 }
922
923 entry->second.reasons.insert(reason);
924 }
925
926 /**
927 * Adds a feature to a set of implied features for all features that are implied by the presence
928 * of the permission.
929 **/
930 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
931 if (name == "android.permission.CAMERA") {
932 addImpliedFeature("android.hardware.camera",
933 StringPrintf("requested %s permission", name.data()),
934 sdk23);
935
936 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
937 if (targetSdk < SDK_LOLLIPOP) {
938 addImpliedFeature("android.hardware.location.gps",
939 StringPrintf("requested %s permission", name.data()),
940 sdk23);
941 addImpliedFeature("android.hardware.location.gps",
942 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
943 sdk23);
944 }
945 addImpliedFeature("android.hardware.location",
946 StringPrintf("requested %s permission", name.data()),
947 sdk23);
948
949 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
950 if (targetSdk < SDK_LOLLIPOP) {
951 addImpliedFeature("android.hardware.location.network",
952 StringPrintf("requested %s permission", name.data()),
953 sdk23);
954 addImpliedFeature("android.hardware.location.network",
955 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
956 sdk23);
957 }
958 addImpliedFeature("android.hardware.location",
959 StringPrintf("requested %s permission", name.data()),
960 sdk23);
961
962 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
963 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
964 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
965 addImpliedFeature("android.hardware.location",
966 StringPrintf("requested %s permission", name.data()),
967 sdk23);
968
969 } else if (name == "android.permission.BLUETOOTH" ||
970 name == "android.permission.BLUETOOTH_ADMIN") {
971 if (targetSdk > SDK_DONUT) {
972 addImpliedFeature("android.hardware.bluetooth",
973 StringPrintf("requested %s permission", name.data()),
974 sdk23);
975 addImpliedFeature("android.hardware.bluetooth",
976 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
977 sdk23);
978 }
979
980 } else if (name == "android.permission.RECORD_AUDIO") {
981 addImpliedFeature("android.hardware.microphone",
982 StringPrintf("requested %s permission", name.data()),
983 sdk23);
984
985 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
986 name == "android.permission.CHANGE_WIFI_STATE" ||
987 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
988 addImpliedFeature("android.hardware.wifi",
989 StringPrintf("requested %s permission", name.data()),
990 sdk23);
991
992 } else if (name == "android.permission.CALL_PHONE" ||
993 name == "android.permission.CALL_PRIVILEGED" ||
994 name == "android.permission.MODIFY_PHONE_STATE" ||
995 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
996 name == "android.permission.READ_SMS" ||
997 name == "android.permission.RECEIVE_SMS" ||
998 name == "android.permission.RECEIVE_MMS" ||
999 name == "android.permission.RECEIVE_WAP_PUSH" ||
1000 name == "android.permission.SEND_SMS" ||
1001 name == "android.permission.WRITE_APN_SETTINGS" ||
1002 name == "android.permission.WRITE_SMS") {
1003 addImpliedFeature("android.hardware.telephony",
1004 "requested a telephony permission",
1005 sdk23);
1006 }
1007 }
1008
1009 private:
1010 /**
1011 * Represents a feature that has been automatically added due to a pre-requisite or for some
1012 * other reason.
1013 */
1014 struct ImpliedFeature {
1015 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1016
1017 /** List of human-readable reasons for why this feature was implied. */
1018 std::set<std::string> reasons;
1019
1020 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1021 bool implied_from_sdk_k23;
1022 };
1023
1024 /* Mapping of implied feature names to their properties. */
1025 std::map<std::string, ImpliedFeature> implied_features_;
1026};
1027
1028/** Represents <uses-feature> elements. **/
1029class UsesFeature : public ManifestExtractor::Element {
1030 public:
1031 UsesFeature() = default;
1032 void Extract(xml::Element* element) override {
1033 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1034 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1035 bool required = GetAttributeIntegerDefault(
1036 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1037 int32_t version = GetAttributeIntegerDefault(
1038 FindAttribute(element, kAndroidNamespace, "version"), 0);
1039
1040 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1041 // common feature group
1042 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1043 if (!feature_group) {
1044 feature_group = extractor()->GetCommonFeatureGroup();
1045 } else {
1046 // All features in side of <feature-group> elements are required.
1047 required = true;
1048 }
1049
1050 if (name) {
1051 feature_group->AddFeature(*name, required, version);
1052 } else if (gl) {
1053 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1054 }
1055 }
1056};
1057
1058/** Represents <uses-permission> elements. **/
1059class UsesPermission : public ManifestExtractor::Element {
1060 public:
1061 UsesPermission() = default;
1062 std::string name;
1063 std::string requiredFeature;
1064 std::string requiredNotFeature;
1065 int32_t required = true;
1066 int32_t maxSdkVersion = -1;
1067
1068 void Extract(xml::Element* element) override {
1069 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1070 requiredFeature = GetAttributeStringDefault(
1071 FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1072 requiredNotFeature = GetAttributeStringDefault(
1073 FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1074 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1075 maxSdkVersion = GetAttributeIntegerDefault(
1076 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1077
1078 if (!name.empty()) {
1079 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1080 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1081 }
1082 }
1083
Ryan Mitchell214846d2018-09-19 16:57:01 -07001084 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001085 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001086 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001087 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001088 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001089 }
1090 if (!requiredFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001091 printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001092 }
1093 if (!requiredNotFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001094 printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001095 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001096 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001097 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001098 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001099 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001100 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001101 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001102 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001103 }
1104 }
1105 }
1106
Ryan Mitchell214846d2018-09-19 16:57:01 -07001107 void PrintImplied(text::Printer* printer, const std::string& reason) {
1108 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001109 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001110 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001111 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001112 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001113 }
1114};
1115
1116/** Represents <uses-permission-sdk-23> elements. **/
1117class UsesPermissionSdk23 : public ManifestExtractor::Element {
1118 public:
1119 UsesPermissionSdk23() = default;
1120 const std::string* name = nullptr;
1121 const int32_t* maxSdkVersion = nullptr;
1122
1123 void Extract(xml::Element* element) override {
1124 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1125 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1126
1127 if (name) {
1128 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1129 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1130 }
1131 }
1132
Ryan Mitchell214846d2018-09-19 16:57:01 -07001133 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001134 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001135 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001136 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001137 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001138 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001139 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001140 }
1141 }
1142};
1143
1144/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1145class Permission : public ManifestExtractor::Element {
1146 public:
1147 Permission() = default;
1148 std::string name;
1149
1150 void Extract(xml::Element* element) override {
1151 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1152 }
1153
Ryan Mitchell214846d2018-09-19 16:57:01 -07001154 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001155 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001156 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001157 }
1158 }
1159};
1160
1161/** Represents <activity> elements. **/
1162class Activity : public ManifestExtractor::Element {
1163 public:
1164 Activity() = default;
1165 std::string name;
1166 std::string icon;
1167 std::string label;
1168 std::string banner;
1169
1170 bool has_component_ = false;
1171 bool has_launcher_category = false;
1172 bool has_leanback_launcher_category = false;
1173 bool has_main_action = false;
1174
1175 void Extract(xml::Element* element) override {
1176 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1177 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1178 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1179 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1180
1181 // Retrieve the package name from the manifest
1182 std::string package;
1183 for (auto& parent : extractor()->parent_stack()) {
1184 if (auto manifest = ElementCast<Manifest>(parent)) {
1185 package = manifest->package;
1186 break;
1187 }
1188 }
1189
1190 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001191 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001192 if (idx == 0) {
1193 name = package + name;
1194 } else if (idx < 0) {
1195 name = package + "." + name;
1196 }
1197
1198 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1199 if (orientation) {
1200 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1201 int orien = *orientation;
1202 if (orien == 0 || orien == 6 || orien == 8) {
1203 // Requests landscape, sensorLandscape, or reverseLandscape.
1204 common->addImpliedFeature("android.hardware.screen.landscape",
1205 "one or more activities have specified a landscape orientation",
1206 false);
1207 } else if (orien == 1 || orien == 7 || orien == 9) {
1208 // Requests portrait, sensorPortrait, or reversePortrait.
1209 common->addImpliedFeature("android.hardware.screen.portrait",
1210 "one or more activities have specified a portrait orientation",
1211 false);
1212 }
1213 }
1214 }
1215
Ryan Mitchell214846d2018-09-19 16:57:01 -07001216 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001217 // Print whether the activity has the HOME category and a the MAIN action
1218 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001219 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001220 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001221 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001222 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001223 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001224 android::ResTable::normalizeForOutput(label.data()).c_str(),
1225 icon.data()));
1226 }
1227
1228 // Print wether the activity has the HOME category and a the MAIN action
1229 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001230 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001231 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001232 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001233 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001234 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001235 android::ResTable::normalizeForOutput(label.data()).c_str(),
1236 icon.data(), banner.data()));
1237 }
1238 }
1239};
1240
1241/** Represents <intent-filter> elements. */
1242class IntentFilter : public ManifestExtractor::Element {
1243 public:
1244 IntentFilter() = default;
1245};
1246
1247/** Represents <category> elements. */
1248class Category : public ManifestExtractor::Element {
1249 public:
1250 Category() = default;
1251 std::string component = "";
1252
1253 void Extract(xml::Element* element) override {
1254 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1255
1256 auto parent_stack = extractor()->parent_stack();
1257 if (category && ElementCast<IntentFilter>(parent_stack[0])
1258 && ElementCast<Activity>(parent_stack[1])) {
1259 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1260
1261 if (*category == "android.intent.category.LAUNCHER") {
1262 activity->has_launcher_category = true;
1263 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1264 activity->has_leanback_launcher_category = true;
1265 } else if (*category == "android.intent.category.HOME") {
1266 component = "launcher";
1267 }
1268 }
1269 }
1270};
1271
1272/**
1273 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1274 * elements nested within.
1275 **/
1276class Provider : public ManifestExtractor::Element {
1277 public:
1278 Provider() = default;
1279 bool has_required_saf_attributes = false;
1280
1281 void Extract(xml::Element* element) override {
1282 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1283 const int32_t* grant_uri_permissions = GetAttributeInteger(
1284 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1285 const std::string* permission = GetAttributeString(
1286 FindAttribute(element, PERMISSION_ATTR));
1287
1288 has_required_saf_attributes = ((exported && *exported != 0)
1289 && (grant_uri_permissions && *grant_uri_permissions != 0)
1290 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1291 }
1292};
1293
1294/** Represents <receiver> elements. **/
1295class Receiver : public ManifestExtractor::Element {
1296 public:
1297 Receiver() = default;
1298 const std::string* permission = nullptr;
1299 bool has_component = false;
1300
1301 void Extract(xml::Element* element) override {
1302 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1303 }
1304};
1305
1306/**Represents <service> elements. **/
1307class Service : public ManifestExtractor::Element {
1308 public:
1309 Service() = default;
1310 const std::string* permission = nullptr;
1311 bool has_component = false;
1312
1313 void Extract(xml::Element* element) override {
1314 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1315 }
1316};
1317
1318/** Represents <uses-library> elements. **/
1319class UsesLibrary : public ManifestExtractor::Element {
1320 public:
1321 UsesLibrary() = default;
1322 std::string name;
1323 int required;
1324
1325 void Extract(xml::Element* element) override {
1326 auto parent_stack = extractor()->parent_stack();
1327 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1328 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1329 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1330 }
1331 }
1332
Ryan Mitchell214846d2018-09-19 16:57:01 -07001333 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001334 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001335 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001336 (required == 0) ? "-not-required" : "", name.data()));
1337 }
1338 }
1339};
1340
Dianne Hackborn813d7502018-10-02 16:59:46 -07001341/** Represents <static-library> elements. **/
1342class StaticLibrary : public ManifestExtractor::Element {
1343 public:
1344 StaticLibrary() = default;
1345 std::string name;
1346 int version;
1347 int versionMajor;
1348
1349 void Extract(xml::Element* element) override {
1350 auto parent_stack = extractor()->parent_stack();
1351 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1352 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1353 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1354 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1355 }
1356 }
1357
Ryan Mitchell214846d2018-09-19 16:57:01 -07001358 void Print(text::Printer* printer) override {
1359 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001360 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1361 name.data(), version, versionMajor));
1362 }
1363};
1364
1365/** Represents <uses-static-library> elements. **/
1366class UsesStaticLibrary : public ManifestExtractor::Element {
1367 public:
1368 UsesStaticLibrary() = default;
1369 std::string name;
1370 int version;
1371 int versionMajor;
1372 std::vector<std::string> certDigests;
1373
1374 void Extract(xml::Element* element) override {
1375 auto parent_stack = extractor()->parent_stack();
1376 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1377 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1378 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1379 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1380 AddCertDigest(element);
1381 }
1382 }
1383
1384 void AddCertDigest(xml::Element* element) {
1385 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1386 // We allow ":" delimiters in the SHA declaration as this is the format
1387 // emitted by the certtool making it easy for developers to copy/paste.
1388 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1389 if (!digest.empty()) {
1390 certDigests.push_back(digest);
1391 }
1392 }
1393
Ryan Mitchell214846d2018-09-19 16:57:01 -07001394 void Print(text::Printer* printer) override {
1395 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001396 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1397 name.data(), version, versionMajor));
1398 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001399 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001400 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001401 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001402 }
1403};
1404
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001405/**
1406 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1407 * explicitly enable meta data printing.
1408 **/
1409class MetaData : public ManifestExtractor::Element {
1410 public:
1411 MetaData() = default;
1412 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001413 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001414 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001415 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001416 const int* resource_int;
1417
1418 void Extract(xml::Element* element) override {
1419 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001420 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001421 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001422 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001423 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1424 }
1425
Ryan Mitchell214846d2018-09-19 16:57:01 -07001426 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001427 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001428 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001429 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001430 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001431 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001432 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001433 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001434 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001435 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001436 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001437 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001438 }
1439 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001440 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001441 }
1442 }
1443};
1444
1445/**
1446 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1447 * service components.
1448 **/
1449class Action : public ManifestExtractor::Element {
1450 public:
1451 Action() = default;
1452 std::string component = "";
1453
1454 void Extract(xml::Element* element) override {
1455 auto parent_stack = extractor()->parent_stack();
1456 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1457
1458 if (ElementCast<IntentFilter>(parent_stack[0])) {
1459 if (ElementCast<Activity>(parent_stack[1])) {
1460 // Detects the presence of a particular type of activity.
1461 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1462 auto map = std::map<std::string, std::string>({
1463 { "android.intent.action.MAIN" , "main" },
1464 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1465 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1466 });
1467
1468 auto entry = map.find(action);
1469 if (entry != map.end()) {
1470 component = entry->second;
1471 activity->has_component_ = true;
1472 }
1473
1474 if (action == "android.intent.action.MAIN") {
1475 activity->has_main_action = true;
1476 }
1477
1478 } else if (ElementCast<Receiver>(parent_stack[1])) {
1479 // Detects the presence of a particular type of receiver. If the action requires a
1480 // permission, then the receiver element is checked for the permission.
1481 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1482 auto map = std::map<std::string, std::string>({
1483 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1484 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1485 });
1486
1487 auto permissions = std::map<std::string, std::string>({
1488 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1489 });
1490
1491 auto entry = map.find(action);
1492 auto permission = permissions.find(action);
1493 if (entry != map.end() && (permission == permissions.end()
1494 || (receiver->permission && permission->second == *receiver->permission))) {
1495 receiver->has_component = true;
1496 component = entry->second;
1497 }
1498
1499 } else if (ElementCast<Service>(parent_stack[1])) {
1500 // Detects the presence of a particular type of service. If the action requires a
1501 // permission, then the service element is checked for the permission.
1502 Service* service = ElementCast<Service>(parent_stack[1]);
1503 auto map = std::map<std::string, std::string>({
1504 { "android.view.InputMethod" , "ime" },
1505 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1506 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1507 { "android.printservice.PrintService" , "print-service" },
1508 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1509 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1510 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1511 { "android.service.dreams.DreamService" , "dream" },
1512 });
1513
1514 auto permissions = std::map<std::string, std::string>({
1515 { "android.accessibilityservice.AccessibilityService" ,
1516 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1517 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1518 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1519 "android.permission.BIND_NFC_SERVICE" },
1520 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1521 "android.permission.BIND_NFC_SERVICE" },
1522 { "android.service.notification.NotificationListenerService" ,
1523 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1524 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1525 });
1526
1527 auto entry = map.find(action);
1528 auto permission = permissions.find(action);
1529 if (entry != map.end() && (permission == permissions.end()
1530 || (service->permission && permission->second == *service->permission))) {
1531 service->has_component= true;
1532 component = entry->second;
1533 }
1534
1535 } else if (ElementCast<Provider>(parent_stack[1])) {
1536 // Detects the presence of a particular type of receiver. If the provider requires a
1537 // permission, then the provider element is checked for the permission.
1538 // Detect whether this action
1539 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1540 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1541 && provider->has_required_saf_attributes) {
1542 component = "document-provider";
1543 }
1544 }
1545 }
1546
1547 // Represents a searchable interface
1548 if (action == "android.intent.action.SEARCH") {
1549 component = "search";
1550 }
1551 }
1552};
1553
1554/**
1555 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1556 **/
1557class SupportsInput : public ManifestExtractor::Element {
1558 public:
1559 SupportsInput() = default;
1560 std::vector<std::string> inputs;
1561
Ryan Mitchell214846d2018-09-19 16:57:01 -07001562 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001563 const size_t size = inputs.size();
1564 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001565 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001566 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001567 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001568 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001569 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001570 }
1571 }
1572};
1573
1574/** Represents <input-type> elements. **/
1575class InputType : public ManifestExtractor::Element {
1576 public:
1577 InputType() = default;
1578 void Extract(xml::Element* element) override {
1579 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1580 auto parent_stack = extractor()->parent_stack();
1581
1582 // Add the input to the set of supported inputs
1583 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1584 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1585 supports->inputs.push_back(*name);
1586 }
1587 }
1588};
1589
1590/** Represents <original-package> elements. **/
1591class OriginalPackage : public ManifestExtractor::Element {
1592 public:
1593 OriginalPackage() = default;
1594 const std::string* name = nullptr;
1595
1596 void Extract(xml::Element* element) override {
1597 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1598 }
1599
Ryan Mitchell214846d2018-09-19 16:57:01 -07001600 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001601 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001602 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001603 }
1604 }
1605};
1606
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001607
1608/** Represents <overlay> elements. **/
1609class Overlay : public ManifestExtractor::Element {
1610 public:
1611 Overlay() = default;
1612 const std::string* target_package = nullptr;
1613 int priority;
1614 bool is_static;
1615 const std::string* required_property_name = nullptr;
1616 const std::string* required_property_value = nullptr;
1617
1618 void Extract(xml::Element* element) override {
1619 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1620 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1621 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1622 required_property_name = GetAttributeString(
1623 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1624 required_property_value = GetAttributeString(
1625 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1626 }
1627
1628 void Print(text::Printer* printer) override {
1629 printer->Print(StringPrintf("overlay:"));
1630 if (target_package) {
1631 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1632 }
1633 printer->Print(StringPrintf(" priority='%d'", priority));
1634 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1635 if (required_property_name) {
1636 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1637 }
1638 if (required_property_value) {
1639 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1640 }
1641 printer->Print("\n");
1642 }
1643};
1644
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001645/** * Represents <package-verifier> elements. **/
1646class PackageVerifier : public ManifestExtractor::Element {
1647 public:
1648 PackageVerifier() = default;
1649 const std::string* name = nullptr;
1650 const std::string* public_key = nullptr;
1651
1652 void Extract(xml::Element* element) override {
1653 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1654 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1655 }
1656
Ryan Mitchell214846d2018-09-19 16:57:01 -07001657 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001658 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001659 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001660 name->data(), public_key->data()));
1661 }
1662 }
1663};
1664
1665/** Represents <uses-package> elements. **/
1666class UsesPackage : public ManifestExtractor::Element {
1667 public:
1668 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001669 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001670 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001671 int version;
1672 int versionMajor;
1673 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001674
1675 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001676 auto parent_stack = extractor()->parent_stack();
1677 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1678 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1679 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1680 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1681 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1682 AddCertDigest(element);
1683 }
1684 }
1685
1686 void AddCertDigest(xml::Element* element) {
1687 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1688 // We allow ":" delimiters in the SHA declaration as this is the format
1689 // emitted by the certtool making it easy for developers to copy/paste.
1690 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1691 if (!digest.empty()) {
1692 certDigests.push_back(digest);
1693 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001694 }
1695
Ryan Mitchell214846d2018-09-19 16:57:01 -07001696 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001697 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001698 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001699 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001700 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1701 packageType->data(), name->data(), version, versionMajor));
1702 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001703 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001704 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001705 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001706 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001707 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001708 }
1709 }
1710 }
1711};
1712
1713/** Represents <additional-certificate> elements. **/
1714class AdditionalCertificate : public ManifestExtractor::Element {
1715 public:
1716 AdditionalCertificate() = default;
1717
1718 void Extract(xml::Element* element) override {
1719 auto parent_stack = extractor()->parent_stack();
1720 if (parent_stack.size() > 0) {
1721 if (ElementCast<UsesPackage>(parent_stack[0])) {
1722 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1723 uses->AddCertDigest(element);
1724 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1725 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1726 uses->AddCertDigest(element);
1727 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001728 }
1729 }
1730};
1731
1732/** Represents <screen> elements found in <compatible-screens> elements. */
1733class Screen : public ManifestExtractor::Element {
1734 public:
1735 Screen() = default;
1736 const int32_t* size = nullptr;
1737 const int32_t* density = nullptr;
1738
1739 void Extract(xml::Element* element) override {
1740 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1741 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1742 }
1743};
1744
1745/**
1746 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1747 * that each denote a supported screen size and screen density.
1748 **/
1749class CompatibleScreens : public ManifestExtractor::Element {
1750 public:
1751 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001752 void Print(text::Printer* printer) override {
1753 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001754
1755 bool first = true;
1756 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1757 if (auto screen = ElementCast<Screen>(el)) {
1758 if (first) {
1759 first = false;
1760 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001761 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001762 }
1763
1764 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001765 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001766 }
1767 }
1768 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001769 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001770 }
1771};
1772
1773/** Represents <supports-gl-texture> elements. **/
1774class SupportsGlTexture : public ManifestExtractor::Element {
1775 public:
1776 SupportsGlTexture() = default;
1777 const std::string* name = nullptr;
1778
1779 void Extract(xml::Element* element) override {
1780 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1781 }
1782
Ryan Mitchell214846d2018-09-19 16:57:01 -07001783 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001784 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001785 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001786 }
1787 }
1788};
1789
1790/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001791static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001792 el->Print(printer);
1793 for (auto &child : el->children()) {
1794 Print(child.get(), printer);
1795 }
1796}
1797
Ryan Mitchell214846d2018-09-19 16:57:01 -07001798bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001799 // Load the manifest
1800 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1801 if (doc == nullptr) {
1802 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1803 return false;
1804 }
1805
1806 xml::Element* element = doc->root.get();
1807 if (element->name != "manifest") {
1808 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1809 return false;
1810 }
1811
1812 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1813 // printing only permission elements is requested
1814 if (options_.only_permissions) {
1815 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1816 ManifestExtractor::Element::Inflate(this, element);
1817
1818 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1819 for (xml::Element* child : element->GetChildElements()) {
1820 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1821 || child->name == "permission") {
1822 auto permission_element = ManifestExtractor::Element::Inflate(this, child);
1823 manifest->AddChild(permission_element);
1824 }
1825 }
1826
Ryan Mitchell214846d2018-09-19 16:57:01 -07001827 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001828 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1829 el->Print(printer);
1830 });
1831
1832 return true;
1833 }
1834
1835 return false;
1836 }
1837
1838 // Collect information about the resource configurations
1839 if (apk_->GetResourceTable()) {
1840 for (auto &package : apk_->GetResourceTable()->packages) {
1841 for (auto &type : package->types) {
1842 for (auto &entry : type->entries) {
1843 for (auto &value : entry->values) {
1844 std::string locale_str = value->config.GetBcp47LanguageTag();
1845
1846 // Collect all the unique locales of the apk
1847 if (locales_.find(locale_str) == locales_.end()) {
1848 ConfigDescription config = ManifestExtractor::DummyConfig();
1849 config.setBcp47Locale(locale_str.data());
1850 locales_.insert(std::make_pair(locale_str, config));
1851 }
1852
1853 // Collect all the unique density of the apk
1854 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1855 : value->config.density;
1856 if (densities_.find(density) == densities_.end()) {
1857 ConfigDescription config = ManifestExtractor::DummyConfig();
1858 config.density = density;
1859 densities_.insert(std::make_pair(density, config));
1860 }
1861 }
1862 }
1863 }
1864 }
1865 }
1866
1867 // Extract badging information
1868 auto root = Visit(element);
1869
1870 // Print the elements in order seen
1871 Print(root.get(), printer);
1872
1873 /** Recursively checks the extracted elements for the specified permission. **/
1874 auto FindPermission = [&](ManifestExtractor::Element* root,
1875 const std::string& name) -> ManifestExtractor::Element* {
1876 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1877 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1878 return permission->name == name;
1879 }
1880 return false;
1881 });
1882 };
1883
1884 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1885 int32_t max_sdk_version) -> void {
1886 auto permission = util::make_unique<UsesPermission>();
1887 permission->name = name;
1888 permission->maxSdkVersion = max_sdk_version;
1889 permission->Print(printer);
1890 permission->PrintImplied(printer, reason);
1891 };
1892
1893 // Implied permissions
1894 // Pre-1.6 implicitly granted permission compatibility logic
1895 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
1896 bool insert_write_external = false;
1897 auto write_external_permission = ElementCast<UsesPermission>(
1898 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
1899
1900 if (target_sdk() < 4) {
1901 if (!write_external_permission) {
1902 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
1903 insert_write_external = true;
1904 }
1905
1906 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
1907 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
1908 }
1909 }
1910
1911 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
1912 // force them to always take READ_EXTERNAL_STORAGE as well. We always
1913 // do this (regardless of target API version) because we can't have
1914 // an app with write permission but not read permission.
1915 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
1916 if (!read_external && (insert_write_external || write_external_permission)) {
1917 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
1918 "requested WRITE_EXTERNAL_STORAGE",
1919 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
1920 }
1921
1922 // Pre-JellyBean call log permission compatibility.
1923 if (target_sdk() < 16) {
1924 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
1925 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
1926 PrintPermission("android.permission.READ_CALL_LOG",
1927 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
1928 }
1929
1930 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
1931 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
1932 PrintPermission("android.permission.WRITE_CALL_LOG",
1933 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
1934 }
1935 }
1936
1937 // If the app hasn't declared the touchscreen as a feature requirement (either
1938 // directly or implied, required or not), then the faketouch feature is implied.
1939 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
1940 common_feature_group->addImpliedFeature("android.hardware.faketouch",
1941 "default feature for all apps", false);
1942 }
1943
1944 // Only print the common feature group if no feature group is defined
1945 std::vector<FeatureGroup*> feature_groups;
1946 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
1947 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
1948 feature_groups.push_back(feature_group);
1949 }
1950 });
1951
1952 if (feature_groups.empty()) {
1953 common_feature_group->PrintGroup(printer);
1954 } else {
1955 // Merge the common feature group into the feature group
1956 for (auto& feature_group : feature_groups) {
1957 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
1958 common_feature_group->open_gles_version);
1959 feature_group->Merge(common_feature_group);
1960 feature_group->PrintGroup(printer);
1961 }
1962 };
1963
1964 // Collect the component types of the application
1965 std::set<std::string> components;
1966 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
1967 if (ElementCast<Action>(el)) {
1968 auto action = ElementCast<Action>(el);
1969 if (!action->component.empty()) {
1970 components.insert(action->component);
1971 return;
1972 }
1973 }
1974
1975 if (ElementCast<Category>(el)) {
1976 auto category = ElementCast<Category>(el);
1977 if (!category->component.empty()) {
1978 components.insert(category->component);
1979 return;
1980 }
1981 }
1982 });
1983
1984 // Check for the payment component
1985 auto apk = apk_;
1986 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
1987 if (auto service = ElementCast<Service>(el)) {
1988 auto host_apdu_action = ElementCast<Action>(FindElement(service,
1989 [&](ManifestExtractor::Element* el) -> bool {
1990 if (auto action = ElementCast<Action>(el)) {
1991 return (action->component == "host-apdu");
1992 }
1993 return false;
1994 }));
1995
1996 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
1997 [&](ManifestExtractor::Element* el) -> bool {
1998 if (auto action = ElementCast<Action>(el)) {
1999 return (action->component == "offhost-apdu");
2000 }
2001 return false;
2002 }));
2003
2004 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2005 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2006 if (auto meta_data = ElementCast<MetaData>(el)) {
2007 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2008 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2009 && offhost_apdu_action)) {
2010
2011 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002012 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002013 return;
2014 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002015 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002016 if (!resource) {
2017 return;
2018 }
2019
2020 // Look for the payment category on an <aid-group> element
2021 auto& root = resource.get()->root;
2022 if ((host_apdu_action && root->name == "host-apdu-service")
2023 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2024
2025 for (auto& child : root->GetChildElements()) {
2026 if (child->name == "aid-group") {
2027 auto category = FindAttribute(child, CATEGORY_ATTR);
2028 if (category && category->value == "payment") {
2029 components.insert("payment");
2030 return;
2031 }
2032 }
2033 }
2034 }
2035 }
2036 }
2037 });
2038 }
2039 });
2040
2041 // Print the components types if they are present
2042 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2043 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002044 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002045 }
2046 };
2047
2048 PrintComponent("app-widget");
2049 PrintComponent("device-admin");
2050 PrintComponent("ime");
2051 PrintComponent("wallpaper");
2052 PrintComponent("accessibility");
2053 PrintComponent("print-service");
2054 PrintComponent("payment");
2055 PrintComponent("search");
2056 PrintComponent("document-provider");
2057 PrintComponent("launcher");
2058 PrintComponent("notification-listener");
2059 PrintComponent("dream");
2060 PrintComponent("camera");
2061 PrintComponent("camera-secure");
2062
2063 // Print presence of main activity
2064 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002065 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002066 }
2067
2068 // Print presence of activities, recivers, and services with no special components
2069 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2070 if (auto activity = ElementCast<Activity>(el)) {
2071 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002072 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002073 return true;
2074 }
2075 }
2076 return false;
2077 });
2078
2079 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2080 if (auto receiver = ElementCast<Receiver>(el)) {
2081 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002082 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002083 return true;
2084 }
2085 }
2086 return false;
2087 });
2088
2089 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2090 if (auto service = ElementCast<Service>(el)) {
2091 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002092 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002093 return true;
2094 }
2095 }
2096 return false;
2097 });
2098
2099 // Print the supported screens
2100 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2101 [&](ManifestExtractor::Element* el) -> bool {
2102 return ElementCast<SupportsScreen>(el) != nullptr;
2103 }));
2104
2105 if (screen) {
2106 screen->PrintScreens(printer, target_sdk_);
2107 } else {
2108 // Print the default supported screens
2109 SupportsScreen default_screens;
2110 default_screens.PrintScreens(printer, target_sdk_);
2111 }
2112
2113 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002114 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002115 for (auto& config : locales_) {
2116 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002117 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002118 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002119 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002120 }
2121 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002122 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002123
2124 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002125 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002126 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002127 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002128 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002129 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002130
2131 // Print the supported architectures of the app
2132 std::set<std::string> architectures;
2133 auto it = apk_->GetFileCollection()->Iterator();
2134 while (it->HasNext()) {
2135 auto file_path = it->Next()->GetSource().path;
2136
2137
2138 size_t pos = file_path.find("lib/");
2139 if (pos != std::string::npos) {
2140 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002141 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002142 if (pos != std::string::npos) {
2143 file_path = file_path.substr(0, pos);
2144 }
2145
2146 architectures.insert(file_path);
2147 }
2148 }
2149
2150 // Determine if the application has multiArch supports
2151 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2152 if (auto application = ElementCast<Application>(el)) {
2153 return application->has_multi_arch;
2154 }
2155 return false;
2156 });
2157
2158 bool output_alt_native_code = false;
2159 // A multiArch package is one that contains 64-bit and
2160 // 32-bit versions of native code and expects 3rd-party
2161 // apps to load these native code libraries. Since most
2162 // 64-bit systems also support 32-bit apps, the apps
2163 // loading this multiArch package's code may be either
2164 if (has_multi_arch) {
2165 // If this is a multiArch package, report the 64-bit
2166 // version only. Then as a separate entry, report the
2167 // rest.
2168 //
2169 // If we report the 32-bit architecture, this APK will
2170 // be installed on a 32-bit device, causing a large waste
2171 // of bandwidth and disk space. This assumes that
2172 // the developer of the multiArch package has also
2173 // made a version that is 32-bit only.
2174 const std::string kIntel64 = "x86_64";
2175 const std::string kArm64 = "arm64-v8a";
2176
2177 auto arch = architectures.find(kIntel64);
2178 if (arch == architectures.end()) {
2179 arch = architectures.find(kArm64);
2180 }
2181
2182 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002183 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002184 architectures.erase(arch);
2185 output_alt_native_code = true;
2186 }
2187 }
2188
2189 if (architectures.size() > 0) {
2190 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002191 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002192 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002193 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002194 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002195 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002196 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002197 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002198 }
2199
2200 return true;
2201}
2202
2203/**
2204 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2205 * pointer.
2206 **/
2207template<typename T>
2208T* ElementCast(ManifestExtractor::Element* element) {
2209 if (element == nullptr) {
2210 return nullptr;
2211 }
2212
2213 const std::unordered_map<std::string, bool> kTagCheck = {
2214 {"action", std::is_base_of<Action, T>::value},
2215 {"activity", std::is_base_of<Activity, T>::value},
2216 {"application", std::is_base_of<Application, T>::value},
2217 {"category", std::is_base_of<Category, T>::value},
2218 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2219 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2220 {"input-type", std::is_base_of<InputType, T>::value},
2221 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2222 {"meta-data", std::is_base_of<MetaData, T>::value},
2223 {"manifest", std::is_base_of<Manifest, T>::value},
2224 {"original-package", std::is_base_of<OriginalPackage, T>::value},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002225 {"overlay", std::is_base_of<Overlay, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002226 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2227 {"permission", std::is_base_of<Permission, T>::value},
2228 {"provider", std::is_base_of<Provider, T>::value},
2229 {"receiver", std::is_base_of<Receiver, T>::value},
2230 {"screen", std::is_base_of<Screen, T>::value},
2231 {"service", std::is_base_of<Service, T>::value},
2232 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2233 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2234 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2235 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2236 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2237 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2238 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2239 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2240 {"uses-package", std::is_base_of<UsesPackage, T>::value},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002241 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2242 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
2243 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002244 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2245 };
2246
2247 auto check = kTagCheck.find(element->tag());
2248 if (check != kTagCheck.end() && check->second) {
2249 return static_cast<T*>(element);
2250 }
2251 return nullptr;
2252}
2253
2254template<typename T>
2255std::unique_ptr<T> CreateType() {
2256 return std::move(util::make_unique<T>());
2257}
2258
2259std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2260 ManifestExtractor* extractor, xml::Element* el) {
2261 const std::unordered_map<std::string,
2262 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2263 kTagCheck = {
2264 {"action", &CreateType<Action>},
2265 {"activity", &CreateType<Activity>},
2266 {"application", &CreateType<Application>},
2267 {"category", &CreateType<Category>},
2268 {"compatible-screens", &CreateType<CompatibleScreens>},
2269 {"feature-group", &CreateType<FeatureGroup>},
2270 {"input-type", &CreateType<InputType>},
2271 {"intent-filter",&CreateType<IntentFilter>},
2272 {"manifest", &CreateType<Manifest>},
2273 {"meta-data", &CreateType<MetaData>},
2274 {"original-package", &CreateType<OriginalPackage>},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002275 {"overlay", &CreateType<Overlay>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002276 {"package-verifier", &CreateType<PackageVerifier>},
2277 {"permission", &CreateType<Permission>},
2278 {"provider", &CreateType<Provider>},
2279 {"receiver", &CreateType<Receiver>},
2280 {"screen", &CreateType<Screen>},
2281 {"service", &CreateType<Service>},
2282 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2283 {"supports-input", &CreateType<SupportsInput>},
2284 {"supports-screens", &CreateType<SupportsScreen>},
2285 {"uses-configuration", &CreateType<UsesConfiguarion>},
2286 {"uses-feature", &CreateType<UsesFeature>},
2287 {"uses-permission", &CreateType<UsesPermission>},
2288 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2289 {"uses-library", &CreateType<UsesLibrary>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002290 {"static-library", &CreateType<StaticLibrary>},
2291 {"uses-static-library", &CreateType<UsesStaticLibrary>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002292 {"uses-package", &CreateType<UsesPackage>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002293 {"additional-certificate", &CreateType<AdditionalCertificate>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002294 {"uses-sdk", &CreateType<UsesSdkBadging>},
2295 };
2296
2297 // Attempt to map the xml tag to a element inflater
2298 std::unique_ptr<ManifestExtractor::Element> element;
2299 auto check = kTagCheck.find(el->name);
2300 if (check != kTagCheck.end()) {
2301 element = check->second();
2302 } else {
2303 element = util::make_unique<ManifestExtractor::Element>();
2304 }
2305
2306 element->extractor_ = extractor;
2307 element->tag_ = el->name;
2308 element->Extract(el);
2309 return element;
2310}
2311
2312std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2313 auto element = ManifestExtractor::Element::Inflate(this, el);
2314 parent_stack_.insert(parent_stack_.begin(), element.get());
2315
2316 // Process the element and recursively visit the children
2317 for (xml::Element* child : el->GetChildElements()) {
2318 auto v = Visit(child);
2319 element->AddChild(v);
2320 }
2321
2322 parent_stack_.erase(parent_stack_.begin());
2323 return element;
2324}
2325
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002326
Ryan Mitchell214846d2018-09-19 16:57:01 -07002327int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2328 IDiagnostics* diag) {
2329 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002330 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002331}
2332
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002333} // namespace aapt