blob: 53d9ffe2194993cb86e08a6bc31149a8eccbf476 [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";
Ryan Mitchell95f02422020-04-30 10:25:53 -070094constexpr int kCurrentDevelopmentVersion = 10000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070095
96/** Retrieves the attribute of the element with the specified attribute resource id. */
97static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
98 for (auto& a : el->attributes) {
99 if (a.compiled_attribute && a.compiled_attribute.value().id) {
100 if (a.compiled_attribute.value().id.value() == resd_id) {
101 return std::move(&a);
102 }
103 }
104 }
105 return nullptr;
106}
107
108/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
109static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
110 const std::string &name) {
111 return el->FindAttribute(package, name);
112}
113
114class CommonFeatureGroup;
115
116class ManifestExtractor {
117 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700118
Ryan Mitchell214846d2018-09-19 16:57:01 -0700119 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700120 : apk_(apk), options_(options) { }
121
122 class Element {
123 public:
124 Element() = default;
125 virtual ~Element() = default;
126
127 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
128
129 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700130 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700131
132 /** Adds an element to the list of children of the element. */
133 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
134
135 /** Retrieves the list of children of the element. */
136 const std::vector<std::unique_ptr<Element>>& children() const {
137 return children_;
138 }
139
140 /** Retrieves the extracted xml element tag. */
141 const std::string tag() const {
142 return tag_;
143 }
144
145 protected:
146 ManifestExtractor* extractor() const {
147 return extractor_;
148 }
149
150 /** Retrieves and stores the information extracted from the xml element. */
151 virtual void Extract(xml::Element* el) { }
152
153 /*
154 * Retrieves a configuration value of the resource entry that best matches the specified
155 * configuration.
156 */
157 static Value* BestConfigValue(ResourceEntry* entry,
158 const ConfigDescription& match) {
159 if (!entry) {
160 return nullptr;
161 }
162
163 // Determine the config that best matches the desired config
164 ResourceConfigValue* best_value = nullptr;
165 for (auto& value : entry->values) {
166 if (!value->config.match(match)) {
167 continue;
168 }
169
170 if (best_value != nullptr) {
171 if (!value->config.isBetterThan(best_value->config, &match)) {
172 if (value->config.compare(best_value->config) != 0) {
173 continue;
174 }
175 }
176 }
177
178 best_value = value.get();
179 }
180
181 // The entry has no values
182 if (!best_value) {
183 return nullptr;
184 }
185
186 return best_value->value.get();
187 }
188
189 /** Retrieves the resource assigned to the specified resource id if one exists. */
190 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
191 const ConfigDescription& config = DummyConfig()) {
192 if (table) {
193 for (auto& package : table->packages) {
194 if (package->id && package->id.value() == res_id.package_id()) {
195 for (auto& type : package->types) {
196 if (type->id && type->id.value() == res_id.type_id()) {
197 for (auto& entry : type->entries) {
198 if (entry->id && entry->id.value() == res_id.entry_id()) {
199 if (auto value = BestConfigValue(entry.get(), config)) {
200 return value;
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 return nullptr;
210 }
211
212 /** Attempts to resolve the reference to a non-reference value. */
213 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DummyConfig()) {
214 const int kMaxIterations = 40;
215 int i = 0;
216 while (ref && ref->id && i++ < kMaxIterations) {
217 auto table = extractor_->apk_->GetResourceTable();
218 if (auto value = FindValueById(table, ref->id.value(), config)) {
219 if (ValueCast<Reference>(value)) {
220 ref = ValueCast<Reference>(value);
221 } else {
222 return value;
223 }
224 }
225 }
226 return nullptr;
227 }
228
229 /**
230 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
231 * this will attempt to resolve the reference to an integer value.
232 **/
233 int32_t* GetAttributeInteger(xml::Attribute* attr,
234 const ConfigDescription& config = DummyConfig()) {
235 if (attr != nullptr) {
236 if (attr->compiled_value) {
237 // Resolve references using the dummy configuration
238 Value* value = attr->compiled_value.get();
239 if (ValueCast<Reference>(value)) {
240 value = ResolveReference(ValueCast<Reference>(value), config);
241 } else {
242 value = attr->compiled_value.get();
243 }
244 // Retrieve the integer data if possible
245 if (value != nullptr) {
246 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
247 return (int32_t*) &intValue->value.data;
248 }
249 }
250 }
251 }
252 return nullptr;
253 }
254
255 /**
256 * A version of GetAttributeInteger that returns a default integer if the attribute does not
257 * exist or cannot be resolved to an integer value.
258 **/
259 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
260 const ConfigDescription& config = DummyConfig()) {
261 auto value = GetAttributeInteger(attr, config);
262 if (value) {
263 return *value;
264 }
265 return def;
266 }
267
268 /**
269 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
270 * this will attempt to resolve the reference to a string value.
271 **/
272 const std::string* GetAttributeString(xml::Attribute* attr,
273 const ConfigDescription& config = DummyConfig()) {
274 if (attr != nullptr) {
275 if (attr->compiled_value) {
276 // Resolve references using the dummy configuration
277 Value* value = attr->compiled_value.get();
278 if (ValueCast<Reference>(value)) {
279 value = ResolveReference(ValueCast<Reference>(value), config);
280 } else {
281 value = attr->compiled_value.get();
282 }
283
284 // Retrieve the string data of the value if possible
285 if (value != nullptr) {
286 if (String* intValue = ValueCast<String>(value)) {
287 return &(*intValue->value);
288 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
289 return &(*rawValue->value);
290 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
291 return &(*strValue->path);
292 }
293 }
294 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700295
296 if (!attr->value.empty()) {
297 return &attr->value;
298 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700299 }
300 return nullptr;
301 }
302
303 /**
304 * A version of GetAttributeString that returns a default string if the attribute does not
305 * exist or cannot be resolved to an string value.
306 **/
307 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
308 const ConfigDescription& config = DummyConfig()) {
309 auto value = GetAttributeString(attr, config);
310 if (value) {
311 return *value;
312 }
313 return def;
314 }
315
316 private:
317 ManifestExtractor* extractor_;
318 std::vector<std::unique_ptr<Element>> children_;
319 std::string tag_;
320 };
321
322 friend Element;
323
324 /** Creates a default configuration used to retrieve resources. */
325 static ConfigDescription DummyConfig() {
326 ConfigDescription config;
327 config.orientation = android::ResTable_config::ORIENTATION_PORT;
328 config.density = android::ResTable_config::DENSITY_MEDIUM;
Ryan Mitchell95f02422020-04-30 10:25:53 -0700329 config.sdkVersion = kCurrentDevelopmentVersion; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700330 config.screenWidthDp = 320;
331 config.screenHeightDp = 480;
332 config.smallestScreenWidthDp = 320;
333 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
334 return config;
335 }
336
Ryan Mitchell214846d2018-09-19 16:57:01 -0700337 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700338
339 /** Recursively visit the xml element tree and return a processed badging element tree. */
340 std::unique_ptr<Element> Visit(xml::Element* element);
341
342 /** Raises the target sdk value if the min target is greater than the current target. */
343 void RaiseTargetSdk(int32_t min_target) {
344 if (min_target > target_sdk_) {
345 target_sdk_ = min_target;
346 }
347 }
348
349 /**
350 * Retrieves the default feature group that features are added into when <uses-feature>
351 * are not in a <feature-group> element.
352 **/
353 CommonFeatureGroup* GetCommonFeatureGroup() {
354 return commonFeatureGroup_.get();
355 }
356
357 /**
358 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
359 * used for that density setting.
360 **/
361 const std::map<uint16_t, ConfigDescription> densities() const {
362 return densities_;
363 }
364
365 /**
366 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
367 * would be used for that locale setting.
368 **/
369 const std::map<std::string, ConfigDescription> locales() const {
370 return locales_;
371 }
372
373 /** Retrieves the current stack of parent during data extraction. */
374 const std::vector<Element*> parent_stack() const {
375 return parent_stack_;
376 }
377
378 int32_t target_sdk() const {
379 return target_sdk_;
380 }
381
382 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700383 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700384
385 private:
386 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
387 std::map<std::string, ConfigDescription> locales_;
388 std::map<uint16_t, ConfigDescription> densities_;
389 std::vector<Element*> parent_stack_;
390 int32_t target_sdk_ = 0;
391};
392
393template<typename T> T* ElementCast(ManifestExtractor::Element* element);
394
395/** Recurs through the children of the specified root in depth-first order. */
396static void ForEachChild(ManifestExtractor::Element* root,
397 std::function<void(ManifestExtractor::Element*)> f) {
398 for (auto& child : root->children()) {
399 f(child.get());
400 ForEachChild(child.get(), f);
401 }
402}
403
404/**
405 * Checks the element and its recursive children for an element that makes the specified
406 * conditional function return true. Returns the first element that makes the conditional function
407 * return true.
408 **/
409static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
410 std::function<bool(ManifestExtractor::Element*)> f) {
411 if (f(root)) {
412 return root;
413 }
414 for (auto& child : root->children()) {
415 if (auto b2 = FindElement(child.get(), f)) {
416 return b2;
417 }
418 }
419 return nullptr;
420}
421
422/** Represents the <manifest> elements **/
423class Manifest : public ManifestExtractor::Element {
424 public:
425 Manifest() = default;
426 std::string package;
427 int32_t versionCode;
428 std::string versionName;
429 const std::string* split = nullptr;
430 const std::string* platformVersionName = nullptr;
431 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700432 const int32_t* platformVersionNameInt = nullptr;
433 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700434 const int32_t* compilesdkVersion = nullptr;
435 const std::string* compilesdkVersionCodename = nullptr;
436 const int32_t* installLocation = nullptr;
437
438 void Extract(xml::Element* manifest) override {
439 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
440 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
441 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
442 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
443
444 // Extract the platform build info
445 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
446 "platformBuildVersionName"));
447 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
448 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700449 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
450 "platformBuildVersionName"));
451 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
452 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700453
454 // Extract the compile sdk info
455 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
456 compilesdkVersionCodename = GetAttributeString(
457 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
458 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
459 }
460
Ryan Mitchell214846d2018-09-19 16:57:01 -0700461 void Print(text::Printer* printer) override {
462 printer->Print(StringPrintf("package: name='%s' ", package.data()));
463 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700464 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700465 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700466
467 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700468 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700469 }
470 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700471 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700472 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700473 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
474 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700475 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700476 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700477 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700478 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
479 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700480 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700481 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700482 }
483 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700484 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700485 compilesdkVersionCodename->data()));
486 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700487 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700488
489 if (installLocation) {
490 switch (*installLocation) {
491 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700492 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700493 break;
494 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700495 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700496 break;
497 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700498 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700499 break;
500 default:
501 break;
502 }
503 }
504 }
505};
506
507/** Represents <application> elements. **/
508class Application : public ManifestExtractor::Element {
509 public:
510 Application() = default;
511 std::string label;
512 std::string icon;
513 std::string banner;
514 int32_t is_game;
515 int32_t debuggable;
516 int32_t test_only;
517 bool has_multi_arch;
518
519 /** Mapping from locales to app names. */
520 std::map<std::string, std::string> locale_labels;
521
522 /** Mapping from densities to app icons. */
523 std::map<uint16_t, std::string> density_icons;
524
525 void Extract(xml::Element* element) override {
526 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
527 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
528 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
529 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
530 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
531 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
532
533 // We must search by name because the multiArch flag hasn't been API
534 // frozen yet.
535 has_multi_arch = (GetAttributeIntegerDefault(
536 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
537
538 // Retrieve the app names for every locale the app supports
539 auto attr = FindAttribute(element, LABEL_ATTR);
540 for (auto& config : extractor()->locales()) {
541 if (auto label = GetAttributeString(attr, config.second)) {
542 if (label) {
543 locale_labels.insert(std::make_pair(config.first, *label));
544 }
545 }
546 }
547
548 // Retrieve the icons for the densities the app supports
549 attr = FindAttribute(element, ICON_ATTR);
550 for (auto& config : extractor()->densities()) {
551 if (auto resource = GetAttributeString(attr, config.second)) {
552 if (resource) {
553 density_icons.insert(std::make_pair(config.first, *resource));
554 }
555 }
556 }
557 }
558
Ryan Mitchell214846d2018-09-19 16:57:01 -0700559 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700560 // Print the labels for every locale
561 for (auto p : locale_labels) {
562 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700563 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700564 android::ResTable::normalizeForOutput(p.second.data())
565 .c_str()));
566 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700567 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700568 android::ResTable::normalizeForOutput(p.second.data())
569 .c_str()));
570 }
571 }
572
573 // Print the icon paths for every density
574 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700575 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700576 }
577
578 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700579 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700580 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700581 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700582 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700583 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700584 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700585 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700586
587 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700588 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700589 }
590 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700591 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700592 }
593 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700594 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700595 }
596 }
597};
598
599/** Represents <uses-sdk> elements. **/
600class UsesSdkBadging : public ManifestExtractor::Element {
601 public:
602 UsesSdkBadging() = default;
603 const int32_t* min_sdk = nullptr;
604 const std::string* min_sdk_name = nullptr;
605 const int32_t* max_sdk = nullptr;
606 const int32_t* target_sdk = nullptr;
607 const std::string* target_sdk_name = nullptr;
608
609 void Extract(xml::Element* element) override {
610 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
611 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
612 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
613 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
614 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
615
616 // Detect the target sdk of the element
617 if ((min_sdk_name && *min_sdk_name == "Donut")
618 || (target_sdk_name && *target_sdk_name == "Donut")) {
619 extractor()->RaiseTargetSdk(4);
620 }
621 if (min_sdk) {
622 extractor()->RaiseTargetSdk(*min_sdk);
623 }
624 if (target_sdk) {
625 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700626 } else if (target_sdk_name) {
627 extractor()->RaiseTargetSdk(kCurrentDevelopmentVersion);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700628 }
629 }
630
Ryan Mitchell214846d2018-09-19 16:57:01 -0700631 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700632 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700633 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700634 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700636 }
637 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700638 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700639 }
640 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700641 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700642 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700643 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700644 }
645 }
646};
647
648/** Represents <uses-configuration> elements. **/
649class UsesConfiguarion : public ManifestExtractor::Element {
650 public:
651 UsesConfiguarion() = default;
652 int32_t req_touch_screen = 0;
653 int32_t req_keyboard_type = 0;
654 int32_t req_hard_keyboard = 0;
655 int32_t req_navigation = 0;
656 int32_t req_five_way_nav = 0;
657
658 void Extract(xml::Element* element) override {
659 req_touch_screen = GetAttributeIntegerDefault(
660 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
661 req_keyboard_type = GetAttributeIntegerDefault(
662 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
663 req_hard_keyboard = GetAttributeIntegerDefault(
664 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
665 req_navigation = GetAttributeIntegerDefault(
666 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
667 req_five_way_nav = GetAttributeIntegerDefault(
668 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
669 }
670
Ryan Mitchell214846d2018-09-19 16:57:01 -0700671 void Print(text::Printer* printer) override {
672 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700673 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700674 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700675 }
676 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700677 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700678 }
679 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700680 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700681 }
682 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700683 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700684 }
685 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700686 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700687 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700688 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700689 }
690};
691
692/** Represents <supports-screen> elements. **/
693class SupportsScreen : public ManifestExtractor::Element {
694 public:
695 SupportsScreen() = default;
696 int32_t small_screen = 1;
697 int32_t normal_screen = 1;
698 int32_t large_screen = 1;
699 int32_t xlarge_screen = 1;
700 int32_t any_density = 1;
701 int32_t requires_smallest_width_dp = 0;
702 int32_t compatible_width_limit_dp = 0;
703 int32_t largest_width_limit_dp = 0;
704
705 void Extract(xml::Element* element) override {
706 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
707 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
708 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
709 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
710 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
711
712 requires_smallest_width_dp = GetAttributeIntegerDefault(
713 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
714 compatible_width_limit_dp = GetAttributeIntegerDefault(
715 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
716 largest_width_limit_dp = GetAttributeIntegerDefault(
717 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
718
719 // For modern apps, if screen size buckets haven't been specified
720 // but the new width ranges have, then infer the buckets from them.
721 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
722 && requires_smallest_width_dp > 0) {
723 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
724 : requires_smallest_width_dp;
725 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
726 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
727 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
728 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
729 }
730 }
731
Ryan Mitchell214846d2018-09-19 16:57:01 -0700732 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700733 int32_t small_screen_temp = small_screen;
734 int32_t normal_screen_temp = normal_screen;
735 int32_t large_screen_temp = large_screen;
736 int32_t xlarge_screen_temp = xlarge_screen;
737 int32_t any_density_temp = any_density;
738
739 // Determine default values for any unspecified screen sizes,
740 // based on the target SDK of the package. As of 4 (donut)
741 // the screen size support was introduced, so all default to
742 // enabled.
743 if (small_screen_temp > 0) {
744 small_screen_temp = target_sdk >= 4 ? -1 : 0;
745 }
746 if (normal_screen_temp > 0) {
747 normal_screen_temp = -1;
748 }
749 if (large_screen_temp > 0) {
750 large_screen_temp = target_sdk >= 4 ? -1 : 0;
751 }
752 if (xlarge_screen_temp > 0) {
753 // Introduced in Gingerbread.
754 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
755 }
756 if (any_density_temp > 0) {
757 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
758 || compatible_width_limit_dp > 0) ? -1 : 0;
759 }
760
761 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700762 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700763 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700764 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700765 }
766 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700767 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700768 }
769 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700770 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700771 }
772 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700773 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700774 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700775 printer->Print("\n");
776 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700777 (any_density_temp ) ? "true" : "false"));
778 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700779 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700780 }
781 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700782 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700783 }
784 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700785 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700786 }
787 }
788};
789
790/** Represents <feature-group> elements. **/
791class FeatureGroup : public ManifestExtractor::Element {
792 public:
793 FeatureGroup() = default;
794 std::string label;
795 int32_t open_gles_version = 0;
796
797 void Extract(xml::Element* element) override {
798 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
799 }
800
Ryan Mitchell214846d2018-09-19 16:57:01 -0700801 virtual void PrintGroup(text::Printer* printer) {
802 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700803 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700804 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700805 }
806
807 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700808 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700809 (feature.second.required ? "" : "-not-required"),
810 feature.first.data()));
811 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700812 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700813 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700814 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700815 }
816 }
817
818 /** Adds a feature to the feature group. */
819 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
820 features_.insert(std::make_pair(name, Feature{ required, version }));
821 if (required) {
822 if (name == "android.hardware.camera.autofocus" ||
823 name == "android.hardware.camera.flash") {
824 AddFeature("android.hardware.camera", true);
825 } else if (name == "android.hardware.location.gps" ||
826 name == "android.hardware.location.network") {
827 AddFeature("android.hardware.location", true);
828 } else if (name == "android.hardware.faketouch.multitouch") {
829 AddFeature("android.hardware.faketouch", true);
830 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
831 name == "android.hardware.faketouch.multitouch.jazzhands") {
832 AddFeature("android.hardware.faketouch.multitouch", true);
833 AddFeature("android.hardware.faketouch", true);
834 } else if (name == "android.hardware.touchscreen.multitouch") {
835 AddFeature("android.hardware.touchscreen", true);
836 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
837 name == "android.hardware.touchscreen.multitouch.jazzhands") {
838 AddFeature("android.hardware.touchscreen.multitouch", true);
839 AddFeature("android.hardware.touchscreen", true);
840 } else if (name == "android.hardware.opengles.aep") {
841 const int kOpenGLESVersion31 = 0x00030001;
842 if (kOpenGLESVersion31 > open_gles_version) {
843 open_gles_version = kOpenGLESVersion31;
844 }
845 }
846 }
847 }
848
849 /** Returns true if the feature group has the given feature. */
850 virtual bool HasFeature(const std::string& name) {
851 return features_.find(name) != features_.end();
852 }
853
854 /** Merges the features of another feature group into this group. */
855 void Merge(FeatureGroup* group) {
856 open_gles_version = std::max(open_gles_version, group->open_gles_version);
857 for (auto& feature : group->features_) {
858 features_.insert(feature);
859 }
860 }
861
862 protected:
863 struct Feature {
864 public:
865 bool required = false;
866 int32_t version = -1;
867 };
868
869 /* Mapping of feature names to their properties. */
870 std::map<std::string, Feature> features_;
871};
872
873/**
874 * Represents the default feature group for the application if no <feature-group> elements are
875 * present in the manifest.
876 **/
877class CommonFeatureGroup : public FeatureGroup {
878 public:
879 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700880 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700881 FeatureGroup::PrintGroup(printer);
882
883 // Also print the implied features
884 for (auto feature : implied_features_) {
885 if (features_.find(feature.first) == features_.end()) {
886 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700887 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
888 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700889 feature.first.data()));
890
891 // Print the reasons as a sentence
892 size_t count = 0;
893 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700894 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700895 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700896 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700897 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 }
900 count++;
901 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700902 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700903 }
904 }
905 }
906
907 /** Returns true if the feature group has the given feature. */
908 bool HasFeature(const std::string& name) override {
909 return FeatureGroup::HasFeature(name)
910 || implied_features_.find(name) != implied_features_.end();
911 }
912
913 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
914 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
915 auto entry = implied_features_.find(name);
916 if (entry == implied_features_.end()) {
917 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
918 entry = implied_features_.find(name);
919 }
920
921 // A non-sdk 23 implied feature takes precedence.
922 if (entry->second.implied_from_sdk_k23 && !sdk23) {
923 entry->second.implied_from_sdk_k23 = false;
924 }
925
926 entry->second.reasons.insert(reason);
927 }
928
929 /**
930 * Adds a feature to a set of implied features for all features that are implied by the presence
931 * of the permission.
932 **/
933 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
934 if (name == "android.permission.CAMERA") {
935 addImpliedFeature("android.hardware.camera",
936 StringPrintf("requested %s permission", name.data()),
937 sdk23);
938
939 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
940 if (targetSdk < SDK_LOLLIPOP) {
941 addImpliedFeature("android.hardware.location.gps",
942 StringPrintf("requested %s permission", name.data()),
943 sdk23);
944 addImpliedFeature("android.hardware.location.gps",
945 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
946 sdk23);
947 }
948 addImpliedFeature("android.hardware.location",
949 StringPrintf("requested %s permission", name.data()),
950 sdk23);
951
952 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
953 if (targetSdk < SDK_LOLLIPOP) {
954 addImpliedFeature("android.hardware.location.network",
955 StringPrintf("requested %s permission", name.data()),
956 sdk23);
957 addImpliedFeature("android.hardware.location.network",
958 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
959 sdk23);
960 }
961 addImpliedFeature("android.hardware.location",
962 StringPrintf("requested %s permission", name.data()),
963 sdk23);
964
965 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
966 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
967 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
968 addImpliedFeature("android.hardware.location",
969 StringPrintf("requested %s permission", name.data()),
970 sdk23);
971
972 } else if (name == "android.permission.BLUETOOTH" ||
973 name == "android.permission.BLUETOOTH_ADMIN") {
974 if (targetSdk > SDK_DONUT) {
975 addImpliedFeature("android.hardware.bluetooth",
976 StringPrintf("requested %s permission", name.data()),
977 sdk23);
978 addImpliedFeature("android.hardware.bluetooth",
979 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
980 sdk23);
981 }
982
983 } else if (name == "android.permission.RECORD_AUDIO") {
984 addImpliedFeature("android.hardware.microphone",
985 StringPrintf("requested %s permission", name.data()),
986 sdk23);
987
988 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
989 name == "android.permission.CHANGE_WIFI_STATE" ||
990 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
991 addImpliedFeature("android.hardware.wifi",
992 StringPrintf("requested %s permission", name.data()),
993 sdk23);
994
995 } else if (name == "android.permission.CALL_PHONE" ||
996 name == "android.permission.CALL_PRIVILEGED" ||
997 name == "android.permission.MODIFY_PHONE_STATE" ||
998 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
999 name == "android.permission.READ_SMS" ||
1000 name == "android.permission.RECEIVE_SMS" ||
1001 name == "android.permission.RECEIVE_MMS" ||
1002 name == "android.permission.RECEIVE_WAP_PUSH" ||
1003 name == "android.permission.SEND_SMS" ||
1004 name == "android.permission.WRITE_APN_SETTINGS" ||
1005 name == "android.permission.WRITE_SMS") {
1006 addImpliedFeature("android.hardware.telephony",
1007 "requested a telephony permission",
1008 sdk23);
1009 }
1010 }
1011
1012 private:
1013 /**
1014 * Represents a feature that has been automatically added due to a pre-requisite or for some
1015 * other reason.
1016 */
1017 struct ImpliedFeature {
1018 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1019
1020 /** List of human-readable reasons for why this feature was implied. */
1021 std::set<std::string> reasons;
1022
1023 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1024 bool implied_from_sdk_k23;
1025 };
1026
1027 /* Mapping of implied feature names to their properties. */
1028 std::map<std::string, ImpliedFeature> implied_features_;
1029};
1030
1031/** Represents <uses-feature> elements. **/
1032class UsesFeature : public ManifestExtractor::Element {
1033 public:
1034 UsesFeature() = default;
1035 void Extract(xml::Element* element) override {
1036 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1037 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1038 bool required = GetAttributeIntegerDefault(
1039 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1040 int32_t version = GetAttributeIntegerDefault(
1041 FindAttribute(element, kAndroidNamespace, "version"), 0);
1042
1043 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1044 // common feature group
1045 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1046 if (!feature_group) {
1047 feature_group = extractor()->GetCommonFeatureGroup();
1048 } else {
1049 // All features in side of <feature-group> elements are required.
1050 required = true;
1051 }
1052
1053 if (name) {
1054 feature_group->AddFeature(*name, required, version);
1055 } else if (gl) {
1056 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1057 }
1058 }
1059};
1060
1061/** Represents <uses-permission> elements. **/
1062class UsesPermission : public ManifestExtractor::Element {
1063 public:
1064 UsesPermission() = default;
1065 std::string name;
1066 std::string requiredFeature;
1067 std::string requiredNotFeature;
1068 int32_t required = true;
1069 int32_t maxSdkVersion = -1;
1070
1071 void Extract(xml::Element* element) override {
1072 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1073 requiredFeature = GetAttributeStringDefault(
1074 FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1075 requiredNotFeature = GetAttributeStringDefault(
1076 FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1077 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1078 maxSdkVersion = GetAttributeIntegerDefault(
1079 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1080
1081 if (!name.empty()) {
1082 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1083 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1084 }
1085 }
1086
Ryan Mitchell214846d2018-09-19 16:57:01 -07001087 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001088 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001089 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001090 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001091 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001092 }
1093 if (!requiredFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001094 printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001095 }
1096 if (!requiredNotFeature.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001097 printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001098 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001099 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001100 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001101 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001102 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001103 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001104 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001105 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001106 }
1107 }
1108 }
1109
Ryan Mitchell214846d2018-09-19 16:57:01 -07001110 void PrintImplied(text::Printer* printer, const std::string& reason) {
1111 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001112 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001113 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001114 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001115 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001116 }
1117};
1118
1119/** Represents <uses-permission-sdk-23> elements. **/
1120class UsesPermissionSdk23 : public ManifestExtractor::Element {
1121 public:
1122 UsesPermissionSdk23() = default;
1123 const std::string* name = nullptr;
1124 const int32_t* maxSdkVersion = nullptr;
1125
1126 void Extract(xml::Element* element) override {
1127 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1128 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1129
1130 if (name) {
1131 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1132 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1133 }
1134 }
1135
Ryan Mitchell214846d2018-09-19 16:57:01 -07001136 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001137 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001138 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001139 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001140 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001141 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001142 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001143 }
1144 }
1145};
1146
1147/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1148class Permission : public ManifestExtractor::Element {
1149 public:
1150 Permission() = default;
1151 std::string name;
1152
1153 void Extract(xml::Element* element) override {
1154 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1155 }
1156
Ryan Mitchell214846d2018-09-19 16:57:01 -07001157 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001158 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001159 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001160 }
1161 }
1162};
1163
1164/** Represents <activity> elements. **/
1165class Activity : public ManifestExtractor::Element {
1166 public:
1167 Activity() = default;
1168 std::string name;
1169 std::string icon;
1170 std::string label;
1171 std::string banner;
1172
1173 bool has_component_ = false;
1174 bool has_launcher_category = false;
1175 bool has_leanback_launcher_category = false;
1176 bool has_main_action = false;
1177
1178 void Extract(xml::Element* element) override {
1179 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1180 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1181 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1182 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1183
1184 // Retrieve the package name from the manifest
1185 std::string package;
1186 for (auto& parent : extractor()->parent_stack()) {
1187 if (auto manifest = ElementCast<Manifest>(parent)) {
1188 package = manifest->package;
1189 break;
1190 }
1191 }
1192
1193 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001194 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001195 if (idx == 0) {
1196 name = package + name;
1197 } else if (idx < 0) {
1198 name = package + "." + name;
1199 }
1200
1201 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1202 if (orientation) {
1203 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1204 int orien = *orientation;
1205 if (orien == 0 || orien == 6 || orien == 8) {
1206 // Requests landscape, sensorLandscape, or reverseLandscape.
1207 common->addImpliedFeature("android.hardware.screen.landscape",
1208 "one or more activities have specified a landscape orientation",
1209 false);
1210 } else if (orien == 1 || orien == 7 || orien == 9) {
1211 // Requests portrait, sensorPortrait, or reversePortrait.
1212 common->addImpliedFeature("android.hardware.screen.portrait",
1213 "one or more activities have specified a portrait orientation",
1214 false);
1215 }
1216 }
1217 }
1218
Ryan Mitchell214846d2018-09-19 16:57:01 -07001219 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001220 // Print whether the activity has the HOME category and a the MAIN action
1221 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001222 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001223 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001224 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001225 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001226 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001227 android::ResTable::normalizeForOutput(label.data()).c_str(),
1228 icon.data()));
1229 }
1230
1231 // Print wether the activity has the HOME category and a the MAIN action
1232 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001233 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001234 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001235 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001236 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001237 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001238 android::ResTable::normalizeForOutput(label.data()).c_str(),
1239 icon.data(), banner.data()));
1240 }
1241 }
1242};
1243
1244/** Represents <intent-filter> elements. */
1245class IntentFilter : public ManifestExtractor::Element {
1246 public:
1247 IntentFilter() = default;
1248};
1249
1250/** Represents <category> elements. */
1251class Category : public ManifestExtractor::Element {
1252 public:
1253 Category() = default;
1254 std::string component = "";
1255
1256 void Extract(xml::Element* element) override {
1257 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1258
1259 auto parent_stack = extractor()->parent_stack();
1260 if (category && ElementCast<IntentFilter>(parent_stack[0])
1261 && ElementCast<Activity>(parent_stack[1])) {
1262 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1263
1264 if (*category == "android.intent.category.LAUNCHER") {
1265 activity->has_launcher_category = true;
1266 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1267 activity->has_leanback_launcher_category = true;
1268 } else if (*category == "android.intent.category.HOME") {
1269 component = "launcher";
1270 }
1271 }
1272 }
1273};
1274
1275/**
1276 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1277 * elements nested within.
1278 **/
1279class Provider : public ManifestExtractor::Element {
1280 public:
1281 Provider() = default;
1282 bool has_required_saf_attributes = false;
1283
1284 void Extract(xml::Element* element) override {
1285 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1286 const int32_t* grant_uri_permissions = GetAttributeInteger(
1287 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1288 const std::string* permission = GetAttributeString(
1289 FindAttribute(element, PERMISSION_ATTR));
1290
1291 has_required_saf_attributes = ((exported && *exported != 0)
1292 && (grant_uri_permissions && *grant_uri_permissions != 0)
1293 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1294 }
1295};
1296
1297/** Represents <receiver> elements. **/
1298class Receiver : public ManifestExtractor::Element {
1299 public:
1300 Receiver() = default;
1301 const std::string* permission = nullptr;
1302 bool has_component = false;
1303
1304 void Extract(xml::Element* element) override {
1305 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1306 }
1307};
1308
1309/**Represents <service> elements. **/
1310class Service : public ManifestExtractor::Element {
1311 public:
1312 Service() = default;
1313 const std::string* permission = nullptr;
1314 bool has_component = false;
1315
1316 void Extract(xml::Element* element) override {
1317 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1318 }
1319};
1320
1321/** Represents <uses-library> elements. **/
1322class UsesLibrary : public ManifestExtractor::Element {
1323 public:
1324 UsesLibrary() = default;
1325 std::string name;
1326 int required;
1327
1328 void Extract(xml::Element* element) override {
1329 auto parent_stack = extractor()->parent_stack();
1330 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1331 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1332 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1333 }
1334 }
1335
Ryan Mitchell214846d2018-09-19 16:57:01 -07001336 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001337 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001338 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001339 (required == 0) ? "-not-required" : "", name.data()));
1340 }
1341 }
1342};
1343
Dianne Hackborn813d7502018-10-02 16:59:46 -07001344/** Represents <static-library> elements. **/
1345class StaticLibrary : public ManifestExtractor::Element {
1346 public:
1347 StaticLibrary() = default;
1348 std::string name;
1349 int version;
1350 int versionMajor;
1351
1352 void Extract(xml::Element* element) override {
1353 auto parent_stack = extractor()->parent_stack();
1354 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1355 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1356 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1357 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1358 }
1359 }
1360
Ryan Mitchell214846d2018-09-19 16:57:01 -07001361 void Print(text::Printer* printer) override {
1362 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001363 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1364 name.data(), version, versionMajor));
1365 }
1366};
1367
1368/** Represents <uses-static-library> elements. **/
1369class UsesStaticLibrary : public ManifestExtractor::Element {
1370 public:
1371 UsesStaticLibrary() = default;
1372 std::string name;
1373 int version;
1374 int versionMajor;
1375 std::vector<std::string> certDigests;
1376
1377 void Extract(xml::Element* element) override {
1378 auto parent_stack = extractor()->parent_stack();
1379 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1380 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1381 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1382 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1383 AddCertDigest(element);
1384 }
1385 }
1386
1387 void AddCertDigest(xml::Element* element) {
1388 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1389 // We allow ":" delimiters in the SHA declaration as this is the format
1390 // emitted by the certtool making it easy for developers to copy/paste.
1391 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1392 if (!digest.empty()) {
1393 certDigests.push_back(digest);
1394 }
1395 }
1396
Ryan Mitchell214846d2018-09-19 16:57:01 -07001397 void Print(text::Printer* printer) override {
1398 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001399 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1400 name.data(), version, versionMajor));
1401 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001402 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001403 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001404 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001405 }
1406};
1407
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001408/** Represents <uses-native-library> elements. **/
1409class UsesNativeLibrary : public ManifestExtractor::Element {
1410 public:
1411 UsesNativeLibrary() = default;
1412 std::string name;
1413 int required;
1414
1415 void Extract(xml::Element* element) override {
1416 auto parent_stack = extractor()->parent_stack();
1417 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1418 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1419 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1420 }
1421 }
1422
1423 void Print(text::Printer* printer) override {
1424 if (!name.empty()) {
1425 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1426 (required == 0) ? "-not-required" : "", name.data()));
1427 }
1428 }
1429};
1430
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001431/**
1432 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1433 * explicitly enable meta data printing.
1434 **/
1435class MetaData : public ManifestExtractor::Element {
1436 public:
1437 MetaData() = default;
1438 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001439 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001440 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001441 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001442 const int* resource_int;
1443
1444 void Extract(xml::Element* element) override {
1445 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001446 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001447 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001448 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001449 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1450 }
1451
Ryan Mitchell214846d2018-09-19 16:57:01 -07001452 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001453 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001454 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001455 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001456 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001457 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001458 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001459 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001460 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001461 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001462 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001463 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001464 }
1465 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001466 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001467 }
1468 }
1469};
1470
1471/**
1472 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1473 * service components.
1474 **/
1475class Action : public ManifestExtractor::Element {
1476 public:
1477 Action() = default;
1478 std::string component = "";
1479
1480 void Extract(xml::Element* element) override {
1481 auto parent_stack = extractor()->parent_stack();
1482 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1483
1484 if (ElementCast<IntentFilter>(parent_stack[0])) {
1485 if (ElementCast<Activity>(parent_stack[1])) {
1486 // Detects the presence of a particular type of activity.
1487 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1488 auto map = std::map<std::string, std::string>({
1489 { "android.intent.action.MAIN" , "main" },
1490 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1491 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1492 });
1493
1494 auto entry = map.find(action);
1495 if (entry != map.end()) {
1496 component = entry->second;
1497 activity->has_component_ = true;
1498 }
1499
1500 if (action == "android.intent.action.MAIN") {
1501 activity->has_main_action = true;
1502 }
1503
1504 } else if (ElementCast<Receiver>(parent_stack[1])) {
1505 // Detects the presence of a particular type of receiver. If the action requires a
1506 // permission, then the receiver element is checked for the permission.
1507 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1508 auto map = std::map<std::string, std::string>({
1509 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1510 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1511 });
1512
1513 auto permissions = std::map<std::string, std::string>({
1514 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1515 });
1516
1517 auto entry = map.find(action);
1518 auto permission = permissions.find(action);
1519 if (entry != map.end() && (permission == permissions.end()
1520 || (receiver->permission && permission->second == *receiver->permission))) {
1521 receiver->has_component = true;
1522 component = entry->second;
1523 }
1524
1525 } else if (ElementCast<Service>(parent_stack[1])) {
1526 // Detects the presence of a particular type of service. If the action requires a
1527 // permission, then the service element is checked for the permission.
1528 Service* service = ElementCast<Service>(parent_stack[1]);
1529 auto map = std::map<std::string, std::string>({
1530 { "android.view.InputMethod" , "ime" },
1531 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1532 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1533 { "android.printservice.PrintService" , "print-service" },
1534 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1535 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1536 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1537 { "android.service.dreams.DreamService" , "dream" },
1538 });
1539
1540 auto permissions = std::map<std::string, std::string>({
1541 { "android.accessibilityservice.AccessibilityService" ,
1542 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1543 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1544 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1545 "android.permission.BIND_NFC_SERVICE" },
1546 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1547 "android.permission.BIND_NFC_SERVICE" },
1548 { "android.service.notification.NotificationListenerService" ,
1549 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1550 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1551 });
1552
1553 auto entry = map.find(action);
1554 auto permission = permissions.find(action);
1555 if (entry != map.end() && (permission == permissions.end()
1556 || (service->permission && permission->second == *service->permission))) {
1557 service->has_component= true;
1558 component = entry->second;
1559 }
1560
1561 } else if (ElementCast<Provider>(parent_stack[1])) {
1562 // Detects the presence of a particular type of receiver. If the provider requires a
1563 // permission, then the provider element is checked for the permission.
1564 // Detect whether this action
1565 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1566 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1567 && provider->has_required_saf_attributes) {
1568 component = "document-provider";
1569 }
1570 }
1571 }
1572
1573 // Represents a searchable interface
1574 if (action == "android.intent.action.SEARCH") {
1575 component = "search";
1576 }
1577 }
1578};
1579
1580/**
1581 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1582 **/
1583class SupportsInput : public ManifestExtractor::Element {
1584 public:
1585 SupportsInput() = default;
1586 std::vector<std::string> inputs;
1587
Ryan Mitchell214846d2018-09-19 16:57:01 -07001588 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001589 const size_t size = inputs.size();
1590 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001591 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001592 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001593 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001594 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001595 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001596 }
1597 }
1598};
1599
1600/** Represents <input-type> elements. **/
1601class InputType : public ManifestExtractor::Element {
1602 public:
1603 InputType() = default;
1604 void Extract(xml::Element* element) override {
1605 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1606 auto parent_stack = extractor()->parent_stack();
1607
1608 // Add the input to the set of supported inputs
1609 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1610 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1611 supports->inputs.push_back(*name);
1612 }
1613 }
1614};
1615
1616/** Represents <original-package> elements. **/
1617class OriginalPackage : public ManifestExtractor::Element {
1618 public:
1619 OriginalPackage() = default;
1620 const std::string* name = nullptr;
1621
1622 void Extract(xml::Element* element) override {
1623 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1624 }
1625
Ryan Mitchell214846d2018-09-19 16:57:01 -07001626 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001627 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001628 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001629 }
1630 }
1631};
1632
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001633
1634/** Represents <overlay> elements. **/
1635class Overlay : public ManifestExtractor::Element {
1636 public:
1637 Overlay() = default;
1638 const std::string* target_package = nullptr;
1639 int priority;
1640 bool is_static;
1641 const std::string* required_property_name = nullptr;
1642 const std::string* required_property_value = nullptr;
1643
1644 void Extract(xml::Element* element) override {
1645 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1646 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1647 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1648 required_property_name = GetAttributeString(
1649 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1650 required_property_value = GetAttributeString(
1651 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1652 }
1653
1654 void Print(text::Printer* printer) override {
1655 printer->Print(StringPrintf("overlay:"));
1656 if (target_package) {
1657 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1658 }
1659 printer->Print(StringPrintf(" priority='%d'", priority));
1660 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1661 if (required_property_name) {
1662 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1663 }
1664 if (required_property_value) {
1665 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1666 }
1667 printer->Print("\n");
1668 }
1669};
1670
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001671/** * Represents <package-verifier> elements. **/
1672class PackageVerifier : public ManifestExtractor::Element {
1673 public:
1674 PackageVerifier() = default;
1675 const std::string* name = nullptr;
1676 const std::string* public_key = nullptr;
1677
1678 void Extract(xml::Element* element) override {
1679 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1680 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1681 }
1682
Ryan Mitchell214846d2018-09-19 16:57:01 -07001683 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001684 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001685 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001686 name->data(), public_key->data()));
1687 }
1688 }
1689};
1690
1691/** Represents <uses-package> elements. **/
1692class UsesPackage : public ManifestExtractor::Element {
1693 public:
1694 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001695 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001696 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001697 int version;
1698 int versionMajor;
1699 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001700
1701 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001702 auto parent_stack = extractor()->parent_stack();
1703 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1704 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1705 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1706 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1707 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1708 AddCertDigest(element);
1709 }
1710 }
1711
1712 void AddCertDigest(xml::Element* element) {
1713 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1714 // We allow ":" delimiters in the SHA declaration as this is the format
1715 // emitted by the certtool making it easy for developers to copy/paste.
1716 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1717 if (!digest.empty()) {
1718 certDigests.push_back(digest);
1719 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001720 }
1721
Ryan Mitchell214846d2018-09-19 16:57:01 -07001722 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001723 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001724 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001725 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001726 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1727 packageType->data(), name->data(), version, versionMajor));
1728 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001729 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001730 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001731 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001732 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001733 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001734 }
1735 }
1736 }
1737};
1738
1739/** Represents <additional-certificate> elements. **/
1740class AdditionalCertificate : public ManifestExtractor::Element {
1741 public:
1742 AdditionalCertificate() = default;
1743
1744 void Extract(xml::Element* element) override {
1745 auto parent_stack = extractor()->parent_stack();
1746 if (parent_stack.size() > 0) {
1747 if (ElementCast<UsesPackage>(parent_stack[0])) {
1748 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1749 uses->AddCertDigest(element);
1750 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1751 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1752 uses->AddCertDigest(element);
1753 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001754 }
1755 }
1756};
1757
1758/** Represents <screen> elements found in <compatible-screens> elements. */
1759class Screen : public ManifestExtractor::Element {
1760 public:
1761 Screen() = default;
1762 const int32_t* size = nullptr;
1763 const int32_t* density = nullptr;
1764
1765 void Extract(xml::Element* element) override {
1766 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1767 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1768 }
1769};
1770
1771/**
1772 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1773 * that each denote a supported screen size and screen density.
1774 **/
1775class CompatibleScreens : public ManifestExtractor::Element {
1776 public:
1777 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001778 void Print(text::Printer* printer) override {
1779 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001780
1781 bool first = true;
1782 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1783 if (auto screen = ElementCast<Screen>(el)) {
1784 if (first) {
1785 first = false;
1786 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001787 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001788 }
1789
1790 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001791 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001792 }
1793 }
1794 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001795 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001796 }
1797};
1798
1799/** Represents <supports-gl-texture> elements. **/
1800class SupportsGlTexture : public ManifestExtractor::Element {
1801 public:
1802 SupportsGlTexture() = default;
1803 const std::string* name = nullptr;
1804
1805 void Extract(xml::Element* element) override {
1806 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1807 }
1808
Ryan Mitchell214846d2018-09-19 16:57:01 -07001809 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001810 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001811 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001812 }
1813 }
1814};
1815
1816/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001817static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001818 el->Print(printer);
1819 for (auto &child : el->children()) {
1820 Print(child.get(), printer);
1821 }
1822}
1823
Ryan Mitchell214846d2018-09-19 16:57:01 -07001824bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001825 // Load the manifest
1826 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1827 if (doc == nullptr) {
1828 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1829 return false;
1830 }
1831
1832 xml::Element* element = doc->root.get();
1833 if (element->name != "manifest") {
1834 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1835 return false;
1836 }
1837
1838 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1839 // printing only permission elements is requested
1840 if (options_.only_permissions) {
1841 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1842 ManifestExtractor::Element::Inflate(this, element);
1843
1844 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1845 for (xml::Element* child : element->GetChildElements()) {
1846 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1847 || child->name == "permission") {
1848 auto permission_element = ManifestExtractor::Element::Inflate(this, child);
1849 manifest->AddChild(permission_element);
1850 }
1851 }
1852
Ryan Mitchell214846d2018-09-19 16:57:01 -07001853 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001854 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1855 el->Print(printer);
1856 });
1857
1858 return true;
1859 }
1860
1861 return false;
1862 }
1863
1864 // Collect information about the resource configurations
1865 if (apk_->GetResourceTable()) {
1866 for (auto &package : apk_->GetResourceTable()->packages) {
1867 for (auto &type : package->types) {
1868 for (auto &entry : type->entries) {
1869 for (auto &value : entry->values) {
1870 std::string locale_str = value->config.GetBcp47LanguageTag();
1871
1872 // Collect all the unique locales of the apk
1873 if (locales_.find(locale_str) == locales_.end()) {
1874 ConfigDescription config = ManifestExtractor::DummyConfig();
1875 config.setBcp47Locale(locale_str.data());
1876 locales_.insert(std::make_pair(locale_str, config));
1877 }
1878
1879 // Collect all the unique density of the apk
1880 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1881 : value->config.density;
1882 if (densities_.find(density) == densities_.end()) {
1883 ConfigDescription config = ManifestExtractor::DummyConfig();
1884 config.density = density;
1885 densities_.insert(std::make_pair(density, config));
1886 }
1887 }
1888 }
1889 }
1890 }
1891 }
1892
1893 // Extract badging information
1894 auto root = Visit(element);
1895
1896 // Print the elements in order seen
1897 Print(root.get(), printer);
1898
1899 /** Recursively checks the extracted elements for the specified permission. **/
1900 auto FindPermission = [&](ManifestExtractor::Element* root,
1901 const std::string& name) -> ManifestExtractor::Element* {
1902 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1903 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1904 return permission->name == name;
1905 }
1906 return false;
1907 });
1908 };
1909
1910 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
1911 int32_t max_sdk_version) -> void {
1912 auto permission = util::make_unique<UsesPermission>();
1913 permission->name = name;
1914 permission->maxSdkVersion = max_sdk_version;
1915 permission->Print(printer);
1916 permission->PrintImplied(printer, reason);
1917 };
1918
1919 // Implied permissions
1920 // Pre-1.6 implicitly granted permission compatibility logic
1921 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
1922 bool insert_write_external = false;
1923 auto write_external_permission = ElementCast<UsesPermission>(
1924 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
1925
1926 if (target_sdk() < 4) {
1927 if (!write_external_permission) {
1928 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
1929 insert_write_external = true;
1930 }
1931
1932 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
1933 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
1934 }
1935 }
1936
1937 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
1938 // force them to always take READ_EXTERNAL_STORAGE as well. We always
1939 // do this (regardless of target API version) because we can't have
1940 // an app with write permission but not read permission.
1941 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
1942 if (!read_external && (insert_write_external || write_external_permission)) {
1943 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
1944 "requested WRITE_EXTERNAL_STORAGE",
1945 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
1946 }
1947
1948 // Pre-JellyBean call log permission compatibility.
1949 if (target_sdk() < 16) {
1950 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
1951 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
1952 PrintPermission("android.permission.READ_CALL_LOG",
1953 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
1954 }
1955
1956 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
1957 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
1958 PrintPermission("android.permission.WRITE_CALL_LOG",
1959 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
1960 }
1961 }
1962
1963 // If the app hasn't declared the touchscreen as a feature requirement (either
1964 // directly or implied, required or not), then the faketouch feature is implied.
1965 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
1966 common_feature_group->addImpliedFeature("android.hardware.faketouch",
1967 "default feature for all apps", false);
1968 }
1969
1970 // Only print the common feature group if no feature group is defined
1971 std::vector<FeatureGroup*> feature_groups;
1972 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
1973 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
1974 feature_groups.push_back(feature_group);
1975 }
1976 });
1977
1978 if (feature_groups.empty()) {
1979 common_feature_group->PrintGroup(printer);
1980 } else {
1981 // Merge the common feature group into the feature group
1982 for (auto& feature_group : feature_groups) {
1983 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
1984 common_feature_group->open_gles_version);
1985 feature_group->Merge(common_feature_group);
1986 feature_group->PrintGroup(printer);
1987 }
1988 };
1989
1990 // Collect the component types of the application
1991 std::set<std::string> components;
1992 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
1993 if (ElementCast<Action>(el)) {
1994 auto action = ElementCast<Action>(el);
1995 if (!action->component.empty()) {
1996 components.insert(action->component);
1997 return;
1998 }
1999 }
2000
2001 if (ElementCast<Category>(el)) {
2002 auto category = ElementCast<Category>(el);
2003 if (!category->component.empty()) {
2004 components.insert(category->component);
2005 return;
2006 }
2007 }
2008 });
2009
2010 // Check for the payment component
2011 auto apk = apk_;
2012 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2013 if (auto service = ElementCast<Service>(el)) {
2014 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2015 [&](ManifestExtractor::Element* el) -> bool {
2016 if (auto action = ElementCast<Action>(el)) {
2017 return (action->component == "host-apdu");
2018 }
2019 return false;
2020 }));
2021
2022 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2023 [&](ManifestExtractor::Element* el) -> bool {
2024 if (auto action = ElementCast<Action>(el)) {
2025 return (action->component == "offhost-apdu");
2026 }
2027 return false;
2028 }));
2029
2030 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2031 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2032 if (auto meta_data = ElementCast<MetaData>(el)) {
2033 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2034 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2035 && offhost_apdu_action)) {
2036
2037 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002038 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002039 return;
2040 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002041 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002042 if (!resource) {
2043 return;
2044 }
2045
2046 // Look for the payment category on an <aid-group> element
2047 auto& root = resource.get()->root;
2048 if ((host_apdu_action && root->name == "host-apdu-service")
2049 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2050
2051 for (auto& child : root->GetChildElements()) {
2052 if (child->name == "aid-group") {
2053 auto category = FindAttribute(child, CATEGORY_ATTR);
2054 if (category && category->value == "payment") {
2055 components.insert("payment");
2056 return;
2057 }
2058 }
2059 }
2060 }
2061 }
2062 }
2063 });
2064 }
2065 });
2066
2067 // Print the components types if they are present
2068 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2069 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002070 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002071 }
2072 };
2073
2074 PrintComponent("app-widget");
2075 PrintComponent("device-admin");
2076 PrintComponent("ime");
2077 PrintComponent("wallpaper");
2078 PrintComponent("accessibility");
2079 PrintComponent("print-service");
2080 PrintComponent("payment");
2081 PrintComponent("search");
2082 PrintComponent("document-provider");
2083 PrintComponent("launcher");
2084 PrintComponent("notification-listener");
2085 PrintComponent("dream");
2086 PrintComponent("camera");
2087 PrintComponent("camera-secure");
2088
2089 // Print presence of main activity
2090 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002091 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002092 }
2093
2094 // Print presence of activities, recivers, and services with no special components
2095 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2096 if (auto activity = ElementCast<Activity>(el)) {
2097 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002098 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002099 return true;
2100 }
2101 }
2102 return false;
2103 });
2104
2105 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2106 if (auto receiver = ElementCast<Receiver>(el)) {
2107 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002108 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002109 return true;
2110 }
2111 }
2112 return false;
2113 });
2114
2115 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2116 if (auto service = ElementCast<Service>(el)) {
2117 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002118 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002119 return true;
2120 }
2121 }
2122 return false;
2123 });
2124
2125 // Print the supported screens
2126 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2127 [&](ManifestExtractor::Element* el) -> bool {
2128 return ElementCast<SupportsScreen>(el) != nullptr;
2129 }));
2130
2131 if (screen) {
2132 screen->PrintScreens(printer, target_sdk_);
2133 } else {
2134 // Print the default supported screens
2135 SupportsScreen default_screens;
2136 default_screens.PrintScreens(printer, target_sdk_);
2137 }
2138
2139 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002140 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002141 for (auto& config : locales_) {
2142 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002143 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002144 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002145 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002146 }
2147 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002148 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002149
2150 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002151 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002152 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002153 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002154 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002155 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002156
2157 // Print the supported architectures of the app
2158 std::set<std::string> architectures;
2159 auto it = apk_->GetFileCollection()->Iterator();
2160 while (it->HasNext()) {
2161 auto file_path = it->Next()->GetSource().path;
2162
2163
2164 size_t pos = file_path.find("lib/");
2165 if (pos != std::string::npos) {
2166 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002167 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002168 if (pos != std::string::npos) {
2169 file_path = file_path.substr(0, pos);
2170 }
2171
2172 architectures.insert(file_path);
2173 }
2174 }
2175
2176 // Determine if the application has multiArch supports
2177 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2178 if (auto application = ElementCast<Application>(el)) {
2179 return application->has_multi_arch;
2180 }
2181 return false;
2182 });
2183
2184 bool output_alt_native_code = false;
2185 // A multiArch package is one that contains 64-bit and
2186 // 32-bit versions of native code and expects 3rd-party
2187 // apps to load these native code libraries. Since most
2188 // 64-bit systems also support 32-bit apps, the apps
2189 // loading this multiArch package's code may be either
2190 if (has_multi_arch) {
2191 // If this is a multiArch package, report the 64-bit
2192 // version only. Then as a separate entry, report the
2193 // rest.
2194 //
2195 // If we report the 32-bit architecture, this APK will
2196 // be installed on a 32-bit device, causing a large waste
2197 // of bandwidth and disk space. This assumes that
2198 // the developer of the multiArch package has also
2199 // made a version that is 32-bit only.
2200 const std::string kIntel64 = "x86_64";
2201 const std::string kArm64 = "arm64-v8a";
2202
2203 auto arch = architectures.find(kIntel64);
2204 if (arch == architectures.end()) {
2205 arch = architectures.find(kArm64);
2206 }
2207
2208 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002209 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002210 architectures.erase(arch);
2211 output_alt_native_code = true;
2212 }
2213 }
2214
2215 if (architectures.size() > 0) {
2216 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002217 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002218 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002219 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002220 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002221 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002222 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002223 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002224 }
2225
2226 return true;
2227}
2228
2229/**
2230 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2231 * pointer.
2232 **/
2233template<typename T>
2234T* ElementCast(ManifestExtractor::Element* element) {
2235 if (element == nullptr) {
2236 return nullptr;
2237 }
2238
2239 const std::unordered_map<std::string, bool> kTagCheck = {
2240 {"action", std::is_base_of<Action, T>::value},
2241 {"activity", std::is_base_of<Activity, T>::value},
2242 {"application", std::is_base_of<Application, T>::value},
2243 {"category", std::is_base_of<Category, T>::value},
2244 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2245 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2246 {"input-type", std::is_base_of<InputType, T>::value},
2247 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2248 {"meta-data", std::is_base_of<MetaData, T>::value},
2249 {"manifest", std::is_base_of<Manifest, T>::value},
2250 {"original-package", std::is_base_of<OriginalPackage, T>::value},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002251 {"overlay", std::is_base_of<Overlay, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002252 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2253 {"permission", std::is_base_of<Permission, T>::value},
2254 {"provider", std::is_base_of<Provider, T>::value},
2255 {"receiver", std::is_base_of<Receiver, T>::value},
2256 {"screen", std::is_base_of<Screen, T>::value},
2257 {"service", std::is_base_of<Service, T>::value},
2258 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2259 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2260 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2261 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2262 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2263 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2264 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2265 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2266 {"uses-package", std::is_base_of<UsesPackage, T>::value},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002267 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2268 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
2269 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002270 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
Jiyong Park6a5b8b12020-06-30 13:23:36 +09002271 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002272 };
2273
2274 auto check = kTagCheck.find(element->tag());
2275 if (check != kTagCheck.end() && check->second) {
2276 return static_cast<T*>(element);
2277 }
2278 return nullptr;
2279}
2280
2281template<typename T>
2282std::unique_ptr<T> CreateType() {
2283 return std::move(util::make_unique<T>());
2284}
2285
2286std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2287 ManifestExtractor* extractor, xml::Element* el) {
2288 const std::unordered_map<std::string,
2289 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2290 kTagCheck = {
2291 {"action", &CreateType<Action>},
2292 {"activity", &CreateType<Activity>},
2293 {"application", &CreateType<Application>},
2294 {"category", &CreateType<Category>},
2295 {"compatible-screens", &CreateType<CompatibleScreens>},
2296 {"feature-group", &CreateType<FeatureGroup>},
2297 {"input-type", &CreateType<InputType>},
2298 {"intent-filter",&CreateType<IntentFilter>},
2299 {"manifest", &CreateType<Manifest>},
2300 {"meta-data", &CreateType<MetaData>},
2301 {"original-package", &CreateType<OriginalPackage>},
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002302 {"overlay", &CreateType<Overlay>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002303 {"package-verifier", &CreateType<PackageVerifier>},
2304 {"permission", &CreateType<Permission>},
2305 {"provider", &CreateType<Provider>},
2306 {"receiver", &CreateType<Receiver>},
2307 {"screen", &CreateType<Screen>},
2308 {"service", &CreateType<Service>},
2309 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2310 {"supports-input", &CreateType<SupportsInput>},
2311 {"supports-screens", &CreateType<SupportsScreen>},
2312 {"uses-configuration", &CreateType<UsesConfiguarion>},
2313 {"uses-feature", &CreateType<UsesFeature>},
2314 {"uses-permission", &CreateType<UsesPermission>},
2315 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2316 {"uses-library", &CreateType<UsesLibrary>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002317 {"static-library", &CreateType<StaticLibrary>},
2318 {"uses-static-library", &CreateType<UsesStaticLibrary>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002319 {"uses-package", &CreateType<UsesPackage>},
Dianne Hackborn813d7502018-10-02 16:59:46 -07002320 {"additional-certificate", &CreateType<AdditionalCertificate>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002321 {"uses-sdk", &CreateType<UsesSdkBadging>},
Jiyong Park6a5b8b12020-06-30 13:23:36 +09002322 {"uses-native-library", &CreateType<UsesNativeLibrary>},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002323 };
2324
2325 // Attempt to map the xml tag to a element inflater
2326 std::unique_ptr<ManifestExtractor::Element> element;
2327 auto check = kTagCheck.find(el->name);
2328 if (check != kTagCheck.end()) {
2329 element = check->second();
2330 } else {
2331 element = util::make_unique<ManifestExtractor::Element>();
2332 }
2333
2334 element->extractor_ = extractor;
2335 element->tag_ = el->name;
2336 element->Extract(el);
2337 return element;
2338}
2339
2340std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2341 auto element = ManifestExtractor::Element::Inflate(this, el);
2342 parent_stack_.insert(parent_stack_.begin(), element.get());
2343
2344 // Process the element and recursively visit the children
2345 for (xml::Element* child : el->GetChildElements()) {
2346 auto v = Visit(child);
2347 element->AddChild(v);
2348 }
2349
2350 parent_stack_.erase(parent_stack_.begin());
2351 return element;
2352}
2353
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002354
Ryan Mitchell214846d2018-09-19 16:57:01 -07002355int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2356 IDiagnostics* diag) {
2357 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002358 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002359}
2360
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002361} // namespace aapt