blob: 9574d275690bbb85de0a0128aad28e60326c09d0 [file] [log] [blame]
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "DumpManifest.h"
18
Dianne Hackborn813d7502018-10-02 16:59:46 -070019#include <algorithm>
20
Ryan Mitchellfc225b22018-08-21 14:52:51 -070021#include "LoadedApk.h"
22#include "SdkConstants.h"
23#include "ValueVisitor.h"
24#include "io/File.h"
25#include "io/FileStream.h"
26#include "process/IResourceTableConsumer.h"
27#include "xml/XmlDom.h"
28
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020029#include "androidfw/ConfigDescription.h"
30
Ryan Mitchellfc225b22018-08-21 14:52:51 -070031using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070033
34namespace aapt {
35
36/**
37 * These are attribute resource constants for the platform, as found in android.R.attr.
38 */
39enum {
40 LABEL_ATTR = 0x01010001,
41 ICON_ATTR = 0x01010002,
42 NAME_ATTR = 0x01010003,
43 PERMISSION_ATTR = 0x01010006,
44 EXPORTED_ATTR = 0x01010010,
45 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000046 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070047 RESOURCE_ATTR = 0x01010025,
48 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000049 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070050 VALUE_ATTR = 0x01010024,
51 VERSION_CODE_ATTR = 0x0101021b,
52 VERSION_NAME_ATTR = 0x0101021c,
53 SCREEN_ORIENTATION_ATTR = 0x0101001e,
54 MIN_SDK_VERSION_ATTR = 0x0101020c,
55 MAX_SDK_VERSION_ATTR = 0x01010271,
56 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
57 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
58 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
59 REQ_NAVIGATION_ATTR = 0x0101022a,
60 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
61 TARGET_SDK_VERSION_ATTR = 0x01010270,
62 TEST_ONLY_ATTR = 0x01010272,
63 ANY_DENSITY_ATTR = 0x0101026c,
64 GL_ES_VERSION_ATTR = 0x01010281,
65 SMALL_SCREEN_ATTR = 0x01010284,
66 NORMAL_SCREEN_ATTR = 0x01010285,
67 LARGE_SCREEN_ATTR = 0x01010286,
68 XLARGE_SCREEN_ATTR = 0x010102bf,
69 REQUIRED_ATTR = 0x0101028e,
70 INSTALL_LOCATION_ATTR = 0x010102b7,
71 SCREEN_SIZE_ATTR = 0x010102ca,
72 SCREEN_DENSITY_ATTR = 0x010102cb,
73 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
74 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
75 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
76 PUBLIC_KEY_ATTR = 0x010103a6,
77 CATEGORY_ATTR = 0x010103e8,
78 BANNER_ATTR = 0x10103f2,
79 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070080 VERSION_ATTR = 0x01010519,
81 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000082 REQUIRED_FEATURE_ATTR = 0x01010554,
83 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000084 IS_STATIC_ATTR = 0x0101055a,
85 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
86 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070087 COMPILE_SDK_VERSION_ATTR = 0x01010572,
88 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070089 VERSION_MAJOR_ATTR = 0x01010577,
90 PACKAGE_TYPE_ATTR = 0x01010587,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070091};
92
93const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Ryan Mitchell95f02422020-04-30 10:25:53 -070094constexpr int kCurrentDevelopmentVersion = 10000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070095
96/** Retrieves the attribute of the element with the specified attribute resource id. */
97static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
98 for (auto& a : el->attributes) {
99 if (a.compiled_attribute && a.compiled_attribute.value().id) {
100 if (a.compiled_attribute.value().id.value() == resd_id) {
101 return std::move(&a);
102 }
103 }
104 }
105 return nullptr;
106}
107
108/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
109static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
110 const std::string &name) {
111 return el->FindAttribute(package, name);
112}
113
114class CommonFeatureGroup;
115
116class ManifestExtractor {
117 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700118
Ryan Mitchell214846d2018-09-19 16:57:01 -0700119 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700120 : apk_(apk), options_(options) { }
121
122 class Element {
123 public:
124 Element() = default;
125 virtual ~Element() = default;
126
127 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
128
129 /** Writes out the extracted contents of the element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -0700130 virtual void Print(text::Printer* printer) { }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700131
132 /** Adds an element to the list of children of the element. */
133 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
134
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700135 template <typename Predicate>
136 void Filter(Predicate&& func) {
137 children_.erase(std::remove_if(children_.begin(), children_.end(),
Kelvin Zhang3965584d2021-05-10 12:17:14 -0400138 [&](const auto& e) { return func(e.get()); }),
139 children_.end());
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700140 }
141
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700142 /** Retrieves the list of children of the element. */
143 const std::vector<std::unique_ptr<Element>>& children() const {
144 return children_;
145 }
146
147 /** Retrieves the extracted xml element tag. */
148 const std::string tag() const {
149 return tag_;
150 }
151
152 protected:
153 ManifestExtractor* extractor() const {
154 return extractor_;
155 }
156
157 /** Retrieves and stores the information extracted from the xml element. */
158 virtual void Extract(xml::Element* el) { }
159
160 /*
161 * Retrieves a configuration value of the resource entry that best matches the specified
162 * configuration.
163 */
164 static Value* BestConfigValue(ResourceEntry* entry,
165 const ConfigDescription& match) {
166 if (!entry) {
167 return nullptr;
168 }
169
170 // Determine the config that best matches the desired config
171 ResourceConfigValue* best_value = nullptr;
172 for (auto& value : entry->values) {
173 if (!value->config.match(match)) {
174 continue;
175 }
176
177 if (best_value != nullptr) {
178 if (!value->config.isBetterThan(best_value->config, &match)) {
179 if (value->config.compare(best_value->config) != 0) {
180 continue;
181 }
182 }
183 }
184
185 best_value = value.get();
186 }
187
188 // The entry has no values
189 if (!best_value) {
190 return nullptr;
191 }
192
193 return best_value->value.get();
194 }
195
196 /** Retrieves the resource assigned to the specified resource id if one exists. */
197 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700198 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700199 if (table) {
200 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700201 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700202 for (auto& entry : type->entries) {
203 if (entry->id && entry->id.value() == res_id.id) {
204 if (auto value = BestConfigValue(entry.get(), config)) {
205 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700206 }
207 }
208 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700209 }
210 }
211 }
212 return nullptr;
213 }
214
215 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700216 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700217 const int kMaxIterations = 40;
218 int i = 0;
219 while (ref && ref->id && i++ < kMaxIterations) {
220 auto table = extractor_->apk_->GetResourceTable();
221 if (auto value = FindValueById(table, ref->id.value(), config)) {
222 if (ValueCast<Reference>(value)) {
223 ref = ValueCast<Reference>(value);
224 } else {
225 return value;
226 }
227 }
228 }
229 return nullptr;
230 }
231
232 /**
233 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
234 * this will attempt to resolve the reference to an integer value.
235 **/
236 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700237 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700238 if (attr != nullptr) {
239 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700240 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700241 Value* value = attr->compiled_value.get();
242 if (ValueCast<Reference>(value)) {
243 value = ResolveReference(ValueCast<Reference>(value), config);
244 } else {
245 value = attr->compiled_value.get();
246 }
247 // Retrieve the integer data if possible
248 if (value != nullptr) {
249 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
250 return (int32_t*) &intValue->value.data;
251 }
252 }
253 }
254 }
255 return nullptr;
256 }
257
258 /**
259 * A version of GetAttributeInteger that returns a default integer if the attribute does not
260 * exist or cannot be resolved to an integer value.
261 **/
262 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700263 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700264 auto value = GetAttributeInteger(attr, config);
265 if (value) {
266 return *value;
267 }
268 return def;
269 }
270
271 /**
272 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
273 * this will attempt to resolve the reference to a string value.
274 **/
275 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700276 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700277 if (attr != nullptr) {
278 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700279 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700280 Value* value = attr->compiled_value.get();
281 if (ValueCast<Reference>(value)) {
282 value = ResolveReference(ValueCast<Reference>(value), config);
283 } else {
284 value = attr->compiled_value.get();
285 }
286
287 // Retrieve the string data of the value if possible
288 if (value != nullptr) {
289 if (String* intValue = ValueCast<String>(value)) {
290 return &(*intValue->value);
291 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
292 return &(*rawValue->value);
293 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
294 return &(*strValue->path);
295 }
296 }
297 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700298
299 if (!attr->value.empty()) {
300 return &attr->value;
301 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700302 }
303 return nullptr;
304 }
305
306 /**
307 * A version of GetAttributeString that returns a default string if the attribute does not
308 * exist or cannot be resolved to an string value.
309 **/
310 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700311 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700312 auto value = GetAttributeString(attr, config);
313 if (value) {
314 return *value;
315 }
316 return def;
317 }
318
319 private:
320 ManifestExtractor* extractor_;
321 std::vector<std::unique_ptr<Element>> children_;
322 std::string tag_;
323 };
324
325 friend Element;
326
327 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700328 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700329 ConfigDescription config;
330 config.orientation = android::ResTable_config::ORIENTATION_PORT;
331 config.density = android::ResTable_config::DENSITY_MEDIUM;
Ryan Mitchell95f02422020-04-30 10:25:53 -0700332 config.sdkVersion = kCurrentDevelopmentVersion; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700333 config.screenWidthDp = 320;
334 config.screenHeightDp = 480;
335 config.smallestScreenWidthDp = 320;
336 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
337 return config;
338 }
339
Ryan Mitchell214846d2018-09-19 16:57:01 -0700340 bool Dump(text::Printer* printer, IDiagnostics* diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700341
342 /** Recursively visit the xml element tree and return a processed badging element tree. */
343 std::unique_ptr<Element> Visit(xml::Element* element);
344
345 /** Raises the target sdk value if the min target is greater than the current target. */
346 void RaiseTargetSdk(int32_t min_target) {
347 if (min_target > target_sdk_) {
348 target_sdk_ = min_target;
349 }
350 }
351
352 /**
353 * Retrieves the default feature group that features are added into when <uses-feature>
354 * are not in a <feature-group> element.
355 **/
356 CommonFeatureGroup* GetCommonFeatureGroup() {
357 return commonFeatureGroup_.get();
358 }
359
360 /**
361 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
362 * used for that density setting.
363 **/
364 const std::map<uint16_t, ConfigDescription> densities() const {
365 return densities_;
366 }
367
368 /**
369 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
370 * would be used for that locale setting.
371 **/
372 const std::map<std::string, ConfigDescription> locales() const {
373 return locales_;
374 }
375
376 /** Retrieves the current stack of parent during data extraction. */
377 const std::vector<Element*> parent_stack() const {
378 return parent_stack_;
379 }
380
381 int32_t target_sdk() const {
382 return target_sdk_;
383 }
384
385 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700386 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700387
388 private:
389 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
390 std::map<std::string, ConfigDescription> locales_;
391 std::map<uint16_t, ConfigDescription> densities_;
392 std::vector<Element*> parent_stack_;
393 int32_t target_sdk_ = 0;
394};
395
396template<typename T> T* ElementCast(ManifestExtractor::Element* element);
397
398/** Recurs through the children of the specified root in depth-first order. */
399static void ForEachChild(ManifestExtractor::Element* root,
400 std::function<void(ManifestExtractor::Element*)> f) {
401 for (auto& child : root->children()) {
402 f(child.get());
403 ForEachChild(child.get(), f);
404 }
405}
406
407/**
408 * Checks the element and its recursive children for an element that makes the specified
409 * conditional function return true. Returns the first element that makes the conditional function
410 * return true.
411 **/
412static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
413 std::function<bool(ManifestExtractor::Element*)> f) {
414 if (f(root)) {
415 return root;
416 }
417 for (auto& child : root->children()) {
418 if (auto b2 = FindElement(child.get(), f)) {
419 return b2;
420 }
421 }
422 return nullptr;
423}
424
425/** Represents the <manifest> elements **/
426class Manifest : public ManifestExtractor::Element {
427 public:
428 Manifest() = default;
429 std::string package;
430 int32_t versionCode;
431 std::string versionName;
432 const std::string* split = nullptr;
433 const std::string* platformVersionName = nullptr;
434 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700435 const int32_t* platformVersionNameInt = nullptr;
436 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700437 const int32_t* compilesdkVersion = nullptr;
438 const std::string* compilesdkVersionCodename = nullptr;
439 const int32_t* installLocation = nullptr;
440
441 void Extract(xml::Element* manifest) override {
442 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
443 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
444 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
445 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
446
447 // Extract the platform build info
448 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
449 "platformBuildVersionName"));
450 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
451 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700452 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
453 "platformBuildVersionName"));
454 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
455 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700456
457 // Extract the compile sdk info
458 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
459 compilesdkVersionCodename = GetAttributeString(
460 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
461 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
462 }
463
Ryan Mitchell214846d2018-09-19 16:57:01 -0700464 void Print(text::Printer* printer) override {
465 printer->Print(StringPrintf("package: name='%s' ", package.data()));
466 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700467 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700468 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700469
470 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700471 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700472 }
473 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700474 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700475 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700476 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
477 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700478 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700479 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700480 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700481 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
482 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700483 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700484 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700485 }
486 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700487 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700488 compilesdkVersionCodename->data()));
489 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700490 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700491
492 if (installLocation) {
493 switch (*installLocation) {
494 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700495 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700496 break;
497 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700498 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700499 break;
500 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700501 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700502 break;
503 default:
504 break;
505 }
506 }
507 }
508};
509
510/** Represents <application> elements. **/
511class Application : public ManifestExtractor::Element {
512 public:
513 Application() = default;
514 std::string label;
515 std::string icon;
516 std::string banner;
517 int32_t is_game;
518 int32_t debuggable;
519 int32_t test_only;
520 bool has_multi_arch;
521
522 /** Mapping from locales to app names. */
523 std::map<std::string, std::string> locale_labels;
524
525 /** Mapping from densities to app icons. */
526 std::map<uint16_t, std::string> density_icons;
527
528 void Extract(xml::Element* element) override {
529 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
530 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
531 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
532 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
533 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
534 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
535
536 // We must search by name because the multiArch flag hasn't been API
537 // frozen yet.
538 has_multi_arch = (GetAttributeIntegerDefault(
539 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
540
541 // Retrieve the app names for every locale the app supports
542 auto attr = FindAttribute(element, LABEL_ATTR);
543 for (auto& config : extractor()->locales()) {
544 if (auto label = GetAttributeString(attr, config.second)) {
545 if (label) {
546 locale_labels.insert(std::make_pair(config.first, *label));
547 }
548 }
549 }
550
551 // Retrieve the icons for the densities the app supports
552 attr = FindAttribute(element, ICON_ATTR);
553 for (auto& config : extractor()->densities()) {
554 if (auto resource = GetAttributeString(attr, config.second)) {
555 if (resource) {
556 density_icons.insert(std::make_pair(config.first, *resource));
557 }
558 }
559 }
560 }
561
Ryan Mitchell214846d2018-09-19 16:57:01 -0700562 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700563 // Print the labels for every locale
564 for (auto p : locale_labels) {
565 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700566 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700567 android::ResTable::normalizeForOutput(p.second.data())
568 .c_str()));
569 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700570 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700571 android::ResTable::normalizeForOutput(p.second.data())
572 .c_str()));
573 }
574 }
575
576 // Print the icon paths for every density
577 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700578 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700579 }
580
581 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700582 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700583 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700584 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700585 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700586 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700587 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700588 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700589
590 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700591 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700592 }
593 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700594 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700595 }
596 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700597 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700598 }
599 }
600};
601
602/** Represents <uses-sdk> elements. **/
603class UsesSdkBadging : public ManifestExtractor::Element {
604 public:
605 UsesSdkBadging() = default;
606 const int32_t* min_sdk = nullptr;
607 const std::string* min_sdk_name = nullptr;
608 const int32_t* max_sdk = nullptr;
609 const int32_t* target_sdk = nullptr;
610 const std::string* target_sdk_name = nullptr;
611
612 void Extract(xml::Element* element) override {
613 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
614 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
615 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
616 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
617 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
618
619 // Detect the target sdk of the element
620 if ((min_sdk_name && *min_sdk_name == "Donut")
621 || (target_sdk_name && *target_sdk_name == "Donut")) {
622 extractor()->RaiseTargetSdk(4);
623 }
624 if (min_sdk) {
625 extractor()->RaiseTargetSdk(*min_sdk);
626 }
627 if (target_sdk) {
628 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700629 } else if (target_sdk_name) {
630 extractor()->RaiseTargetSdk(kCurrentDevelopmentVersion);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700631 }
632 }
633
Ryan Mitchell214846d2018-09-19 16:57:01 -0700634 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700635 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700636 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700637 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700638 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700639 }
640 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700641 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700642 }
643 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700644 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700645 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700646 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700647 }
648 }
649};
650
651/** Represents <uses-configuration> elements. **/
652class UsesConfiguarion : public ManifestExtractor::Element {
653 public:
654 UsesConfiguarion() = default;
655 int32_t req_touch_screen = 0;
656 int32_t req_keyboard_type = 0;
657 int32_t req_hard_keyboard = 0;
658 int32_t req_navigation = 0;
659 int32_t req_five_way_nav = 0;
660
661 void Extract(xml::Element* element) override {
662 req_touch_screen = GetAttributeIntegerDefault(
663 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
664 req_keyboard_type = GetAttributeIntegerDefault(
665 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
666 req_hard_keyboard = GetAttributeIntegerDefault(
667 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
668 req_navigation = GetAttributeIntegerDefault(
669 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
670 req_five_way_nav = GetAttributeIntegerDefault(
671 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
672 }
673
Ryan Mitchell214846d2018-09-19 16:57:01 -0700674 void Print(text::Printer* printer) override {
675 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700676 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700677 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700678 }
679 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700680 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700681 }
682 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700683 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700684 }
685 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700686 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700687 }
688 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700689 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700690 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700691 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700692 }
693};
694
695/** Represents <supports-screen> elements. **/
696class SupportsScreen : public ManifestExtractor::Element {
697 public:
698 SupportsScreen() = default;
699 int32_t small_screen = 1;
700 int32_t normal_screen = 1;
701 int32_t large_screen = 1;
702 int32_t xlarge_screen = 1;
703 int32_t any_density = 1;
704 int32_t requires_smallest_width_dp = 0;
705 int32_t compatible_width_limit_dp = 0;
706 int32_t largest_width_limit_dp = 0;
707
708 void Extract(xml::Element* element) override {
709 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
710 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
711 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
712 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
713 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
714
715 requires_smallest_width_dp = GetAttributeIntegerDefault(
716 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
717 compatible_width_limit_dp = GetAttributeIntegerDefault(
718 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
719 largest_width_limit_dp = GetAttributeIntegerDefault(
720 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
721
722 // For modern apps, if screen size buckets haven't been specified
723 // but the new width ranges have, then infer the buckets from them.
724 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
725 && requires_smallest_width_dp > 0) {
726 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
727 : requires_smallest_width_dp;
728 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
729 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
730 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
731 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
732 }
733 }
734
Ryan Mitchell214846d2018-09-19 16:57:01 -0700735 void PrintScreens(text::Printer* printer, int32_t target_sdk) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700736 int32_t small_screen_temp = small_screen;
737 int32_t normal_screen_temp = normal_screen;
738 int32_t large_screen_temp = large_screen;
739 int32_t xlarge_screen_temp = xlarge_screen;
740 int32_t any_density_temp = any_density;
741
742 // Determine default values for any unspecified screen sizes,
743 // based on the target SDK of the package. As of 4 (donut)
744 // the screen size support was introduced, so all default to
745 // enabled.
746 if (small_screen_temp > 0) {
747 small_screen_temp = target_sdk >= 4 ? -1 : 0;
748 }
749 if (normal_screen_temp > 0) {
750 normal_screen_temp = -1;
751 }
752 if (large_screen_temp > 0) {
753 large_screen_temp = target_sdk >= 4 ? -1 : 0;
754 }
755 if (xlarge_screen_temp > 0) {
756 // Introduced in Gingerbread.
757 xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
758 }
759 if (any_density_temp > 0) {
760 any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
761 || compatible_width_limit_dp > 0) ? -1 : 0;
762 }
763
764 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700765 printer->Print("supports-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700766 if (small_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700767 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700768 }
769 if (normal_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700770 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700771 }
772 if (large_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700773 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700774 }
775 if (xlarge_screen_temp != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700776 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700777 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700778 printer->Print("\n");
779 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700780 (any_density_temp ) ? "true" : "false"));
781 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700782 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700783 }
784 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700785 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700786 }
787 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700788 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700789 }
790 }
791};
792
793/** Represents <feature-group> elements. **/
794class FeatureGroup : public ManifestExtractor::Element {
795 public:
796 FeatureGroup() = default;
797 std::string label;
798 int32_t open_gles_version = 0;
799
800 void Extract(xml::Element* element) override {
801 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
802 }
803
Ryan Mitchell214846d2018-09-19 16:57:01 -0700804 virtual void PrintGroup(text::Printer* printer) {
805 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700806 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700807 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700808 }
809
810 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700811 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700812 (feature.second.required ? "" : "-not-required"),
813 feature.first.data()));
814 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700815 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700816 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700817 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700818 }
819 }
820
821 /** Adds a feature to the feature group. */
822 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
823 features_.insert(std::make_pair(name, Feature{ required, version }));
824 if (required) {
825 if (name == "android.hardware.camera.autofocus" ||
826 name == "android.hardware.camera.flash") {
827 AddFeature("android.hardware.camera", true);
828 } else if (name == "android.hardware.location.gps" ||
829 name == "android.hardware.location.network") {
830 AddFeature("android.hardware.location", true);
831 } else if (name == "android.hardware.faketouch.multitouch") {
832 AddFeature("android.hardware.faketouch", true);
833 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
834 name == "android.hardware.faketouch.multitouch.jazzhands") {
835 AddFeature("android.hardware.faketouch.multitouch", true);
836 AddFeature("android.hardware.faketouch", true);
837 } else if (name == "android.hardware.touchscreen.multitouch") {
838 AddFeature("android.hardware.touchscreen", true);
839 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
840 name == "android.hardware.touchscreen.multitouch.jazzhands") {
841 AddFeature("android.hardware.touchscreen.multitouch", true);
842 AddFeature("android.hardware.touchscreen", true);
843 } else if (name == "android.hardware.opengles.aep") {
844 const int kOpenGLESVersion31 = 0x00030001;
845 if (kOpenGLESVersion31 > open_gles_version) {
846 open_gles_version = kOpenGLESVersion31;
847 }
848 }
849 }
850 }
851
852 /** Returns true if the feature group has the given feature. */
853 virtual bool HasFeature(const std::string& name) {
854 return features_.find(name) != features_.end();
855 }
856
857 /** Merges the features of another feature group into this group. */
858 void Merge(FeatureGroup* group) {
859 open_gles_version = std::max(open_gles_version, group->open_gles_version);
860 for (auto& feature : group->features_) {
861 features_.insert(feature);
862 }
863 }
864
865 protected:
866 struct Feature {
867 public:
868 bool required = false;
869 int32_t version = -1;
870 };
871
872 /* Mapping of feature names to their properties. */
873 std::map<std::string, Feature> features_;
874};
875
876/**
877 * Represents the default feature group for the application if no <feature-group> elements are
878 * present in the manifest.
879 **/
880class CommonFeatureGroup : public FeatureGroup {
881 public:
882 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700883 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700884 FeatureGroup::PrintGroup(printer);
885
886 // Also print the implied features
887 for (auto feature : implied_features_) {
888 if (features_.find(feature.first) == features_.end()) {
889 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -0700890 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
891 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700892 feature.first.data()));
893
894 // Print the reasons as a sentence
895 size_t count = 0;
896 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700897 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700898 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700899 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700900 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700901 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700902 }
903 count++;
904 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700905 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700906 }
907 }
908 }
909
910 /** Returns true if the feature group has the given feature. */
911 bool HasFeature(const std::string& name) override {
912 return FeatureGroup::HasFeature(name)
913 || implied_features_.find(name) != implied_features_.end();
914 }
915
916 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
917 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
918 auto entry = implied_features_.find(name);
919 if (entry == implied_features_.end()) {
920 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
921 entry = implied_features_.find(name);
922 }
923
924 // A non-sdk 23 implied feature takes precedence.
925 if (entry->second.implied_from_sdk_k23 && !sdk23) {
926 entry->second.implied_from_sdk_k23 = false;
927 }
928
929 entry->second.reasons.insert(reason);
930 }
931
932 /**
933 * Adds a feature to a set of implied features for all features that are implied by the presence
934 * of the permission.
935 **/
936 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
937 if (name == "android.permission.CAMERA") {
938 addImpliedFeature("android.hardware.camera",
939 StringPrintf("requested %s permission", name.data()),
940 sdk23);
941
942 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
943 if (targetSdk < SDK_LOLLIPOP) {
944 addImpliedFeature("android.hardware.location.gps",
945 StringPrintf("requested %s permission", name.data()),
946 sdk23);
947 addImpliedFeature("android.hardware.location.gps",
948 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
949 sdk23);
950 }
951 addImpliedFeature("android.hardware.location",
952 StringPrintf("requested %s permission", name.data()),
953 sdk23);
954
955 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
956 if (targetSdk < SDK_LOLLIPOP) {
957 addImpliedFeature("android.hardware.location.network",
958 StringPrintf("requested %s permission", name.data()),
959 sdk23);
960 addImpliedFeature("android.hardware.location.network",
961 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
962 sdk23);
963 }
964 addImpliedFeature("android.hardware.location",
965 StringPrintf("requested %s permission", name.data()),
966 sdk23);
967
968 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
969 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
970 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
971 addImpliedFeature("android.hardware.location",
972 StringPrintf("requested %s permission", name.data()),
973 sdk23);
974
975 } else if (name == "android.permission.BLUETOOTH" ||
976 name == "android.permission.BLUETOOTH_ADMIN") {
977 if (targetSdk > SDK_DONUT) {
978 addImpliedFeature("android.hardware.bluetooth",
979 StringPrintf("requested %s permission", name.data()),
980 sdk23);
981 addImpliedFeature("android.hardware.bluetooth",
982 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
983 sdk23);
984 }
985
986 } else if (name == "android.permission.RECORD_AUDIO") {
987 addImpliedFeature("android.hardware.microphone",
988 StringPrintf("requested %s permission", name.data()),
989 sdk23);
990
991 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
992 name == "android.permission.CHANGE_WIFI_STATE" ||
993 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
994 addImpliedFeature("android.hardware.wifi",
995 StringPrintf("requested %s permission", name.data()),
996 sdk23);
997
998 } else if (name == "android.permission.CALL_PHONE" ||
999 name == "android.permission.CALL_PRIVILEGED" ||
1000 name == "android.permission.MODIFY_PHONE_STATE" ||
1001 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1002 name == "android.permission.READ_SMS" ||
1003 name == "android.permission.RECEIVE_SMS" ||
1004 name == "android.permission.RECEIVE_MMS" ||
1005 name == "android.permission.RECEIVE_WAP_PUSH" ||
1006 name == "android.permission.SEND_SMS" ||
1007 name == "android.permission.WRITE_APN_SETTINGS" ||
1008 name == "android.permission.WRITE_SMS") {
1009 addImpliedFeature("android.hardware.telephony",
1010 "requested a telephony permission",
1011 sdk23);
1012 }
1013 }
1014
1015 private:
1016 /**
1017 * Represents a feature that has been automatically added due to a pre-requisite or for some
1018 * other reason.
1019 */
1020 struct ImpliedFeature {
1021 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1022
1023 /** List of human-readable reasons for why this feature was implied. */
1024 std::set<std::string> reasons;
1025
1026 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1027 bool implied_from_sdk_k23;
1028 };
1029
1030 /* Mapping of implied feature names to their properties. */
1031 std::map<std::string, ImpliedFeature> implied_features_;
1032};
1033
1034/** Represents <uses-feature> elements. **/
1035class UsesFeature : public ManifestExtractor::Element {
1036 public:
1037 UsesFeature() = default;
1038 void Extract(xml::Element* element) override {
1039 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1040 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1041 bool required = GetAttributeIntegerDefault(
1042 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1043 int32_t version = GetAttributeIntegerDefault(
1044 FindAttribute(element, kAndroidNamespace, "version"), 0);
1045
1046 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1047 // common feature group
1048 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1049 if (!feature_group) {
1050 feature_group = extractor()->GetCommonFeatureGroup();
1051 } else {
1052 // All features in side of <feature-group> elements are required.
1053 required = true;
1054 }
1055
1056 if (name) {
1057 feature_group->AddFeature(*name, required, version);
1058 } else if (gl) {
1059 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1060 }
1061 }
1062};
1063
1064/** Represents <uses-permission> elements. **/
1065class UsesPermission : public ManifestExtractor::Element {
1066 public:
1067 UsesPermission() = default;
1068 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001069 std::vector<std::string> requiredFeatures;
1070 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001071 int32_t required = true;
1072 int32_t maxSdkVersion = -1;
1073
1074 void Extract(xml::Element* element) override {
1075 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001076 std::string feature =
1077 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1078 if (!feature.empty()) {
1079 requiredFeatures.push_back(feature);
1080 }
1081 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1082 if (!feature.empty()) {
1083 requiredNotFeatures.push_back(feature);
1084 }
1085
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001086 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1087 maxSdkVersion = GetAttributeIntegerDefault(
1088 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
1089
1090 if (!name.empty()) {
1091 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1092 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1093 }
1094 }
1095
Ryan Mitchell214846d2018-09-19 16:57:01 -07001096 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001097 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001098 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001099 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001100 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001101 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001102 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001103 for (const std::string& requiredFeature : requiredFeatures) {
1104 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1105 }
1106 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1107 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1108 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001109 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001110 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001111 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001112 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001113 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001114 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001115 }
1116 }
1117 }
1118
Ryan Mitchell214846d2018-09-19 16:57:01 -07001119 void PrintImplied(text::Printer* printer, const std::string& reason) {
1120 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001121 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001122 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001123 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001124 printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001125 }
1126};
1127
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001128/** Represents <required-feature> elements. **/
1129class RequiredFeature : public ManifestExtractor::Element {
1130 public:
1131 RequiredFeature() = default;
1132 std::string name;
1133
1134 void Extract(xml::Element* element) override {
1135 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1136 auto parent_stack = extractor()->parent_stack();
1137 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1138 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1139 uses_permission->requiredFeatures.push_back(name);
1140 }
1141 }
1142};
1143
1144/** Represents <required-not-feature> elements. **/
1145class RequiredNotFeature : public ManifestExtractor::Element {
1146 public:
1147 RequiredNotFeature() = default;
1148 std::string name;
1149
1150 void Extract(xml::Element* element) override {
1151 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1152 auto parent_stack = extractor()->parent_stack();
1153 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1154 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1155 uses_permission->requiredNotFeatures.push_back(name);
1156 }
1157 }
1158};
1159
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001160/** Represents <uses-permission-sdk-23> elements. **/
1161class UsesPermissionSdk23 : public ManifestExtractor::Element {
1162 public:
1163 UsesPermissionSdk23() = default;
1164 const std::string* name = nullptr;
1165 const int32_t* maxSdkVersion = nullptr;
1166
1167 void Extract(xml::Element* element) override {
1168 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1169 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1170
1171 if (name) {
1172 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1173 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1174 }
1175 }
1176
Ryan Mitchell214846d2018-09-19 16:57:01 -07001177 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001178 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001179 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001180 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001181 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001182 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001183 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001184 }
1185 }
1186};
1187
1188/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1189class Permission : public ManifestExtractor::Element {
1190 public:
1191 Permission() = default;
1192 std::string name;
1193
1194 void Extract(xml::Element* element) override {
1195 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1196 }
1197
Ryan Mitchell214846d2018-09-19 16:57:01 -07001198 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001199 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001200 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001201 }
1202 }
1203};
1204
1205/** Represents <activity> elements. **/
1206class Activity : public ManifestExtractor::Element {
1207 public:
1208 Activity() = default;
1209 std::string name;
1210 std::string icon;
1211 std::string label;
1212 std::string banner;
1213
1214 bool has_component_ = false;
1215 bool has_launcher_category = false;
1216 bool has_leanback_launcher_category = false;
1217 bool has_main_action = false;
1218
1219 void Extract(xml::Element* element) override {
1220 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1221 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1222 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1223 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1224
1225 // Retrieve the package name from the manifest
1226 std::string package;
1227 for (auto& parent : extractor()->parent_stack()) {
1228 if (auto manifest = ElementCast<Manifest>(parent)) {
1229 package = manifest->package;
1230 break;
1231 }
1232 }
1233
1234 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001235 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001236 if (idx == 0) {
1237 name = package + name;
1238 } else if (idx < 0) {
1239 name = package + "." + name;
1240 }
1241
1242 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1243 if (orientation) {
1244 CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
1245 int orien = *orientation;
1246 if (orien == 0 || orien == 6 || orien == 8) {
1247 // Requests landscape, sensorLandscape, or reverseLandscape.
1248 common->addImpliedFeature("android.hardware.screen.landscape",
1249 "one or more activities have specified a landscape orientation",
1250 false);
1251 } else if (orien == 1 || orien == 7 || orien == 9) {
1252 // Requests portrait, sensorPortrait, or reversePortrait.
1253 common->addImpliedFeature("android.hardware.screen.portrait",
1254 "one or more activities have specified a portrait orientation",
1255 false);
1256 }
1257 }
1258 }
1259
Ryan Mitchell214846d2018-09-19 16:57:01 -07001260 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001261 // Print whether the activity has the HOME category and a the MAIN action
1262 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001263 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001264 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001265 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001266 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001267 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001268 android::ResTable::normalizeForOutput(label.data()).c_str(),
1269 icon.data()));
1270 }
1271
1272 // Print wether the activity has the HOME category and a the MAIN action
1273 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001274 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001275 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001276 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001277 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001278 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001279 android::ResTable::normalizeForOutput(label.data()).c_str(),
1280 icon.data(), banner.data()));
1281 }
1282 }
1283};
1284
1285/** Represents <intent-filter> elements. */
1286class IntentFilter : public ManifestExtractor::Element {
1287 public:
1288 IntentFilter() = default;
1289};
1290
1291/** Represents <category> elements. */
1292class Category : public ManifestExtractor::Element {
1293 public:
1294 Category() = default;
1295 std::string component = "";
1296
1297 void Extract(xml::Element* element) override {
1298 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1299
1300 auto parent_stack = extractor()->parent_stack();
1301 if (category && ElementCast<IntentFilter>(parent_stack[0])
1302 && ElementCast<Activity>(parent_stack[1])) {
1303 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1304
1305 if (*category == "android.intent.category.LAUNCHER") {
1306 activity->has_launcher_category = true;
1307 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1308 activity->has_leanback_launcher_category = true;
1309 } else if (*category == "android.intent.category.HOME") {
1310 component = "launcher";
1311 }
1312 }
1313 }
1314};
1315
1316/**
1317 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1318 * elements nested within.
1319 **/
1320class Provider : public ManifestExtractor::Element {
1321 public:
1322 Provider() = default;
1323 bool has_required_saf_attributes = false;
1324
1325 void Extract(xml::Element* element) override {
1326 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1327 const int32_t* grant_uri_permissions = GetAttributeInteger(
1328 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1329 const std::string* permission = GetAttributeString(
1330 FindAttribute(element, PERMISSION_ATTR));
1331
1332 has_required_saf_attributes = ((exported && *exported != 0)
1333 && (grant_uri_permissions && *grant_uri_permissions != 0)
1334 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1335 }
1336};
1337
1338/** Represents <receiver> elements. **/
1339class Receiver : public ManifestExtractor::Element {
1340 public:
1341 Receiver() = default;
1342 const std::string* permission = nullptr;
1343 bool has_component = false;
1344
1345 void Extract(xml::Element* element) override {
1346 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1347 }
1348};
1349
1350/**Represents <service> elements. **/
1351class Service : public ManifestExtractor::Element {
1352 public:
1353 Service() = default;
1354 const std::string* permission = nullptr;
1355 bool has_component = false;
1356
1357 void Extract(xml::Element* element) override {
1358 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1359 }
1360};
1361
1362/** Represents <uses-library> elements. **/
1363class UsesLibrary : public ManifestExtractor::Element {
1364 public:
1365 UsesLibrary() = default;
1366 std::string name;
1367 int required;
1368
1369 void Extract(xml::Element* element) override {
1370 auto parent_stack = extractor()->parent_stack();
1371 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1372 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1373 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1374 }
1375 }
1376
Ryan Mitchell214846d2018-09-19 16:57:01 -07001377 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001378 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001379 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001380 (required == 0) ? "-not-required" : "", name.data()));
1381 }
1382 }
1383};
1384
Dianne Hackborn813d7502018-10-02 16:59:46 -07001385/** Represents <static-library> elements. **/
1386class StaticLibrary : public ManifestExtractor::Element {
1387 public:
1388 StaticLibrary() = default;
1389 std::string name;
1390 int version;
1391 int versionMajor;
1392
1393 void Extract(xml::Element* element) override {
1394 auto parent_stack = extractor()->parent_stack();
1395 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1396 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1397 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1398 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1399 }
1400 }
1401
Ryan Mitchell214846d2018-09-19 16:57:01 -07001402 void Print(text::Printer* printer) override {
1403 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001404 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1405 name.data(), version, versionMajor));
1406 }
1407};
1408
1409/** Represents <uses-static-library> elements. **/
1410class UsesStaticLibrary : public ManifestExtractor::Element {
1411 public:
1412 UsesStaticLibrary() = default;
1413 std::string name;
1414 int version;
1415 int versionMajor;
1416 std::vector<std::string> certDigests;
1417
1418 void Extract(xml::Element* element) override {
1419 auto parent_stack = extractor()->parent_stack();
1420 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1421 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1422 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1423 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1424 AddCertDigest(element);
1425 }
1426 }
1427
1428 void AddCertDigest(xml::Element* element) {
1429 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1430 // We allow ":" delimiters in the SHA declaration as this is the format
1431 // emitted by the certtool making it easy for developers to copy/paste.
1432 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1433 if (!digest.empty()) {
1434 certDigests.push_back(digest);
1435 }
1436 }
1437
Ryan Mitchell214846d2018-09-19 16:57:01 -07001438 void Print(text::Printer* printer) override {
1439 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001440 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1441 name.data(), version, versionMajor));
1442 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001443 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001444 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001445 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001446 }
1447};
1448
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001449/** Represents <uses-native-library> elements. **/
1450class UsesNativeLibrary : public ManifestExtractor::Element {
1451 public:
1452 UsesNativeLibrary() = default;
1453 std::string name;
1454 int required;
1455
1456 void Extract(xml::Element* element) override {
1457 auto parent_stack = extractor()->parent_stack();
1458 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1459 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1460 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1461 }
1462 }
1463
1464 void Print(text::Printer* printer) override {
1465 if (!name.empty()) {
1466 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1467 (required == 0) ? "-not-required" : "", name.data()));
1468 }
1469 }
1470};
1471
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001472/**
1473 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1474 * explicitly enable meta data printing.
1475 **/
1476class MetaData : public ManifestExtractor::Element {
1477 public:
1478 MetaData() = default;
1479 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001480 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001481 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001482 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001483 const int* resource_int;
1484
1485 void Extract(xml::Element* element) override {
1486 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001487 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001488 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001489 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001490 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1491 }
1492
Ryan Mitchell214846d2018-09-19 16:57:01 -07001493 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001494 if (extractor()->options_.include_meta_data && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001495 printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001496 if (!value.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001497 printer->Print(StringPrintf("value='%s' ", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001498 } else if (value_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001499 printer->Print(StringPrintf("value='%d' ", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001500 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001501 if (!resource.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001502 printer->Print(StringPrintf("resource='%s' ", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001503 } else if (resource_int) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001504 printer->Print(StringPrintf("resource='%d' ", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001505 }
1506 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001507 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001508 }
1509 }
1510};
1511
1512/**
1513 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1514 * service components.
1515 **/
1516class Action : public ManifestExtractor::Element {
1517 public:
1518 Action() = default;
1519 std::string component = "";
1520
1521 void Extract(xml::Element* element) override {
1522 auto parent_stack = extractor()->parent_stack();
1523 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1524
1525 if (ElementCast<IntentFilter>(parent_stack[0])) {
1526 if (ElementCast<Activity>(parent_stack[1])) {
1527 // Detects the presence of a particular type of activity.
1528 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1529 auto map = std::map<std::string, std::string>({
1530 { "android.intent.action.MAIN" , "main" },
1531 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1532 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1533 });
1534
1535 auto entry = map.find(action);
1536 if (entry != map.end()) {
1537 component = entry->second;
1538 activity->has_component_ = true;
1539 }
1540
1541 if (action == "android.intent.action.MAIN") {
1542 activity->has_main_action = true;
1543 }
1544
1545 } else if (ElementCast<Receiver>(parent_stack[1])) {
1546 // Detects the presence of a particular type of receiver. If the action requires a
1547 // permission, then the receiver element is checked for the permission.
1548 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
1549 auto map = std::map<std::string, std::string>({
1550 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
1551 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
1552 });
1553
1554 auto permissions = std::map<std::string, std::string>({
1555 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
1556 });
1557
1558 auto entry = map.find(action);
1559 auto permission = permissions.find(action);
1560 if (entry != map.end() && (permission == permissions.end()
1561 || (receiver->permission && permission->second == *receiver->permission))) {
1562 receiver->has_component = true;
1563 component = entry->second;
1564 }
1565
1566 } else if (ElementCast<Service>(parent_stack[1])) {
1567 // Detects the presence of a particular type of service. If the action requires a
1568 // permission, then the service element is checked for the permission.
1569 Service* service = ElementCast<Service>(parent_stack[1]);
1570 auto map = std::map<std::string, std::string>({
1571 { "android.view.InputMethod" , "ime" },
1572 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
1573 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
1574 { "android.printservice.PrintService" , "print-service" },
1575 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
1576 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
1577 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
1578 { "android.service.dreams.DreamService" , "dream" },
1579 });
1580
1581 auto permissions = std::map<std::string, std::string>({
1582 { "android.accessibilityservice.AccessibilityService" ,
1583 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
1584 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
1585 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
1586 "android.permission.BIND_NFC_SERVICE" },
1587 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
1588 "android.permission.BIND_NFC_SERVICE" },
1589 { "android.service.notification.NotificationListenerService" ,
1590 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
1591 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
1592 });
1593
1594 auto entry = map.find(action);
1595 auto permission = permissions.find(action);
1596 if (entry != map.end() && (permission == permissions.end()
1597 || (service->permission && permission->second == *service->permission))) {
1598 service->has_component= true;
1599 component = entry->second;
1600 }
1601
1602 } else if (ElementCast<Provider>(parent_stack[1])) {
1603 // Detects the presence of a particular type of receiver. If the provider requires a
1604 // permission, then the provider element is checked for the permission.
1605 // Detect whether this action
1606 Provider* provider = ElementCast<Provider>(parent_stack[1]);
1607 if (action == "android.content.action.DOCUMENTS_PROVIDER"
1608 && provider->has_required_saf_attributes) {
1609 component = "document-provider";
1610 }
1611 }
1612 }
1613
1614 // Represents a searchable interface
1615 if (action == "android.intent.action.SEARCH") {
1616 component = "search";
1617 }
1618 }
1619};
1620
1621/**
1622 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
1623 **/
1624class SupportsInput : public ManifestExtractor::Element {
1625 public:
1626 SupportsInput() = default;
1627 std::vector<std::string> inputs;
1628
Ryan Mitchell214846d2018-09-19 16:57:01 -07001629 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001630 const size_t size = inputs.size();
1631 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001632 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001633 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001634 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001635 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001636 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001637 }
1638 }
1639};
1640
1641/** Represents <input-type> elements. **/
1642class InputType : public ManifestExtractor::Element {
1643 public:
1644 InputType() = default;
1645 void Extract(xml::Element* element) override {
1646 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1647 auto parent_stack = extractor()->parent_stack();
1648
1649 // Add the input to the set of supported inputs
1650 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
1651 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
1652 supports->inputs.push_back(*name);
1653 }
1654 }
1655};
1656
1657/** Represents <original-package> elements. **/
1658class OriginalPackage : public ManifestExtractor::Element {
1659 public:
1660 OriginalPackage() = default;
1661 const std::string* name = nullptr;
1662
1663 void Extract(xml::Element* element) override {
1664 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1665 }
1666
Ryan Mitchell214846d2018-09-19 16:57:01 -07001667 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001668 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001669 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001670 }
1671 }
1672};
1673
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00001674
1675/** Represents <overlay> elements. **/
1676class Overlay : public ManifestExtractor::Element {
1677 public:
1678 Overlay() = default;
1679 const std::string* target_package = nullptr;
1680 int priority;
1681 bool is_static;
1682 const std::string* required_property_name = nullptr;
1683 const std::string* required_property_value = nullptr;
1684
1685 void Extract(xml::Element* element) override {
1686 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
1687 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
1688 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
1689 required_property_name = GetAttributeString(
1690 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
1691 required_property_value = GetAttributeString(
1692 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
1693 }
1694
1695 void Print(text::Printer* printer) override {
1696 printer->Print(StringPrintf("overlay:"));
1697 if (target_package) {
1698 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
1699 }
1700 printer->Print(StringPrintf(" priority='%d'", priority));
1701 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
1702 if (required_property_name) {
1703 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
1704 }
1705 if (required_property_value) {
1706 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
1707 }
1708 printer->Print("\n");
1709 }
1710};
1711
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001712/** * Represents <package-verifier> elements. **/
1713class PackageVerifier : public ManifestExtractor::Element {
1714 public:
1715 PackageVerifier() = default;
1716 const std::string* name = nullptr;
1717 const std::string* public_key = nullptr;
1718
1719 void Extract(xml::Element* element) override {
1720 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1721 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
1722 }
1723
Ryan Mitchell214846d2018-09-19 16:57:01 -07001724 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001725 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001726 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001727 name->data(), public_key->data()));
1728 }
1729 }
1730};
1731
1732/** Represents <uses-package> elements. **/
1733class UsesPackage : public ManifestExtractor::Element {
1734 public:
1735 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001736 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001737 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07001738 int version;
1739 int versionMajor;
1740 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001741
1742 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001743 auto parent_stack = extractor()->parent_stack();
1744 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1745 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
1746 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1747 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1748 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1749 AddCertDigest(element);
1750 }
1751 }
1752
1753 void AddCertDigest(xml::Element* element) {
1754 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1755 // We allow ":" delimiters in the SHA declaration as this is the format
1756 // emitted by the certtool making it easy for developers to copy/paste.
1757 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1758 if (!digest.empty()) {
1759 certDigests.push_back(digest);
1760 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001761 }
1762
Ryan Mitchell214846d2018-09-19 16:57:01 -07001763 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001764 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07001765 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001766 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001767 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
1768 packageType->data(), name->data(), version, versionMajor));
1769 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001770 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001771 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001772 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001773 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001774 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001775 }
1776 }
1777 }
1778};
1779
1780/** Represents <additional-certificate> elements. **/
1781class AdditionalCertificate : public ManifestExtractor::Element {
1782 public:
1783 AdditionalCertificate() = default;
1784
1785 void Extract(xml::Element* element) override {
1786 auto parent_stack = extractor()->parent_stack();
1787 if (parent_stack.size() > 0) {
1788 if (ElementCast<UsesPackage>(parent_stack[0])) {
1789 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
1790 uses->AddCertDigest(element);
1791 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
1792 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
1793 uses->AddCertDigest(element);
1794 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001795 }
1796 }
1797};
1798
1799/** Represents <screen> elements found in <compatible-screens> elements. */
1800class Screen : public ManifestExtractor::Element {
1801 public:
1802 Screen() = default;
1803 const int32_t* size = nullptr;
1804 const int32_t* density = nullptr;
1805
1806 void Extract(xml::Element* element) override {
1807 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
1808 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
1809 }
1810};
1811
1812/**
1813 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
1814 * that each denote a supported screen size and screen density.
1815 **/
1816class CompatibleScreens : public ManifestExtractor::Element {
1817 public:
1818 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001819 void Print(text::Printer* printer) override {
1820 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001821
1822 bool first = true;
1823 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
1824 if (auto screen = ElementCast<Screen>(el)) {
1825 if (first) {
1826 first = false;
1827 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001828 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001829 }
1830
1831 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001832 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001833 }
1834 }
1835 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07001836 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001837 }
1838};
1839
1840/** Represents <supports-gl-texture> elements. **/
1841class SupportsGlTexture : public ManifestExtractor::Element {
1842 public:
1843 SupportsGlTexture() = default;
1844 const std::string* name = nullptr;
1845
1846 void Extract(xml::Element* element) override {
1847 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1848 }
1849
Ryan Mitchell214846d2018-09-19 16:57:01 -07001850 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001851 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001852 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001853 }
1854 }
1855};
1856
Todd Kennedyce3e1292020-10-29 17:14:24 -07001857/** Represents <property> elements. **/
1858class Property : public ManifestExtractor::Element {
1859 public:
1860 Property() = default;
1861 std::string name;
1862 std::string value;
1863 const int* value_int;
1864 std::string resource;
1865 const int* resource_int;
1866
1867 void Extract(xml::Element* element) override {
1868 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1869 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
1870 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
1871 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
1872 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1873 }
1874
1875 void Print(text::Printer* printer) override {
1876 printer->Print(StringPrintf("property: name='%s' ", name.data()));
1877 if (!value.empty()) {
1878 printer->Print(StringPrintf("value='%s' ", value.data()));
1879 } else if (value_int) {
1880 printer->Print(StringPrintf("value='%d' ", *value_int));
1881 } else {
1882 if (!resource.empty()) {
1883 printer->Print(StringPrintf("resource='%s' ", resource.data()));
1884 } else if (resource_int) {
1885 printer->Print(StringPrintf("resource='%d' ", *resource_int));
1886 }
1887 }
1888 printer->Print("\n");
1889 }
1890};
1891
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001892/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07001893static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001894 el->Print(printer);
1895 for (auto &child : el->children()) {
1896 Print(child.get(), printer);
1897 }
1898}
1899
Ryan Mitchell214846d2018-09-19 16:57:01 -07001900bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001901 // Load the manifest
1902 std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
1903 if (doc == nullptr) {
1904 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
1905 return false;
1906 }
1907
1908 xml::Element* element = doc->root.get();
1909 if (element->name != "manifest") {
1910 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
1911 return false;
1912 }
1913
1914 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
1915 // printing only permission elements is requested
1916 if (options_.only_permissions) {
1917 std::unique_ptr<ManifestExtractor::Element> manifest_element =
1918 ManifestExtractor::Element::Inflate(this, element);
1919
1920 if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
1921 for (xml::Element* child : element->GetChildElements()) {
1922 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
1923 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001924 // Inflate the element and its descendants
1925 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001926 manifest->AddChild(permission_element);
1927 }
1928 }
1929
Ryan Mitchell214846d2018-09-19 16:57:01 -07001930 printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001931 ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
1932 el->Print(printer);
1933 });
1934
1935 return true;
1936 }
1937
1938 return false;
1939 }
1940
1941 // Collect information about the resource configurations
1942 if (apk_->GetResourceTable()) {
1943 for (auto &package : apk_->GetResourceTable()->packages) {
1944 for (auto &type : package->types) {
1945 for (auto &entry : type->entries) {
1946 for (auto &value : entry->values) {
1947 std::string locale_str = value->config.GetBcp47LanguageTag();
1948
1949 // Collect all the unique locales of the apk
1950 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001951 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001952 config.setBcp47Locale(locale_str.data());
1953 locales_.insert(std::make_pair(locale_str, config));
1954 }
1955
1956 // Collect all the unique density of the apk
1957 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
1958 : value->config.density;
1959 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07001960 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001961 config.density = density;
1962 densities_.insert(std::make_pair(density, config));
1963 }
1964 }
1965 }
1966 }
1967 }
1968 }
1969
1970 // Extract badging information
1971 auto root = Visit(element);
1972
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07001973 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
1974 // attribute values from the last defined tag.
1975 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
1976 for (const auto& child : root->children()) {
1977 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
1978 filtered_uses_sdk_tags.emplace_back(uses_sdk);
1979 }
1980 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07001981 if (filtered_uses_sdk_tags.size() >= 2U) {
1982 filtered_uses_sdk_tags.pop_back();
1983 root->Filter([&](const ManifestExtractor::Element* e) {
1984 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
1985 filtered_uses_sdk_tags.end();
1986 });
1987 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07001988
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001989 // Print the elements in order seen
1990 Print(root.get(), printer);
1991
1992 /** Recursively checks the extracted elements for the specified permission. **/
1993 auto FindPermission = [&](ManifestExtractor::Element* root,
1994 const std::string& name) -> ManifestExtractor::Element* {
1995 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
1996 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
1997 return permission->name == name;
1998 }
1999 return false;
2000 });
2001 };
2002
2003 auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
2004 int32_t max_sdk_version) -> void {
2005 auto permission = util::make_unique<UsesPermission>();
2006 permission->name = name;
2007 permission->maxSdkVersion = max_sdk_version;
2008 permission->Print(printer);
2009 permission->PrintImplied(printer, reason);
2010 };
2011
2012 // Implied permissions
2013 // Pre-1.6 implicitly granted permission compatibility logic
2014 CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
2015 bool insert_write_external = false;
2016 auto write_external_permission = ElementCast<UsesPermission>(
2017 FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
2018
2019 if (target_sdk() < 4) {
2020 if (!write_external_permission) {
2021 PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
2022 insert_write_external = true;
2023 }
2024
2025 if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
2026 PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
2027 }
2028 }
2029
2030 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2031 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2032 // do this (regardless of target API version) because we can't have
2033 // an app with write permission but not read permission.
2034 auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
2035 if (!read_external && (insert_write_external || write_external_permission)) {
2036 PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
2037 "requested WRITE_EXTERNAL_STORAGE",
2038 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
2039 }
2040
2041 // Pre-JellyBean call log permission compatibility.
2042 if (target_sdk() < 16) {
2043 if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
2044 && FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
2045 PrintPermission("android.permission.READ_CALL_LOG",
2046 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
2047 }
2048
2049 if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
2050 && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
2051 PrintPermission("android.permission.WRITE_CALL_LOG",
2052 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
2053 }
2054 }
2055
2056 // If the app hasn't declared the touchscreen as a feature requirement (either
2057 // directly or implied, required or not), then the faketouch feature is implied.
2058 if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
2059 common_feature_group->addImpliedFeature("android.hardware.faketouch",
2060 "default feature for all apps", false);
2061 }
2062
2063 // Only print the common feature group if no feature group is defined
2064 std::vector<FeatureGroup*> feature_groups;
2065 ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
2066 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2067 feature_groups.push_back(feature_group);
2068 }
2069 });
2070
2071 if (feature_groups.empty()) {
2072 common_feature_group->PrintGroup(printer);
2073 } else {
2074 // Merge the common feature group into the feature group
2075 for (auto& feature_group : feature_groups) {
2076 feature_group->open_gles_version = std::max(feature_group->open_gles_version,
2077 common_feature_group->open_gles_version);
2078 feature_group->Merge(common_feature_group);
2079 feature_group->PrintGroup(printer);
2080 }
2081 };
2082
2083 // Collect the component types of the application
2084 std::set<std::string> components;
2085 ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
2086 if (ElementCast<Action>(el)) {
2087 auto action = ElementCast<Action>(el);
2088 if (!action->component.empty()) {
2089 components.insert(action->component);
2090 return;
2091 }
2092 }
2093
2094 if (ElementCast<Category>(el)) {
2095 auto category = ElementCast<Category>(el);
2096 if (!category->component.empty()) {
2097 components.insert(category->component);
2098 return;
2099 }
2100 }
2101 });
2102
2103 // Check for the payment component
2104 auto apk = apk_;
2105 ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
2106 if (auto service = ElementCast<Service>(el)) {
2107 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2108 [&](ManifestExtractor::Element* el) -> bool {
2109 if (auto action = ElementCast<Action>(el)) {
2110 return (action->component == "host-apdu");
2111 }
2112 return false;
2113 }));
2114
2115 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2116 [&](ManifestExtractor::Element* el) -> bool {
2117 if (auto action = ElementCast<Action>(el)) {
2118 return (action->component == "offhost-apdu");
2119 }
2120 return false;
2121 }));
2122
2123 ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
2124 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2125 if (auto meta_data = ElementCast<MetaData>(el)) {
2126 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
2127 || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
2128 && offhost_apdu_action)) {
2129
2130 // Attempt to load the resource file
Ryan Mitchell2250c932018-10-04 11:07:40 -07002131 if (!meta_data->resource.empty()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002132 return;
2133 }
Ryan Mitchell2250c932018-10-04 11:07:40 -07002134 auto resource = apk->LoadXml(meta_data->resource, diag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002135 if (!resource) {
2136 return;
2137 }
2138
2139 // Look for the payment category on an <aid-group> element
2140 auto& root = resource.get()->root;
2141 if ((host_apdu_action && root->name == "host-apdu-service")
2142 || (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2143
2144 for (auto& child : root->GetChildElements()) {
2145 if (child->name == "aid-group") {
2146 auto category = FindAttribute(child, CATEGORY_ATTR);
2147 if (category && category->value == "payment") {
2148 components.insert("payment");
2149 return;
2150 }
2151 }
2152 }
2153 }
2154 }
2155 }
2156 });
2157 }
2158 });
2159
2160 // Print the components types if they are present
2161 auto PrintComponent = [&components, &printer](const std::string& component) -> void {
2162 if (components.find(component) != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002163 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002164 }
2165 };
2166
2167 PrintComponent("app-widget");
2168 PrintComponent("device-admin");
2169 PrintComponent("ime");
2170 PrintComponent("wallpaper");
2171 PrintComponent("accessibility");
2172 PrintComponent("print-service");
2173 PrintComponent("payment");
2174 PrintComponent("search");
2175 PrintComponent("document-provider");
2176 PrintComponent("launcher");
2177 PrintComponent("notification-listener");
2178 PrintComponent("dream");
2179 PrintComponent("camera");
2180 PrintComponent("camera-secure");
2181
2182 // Print presence of main activity
2183 if (components.find("main") != components.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002184 printer->Print("main\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002185 }
2186
2187 // Print presence of activities, recivers, and services with no special components
2188 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2189 if (auto activity = ElementCast<Activity>(el)) {
2190 if (!activity->has_component_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002191 printer->Print("other-activities\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002192 return true;
2193 }
2194 }
2195 return false;
2196 });
2197
2198 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2199 if (auto receiver = ElementCast<Receiver>(el)) {
2200 if (!receiver->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002201 printer->Print("other-receivers\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002202 return true;
2203 }
2204 }
2205 return false;
2206 });
2207
2208 FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
2209 if (auto service = ElementCast<Service>(el)) {
2210 if (!service->has_component) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002211 printer->Print("other-services\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002212 return true;
2213 }
2214 }
2215 return false;
2216 });
2217
2218 // Print the supported screens
2219 SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
2220 [&](ManifestExtractor::Element* el) -> bool {
2221 return ElementCast<SupportsScreen>(el) != nullptr;
2222 }));
2223
2224 if (screen) {
2225 screen->PrintScreens(printer, target_sdk_);
2226 } else {
2227 // Print the default supported screens
2228 SupportsScreen default_screens;
2229 default_screens.PrintScreens(printer, target_sdk_);
2230 }
2231
2232 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002233 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002234 for (auto& config : locales_) {
2235 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002236 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002237 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002238 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002239 }
2240 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002241 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002242
2243 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002244 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002245 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002246 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002247 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002248 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002249
2250 // Print the supported architectures of the app
2251 std::set<std::string> architectures;
2252 auto it = apk_->GetFileCollection()->Iterator();
2253 while (it->HasNext()) {
2254 auto file_path = it->Next()->GetSource().path;
2255
2256
2257 size_t pos = file_path.find("lib/");
2258 if (pos != std::string::npos) {
2259 file_path = file_path.substr(pos + 4);
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08002260 pos = file_path.find('/');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002261 if (pos != std::string::npos) {
2262 file_path = file_path.substr(0, pos);
2263 }
2264
2265 architectures.insert(file_path);
2266 }
2267 }
2268
2269 // Determine if the application has multiArch supports
2270 auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
2271 if (auto application = ElementCast<Application>(el)) {
2272 return application->has_multi_arch;
2273 }
2274 return false;
2275 });
2276
2277 bool output_alt_native_code = false;
2278 // A multiArch package is one that contains 64-bit and
2279 // 32-bit versions of native code and expects 3rd-party
2280 // apps to load these native code libraries. Since most
2281 // 64-bit systems also support 32-bit apps, the apps
2282 // loading this multiArch package's code may be either
2283 if (has_multi_arch) {
2284 // If this is a multiArch package, report the 64-bit
2285 // version only. Then as a separate entry, report the
2286 // rest.
2287 //
2288 // If we report the 32-bit architecture, this APK will
2289 // be installed on a 32-bit device, causing a large waste
2290 // of bandwidth and disk space. This assumes that
2291 // the developer of the multiArch package has also
2292 // made a version that is 32-bit only.
2293 const std::string kIntel64 = "x86_64";
2294 const std::string kArm64 = "arm64-v8a";
2295
2296 auto arch = architectures.find(kIntel64);
2297 if (arch == architectures.end()) {
2298 arch = architectures.find(kArm64);
2299 }
2300
2301 if (arch != architectures.end()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002302 printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002303 architectures.erase(arch);
2304 output_alt_native_code = true;
2305 }
2306 }
2307
2308 if (architectures.size() > 0) {
2309 if (output_alt_native_code) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002310 printer->Print("alt-");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002311 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002312 printer->Print("native-code:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002313 for (auto& arch : architectures) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002314 printer->Print(StringPrintf(" '%s'", arch.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002315 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002316 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002317 }
2318
2319 return true;
2320}
2321
2322/**
2323 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2324 * pointer.
2325 **/
2326template<typename T>
2327T* ElementCast(ManifestExtractor::Element* element) {
2328 if (element == nullptr) {
2329 return nullptr;
2330 }
2331
2332 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002333 {"action", std::is_base_of<Action, T>::value},
2334 {"activity", std::is_base_of<Activity, T>::value},
2335 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2336 {"application", std::is_base_of<Application, T>::value},
2337 {"category", std::is_base_of<Category, T>::value},
2338 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2339 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2340 {"input-type", std::is_base_of<InputType, T>::value},
2341 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2342 {"meta-data", std::is_base_of<MetaData, T>::value},
2343 {"manifest", std::is_base_of<Manifest, T>::value},
2344 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2345 {"overlay", std::is_base_of<Overlay, T>::value},
2346 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2347 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002348 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002349 {"provider", std::is_base_of<Provider, T>::value},
2350 {"receiver", std::is_base_of<Receiver, T>::value},
2351 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2352 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2353 {"screen", std::is_base_of<Screen, T>::value},
2354 {"service", std::is_base_of<Service, T>::value},
2355 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2356 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2357 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2358 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2359 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2360 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2361 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2362 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2363 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2364 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2365 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2366 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
2367 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002368 };
2369
2370 auto check = kTagCheck.find(element->tag());
2371 if (check != kTagCheck.end() && check->second) {
2372 return static_cast<T*>(element);
2373 }
2374 return nullptr;
2375}
2376
2377template<typename T>
2378std::unique_ptr<T> CreateType() {
2379 return std::move(util::make_unique<T>());
2380}
2381
2382std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2383 ManifestExtractor* extractor, xml::Element* el) {
2384 const std::unordered_map<std::string,
2385 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2386 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002387 {"action", &CreateType<Action>},
2388 {"activity", &CreateType<Activity>},
2389 {"additional-certificate", &CreateType<AdditionalCertificate>},
2390 {"application", &CreateType<Application>},
2391 {"category", &CreateType<Category>},
2392 {"compatible-screens", &CreateType<CompatibleScreens>},
2393 {"feature-group", &CreateType<FeatureGroup>},
2394 {"input-type", &CreateType<InputType>},
2395 {"intent-filter", &CreateType<IntentFilter>},
2396 {"manifest", &CreateType<Manifest>},
2397 {"meta-data", &CreateType<MetaData>},
2398 {"original-package", &CreateType<OriginalPackage>},
2399 {"overlay", &CreateType<Overlay>},
2400 {"package-verifier", &CreateType<PackageVerifier>},
2401 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002402 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002403 {"provider", &CreateType<Provider>},
2404 {"receiver", &CreateType<Receiver>},
2405 {"required-feature", &CreateType<RequiredFeature>},
2406 {"required-not-feature", &CreateType<RequiredNotFeature>},
2407 {"screen", &CreateType<Screen>},
2408 {"service", &CreateType<Service>},
2409 {"static-library", &CreateType<StaticLibrary>},
2410 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2411 {"supports-input", &CreateType<SupportsInput>},
2412 {"supports-screens", &CreateType<SupportsScreen>},
2413 {"uses-configuration", &CreateType<UsesConfiguarion>},
2414 {"uses-feature", &CreateType<UsesFeature>},
2415 {"uses-library", &CreateType<UsesLibrary>},
2416 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2417 {"uses-package", &CreateType<UsesPackage>},
2418 {"uses-permission", &CreateType<UsesPermission>},
2419 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2420 {"uses-sdk", &CreateType<UsesSdkBadging>},
2421 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2422 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002423
2424 // Attempt to map the xml tag to a element inflater
2425 std::unique_ptr<ManifestExtractor::Element> element;
2426 auto check = kTagCheck.find(el->name);
2427 if (check != kTagCheck.end()) {
2428 element = check->second();
2429 } else {
2430 element = util::make_unique<ManifestExtractor::Element>();
2431 }
2432
2433 element->extractor_ = extractor;
2434 element->tag_ = el->name;
2435 element->Extract(el);
2436 return element;
2437}
2438
2439std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2440 auto element = ManifestExtractor::Element::Inflate(this, el);
2441 parent_stack_.insert(parent_stack_.begin(), element.get());
2442
2443 // Process the element and recursively visit the children
2444 for (xml::Element* child : el->GetChildElements()) {
2445 auto v = Visit(child);
2446 element->AddChild(v);
2447 }
2448
2449 parent_stack_.erase(parent_stack_.begin());
2450 return element;
2451}
2452
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002453
Ryan Mitchell214846d2018-09-19 16:57:01 -07002454int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2455 IDiagnostics* diag) {
2456 ManifestExtractor extractor(apk, options);
Ryan Mitchell28c88802019-03-28 11:28:54 -07002457 return extractor.Dump(printer, diag) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002458}
2459
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02002460} // namespace aapt