blob: 54f0816f0398968e244b995c184ca5702ea891e0 [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()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700471 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700472 if (platformVersionNameInt) {
473 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
474 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700475 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700476 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700477 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700478 if (platformVersionCodeInt) {
479 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
480 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700481 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700482 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700483 }
484 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700485 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700486 compilesdkVersionCodename->data()));
487 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700488 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700489
490 if (installLocation) {
491 switch (*installLocation) {
492 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700493 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700494 break;
495 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700496 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700497 break;
498 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700499 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700500 break;
501 default:
502 break;
503 }
504 }
505 }
506};
507
508/** Represents <application> elements. **/
509class Application : public ManifestExtractor::Element {
510 public:
511 Application() = default;
512 std::string label;
513 std::string icon;
514 std::string banner;
515 int32_t is_game;
516 int32_t debuggable;
517 int32_t test_only;
518 bool has_multi_arch;
519
520 /** Mapping from locales to app names. */
521 std::map<std::string, std::string> locale_labels;
522
523 /** Mapping from densities to app icons. */
524 std::map<uint16_t, std::string> density_icons;
525
526 void Extract(xml::Element* element) override {
527 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
528 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
529 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
530 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
531 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
532 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
533
534 // We must search by name because the multiArch flag hasn't been API
535 // frozen yet.
536 has_multi_arch = (GetAttributeIntegerDefault(
537 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
538
539 // Retrieve the app names for every locale the app supports
540 auto attr = FindAttribute(element, LABEL_ATTR);
541 for (auto& config : extractor()->locales()) {
542 if (auto label = GetAttributeString(attr, config.second)) {
543 if (label) {
544 locale_labels.insert(std::make_pair(config.first, *label));
545 }
546 }
547 }
548
549 // Retrieve the icons for the densities the app supports
550 attr = FindAttribute(element, ICON_ATTR);
551 for (auto& config : extractor()->densities()) {
552 if (auto resource = GetAttributeString(attr, config.second)) {
553 if (resource) {
554 density_icons.insert(std::make_pair(config.first, *resource));
555 }
556 }
557 }
558 }
559
Ryan Mitchell214846d2018-09-19 16:57:01 -0700560 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700561 // Print the labels for every locale
562 for (auto p : locale_labels) {
563 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700564 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700565 android::ResTable::normalizeForOutput(p.second.data())
566 .c_str()));
567 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700568 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700569 android::ResTable::normalizeForOutput(p.second.data())
570 .c_str()));
571 }
572 }
573
574 // Print the icon paths for every density
575 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700576 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700577 }
578
579 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700580 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700581 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700582 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700583 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700584 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700585 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700586 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700587
588 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700589 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700590 }
591 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700592 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700593 }
594 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700595 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700596 }
597 }
598};
599
600/** Represents <uses-sdk> elements. **/
601class UsesSdkBadging : public ManifestExtractor::Element {
602 public:
603 UsesSdkBadging() = default;
604 const int32_t* min_sdk = nullptr;
605 const std::string* min_sdk_name = nullptr;
606 const int32_t* max_sdk = nullptr;
607 const int32_t* target_sdk = nullptr;
608 const std::string* target_sdk_name = nullptr;
609
610 void Extract(xml::Element* element) override {
611 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
612 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
613 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
614 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
615 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
616
617 // Detect the target sdk of the element
618 if ((min_sdk_name && *min_sdk_name == "Donut")
619 || (target_sdk_name && *target_sdk_name == "Donut")) {
620 extractor()->RaiseTargetSdk(4);
621 }
622 if (min_sdk) {
623 extractor()->RaiseTargetSdk(*min_sdk);
624 }
625 if (target_sdk) {
626 extractor()->RaiseTargetSdk(*target_sdk);
627 }
628 }
629
Ryan Mitchell214846d2018-09-19 16:57:01 -0700630 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700631 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700632 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700633 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700634 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700635 }
636 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700637 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700638 }
639 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700640 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700641 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700642 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700643 }
644 }
645};
646
647/** Represents <uses-configuration> elements. **/
648class UsesConfiguarion : public ManifestExtractor::Element {
649 public:
650 UsesConfiguarion() = default;
651 int32_t req_touch_screen = 0;
652 int32_t req_keyboard_type = 0;
653 int32_t req_hard_keyboard = 0;
654 int32_t req_navigation = 0;
655 int32_t req_five_way_nav = 0;
656
657 void Extract(xml::Element* element) override {
658 req_touch_screen = GetAttributeIntegerDefault(
659 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
660 req_keyboard_type = GetAttributeIntegerDefault(
661 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
662 req_hard_keyboard = GetAttributeIntegerDefault(
663 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
664 req_navigation = GetAttributeIntegerDefault(
665 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
666 req_five_way_nav = GetAttributeIntegerDefault(
667 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
668 }
669
Ryan Mitchell214846d2018-09-19 16:57:01 -0700670 void Print(text::Printer* printer) override {
671 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700672 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700673 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700674 }
675 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700676 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700677 }
678 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700679 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700680 }
681 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700682 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700683 }
684 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700685 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700686 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700687 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700688 }
689};
690
691/** Represents <supports-screen> elements. **/
692class SupportsScreen : public ManifestExtractor::Element {
693 public:
694 SupportsScreen() = default;
695 int32_t small_screen = 1;
696 int32_t normal_screen = 1;
697 int32_t large_screen = 1;
698 int32_t xlarge_screen = 1;
699 int32_t any_density = 1;
700 int32_t requires_smallest_width_dp = 0;
701 int32_t compatible_width_limit_dp = 0;
702 int32_t largest_width_limit_dp = 0;
703
704 void Extract(xml::Element* element) override {
705 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
706 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
707 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
708 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
709 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
710
711 requires_smallest_width_dp = GetAttributeIntegerDefault(
712 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
713 compatible_width_limit_dp = GetAttributeIntegerDefault(
714 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
715 largest_width_limit_dp = GetAttributeIntegerDefault(
716 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
717
718 // For modern apps, if screen size buckets haven't been specified
719 // but the new width ranges have, then infer the buckets from them.
720 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
721 && requires_smallest_width_dp > 0) {
722 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
723 : requires_smallest_width_dp;
724 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
725 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
726 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
727 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
728 }
729 }
730
Ryan Mitchell214846d2018-09-19 16:57:01 -0700731 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700732 int32_t small_screen_temp = small_screen;
733 int32_t normal_screen_temp = normal_screen;
734 int32_t large_screen_temp = large_screen;
735 int32_t xlarge_screen_temp = xlarge_screen;
736 int32_t any_density_temp = any_density;
737
738 // Determine default values for any unspecified screen sizes,
739 // based on the target SDK of the package. As of 4 (donut)
740 // the screen size support was introduced, so all default to
741 // enabled.
742 if (small_screen_temp > 0) {
743 small_screen_temp = target_sdk >= 4 ? -1 : 0;
744 }
745 if (normal_screen_temp > 0) {
746 normal_screen_temp = -1;
747 }
748 if (large_screen_temp > 0) {
749 large_screen_temp = target_sdk >= 4 ? -1 : 0;
750 }
751 if (xlarge_screen_temp > 0) {
752 // Introduced in Gingerbread.
753 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
754 }
755 if (any_density_temp > 0) {
756 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
757 || compatible_width_limit_dp > 0) ? -1 : 0;
758 }
759
760 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700761 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700762 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700763 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700764 }
765 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700766 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700767 }
768 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700769 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700770 }
771 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700772 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700773 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700774 printer->Print("\n");
775 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700776 (any_density_temp ) ? "true" : "false"));
777 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700778 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700779 }
780 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700781 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700782 }
783 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700784 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700785 }
786 }
787};
788
789/** Represents <feature-group> elements. **/
790class FeatureGroup : public ManifestExtractor::Element {
791 public:
792 FeatureGroup() = default;
793 std::string label;
794 int32_t open_gles_version = 0;
795
796 void Extract(xml::Element* element) override {
797 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
798 }
799
Ryan Mitchell214846d2018-09-19 16:57:01 -0700800 virtual void PrintGroup(text::Printer* printer) {
801 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700802 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700803 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700804 }
805
806 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700807 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700808 (feature.second.required ? "" : "-not-required"),
809 feature.first.data()));
810 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700811 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700812 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700813 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700814 }
815 }
816
817 /** Adds a feature to the feature group. */
818 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
819 features_.insert(std::make_pair(name, Feature{ required, version }));
820 if (required) {
821 if (name == "android.hardware.camera.autofocus" ||
822 name == "android.hardware.camera.flash") {
823 AddFeature("android.hardware.camera", true);
824 } else if (name == "android.hardware.location.gps" ||
825 name == "android.hardware.location.network") {
826 AddFeature("android.hardware.location", true);
827 } else if (name == "android.hardware.faketouch.multitouch") {
828 AddFeature("android.hardware.faketouch", true);
829 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
830 name == "android.hardware.faketouch.multitouch.jazzhands") {
831 AddFeature("android.hardware.faketouch.multitouch", true);
832 AddFeature("android.hardware.faketouch", true);
833 } else if (name == "android.hardware.touchscreen.multitouch") {
834 AddFeature("android.hardware.touchscreen", true);
835 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
836 name == "android.hardware.touchscreen.multitouch.jazzhands") {
837 AddFeature("android.hardware.touchscreen.multitouch", true);
838 AddFeature("android.hardware.touchscreen", true);
839 } else if (name == "android.hardware.opengles.aep") {
840 const int kOpenGLESVersion31 = 0x00030001;
841 if (kOpenGLESVersion31 > open_gles_version) {
842 open_gles_version = kOpenGLESVersion31;
843 }
844 }
845 }
846 }
847
848 /** Returns true if the feature group has the given feature. */
849 virtual bool HasFeature(const std::string& name) {
850 return features_.find(name) != features_.end();
851 }
852
853 /** Merges the features of another feature group into this group. */
854 void Merge(FeatureGroup* group) {
855 open_gles_version = std::max(open_gles_version, group->open_gles_version);
856 for (auto& feature : group->features_) {
857 features_.insert(feature);
858 }
859 }
860
861 protected:
862 struct Feature {
863 public:
864 bool required = false;
865 int32_t version = -1;
866 };
867
868 /* Mapping of feature names to their properties. */
869 std::map<std::string, Feature> features_;
870};
871
872/**
873 * Represents the default feature group for the application if no <feature-group> elements are
874 * present in the manifest.
875 **/
876class CommonFeatureGroup : public FeatureGroup {
877 public:
878 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700879 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700880 FeatureGroup::PrintGroup(printer);
881
882 // Also print the implied features
883 for (auto feature : implied_features_) {
884 if (features_.find(feature.first) == features_.end()) {
885 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700886 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
887 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700888 feature.first.data()));
889
890 // Print the reasons as a sentence
891 size_t count = 0;
892 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700893 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700894 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700895 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700896 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700897 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700898 }
899 count++;
900 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700901 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700902 }
903 }
904 }
905
906 /** Returns true if the feature group has the given feature. */
907 bool HasFeature(const std::string& name) override {
908 return FeatureGroup::HasFeature(name)
909 || implied_features_.find(name) != implied_features_.end();
910 }
911
912 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
913 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
914 auto entry = implied_features_.find(name);
915 if (entry == implied_features_.end()) {
916 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
917 entry = implied_features_.find(name);
918 }
919
920 // A non-sdk 23 implied feature takes precedence.
921 if (entry->second.implied_from_sdk_k23 && !sdk23) {
922 entry->second.implied_from_sdk_k23 = false;
923 }
924
925 entry->second.reasons.insert(reason);
926 }
927
928 /**
929 * Adds a feature to a set of implied features for all features that are implied by the presence
930 * of the permission.
931 **/
932 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
933 if (name == "android.permission.CAMERA") {
934 addImpliedFeature("android.hardware.camera",
935 StringPrintf("requested %s permission", name.data()),
936 sdk23);
937
938 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
939 if (targetSdk < SDK_LOLLIPOP) {
940 addImpliedFeature("android.hardware.location.gps",
941 StringPrintf("requested %s permission", name.data()),
942 sdk23);
943 addImpliedFeature("android.hardware.location.gps",
944 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
945 sdk23);
946 }
947 addImpliedFeature("android.hardware.location",
948 StringPrintf("requested %s permission", name.data()),
949 sdk23);
950
951 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
952 if (targetSdk < SDK_LOLLIPOP) {
953 addImpliedFeature("android.hardware.location.network",
954 StringPrintf("requested %s permission", name.data()),
955 sdk23);
956 addImpliedFeature("android.hardware.location.network",
957 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
958 sdk23);
959 }
960 addImpliedFeature("android.hardware.location",
961 StringPrintf("requested %s permission", name.data()),
962 sdk23);
963
964 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
965 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
966 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
967 addImpliedFeature("android.hardware.location",
968 StringPrintf("requested %s permission", name.data()),
969 sdk23);
970
971 } else if (name == "android.permission.BLUETOOTH" ||
972 name == "android.permission.BLUETOOTH_ADMIN") {
973 if (targetSdk > SDK_DONUT) {
974 addImpliedFeature("android.hardware.bluetooth",
975 StringPrintf("requested %s permission", name.data()),
976 sdk23);
977 addImpliedFeature("android.hardware.bluetooth",
978 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
979 sdk23);
980 }
981
982 } else if (name == "android.permission.RECORD_AUDIO") {
983 addImpliedFeature("android.hardware.microphone",
984 StringPrintf("requested %s permission", name.data()),
985 sdk23);
986
987 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
988 name == "android.permission.CHANGE_WIFI_STATE" ||
989 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
990 addImpliedFeature("android.hardware.wifi",
991 StringPrintf("requested %s permission", name.data()),
992 sdk23);
993
994 } else if (name == "android.permission.CALL_PHONE" ||
995 name == "android.permission.CALL_PRIVILEGED" ||
996 name == "android.permission.MODIFY_PHONE_STATE" ||
997 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
998 name == "android.permission.READ_SMS" ||
999 name == "android.permission.RECEIVE_SMS" ||
1000 name == "android.permission.RECEIVE_MMS" ||
1001 name == "android.permission.RECEIVE_WAP_PUSH" ||
1002 name == "android.permission.SEND_SMS" ||
1003 name == "android.permission.WRITE_APN_SETTINGS" ||
1004 name == "android.permission.WRITE_SMS") {
1005 addImpliedFeature("android.hardware.telephony",
1006 "requested a telephony permission",
1007 sdk23);
1008 }
1009 }
1010
1011 private:
1012 /**
1013 * Represents a feature that has been automatically added due to a pre-requisite or for some
1014 * other reason.
1015 */
1016 struct ImpliedFeature {
1017 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1018
1019 /** List of human-readable reasons for why this feature was implied. */
1020 std::set<std::string> reasons;
1021
1022 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1023 bool implied_from_sdk_k23;
1024 };
1025
1026 /* Mapping of implied feature names to their properties. */
1027 std::map<std::string, ImpliedFeature> implied_features_;
1028};
1029
1030/** Represents <uses-feature> elements. **/
1031class UsesFeature : public ManifestExtractor::Element {
1032 public:
1033 UsesFeature() = default;
1034 void Extract(xml::Element* element) override {
1035 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1036 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1037 bool required = GetAttributeIntegerDefault(
1038 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1039 int32_t version = GetAttributeIntegerDefault(
1040 FindAttribute(element, kAndroidNamespace, "version"), 0);
1041
1042 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1043 // common feature group
1044 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1045 if (!feature_group) {
1046 feature_group = extractor()->GetCommonFeatureGroup();
1047 } else {
1048 // All features in side of <feature-group> elements are required.
1049 required = true;
1050 }
1051
1052 if (name) {
1053 feature_group->AddFeature(*name, required, version);
1054 } else if (gl) {
1055 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1056 }
1057 }
1058};
1059
1060/** Represents <uses-permission> elements. **/
1061class UsesPermission : public ManifestExtractor::Element {
1062 public:
1063 UsesPermission() = default;
1064 std::string name;
1065 std::string requiredFeature;
1066 std::string requiredNotFeature;
1067 int32_t required = true;
1068 int32_t maxSdkVersion = -1;
1069
1070 void Extract(xml::Element* element) override {
1071 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1072 requiredFeature = GetAttributeStringDefault(
1073 FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1074 requiredNotFeature = GetAttributeStringDefault(
1075 FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1076 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1077 maxSdkVersion = GetAttributeIntegerDefault(
1078 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1079
1080 if (!name.empty()) {
1081 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1082 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1083 }
1084 }
1085
Ryan Mitchell214846d2018-09-19 16:57:01 -07001086 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001087 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001088 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001089 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001090 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001091 }
1092 if (!requiredFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001093 printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001094 }
1095 if (!requiredNotFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001096 printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001097 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001098 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001099 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001100 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001101 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001102 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001103 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001104 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001105 }
1106 }
1107 }
1108
Ryan Mitchell214846d2018-09-19 16:57:01 -07001109 void PrintImplied(text::Printer* printer, const std::string& reason) {
1110 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001111 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001112 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001113 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001114 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001115 }
1116};
1117
1118/** Represents <uses-permission-sdk-23> elements. **/
1119class UsesPermissionSdk23 : public ManifestExtractor::Element {
1120 public:
1121 UsesPermissionSdk23() = default;
1122 const std::string* name = nullptr;
1123 const int32_t* maxSdkVersion = nullptr;
1124
1125 void Extract(xml::Element* element) override {
1126 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1127 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1128
1129 if (name) {
1130 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1131 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1132 }
1133 }
1134
Ryan Mitchell214846d2018-09-19 16:57:01 -07001135 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001136 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001137 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001138 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001139 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001140 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001141 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001142 }
1143 }
1144};
1145
1146/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1147class Permission : public ManifestExtractor::Element {
1148 public:
1149 Permission() = default;
1150 std::string name;
1151
1152 void Extract(xml::Element* element) override {
1153 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1154 }
1155
Ryan Mitchell214846d2018-09-19 16:57:01 -07001156 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001157 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001158 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001159 }
1160 }
1161};
1162
1163/** Represents <activity> elements. **/
1164class Activity : public ManifestExtractor::Element {
1165 public:
1166 Activity() = default;
1167 std::string name;
1168 std::string icon;
1169 std::string label;
1170 std::string banner;
1171
1172 bool has_component_ = false;
1173 bool has_launcher_category = false;
1174 bool has_leanback_launcher_category = false;
1175 bool has_main_action = false;
1176
1177 void Extract(xml::Element* element) override {
1178 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1179 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1180 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1181 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1182
1183 // Retrieve the package name from the manifest
1184 std::string package;
1185 for (auto& parent : extractor()->parent_stack()) {
1186 if (auto manifest = ElementCast<Manifest>(parent)) {
1187 package = manifest->package;
1188 break;
1189 }
1190 }
1191
1192 // Fully qualify the activity name
1193 ssize_t idx = name.find(".");
1194 if (idx == 0) {
1195 name = package + name;
1196 } else if (idx < 0) {
1197 name = package + "." + name;
1198 }
1199
1200 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1201 if (orientation) {
1202 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1203 int orien = *orientation;
1204 if (orien == 0 || orien == 6 || orien == 8) {
1205 // Requests landscape, sensorLandscape, or reverseLandscape.
1206 common->addImpliedFeature("android.hardware.screen.landscape",
1207 "one or more activities have specified a landscape orientation",
1208 false);
1209 } else if (orien == 1 || orien == 7 || orien == 9) {
1210 // Requests portrait, sensorPortrait, or reversePortrait.
1211 common->addImpliedFeature("android.hardware.screen.portrait",
1212 "one or more activities have specified a portrait orientation",
1213 false);
1214 }
1215 }
1216 }
1217
Ryan Mitchell214846d2018-09-19 16:57:01 -07001218 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001219 // Print whether the activity has the HOME category and a the MAIN action
1220 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001221 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001222 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001223 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001224 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001225 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001226 android::ResTable::normalizeForOutput(label.data()).c_str(),
1227 icon.data()));
1228 }
1229
1230 // Print wether the activity has the HOME category and a the MAIN action
1231 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001232 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001233 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001234 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001235 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001236 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001237 android::ResTable::normalizeForOutput(label.data()).c_str(),
1238 icon.data(), banner.data()));
1239 }
1240 }
1241};
1242
1243/** Represents <intent-filter> elements. */
1244class IntentFilter : public ManifestExtractor::Element {
1245 public:
1246 IntentFilter() = default;
1247};
1248
1249/** Represents <category> elements. */
1250class Category : public ManifestExtractor::Element {
1251 public:
1252 Category() = default;
1253 std::string component = "";
1254
1255 void Extract(xml::Element* element) override {
1256 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1257
1258 auto parent_stack = extractor()->parent_stack();
1259 if (category && ElementCast<IntentFilter>(parent_stack[0])
1260 && ElementCast<Activity>(parent_stack[1])) {
1261 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1262
1263 if (*category == "android.intent.category.LAUNCHER") {
1264 activity->has_launcher_category = true;
1265 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1266 activity->has_leanback_launcher_category = true;
1267 } else if (*category == "android.intent.category.HOME") {
1268 component = "launcher";
1269 }
1270 }
1271 }
1272};
1273
1274/**
1275 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1276 * elements nested within.
1277 **/
1278class Provider : public ManifestExtractor::Element {
1279 public:
1280 Provider() = default;
1281 bool has_required_saf_attributes = false;
1282
1283 void Extract(xml::Element* element) override {
1284 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1285 const int32_t* grant_uri_permissions = GetAttributeInteger(
1286 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1287 const std::string* permission = GetAttributeString(
1288 FindAttribute(element, PERMISSION_ATTR));
1289
1290 has_required_saf_attributes = ((exported && *exported != 0)
1291 && (grant_uri_permissions && *grant_uri_permissions != 0)
1292 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1293 }
1294};
1295
1296/** Represents <receiver> elements. **/
1297class Receiver : public ManifestExtractor::Element {
1298 public:
1299 Receiver() = default;
1300 const std::string* permission = nullptr;
1301 bool has_component = false;
1302
1303 void Extract(xml::Element* element) override {
1304 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1305 }
1306};
1307
1308/**Represents <service> elements. **/
1309class Service : public ManifestExtractor::Element {
1310 public:
1311 Service() = default;
1312 const std::string* permission = nullptr;
1313 bool has_component = false;
1314
1315 void Extract(xml::Element* element) override {
1316 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1317 }
1318};
1319
1320/** Represents <uses-library> elements. **/
1321class UsesLibrary : public ManifestExtractor::Element {
1322 public:
1323 UsesLibrary() = default;
1324 std::string name;
1325 int required;
1326
1327 void Extract(xml::Element* element) override {
1328 auto parent_stack = extractor()->parent_stack();
1329 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1330 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1331 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1332 }
1333 }
1334
Ryan Mitchell214846d2018-09-19 16:57:01 -07001335 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001336 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001337 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001338 (required == 0) ? "-not-required" : "", name.data()));
1339 }
1340 }
1341};
1342
Dianne Hackborn813d7502018-10-02 16:59:46 -07001343/** Represents <static-library> elements. **/
1344class StaticLibrary : public ManifestExtractor::Element {
1345 public:
1346 StaticLibrary() = default;
1347 std::string name;
1348 int version;
1349 int versionMajor;
1350
1351 void Extract(xml::Element* element) override {
1352 auto parent_stack = extractor()->parent_stack();
1353 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1354 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1355 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1356 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1357 }
1358 }
1359
Ryan Mitchell214846d2018-09-19 16:57:01 -07001360 void Print(text::Printer* printer) override {
1361 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001362 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1363 name.data(), version, versionMajor));
1364 }
1365};
1366
1367/** Represents <uses-static-library> elements. **/
1368class UsesStaticLibrary : public ManifestExtractor::Element {
1369 public:
1370 UsesStaticLibrary() = default;
1371 std::string name;
1372 int version;
1373 int versionMajor;
1374 std::vector<std::string> certDigests;
1375
1376 void Extract(xml::Element* element) override {
1377 auto parent_stack = extractor()->parent_stack();
1378 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1379 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1380 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1381 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1382 AddCertDigest(element);
1383 }
1384 }
1385
1386 void AddCertDigest(xml::Element* element) {
1387 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1388 // We allow ":" delimiters in the SHA declaration as this is the format
1389 // emitted by the certtool making it easy for developers to copy/paste.
1390 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1391 if (!digest.empty()) {
1392 certDigests.push_back(digest);
1393 }
1394 }
1395
Ryan Mitchell214846d2018-09-19 16:57:01 -07001396 void Print(text::Printer* printer) override {
1397 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001398 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1399 name.data(), version, versionMajor));
1400 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001401 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001402 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001403 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001404 }
1405};
1406
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001407/**
1408 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1409 * explicitly enable meta data printing.
1410 **/
1411class MetaData : public ManifestExtractor::Element {
1412 public:
1413 MetaData() = default;
1414 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001415 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001416 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001417 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001418 const int* resource_int;
1419
1420 void Extract(xml::Element* element) override {
1421 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001422 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001423 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001424 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001425 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1426 }
1427
Ryan Mitchell214846d2018-09-19 16:57:01 -07001428 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001429 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001430 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001431 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001432 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001433 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001434 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001435 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001436 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001437 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001438 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001439 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001440 }
1441 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001442 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001443 }
1444 }
1445};
1446
1447/**
1448 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1449 * service components.
1450 **/
1451class Action : public ManifestExtractor::Element {
1452 public:
1453 Action() = default;
1454 std::string component = "";
1455
1456 void Extract(xml::Element* element) override {
1457 auto parent_stack = extractor()->parent_stack();
1458 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1459
1460 if (ElementCast<IntentFilter>(parent_stack[0])) {
1461 if (ElementCast<Activity>(parent_stack[1])) {
1462 // Detects the presence of a particular type of activity.
1463 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1464 auto map = std::map<std::string, std::string>({
1465 { "android.intent.action.MAIN" , "main" },
1466 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1467 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1468 });
1469
1470 auto entry = map.find(action);
1471 if (entry != map.end()) {
1472 component = entry->second;
1473 activity->has_component_ = true;
1474 }
1475
1476 if (action == "android.intent.action.MAIN") {
1477 activity->has_main_action = true;
1478 }
1479
1480 } else if (ElementCast<Receiver>(parent_stack[1])) {
1481 // Detects the presence of a particular type of receiver. If the action requires a
1482 // permission, then the receiver element is checked for the permission.
1483 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1484 auto map = std::map<std::string, std::string>({
1485 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1486 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1487 });
1488
1489 auto permissions = std::map<std::string, std::string>({
1490 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1491 });
1492
1493 auto entry = map.find(action);
1494 auto permission = permissions.find(action);
1495 if (entry != map.end() && (permission == permissions.end()
1496 || (receiver->permission && permission->second == *receiver->permission))) {
1497 receiver->has_component = true;
1498 component = entry->second;
1499 }
1500
1501 } else if (ElementCast<Service>(parent_stack[1])) {
1502 // Detects the presence of a particular type of service. If the action requires a
1503 // permission, then the service element is checked for the permission.
1504 Service* service = ElementCast<Service>(parent_stack[1]);
1505 auto map = std::map<std::string, std::string>({
1506 { "android.view.InputMethod" , "ime" },
1507 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1508 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1509 { "android.printservice.PrintService" , "print-service" },
1510 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1511 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1512 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1513 { "android.service.dreams.DreamService" , "dream" },
1514 });
1515
1516 auto permissions = std::map<std::string, std::string>({
1517 { "android.accessibilityservice.AccessibilityService" ,
1518 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1519 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1520 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1521 "android.permission.BIND_NFC_SERVICE" },
1522 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1523 "android.permission.BIND_NFC_SERVICE" },
1524 { "android.service.notification.NotificationListenerService" ,
1525 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1526 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1527 });
1528
1529 auto entry = map.find(action);
1530 auto permission = permissions.find(action);
1531 if (entry != map.end() && (permission == permissions.end()
1532 || (service->permission && permission->second == *service->permission))) {
1533 service->has_component= true;
1534 component = entry->second;
1535 }
1536
1537 } else if (ElementCast<Provider>(parent_stack[1])) {
1538 // Detects the presence of a particular type of receiver. If the provider requires a
1539 // permission, then the provider element is checked for the permission.
1540 // Detect whether this action
1541 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1542 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1543 && provider->has_required_saf_attributes) {
1544 component = "document-provider";
1545 }
1546 }
1547 }
1548
1549 // Represents a searchable interface
1550 if (action == "android.intent.action.SEARCH") {
1551 component = "search";
1552 }
1553 }
1554};
1555
1556/**
1557 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1558 **/
1559class SupportsInput : public ManifestExtractor::Element {
1560 public:
1561 SupportsInput() = default;
1562 std::vector<std::string> inputs;
1563
Ryan Mitchell214846d2018-09-19 16:57:01 -07001564 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001565 const size_t size = inputs.size();
1566 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001567 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001568 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001569 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001570 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001571 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001572 }
1573 }
1574};
1575
1576/** Represents <input-type> elements. **/
1577class InputType : public ManifestExtractor::Element {
1578 public:
1579 InputType() = default;
1580 void Extract(xml::Element* element) override {
1581 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1582 auto parent_stack = extractor()->parent_stack();
1583
1584 // Add the input to the set of supported inputs
1585 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1586 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1587 supports->inputs.push_back(*name);
1588 }
1589 }
1590};
1591
1592/** Represents <original-package> elements. **/
1593class OriginalPackage : public ManifestExtractor::Element {
1594 public:
1595 OriginalPackage() = default;
1596 const std::string* name = nullptr;
1597
1598 void Extract(xml::Element* element) override {
1599 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1600 }
1601
Ryan Mitchell214846d2018-09-19 16:57:01 -07001602 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001603 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001604 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001605 }
1606 }
1607};
1608
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001609
1610/** Represents <overlay> elements. **/
1611class Overlay : public ManifestExtractor::Element {
1612 public:
1613 Overlay() = default;
1614 const std::string* target_package = nullptr;
1615 int priority;
1616 bool is_static;
1617 const std::string* required_property_name = nullptr;
1618 const std::string* required_property_value = nullptr;
1619
1620 void Extract(xml::Element* element) override {
1621 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1622 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1623 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1624 required_property_name = GetAttributeString(
1625 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1626 required_property_value = GetAttributeString(
1627 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1628 }
1629
1630 void Print(text::Printer* printer) override {
1631 printer->Print(StringPrintf("overlay:"));
1632 if (target_package) {
1633 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1634 }
1635 printer->Print(StringPrintf(" priority='%d'", priority));
1636 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1637 if (required_property_name) {
1638 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1639 }
1640 if (required_property_value) {
1641 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1642 }
1643 printer->Print("\n");
1644 }
1645};
1646
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001647/** * Represents <package-verifier> elements. **/
1648class PackageVerifier : public ManifestExtractor::Element {
1649 public:
1650 PackageVerifier() = default;
1651 const std::string* name = nullptr;
1652 const std::string* public_key = nullptr;
1653
1654 void Extract(xml::Element* element) override {
1655 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1656 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1657 }
1658
Ryan Mitchell214846d2018-09-19 16:57:01 -07001659 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001660 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001661 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001662 name->data(), public_key->data()));
1663 }
1664 }
1665};
1666
1667/** Represents <uses-package> elements. **/
1668class UsesPackage : public ManifestExtractor::Element {
1669 public:
1670 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001671 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001672 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001673 int version;
1674 int versionMajor;
1675 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001676
1677 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001678 auto parent_stack = extractor()->parent_stack();
1679 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1680 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1681 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1682 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1683 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1684 AddCertDigest(element);
1685 }
1686 }
1687
1688 void AddCertDigest(xml::Element* element) {
1689 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1690 // We allow ":" delimiters in the SHA declaration as this is the format
1691 // emitted by the certtool making it easy for developers to copy/paste.
1692 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1693 if (!digest.empty()) {
1694 certDigests.push_back(digest);
1695 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001696 }
1697
Ryan Mitchell214846d2018-09-19 16:57:01 -07001698 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001699 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001700 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001701 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001702 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1703 packageType->data(), name->data(), version, versionMajor));
1704 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001705 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001706 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001707 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001708 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001709 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001710 }
1711 }
1712 }
1713};
1714
1715/** Represents <additional-certificate> elements. **/
1716class AdditionalCertificate : public ManifestExtractor::Element {
1717 public:
1718 AdditionalCertificate() = default;
1719
1720 void Extract(xml::Element* element) override {
1721 auto parent_stack = extractor()->parent_stack();
1722 if (parent_stack.size() > 0) {
1723 if (ElementCast<UsesPackage>(parent_stack[0])) {
1724 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1725 uses->AddCertDigest(element);
1726 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1727 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1728 uses->AddCertDigest(element);
1729 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001730 }
1731 }
1732};
1733
1734/** Represents <screen> elements found in <compatible-screens> elements. */
1735class Screen : public ManifestExtractor::Element {
1736 public:
1737 Screen() = default;
1738 const int32_t* size = nullptr;
1739 const int32_t* density = nullptr;
1740
1741 void Extract(xml::Element* element) override {
1742 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1743 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1744 }
1745};
1746
1747/**
1748 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1749 * that each denote a supported screen size and screen density.
1750 **/
1751class CompatibleScreens : public ManifestExtractor::Element {
1752 public:
1753 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001754 void Print(text::Printer* printer) override {
1755 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001756
1757 bool first = true;
1758 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1759 if (auto screen = ElementCast<Screen>(el)) {
1760 if (first) {
1761 first = false;
1762 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001763 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001764 }
1765
1766 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001767 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001768 }
1769 }
1770 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001771 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001772 }
1773};
1774
1775/** Represents <supports-gl-texture> elements. **/
1776class SupportsGlTexture : public ManifestExtractor::Element {
1777 public:
1778 SupportsGlTexture() = default;
1779 const std::string* name = nullptr;
1780
1781 void Extract(xml::Element* element) override {
1782 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1783 }
1784
Ryan Mitchell214846d2018-09-19 16:57:01 -07001785 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001786 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001787 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001788 }
1789 }
1790};
1791
1792/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001793static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001794 el->Print(printer);
1795 for (auto &child : el->children()) {
1796 Print(child.get(), printer);
1797 }
1798}
1799
Ryan Mitchell214846d2018-09-19 16:57:01 -07001800bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001801 // Load the manifest
1802 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1803 if (doc == nullptr) {
1804 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1805 return false;
1806 }
1807
1808 xml::Element* element = doc->root.get();
1809 if (element->name != "manifest") {
1810 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1811 return false;
1812 }
1813
1814 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1815 // printing only permission elements is requested
1816 if (options_.only_permissions) {
1817 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1818 ManifestExtractor::Element::Inflate(this, element);
1819
1820 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1821 for (xml::Element* child : element->GetChildElements()) {
1822 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1823 || child->name == "permission") {
1824 auto permission_element = ManifestExtractor::Element::Inflate(this, child);
1825 manifest->AddChild(permission_element);
1826 }
1827 }
1828
Ryan Mitchell214846d2018-09-19 16:57:01 -07001829 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001830 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1831 el->Print(printer);
1832 });
1833
1834 return true;
1835 }
1836
1837 return false;
1838 }
1839
1840 // Collect information about the resource configurations
1841 if (apk_->GetResourceTable()) {
1842 for (auto &package : apk_->GetResourceTable()->packages) {
1843 for (auto &type : package->types) {
1844 for (auto &entry : type->entries) {
1845 for (auto &value : entry->values) {
1846 std::string locale_str = value->config.GetBcp47LanguageTag();
1847
1848 // Collect all the unique locales of the apk
1849 if (locales_.find(locale_str) == locales_.end()) {
1850 ConfigDescription config = ManifestExtractor::DummyConfig();
1851 config.setBcp47Locale(locale_str.data());
1852 locales_.insert(std::make_pair(locale_str, config));
1853 }
1854
1855 // Collect all the unique density of the apk
1856 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1857 : value->config.density;
1858 if (densities_.find(density) == densities_.end()) {
1859 ConfigDescription config = ManifestExtractor::DummyConfig();
1860 config.density = density;
1861 densities_.insert(std::make_pair(density, config));
1862 }
1863 }
1864 }
1865 }
1866 }
1867 }
1868
1869 // Extract badging information
1870 auto root = Visit(element);
1871
1872 // Print the elements in order seen
1873 Print(root.get(), printer);
1874
1875 /** Recursively checks the extracted elements for the specified permission. **/
1876 auto FindPermission = [&](ManifestExtractor::Element* root,
1877 const std::string& name) -> ManifestExtractor::Element* {
1878 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1879 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1880 return permission->name == name;
1881 }
1882 return false;
1883 });
1884 };
1885
1886 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1887 int32_t max_sdk_version) -> void {
1888 auto permission = util::make_unique<UsesPermission>();
1889 permission->name = name;
1890 permission->maxSdkVersion = max_sdk_version;
1891 permission->Print(printer);
1892 permission->PrintImplied(printer, reason);
1893 };
1894
1895 // Implied permissions
1896 // Pre-1.6 implicitly granted permission compatibility logic
1897 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
1898 bool insert_write_external = false;
1899 auto write_external_permission = ElementCast<UsesPermission>(
1900 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
1901
1902 if (target_sdk() < 4) {
1903 if (!write_external_permission) {
1904 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
1905 insert_write_external = true;
1906 }
1907
1908 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
1909 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
1910 }
1911 }
1912
1913 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
1914 // force them to always take READ_EXTERNAL_STORAGE as well. We always
1915 // do this (regardless of target API version) because we can't have
1916 // an app with write permission but not read permission.
1917 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
1918 if (!read_external && (insert_write_external || write_external_permission)) {
1919 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
1920 "requested WRITE_EXTERNAL_STORAGE",
1921 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
1922 }
1923
1924 // Pre-JellyBean call log permission compatibility.
1925 if (target_sdk() < 16) {
1926 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
1927 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
1928 PrintPermission("android.permission.READ_CALL_LOG",
1929 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
1930 }
1931
1932 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
1933 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
1934 PrintPermission("android.permission.WRITE_CALL_LOG",
1935 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
1936 }
1937 }
1938
1939 // If the app hasn't declared the touchscreen as a feature requirement (either
1940 // directly or implied, required or not), then the faketouch feature is implied.
1941 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
1942 common_feature_group->addImpliedFeature("android.hardware.faketouch",
1943 "default feature for all apps", false);
1944 }
1945
1946 // Only print the common feature group if no feature group is defined
1947 std::vector<FeatureGroup*> feature_groups;
1948 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
1949 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
1950 feature_groups.push_back(feature_group);
1951 }
1952 });
1953
1954 if (feature_groups.empty()) {
1955 common_feature_group->PrintGroup(printer);
1956 } else {
1957 // Merge the common feature group into the feature group
1958 for (auto& feature_group : feature_groups) {
1959 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
1960 common_feature_group->open_gles_version);
1961 feature_group->Merge(common_feature_group);
1962 feature_group->PrintGroup(printer);
1963 }
1964 };
1965
1966 // Collect the component types of the application
1967 std::set<std::string> components;
1968 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
1969 if (ElementCast<Action>(el)) {
1970 auto action = ElementCast<Action>(el);
1971 if (!action->component.empty()) {
1972 components.insert(action->component);
1973 return;
1974 }
1975 }
1976
1977 if (ElementCast<Category>(el)) {
1978 auto category = ElementCast<Category>(el);
1979 if (!category->component.empty()) {
1980 components.insert(category->component);
1981 return;
1982 }
1983 }
1984 });
1985
1986 // Check for the payment component
1987 auto apk = apk_;
1988 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
1989 if (auto service = ElementCast<Service>(el)) {
1990 auto host_apdu_action = ElementCast<Action>(FindElement(service,
1991 [&](ManifestExtractor::Element* el) -> bool {
1992 if (auto action = ElementCast<Action>(el)) {
1993 return (action->component == "host-apdu");
1994 }
1995 return false;
1996 }));
1997
1998 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
1999 [&](ManifestExtractor::Element* el) -> bool {
2000 if (auto action = ElementCast<Action>(el)) {
2001 return (action->component == "offhost-apdu");
2002 }
2003 return false;
2004 }));
2005
2006 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2007 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2008 if (auto meta_data = ElementCast<MetaData>(el)) {
2009 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2010 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2011 && offhost_apdu_action)) {
2012
2013 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002014 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002015 return;
2016 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002017 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002018 if (!resource) {
2019 return;
2020 }
2021
2022 // Look for the payment category on an <aid-group> element
2023 auto& root = resource.get()->root;
2024 if ((host_apdu_action && root->name == "host-apdu-service")
2025 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2026
2027 for (auto& child : root->GetChildElements()) {
2028 if (child->name == "aid-group") {
2029 auto category = FindAttribute(child, CATEGORY_ATTR);
2030 if (category && category->value == "payment") {
2031 components.insert("payment");
2032 return;
2033 }
2034 }
2035 }
2036 }
2037 }
2038 }
2039 });
2040 }
2041 });
2042
2043 // Print the components types if they are present
2044 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2045 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002046 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002047 }
2048 };
2049
2050 PrintComponent("app-widget");
2051 PrintComponent("device-admin");
2052 PrintComponent("ime");
2053 PrintComponent("wallpaper");
2054 PrintComponent("accessibility");
2055 PrintComponent("print-service");
2056 PrintComponent("payment");
2057 PrintComponent("search");
2058 PrintComponent("document-provider");
2059 PrintComponent("launcher");
2060 PrintComponent("notification-listener");
2061 PrintComponent("dream");
2062 PrintComponent("camera");
2063 PrintComponent("camera-secure");
2064
2065 // Print presence of main activity
2066 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002067 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002068 }
2069
2070 // Print presence of activities, recivers, and services with no special components
2071 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2072 if (auto activity = ElementCast<Activity>(el)) {
2073 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002074 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002075 return true;
2076 }
2077 }
2078 return false;
2079 });
2080
2081 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2082 if (auto receiver = ElementCast<Receiver>(el)) {
2083 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002084 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002085 return true;
2086 }
2087 }
2088 return false;
2089 });
2090
2091 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2092 if (auto service = ElementCast<Service>(el)) {
2093 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002094 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002095 return true;
2096 }
2097 }
2098 return false;
2099 });
2100
2101 // Print the supported screens
2102 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2103 [&](ManifestExtractor::Element* el) -> bool {
2104 return ElementCast<SupportsScreen>(el) != nullptr;
2105 }));
2106
2107 if (screen) {
2108 screen->PrintScreens(printer, target_sdk_);
2109 } else {
2110 // Print the default supported screens
2111 SupportsScreen default_screens;
2112 default_screens.PrintScreens(printer, target_sdk_);
2113 }
2114
2115 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002116 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002117 for (auto& config : locales_) {
2118 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002119 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002120 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002121 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002122 }
2123 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002124 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002125
2126 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002127 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002128 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002129 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002130 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002131 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002132
2133 // Print the supported architectures of the app
2134 std::set<std::string> architectures;
2135 auto it = apk_->GetFileCollection()->Iterator();
2136 while (it->HasNext()) {
2137 auto file_path = it->Next()->GetSource().path;
2138
2139
2140 size_t pos = file_path.find("lib/");
2141 if (pos != std::string::npos) {
2142 file_path = file_path.substr(pos + 4);
2143 pos = file_path.find("/");
2144 if (pos != std::string::npos) {
2145 file_path = file_path.substr(0, pos);
2146 }
2147
2148 architectures.insert(file_path);
2149 }
2150 }
2151
2152 // Determine if the application has multiArch supports
2153 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2154 if (auto application = ElementCast<Application>(el)) {
2155 return application->has_multi_arch;
2156 }
2157 return false;
2158 });
2159
2160 bool output_alt_native_code = false;
2161 // A multiArch package is one that contains 64-bit and
2162 // 32-bit versions of native code and expects 3rd-party
2163 // apps to load these native code libraries. Since most
2164 // 64-bit systems also support 32-bit apps, the apps
2165 // loading this multiArch package's code may be either
2166 if (has_multi_arch) {
2167 // If this is a multiArch package, report the 64-bit
2168 // version only. Then as a separate entry, report the
2169 // rest.
2170 //
2171 // If we report the 32-bit architecture, this APK will
2172 // be installed on a 32-bit device, causing a large waste
2173 // of bandwidth and disk space. This assumes that
2174 // the developer of the multiArch package has also
2175 // made a version that is 32-bit only.
2176 const std::string kIntel64 = "x86_64";
2177 const std::string kArm64 = "arm64-v8a";
2178
2179 auto arch = architectures.find(kIntel64);
2180 if (arch == architectures.end()) {
2181 arch = architectures.find(kArm64);
2182 }
2183
2184 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002185 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002186 architectures.erase(arch);
2187 output_alt_native_code = true;
2188 }
2189 }
2190
2191 if (architectures.size() > 0) {
2192 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002193 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002194 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002195 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002196 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002197 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002198 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002199 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002200 }
2201
2202 return true;
2203}
2204
2205/**
2206 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2207 * pointer.
2208 **/
2209template<typename T>
2210T* ElementCast(ManifestExtractor::Element* element) {
2211 if (element == nullptr) {
2212 return nullptr;
2213 }
2214
2215 const std::unordered_map<std::string, bool> kTagCheck = {
2216 {"action", std::is_base_of<Action, T>::value},
2217 {"activity", std::is_base_of<Activity, T>::value},
2218 {"application", std::is_base_of<Application, T>::value},
2219 {"category", std::is_base_of<Category, T>::value},
2220 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2221 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2222 {"input-type", std::is_base_of<InputType, T>::value},
2223 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2224 {"meta-data", std::is_base_of<MetaData, T>::value},
2225 {"manifest", std::is_base_of<Manifest, T>::value},
2226 {"original-package", std::is_base_of<OriginalPackage, T>::value},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002227 {"overlay", std::is_base_of<Overlay, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002228 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2229 {"permission", std::is_base_of<Permission, T>::value},
2230 {"provider", std::is_base_of<Provider, T>::value},
2231 {"receiver", std::is_base_of<Receiver, T>::value},
2232 {"screen", std::is_base_of<Screen, T>::value},
2233 {"service", std::is_base_of<Service, T>::value},
2234 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2235 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2236 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2237 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2238 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2239 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2240 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2241 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2242 {"uses-package", std::is_base_of<UsesPackage, T>::value},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002243 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2244 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
2245 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002246 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2247 };
2248
2249 auto check = kTagCheck.find(element->tag());
2250 if (check != kTagCheck.end() && check->second) {
2251 return static_cast<T*>(element);
2252 }
2253 return nullptr;
2254}
2255
2256template<typename T>
2257std::unique_ptr<T> CreateType() {
2258 return std::move(util::make_unique<T>());
2259}
2260
2261std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2262 ManifestExtractor* extractor, xml::Element* el) {
2263 const std::unordered_map<std::string,
2264 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2265 kTagCheck = {
2266 {"action", &CreateType<Action>},
2267 {"activity", &CreateType<Activity>},
2268 {"application", &CreateType<Application>},
2269 {"category", &CreateType<Category>},
2270 {"compatible-screens", &CreateType<CompatibleScreens>},
2271 {"feature-group", &CreateType<FeatureGroup>},
2272 {"input-type", &CreateType<InputType>},
2273 {"intent-filter",&CreateType<IntentFilter>},
2274 {"manifest", &CreateType<Manifest>},
2275 {"meta-data", &CreateType<MetaData>},
2276 {"original-package", &CreateType<OriginalPackage>},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002277 {"overlay", &CreateType<Overlay>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002278 {"package-verifier", &CreateType<PackageVerifier>},
2279 {"permission", &CreateType<Permission>},
2280 {"provider", &CreateType<Provider>},
2281 {"receiver", &CreateType<Receiver>},
2282 {"screen", &CreateType<Screen>},
2283 {"service", &CreateType<Service>},
2284 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2285 {"supports-input", &CreateType<SupportsInput>},
2286 {"supports-screens", &CreateType<SupportsScreen>},
2287 {"uses-configuration", &CreateType<UsesConfiguarion>},
2288 {"uses-feature", &CreateType<UsesFeature>},
2289 {"uses-permission", &CreateType<UsesPermission>},
2290 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2291 {"uses-library", &CreateType<UsesLibrary>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002292 {"static-library", &CreateType<StaticLibrary>},
2293 {"uses-static-library", &CreateType<UsesStaticLibrary>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002294 {"uses-package", &CreateType<UsesPackage>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002295 {"additional-certificate", &CreateType<AdditionalCertificate>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002296 {"uses-sdk", &CreateType<UsesSdkBadging>},
2297 };
2298
2299 // Attempt to map the xml tag to a element inflater
2300 std::unique_ptr<ManifestExtractor::Element> element;
2301 auto check = kTagCheck.find(el->name);
2302 if (check != kTagCheck.end()) {
2303 element = check->second();
2304 } else {
2305 element = util::make_unique<ManifestExtractor::Element>();
2306 }
2307
2308 element->extractor_ = extractor;
2309 element->tag_ = el->name;
2310 element->Extract(el);
2311 return element;
2312}
2313
2314std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2315 auto element = ManifestExtractor::Element::Inflate(this, el);
2316 parent_stack_.insert(parent_stack_.begin(), element.get());
2317
2318 // Process the element and recursively visit the children
2319 for (xml::Element* child : el->GetChildElements()) {
2320 auto v = Visit(child);
2321 element->AddChild(v);
2322 }
2323
2324 parent_stack_.erase(parent_stack_.begin());
2325 return element;
2326}
2327
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002328
Ryan Mitchell214846d2018-09-19 16:57:01 -07002329int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2330 IDiagnostics* diag) {
2331 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002332 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002333}
2334
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002335} // namespace aapt