blob: e36112d6126c23666b017d0bee4a7e03400d413c [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>
Iurii Makhno85875a82022-04-26 15:30:01 +000020#include <array>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000021#include <memory>
22#include <set>
Iurii Makhno85875a82022-04-26 15:30:01 +000023#include <string_view>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000024#include <vector>
Dianne Hackborn813d7502018-10-02 16:59:46 -070025
Ryan Mitchellfc225b22018-08-21 14:52:51 -070026#include "LoadedApk.h"
27#include "SdkConstants.h"
28#include "ValueVisitor.h"
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000029#include "androidfw/ConfigDescription.h"
Ryan Mitchellfc225b22018-08-21 14:52:51 -070030#include "io/File.h"
31#include "io/FileStream.h"
32#include "process/IResourceTableConsumer.h"
33#include "xml/XmlDom.h"
34
35using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020036using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070037
38namespace aapt {
39
40/**
41 * These are attribute resource constants for the platform, as found in android.R.attr.
42 */
43enum {
44 LABEL_ATTR = 0x01010001,
45 ICON_ATTR = 0x01010002,
46 NAME_ATTR = 0x01010003,
47 PERMISSION_ATTR = 0x01010006,
48 EXPORTED_ATTR = 0x01010010,
49 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000050 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070051 RESOURCE_ATTR = 0x01010025,
52 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000053 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070054 VALUE_ATTR = 0x01010024,
55 VERSION_CODE_ATTR = 0x0101021b,
56 VERSION_NAME_ATTR = 0x0101021c,
57 SCREEN_ORIENTATION_ATTR = 0x0101001e,
58 MIN_SDK_VERSION_ATTR = 0x0101020c,
59 MAX_SDK_VERSION_ATTR = 0x01010271,
60 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
61 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
62 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
63 REQ_NAVIGATION_ATTR = 0x0101022a,
64 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
65 TARGET_SDK_VERSION_ATTR = 0x01010270,
66 TEST_ONLY_ATTR = 0x01010272,
67 ANY_DENSITY_ATTR = 0x0101026c,
68 GL_ES_VERSION_ATTR = 0x01010281,
69 SMALL_SCREEN_ATTR = 0x01010284,
70 NORMAL_SCREEN_ATTR = 0x01010285,
71 LARGE_SCREEN_ATTR = 0x01010286,
72 XLARGE_SCREEN_ATTR = 0x010102bf,
73 REQUIRED_ATTR = 0x0101028e,
74 INSTALL_LOCATION_ATTR = 0x010102b7,
75 SCREEN_SIZE_ATTR = 0x010102ca,
76 SCREEN_DENSITY_ATTR = 0x010102cb,
77 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
78 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
79 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
80 PUBLIC_KEY_ATTR = 0x010103a6,
81 CATEGORY_ATTR = 0x010103e8,
82 BANNER_ATTR = 0x10103f2,
83 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070084 VERSION_ATTR = 0x01010519,
85 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000086 REQUIRED_FEATURE_ATTR = 0x01010554,
87 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000088 IS_STATIC_ATTR = 0x0101055a,
89 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
90 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070091 COMPILE_SDK_VERSION_ATTR = 0x01010572,
92 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070093 VERSION_MAJOR_ATTR = 0x01010577,
94 PACKAGE_TYPE_ATTR = 0x01010587,
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060095 USES_PERMISSION_FLAGS_ATTR = 0x01010644,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070096};
97
98const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060099constexpr int kNeverForLocation = 0x00010000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700100
101/** Retrieves the attribute of the element with the specified attribute resource id. */
102static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
103 for (auto& a : el->attributes) {
104 if (a.compiled_attribute && a.compiled_attribute.value().id) {
105 if (a.compiled_attribute.value().id.value() == resd_id) {
106 return std::move(&a);
107 }
108 }
109 }
110 return nullptr;
111}
112
113/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
114static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
115 const std::string &name) {
116 return el->FindAttribute(package, name);
117}
118
Iurii Makhno85875a82022-04-26 15:30:01 +0000119class Architectures {
120 public:
121 std::set<std::string> architectures;
122 std::set<std::string> alt_architectures;
123
124 void Print(text::Printer* printer) {
125 if (!architectures.empty()) {
126 printer->Print("native-code:");
127 for (auto& arch : architectures) {
128 printer->Print(StringPrintf(" '%s'", arch.data()));
129 }
130 printer->Print("\n");
131 }
132 if (!alt_architectures.empty()) {
133 printer->Print("alt-native-code:");
134 for (auto& arch : alt_architectures) {
135 printer->Print(StringPrintf(" '%s'", arch.data()));
136 }
137 printer->Print("\n");
138 }
139 }
140
141 void ToProto(pb::Badging* out_badging) {
142 auto out_architectures = out_badging->mutable_architectures();
143 for (auto& arch : architectures) {
144 out_architectures->add_architectures(arch);
145 }
146 for (auto& arch : alt_architectures) {
147 out_architectures->add_alt_architectures(arch);
148 }
149 }
150};
151
152const static std::array<std::string_view, 14> printable_components{"app-widget",
153 "device-admin",
154 "ime",
155 "wallpaper",
156 "accessibility",
157 "print-service",
158 "payment",
159 "search",
160 "document-provider",
161 "launcher",
162 "notification-listener",
163 "dream",
164 "camera",
165 "camera-secure"};
166
167class Components {
168 public:
169 std::set<std::string, std::less<>> discovered_components;
170 bool other_activities = false;
171 bool other_receivers = false;
172 bool other_services = false;
173
174 void Print(text::Printer* printer) {
175 for (auto& component : printable_components) {
176 if (discovered_components.find(component) != discovered_components.end()) {
177 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
178 }
179 }
180 // Print presence of main activity
181 if (discovered_components.find("main") != discovered_components.end()) {
182 printer->Print("main\n");
183 }
184
185 if (other_activities) {
186 printer->Print("other-activities\n");
187 }
188 if (other_receivers) {
189 printer->Print("other-receivers\n");
190 }
191 if (other_services) {
192 printer->Print("other-services\n");
193 }
194 }
195
196 void ToProto(pb::Badging* out_badging) {
197 auto out_components = out_badging->mutable_components();
198 for (auto& component : printable_components) {
199 auto discovered = discovered_components.find(component);
200 if (discovered != discovered_components.end()) {
201 out_components->add_provided_components(*discovered);
202 }
203 }
204 out_components->set_main(discovered_components.find("main") != discovered_components.end());
205 out_components->set_other_activities(other_activities);
206 out_components->set_other_receivers(other_receivers);
207 out_components->set_other_services(other_services);
208 }
209};
210
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700211class CommonFeatureGroup;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000212class FeatureGroup;
Iurii Makhno85875a82022-04-26 15:30:01 +0000213class SupportsScreen;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700214
215class ManifestExtractor {
216 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700217
Ryan Mitchell214846d2018-09-19 16:57:01 -0700218 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700219 : apk_(apk), options_(options) { }
220
221 class Element {
222 public:
223 Element() = default;
224 virtual ~Element() = default;
225
226 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
227
228 /** Writes out the extracted contents of the element. */
Iurii Makhno85875a82022-04-26 15:30:01 +0000229 virtual void Print(text::Printer* printer) {
230 }
231
232 /** Saves extracted information into Badging proto. */
233 virtual void ToProto(pb::Badging* out_badging) {
234 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700235
236 /** Adds an element to the list of children of the element. */
237 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
238
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700239 template <typename Predicate>
240 void Filter(Predicate&& func) {
241 children_.erase(std::remove_if(children_.begin(), children_.end(),
Kelvin Zhang3965584d2021-05-10 12:17:14 -0400242 [&](const auto& e) { return func(e.get()); }),
243 children_.end());
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700244 }
245
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700246 /** Retrieves the list of children of the element. */
247 const std::vector<std::unique_ptr<Element>>& children() const {
248 return children_;
249 }
250
251 /** Retrieves the extracted xml element tag. */
252 const std::string tag() const {
253 return tag_;
254 }
255
256 protected:
257 ManifestExtractor* extractor() const {
258 return extractor_;
259 }
260
261 /** Retrieves and stores the information extracted from the xml element. */
262 virtual void Extract(xml::Element* el) { }
263
264 /*
265 * Retrieves a configuration value of the resource entry that best matches the specified
266 * configuration.
267 */
268 static Value* BestConfigValue(ResourceEntry* entry,
269 const ConfigDescription& match) {
270 if (!entry) {
271 return nullptr;
272 }
273
274 // Determine the config that best matches the desired config
275 ResourceConfigValue* best_value = nullptr;
276 for (auto& value : entry->values) {
277 if (!value->config.match(match)) {
278 continue;
279 }
280
281 if (best_value != nullptr) {
282 if (!value->config.isBetterThan(best_value->config, &match)) {
283 if (value->config.compare(best_value->config) != 0) {
284 continue;
285 }
286 }
287 }
288
289 best_value = value.get();
290 }
291
292 // The entry has no values
293 if (!best_value) {
294 return nullptr;
295 }
296
297 return best_value->value.get();
298 }
299
300 /** Retrieves the resource assigned to the specified resource id if one exists. */
301 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700302 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700303 if (table) {
304 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700305 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700306 for (auto& entry : type->entries) {
307 if (entry->id && entry->id.value() == res_id.id) {
308 if (auto value = BestConfigValue(entry.get(), config)) {
309 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700310 }
311 }
312 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700313 }
314 }
315 }
316 return nullptr;
317 }
318
319 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700320 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700321 const int kMaxIterations = 40;
322 int i = 0;
323 while (ref && ref->id && i++ < kMaxIterations) {
324 auto table = extractor_->apk_->GetResourceTable();
325 if (auto value = FindValueById(table, ref->id.value(), config)) {
326 if (ValueCast<Reference>(value)) {
327 ref = ValueCast<Reference>(value);
328 } else {
329 return value;
330 }
331 }
332 }
333 return nullptr;
334 }
335
336 /**
337 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
338 * this will attempt to resolve the reference to an integer value.
339 **/
340 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700341 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700342 if (attr != nullptr) {
343 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700344 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700345 Value* value = attr->compiled_value.get();
346 if (ValueCast<Reference>(value)) {
347 value = ResolveReference(ValueCast<Reference>(value), config);
348 } else {
349 value = attr->compiled_value.get();
350 }
351 // Retrieve the integer data if possible
352 if (value != nullptr) {
353 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
354 return (int32_t*) &intValue->value.data;
355 }
356 }
357 }
358 }
359 return nullptr;
360 }
361
362 /**
363 * A version of GetAttributeInteger that returns a default integer if the attribute does not
364 * exist or cannot be resolved to an integer value.
365 **/
366 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700367 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700368 auto value = GetAttributeInteger(attr, config);
369 if (value) {
370 return *value;
371 }
372 return def;
373 }
374
375 /**
376 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
377 * this will attempt to resolve the reference to a string value.
378 **/
379 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700380 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700381 if (attr != nullptr) {
382 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700383 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700384 Value* value = attr->compiled_value.get();
385 if (ValueCast<Reference>(value)) {
386 value = ResolveReference(ValueCast<Reference>(value), config);
387 } else {
388 value = attr->compiled_value.get();
389 }
390
391 // Retrieve the string data of the value if possible
392 if (value != nullptr) {
393 if (String* intValue = ValueCast<String>(value)) {
394 return &(*intValue->value);
395 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
396 return &(*rawValue->value);
397 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
398 return &(*strValue->path);
399 }
400 }
401 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700402
403 if (!attr->value.empty()) {
404 return &attr->value;
405 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700406 }
407 return nullptr;
408 }
409
410 /**
411 * A version of GetAttributeString that returns a default string if the attribute does not
412 * exist or cannot be resolved to an string value.
413 **/
414 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700415 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700416 auto value = GetAttributeString(attr, config);
417 if (value) {
418 return *value;
419 }
420 return def;
421 }
422
423 private:
424 ManifestExtractor* extractor_;
425 std::vector<std::unique_ptr<Element>> children_;
426 std::string tag_;
427 };
428
429 friend Element;
430
431 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700432 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700433 ConfigDescription config;
434 config.orientation = android::ResTable_config::ORIENTATION_PORT;
435 config.density = android::ResTable_config::DENSITY_MEDIUM;
Jackal Guo201a60a2021-08-31 12:37:30 +0800436 config.sdkVersion = SDK_CUR_DEVELOPMENT; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700437 config.screenWidthDp = 320;
438 config.screenHeightDp = 480;
439 config.smallestScreenWidthDp = 320;
440 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
441 return config;
442 }
443
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000444 bool Extract(IDiagnostics* diag);
445 bool Dump(text::Printer* printer);
Iurii Makhno85875a82022-04-26 15:30:01 +0000446 bool DumpProto(pb::Badging* out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700447
448 /** Recursively visit the xml element tree and return a processed badging element tree. */
449 std::unique_ptr<Element> Visit(xml::Element* element);
450
451 /** Raises the target sdk value if the min target is greater than the current target. */
452 void RaiseTargetSdk(int32_t min_target) {
453 if (min_target > target_sdk_) {
454 target_sdk_ = min_target;
455 }
456 }
457
458 /**
459 * Retrieves the default feature group that features are added into when <uses-feature>
460 * are not in a <feature-group> element.
461 **/
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000462 CommonFeatureGroup* common_feature_group() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700463 return commonFeatureGroup_.get();
464 }
465
466 /**
467 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
468 * used for that density setting.
469 **/
470 const std::map<uint16_t, ConfigDescription> densities() const {
471 return densities_;
472 }
473
474 /**
475 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
476 * would be used for that locale setting.
477 **/
478 const std::map<std::string, ConfigDescription> locales() const {
479 return locales_;
480 }
481
482 /** Retrieves the current stack of parent during data extraction. */
483 const std::vector<Element*> parent_stack() const {
484 return parent_stack_;
485 }
486
487 int32_t target_sdk() const {
488 return target_sdk_;
489 }
490
491 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700492 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700493
494 private:
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000495 std::unique_ptr<xml::XmlResource> doc_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700496 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
497 std::map<std::string, ConfigDescription> locales_;
498 std::map<uint16_t, ConfigDescription> densities_;
499 std::vector<Element*> parent_stack_;
500 int32_t target_sdk_ = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000501
502 std::unique_ptr<ManifestExtractor::Element> root_element_;
503 std::vector<std::unique_ptr<ManifestExtractor::Element>> implied_permissions_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000504 std::vector<FeatureGroup*> feature_groups_;
Iurii Makhno85875a82022-04-26 15:30:01 +0000505 Components components_;
506 Architectures architectures_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000507 const SupportsScreen* supports_screen_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700508};
509
510template<typename T> T* ElementCast(ManifestExtractor::Element* element);
511
512/** Recurs through the children of the specified root in depth-first order. */
513static void ForEachChild(ManifestExtractor::Element* root,
514 std::function<void(ManifestExtractor::Element*)> f) {
515 for (auto& child : root->children()) {
516 f(child.get());
517 ForEachChild(child.get(), f);
518 }
519}
520
521/**
522 * Checks the element and its recursive children for an element that makes the specified
523 * conditional function return true. Returns the first element that makes the conditional function
524 * return true.
525 **/
526static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
527 std::function<bool(ManifestExtractor::Element*)> f) {
528 if (f(root)) {
529 return root;
530 }
531 for (auto& child : root->children()) {
532 if (auto b2 = FindElement(child.get(), f)) {
533 return b2;
534 }
535 }
536 return nullptr;
537}
538
539/** Represents the <manifest> elements **/
540class Manifest : public ManifestExtractor::Element {
541 public:
542 Manifest() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000543 bool only_package_name;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700544 std::string package;
545 int32_t versionCode;
546 std::string versionName;
547 const std::string* split = nullptr;
548 const std::string* platformVersionName = nullptr;
549 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700550 const int32_t* platformVersionNameInt = nullptr;
551 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700552 const int32_t* compilesdkVersion = nullptr;
553 const std::string* compilesdkVersionCodename = nullptr;
554 const int32_t* installLocation = nullptr;
555
556 void Extract(xml::Element* manifest) override {
557 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
558 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
559 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
560 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
561
562 // Extract the platform build info
563 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
564 "platformBuildVersionName"));
565 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
566 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700567 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
568 "platformBuildVersionName"));
569 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
570 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700571
572 // Extract the compile sdk info
573 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
574 compilesdkVersionCodename = GetAttributeString(
575 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
576 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
577 }
578
Iurii Makhno85875a82022-04-26 15:30:01 +0000579 void ToProto(pb::Badging* out_badging) override {
580 auto out_package = out_badging->mutable_package();
581 out_package->set_package(package);
582 out_package->set_version_code(versionCode);
583 out_package->set_version_name(versionName);
584 if (compilesdkVersion) {
585 out_package->set_compile_sdk_version(*compilesdkVersion);
586 }
587 if (compilesdkVersionCodename) {
588 out_package->set_compile_sdk_version_codename(*compilesdkVersionCodename);
589 }
590 if (platformVersionName) {
591 out_package->set_platform_version_name(*platformVersionName);
592 } else if (platformVersionNameInt) {
593 out_package->set_platform_version_name(std::to_string(*platformVersionNameInt));
594 }
595 if (platformVersionCode) {
596 out_package->set_platform_version_code(*platformVersionCode);
597 } else if (platformVersionCodeInt) {
598 out_package->set_platform_version_code(std::to_string(*platformVersionCodeInt));
599 }
600
601 if (installLocation) {
602 switch (*installLocation) {
603 case 0:
604 out_package->set_install_location(pb::PackageInfo_InstallLocation_AUTO);
605 break;
606 case 1:
607 out_package->set_install_location(pb::PackageInfo_InstallLocation_INTERNAL_ONLY);
608 break;
609 case 2:
610 out_package->set_install_location(pb::PackageInfo_InstallLocation_PREFER_EXTERNAL);
611 break;
612 default:
613 break;
614 }
615 }
616 }
617
Ryan Mitchell214846d2018-09-19 16:57:01 -0700618 void Print(text::Printer* printer) override {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000619 if (only_package_name) {
620 printer->Println(StringPrintf("package: %s", package.data()));
621 } else {
622 PrintFull(printer);
623 }
624 }
625
626 void PrintFull(text::Printer* printer) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700627 printer->Print(StringPrintf("package: name='%s' ", package.data()));
628 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700629 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700630 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700631
632 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700633 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700634 }
635 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700636 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700637 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700638 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
639 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700640 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700641 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700642 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700643 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
644 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700645 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700646 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700647 }
648 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700649 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700650 compilesdkVersionCodename->data()));
651 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700652 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700653
654 if (installLocation) {
655 switch (*installLocation) {
656 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700657 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700658 break;
659 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700660 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700661 break;
662 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700663 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700664 break;
665 default:
666 break;
667 }
668 }
669 }
670};
671
672/** Represents <application> elements. **/
673class Application : public ManifestExtractor::Element {
674 public:
675 Application() = default;
676 std::string label;
677 std::string icon;
678 std::string banner;
679 int32_t is_game;
680 int32_t debuggable;
681 int32_t test_only;
682 bool has_multi_arch;
683
684 /** Mapping from locales to app names. */
685 std::map<std::string, std::string> locale_labels;
686
687 /** Mapping from densities to app icons. */
688 std::map<uint16_t, std::string> density_icons;
689
690 void Extract(xml::Element* element) override {
691 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
692 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
693 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
694 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
695 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
696 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
697
698 // We must search by name because the multiArch flag hasn't been API
699 // frozen yet.
700 has_multi_arch = (GetAttributeIntegerDefault(
701 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
702
703 // Retrieve the app names for every locale the app supports
704 auto attr = FindAttribute(element, LABEL_ATTR);
705 for (auto& config : extractor()->locales()) {
706 if (auto label = GetAttributeString(attr, config.second)) {
707 if (label) {
708 locale_labels.insert(std::make_pair(config.first, *label));
709 }
710 }
711 }
712
713 // Retrieve the icons for the densities the app supports
714 attr = FindAttribute(element, ICON_ATTR);
715 for (auto& config : extractor()->densities()) {
716 if (auto resource = GetAttributeString(attr, config.second)) {
717 if (resource) {
718 density_icons.insert(std::make_pair(config.first, *resource));
719 }
720 }
721 }
722 }
723
Ryan Mitchell214846d2018-09-19 16:57:01 -0700724 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700725 // Print the labels for every locale
726 for (auto p : locale_labels) {
727 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700728 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700729 android::ResTable::normalizeForOutput(p.second.data())
730 .c_str()));
731 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700732 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700733 android::ResTable::normalizeForOutput(p.second.data())
734 .c_str()));
735 }
736 }
737
738 // Print the icon paths for every density
739 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700740 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700741 }
742
743 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700744 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700745 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700746 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700747 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700748 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700749 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700750 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700751
752 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700753 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700754 }
755 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700756 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700757 }
758 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700759 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700760 }
761 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000762
763 void ToProto(pb::Badging* out_badging) override {
764 auto application = out_badging->mutable_application();
765 application->set_label(android::ResTable::normalizeForOutput(label.data()));
766 application->set_icon(icon);
767 application->set_banner(banner);
768 application->set_test_only(test_only != 0);
769 application->set_game(is_game != 0);
770 application->set_debuggable(debuggable != 0);
771
772 auto out_locale_labels = application->mutable_locale_labels();
773 for (auto& p : locale_labels) {
774 if (!p.first.empty()) {
775 (*out_locale_labels)[p.first] = p.second;
776 }
777 }
778 auto out_density_icons = application->mutable_density_icons();
779 for (auto& p : density_icons) {
780 (*out_density_icons)[p.first] = p.second;
781 }
782 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700783};
784
785/** Represents <uses-sdk> elements. **/
786class UsesSdkBadging : public ManifestExtractor::Element {
787 public:
788 UsesSdkBadging() = default;
789 const int32_t* min_sdk = nullptr;
790 const std::string* min_sdk_name = nullptr;
791 const int32_t* max_sdk = nullptr;
792 const int32_t* target_sdk = nullptr;
793 const std::string* target_sdk_name = nullptr;
794
795 void Extract(xml::Element* element) override {
796 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
797 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
798 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
799 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
800 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
801
802 // Detect the target sdk of the element
803 if ((min_sdk_name && *min_sdk_name == "Donut")
804 || (target_sdk_name && *target_sdk_name == "Donut")) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800805 extractor()->RaiseTargetSdk(SDK_DONUT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700806 }
807 if (min_sdk) {
808 extractor()->RaiseTargetSdk(*min_sdk);
809 }
810 if (target_sdk) {
811 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700812 } else if (target_sdk_name) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800813 extractor()->RaiseTargetSdk(SDK_CUR_DEVELOPMENT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700814 }
815 }
816
Ryan Mitchell214846d2018-09-19 16:57:01 -0700817 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700818 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700819 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700820 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700821 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700822 }
823 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700824 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700825 }
826 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700827 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700828 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700829 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700830 }
831 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000832
833 void ToProto(pb::Badging* out_badging) override {
834 auto out_sdks = out_badging->mutable_uses_sdk();
835 if (min_sdk) {
836 out_sdks->set_min_sdk_version(*min_sdk);
837 } else if (min_sdk_name) {
838 out_sdks->set_min_sdk_version_name(*min_sdk_name);
839 }
840 if (max_sdk) {
841 out_sdks->set_max_sdk_version(*max_sdk);
842 }
843 if (target_sdk) {
844 out_sdks->set_target_sdk_version(*target_sdk);
845 } else if (target_sdk_name) {
846 out_sdks->set_target_sdk_version_name(*target_sdk_name);
847 }
848 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700849};
850
851/** Represents <uses-configuration> elements. **/
852class UsesConfiguarion : public ManifestExtractor::Element {
853 public:
854 UsesConfiguarion() = default;
855 int32_t req_touch_screen = 0;
856 int32_t req_keyboard_type = 0;
857 int32_t req_hard_keyboard = 0;
858 int32_t req_navigation = 0;
859 int32_t req_five_way_nav = 0;
860
861 void Extract(xml::Element* element) override {
862 req_touch_screen = GetAttributeIntegerDefault(
863 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
864 req_keyboard_type = GetAttributeIntegerDefault(
865 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
866 req_hard_keyboard = GetAttributeIntegerDefault(
867 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
868 req_navigation = GetAttributeIntegerDefault(
869 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
870 req_five_way_nav = GetAttributeIntegerDefault(
871 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
872 }
873
Ryan Mitchell214846d2018-09-19 16:57:01 -0700874 void Print(text::Printer* printer) override {
875 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700876 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700877 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700878 }
879 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700880 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700881 }
882 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700883 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700884 }
885 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700886 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700887 }
888 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700889 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700890 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700891 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700892 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000893
894 void ToProto(pb::Badging* out_badging) override {
895 auto out_configuration = out_badging->mutable_uses_configuration();
896 out_configuration->set_req_touch_screen(req_touch_screen);
897 out_configuration->set_req_keyboard_type(req_keyboard_type);
898 out_configuration->set_req_hard_keyboard(req_hard_keyboard);
899 out_configuration->set_req_navigation(req_navigation);
900 out_configuration->set_req_five_way_nav(req_five_way_nav);
901 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700902};
903
904/** Represents <supports-screen> elements. **/
905class SupportsScreen : public ManifestExtractor::Element {
906 public:
907 SupportsScreen() = default;
908 int32_t small_screen = 1;
909 int32_t normal_screen = 1;
910 int32_t large_screen = 1;
911 int32_t xlarge_screen = 1;
912 int32_t any_density = 1;
913 int32_t requires_smallest_width_dp = 0;
914 int32_t compatible_width_limit_dp = 0;
915 int32_t largest_width_limit_dp = 0;
916
917 void Extract(xml::Element* element) override {
918 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
919 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
920 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
921 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
922 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
923
924 requires_smallest_width_dp = GetAttributeIntegerDefault(
925 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
926 compatible_width_limit_dp = GetAttributeIntegerDefault(
927 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
928 largest_width_limit_dp = GetAttributeIntegerDefault(
929 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
930
931 // For modern apps, if screen size buckets haven't been specified
932 // but the new width ranges have, then infer the buckets from them.
933 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
934 && requires_smallest_width_dp > 0) {
935 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
936 : requires_smallest_width_dp;
937 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
938 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
939 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
940 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
941 }
942 }
943
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000944 void PrintScreens(text::Printer* printer, int32_t target_sdk) const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700945 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700946 printer->Print("supports-screens:");
Iurii Makhno85875a82022-04-26 15:30:01 +0000947 if (IsSmallScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700948 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700949 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000950 if (normal_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700951 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700952 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000953 if (IsLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700954 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700955 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000956 if (IsXLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700957 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700958 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700959 printer->Print("\n");
960 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Iurii Makhno85875a82022-04-26 15:30:01 +0000961 (IsAnyDensitySupported(target_sdk)) ? "true" : "false"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700962 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700963 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700964 }
965 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700966 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700967 }
968 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700969 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700970 }
971 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000972
973 void ToProtoScreens(pb::Badging* out_badging, int32_t target_sdk) const {
974 auto supports_screen = out_badging->mutable_supports_screen();
975 if (IsSmallScreenSupported(target_sdk)) {
976 supports_screen->add_screens(pb::SupportsScreen_ScreenType_SMALL);
977 }
978 if (normal_screen != 0) {
979 supports_screen->add_screens(pb::SupportsScreen_ScreenType_NORMAL);
980 }
981 if (IsLargeScreenSupported(target_sdk)) {
982 supports_screen->add_screens(pb::SupportsScreen_ScreenType_LARGE);
983 }
984 if (IsXLargeScreenSupported(target_sdk)) {
985 supports_screen->add_screens(pb::SupportsScreen_ScreenType_XLARGE);
986 }
987 supports_screen->set_supports_any_densities(IsAnyDensitySupported(target_sdk));
988 supports_screen->set_requires_smallest_width_dp(requires_smallest_width_dp);
989 supports_screen->set_compatible_width_limit_dp(compatible_width_limit_dp);
990 supports_screen->set_largest_width_limit_dp(largest_width_limit_dp);
991 }
992
993 private:
994 // Determine default values for any unspecified screen sizes,
995 // based on the target SDK of the package. As of 4 (donut)
996 // the screen size support was introduced, so all default to
997 // enabled.
998 bool IsSmallScreenSupported(int32_t target_sdk) const {
999 if (small_screen > 0) {
1000 return target_sdk >= SDK_DONUT;
1001 }
1002 return small_screen != 0;
1003 }
1004
1005 bool IsLargeScreenSupported(int32_t target_sdk) const {
1006 if (large_screen > 0) {
1007 return target_sdk >= SDK_DONUT;
1008 }
1009 return large_screen != 0;
1010 }
1011
1012 bool IsXLargeScreenSupported(int32_t target_sdk) const {
1013 if (xlarge_screen > 0) {
1014 return target_sdk >= SDK_GINGERBREAD;
1015 }
1016 return xlarge_screen != 0;
1017 }
1018
1019 bool IsAnyDensitySupported(int32_t target_sdk) const {
1020 if (any_density > 0) {
1021 return target_sdk >= SDK_DONUT || requires_smallest_width_dp > 0 ||
1022 compatible_width_limit_dp > 0;
1023 }
1024 return any_density != 0;
1025 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001026};
1027
1028/** Represents <feature-group> elements. **/
1029class FeatureGroup : public ManifestExtractor::Element {
1030 public:
1031 FeatureGroup() = default;
1032 std::string label;
1033 int32_t open_gles_version = 0;
1034
1035 void Extract(xml::Element* element) override {
1036 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1037 }
1038
Ryan Mitchell214846d2018-09-19 16:57:01 -07001039 virtual void PrintGroup(text::Printer* printer) {
1040 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001041 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001042 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001043 }
1044
1045 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001046 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001047 (feature.second.required ? "" : "-not-required"),
1048 feature.first.data()));
1049 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001050 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001051 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001052 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001053 }
1054 }
1055
Iurii Makhno85875a82022-04-26 15:30:01 +00001056 virtual void GroupToProto(pb::Badging* out_badging) {
1057 auto feature_group = out_badging->add_feature_groups();
1058 feature_group->set_label(label);
1059 feature_group->set_open_gles_version(open_gles_version);
1060 for (auto& feature : features_) {
1061 auto out_feature = feature_group->add_features();
1062 out_feature->set_name(feature.first);
1063 out_feature->set_required(feature.second.required);
1064 out_feature->set_version(feature.second.version);
1065 }
1066 }
1067
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001068 /** Adds a feature to the feature group. */
1069 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
1070 features_.insert(std::make_pair(name, Feature{ required, version }));
1071 if (required) {
1072 if (name == "android.hardware.camera.autofocus" ||
1073 name == "android.hardware.camera.flash") {
1074 AddFeature("android.hardware.camera", true);
1075 } else if (name == "android.hardware.location.gps" ||
1076 name == "android.hardware.location.network") {
1077 AddFeature("android.hardware.location", true);
1078 } else if (name == "android.hardware.faketouch.multitouch") {
1079 AddFeature("android.hardware.faketouch", true);
1080 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
1081 name == "android.hardware.faketouch.multitouch.jazzhands") {
1082 AddFeature("android.hardware.faketouch.multitouch", true);
1083 AddFeature("android.hardware.faketouch", true);
1084 } else if (name == "android.hardware.touchscreen.multitouch") {
1085 AddFeature("android.hardware.touchscreen", true);
1086 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
1087 name == "android.hardware.touchscreen.multitouch.jazzhands") {
1088 AddFeature("android.hardware.touchscreen.multitouch", true);
1089 AddFeature("android.hardware.touchscreen", true);
1090 } else if (name == "android.hardware.opengles.aep") {
1091 const int kOpenGLESVersion31 = 0x00030001;
1092 if (kOpenGLESVersion31 > open_gles_version) {
1093 open_gles_version = kOpenGLESVersion31;
1094 }
1095 }
1096 }
1097 }
1098
1099 /** Returns true if the feature group has the given feature. */
1100 virtual bool HasFeature(const std::string& name) {
1101 return features_.find(name) != features_.end();
1102 }
1103
1104 /** Merges the features of another feature group into this group. */
1105 void Merge(FeatureGroup* group) {
1106 open_gles_version = std::max(open_gles_version, group->open_gles_version);
1107 for (auto& feature : group->features_) {
1108 features_.insert(feature);
1109 }
1110 }
1111
1112 protected:
1113 struct Feature {
1114 public:
1115 bool required = false;
1116 int32_t version = -1;
1117 };
1118
1119 /* Mapping of feature names to their properties. */
1120 std::map<std::string, Feature> features_;
1121};
1122
1123/**
1124 * Represents the default feature group for the application if no <feature-group> elements are
1125 * present in the manifest.
1126 **/
1127class CommonFeatureGroup : public FeatureGroup {
1128 public:
1129 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001130 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001131 FeatureGroup::PrintGroup(printer);
1132
1133 // Also print the implied features
1134 for (auto feature : implied_features_) {
1135 if (features_.find(feature.first) == features_.end()) {
1136 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -07001137 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
1138 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001139 feature.first.data()));
1140
1141 // Print the reasons as a sentence
1142 size_t count = 0;
1143 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001144 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001145 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001146 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001147 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001148 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001149 }
1150 count++;
1151 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001152 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001153 }
1154 }
1155 }
1156
Iurii Makhno85875a82022-04-26 15:30:01 +00001157 virtual void GroupToProto(pb::Badging* out_badging) override {
1158 FeatureGroup::GroupToProto(out_badging);
1159 auto feature_group =
1160 out_badging->mutable_feature_groups(out_badging->feature_groups_size() - 1);
1161 for (auto& feature : implied_features_) {
1162 if (features_.find(feature.first) == features_.end()) {
1163 auto out_feature = feature_group->add_features();
1164 out_feature->set_name(feature.first);
1165 auto implied_data = out_feature->mutable_implied_data();
1166 implied_data->set_from_sdk_23_permission(feature.second.implied_from_sdk_k23);
1167 for (auto& reason : feature.second.reasons) {
1168 implied_data->add_reasons(reason);
1169 }
1170 }
1171 }
1172 }
1173
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001174 /** Returns true if the feature group has the given feature. */
1175 bool HasFeature(const std::string& name) override {
1176 return FeatureGroup::HasFeature(name)
1177 || implied_features_.find(name) != implied_features_.end();
1178 }
1179
1180 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
1181 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
1182 auto entry = implied_features_.find(name);
1183 if (entry == implied_features_.end()) {
1184 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
1185 entry = implied_features_.find(name);
1186 }
1187
1188 // A non-sdk 23 implied feature takes precedence.
1189 if (entry->second.implied_from_sdk_k23 && !sdk23) {
1190 entry->second.implied_from_sdk_k23 = false;
1191 }
1192
1193 entry->second.reasons.insert(reason);
1194 }
1195
1196 /**
1197 * Adds a feature to a set of implied features for all features that are implied by the presence
1198 * of the permission.
1199 **/
1200 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
1201 if (name == "android.permission.CAMERA") {
1202 addImpliedFeature("android.hardware.camera",
1203 StringPrintf("requested %s permission", name.data()),
1204 sdk23);
1205
1206 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
1207 if (targetSdk < SDK_LOLLIPOP) {
1208 addImpliedFeature("android.hardware.location.gps",
1209 StringPrintf("requested %s permission", name.data()),
1210 sdk23);
1211 addImpliedFeature("android.hardware.location.gps",
1212 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1213 sdk23);
1214 }
1215 addImpliedFeature("android.hardware.location",
1216 StringPrintf("requested %s permission", name.data()),
1217 sdk23);
1218
1219 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
1220 if (targetSdk < SDK_LOLLIPOP) {
1221 addImpliedFeature("android.hardware.location.network",
1222 StringPrintf("requested %s permission", name.data()),
1223 sdk23);
1224 addImpliedFeature("android.hardware.location.network",
1225 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1226 sdk23);
1227 }
1228 addImpliedFeature("android.hardware.location",
1229 StringPrintf("requested %s permission", name.data()),
1230 sdk23);
1231
1232 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
1233 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
1234 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
1235 addImpliedFeature("android.hardware.location",
1236 StringPrintf("requested %s permission", name.data()),
1237 sdk23);
1238
1239 } else if (name == "android.permission.BLUETOOTH" ||
1240 name == "android.permission.BLUETOOTH_ADMIN") {
1241 if (targetSdk > SDK_DONUT) {
1242 addImpliedFeature("android.hardware.bluetooth",
1243 StringPrintf("requested %s permission", name.data()),
1244 sdk23);
1245 addImpliedFeature("android.hardware.bluetooth",
1246 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
1247 sdk23);
1248 }
1249
1250 } else if (name == "android.permission.RECORD_AUDIO") {
1251 addImpliedFeature("android.hardware.microphone",
1252 StringPrintf("requested %s permission", name.data()),
1253 sdk23);
1254
1255 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
1256 name == "android.permission.CHANGE_WIFI_STATE" ||
1257 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
1258 addImpliedFeature("android.hardware.wifi",
1259 StringPrintf("requested %s permission", name.data()),
1260 sdk23);
1261
1262 } else if (name == "android.permission.CALL_PHONE" ||
1263 name == "android.permission.CALL_PRIVILEGED" ||
1264 name == "android.permission.MODIFY_PHONE_STATE" ||
1265 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1266 name == "android.permission.READ_SMS" ||
1267 name == "android.permission.RECEIVE_SMS" ||
1268 name == "android.permission.RECEIVE_MMS" ||
1269 name == "android.permission.RECEIVE_WAP_PUSH" ||
1270 name == "android.permission.SEND_SMS" ||
1271 name == "android.permission.WRITE_APN_SETTINGS" ||
1272 name == "android.permission.WRITE_SMS") {
1273 addImpliedFeature("android.hardware.telephony",
1274 "requested a telephony permission",
1275 sdk23);
1276 }
1277 }
1278
1279 private:
1280 /**
1281 * Represents a feature that has been automatically added due to a pre-requisite or for some
1282 * other reason.
1283 */
1284 struct ImpliedFeature {
1285 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1286
1287 /** List of human-readable reasons for why this feature was implied. */
1288 std::set<std::string> reasons;
1289
1290 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1291 bool implied_from_sdk_k23;
1292 };
1293
1294 /* Mapping of implied feature names to their properties. */
1295 std::map<std::string, ImpliedFeature> implied_features_;
1296};
1297
1298/** Represents <uses-feature> elements. **/
1299class UsesFeature : public ManifestExtractor::Element {
1300 public:
1301 UsesFeature() = default;
1302 void Extract(xml::Element* element) override {
1303 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1304 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1305 bool required = GetAttributeIntegerDefault(
1306 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1307 int32_t version = GetAttributeIntegerDefault(
1308 FindAttribute(element, kAndroidNamespace, "version"), 0);
1309
1310 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1311 // common feature group
1312 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1313 if (!feature_group) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001314 feature_group = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001315 } else {
1316 // All features in side of <feature-group> elements are required.
1317 required = true;
1318 }
1319
1320 if (name) {
1321 feature_group->AddFeature(*name, required, version);
1322 } else if (gl) {
1323 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1324 }
1325 }
1326};
1327
1328/** Represents <uses-permission> elements. **/
1329class UsesPermission : public ManifestExtractor::Element {
1330 public:
1331 UsesPermission() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001332 bool implied;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001333 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001334 std::vector<std::string> requiredFeatures;
1335 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001336 int32_t required = true;
1337 int32_t maxSdkVersion = -1;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001338 int32_t usesPermissionFlags = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001339 std::string impliedReason;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001340
1341 void Extract(xml::Element* element) override {
1342 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001343 std::string feature =
1344 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1345 if (!feature.empty()) {
1346 requiredFeatures.push_back(feature);
1347 }
1348 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1349 if (!feature.empty()) {
1350 requiredNotFeatures.push_back(feature);
1351 }
1352
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001353 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1354 maxSdkVersion = GetAttributeIntegerDefault(
1355 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001356 usesPermissionFlags = GetAttributeIntegerDefault(
1357 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001358
1359 if (!name.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001360 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001361 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1362 }
1363 }
1364
Ryan Mitchell214846d2018-09-19 16:57:01 -07001365 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001366 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001367 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001368 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001369 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001370 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001371 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1372 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1373 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001374 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001375 for (const std::string& requiredFeature : requiredFeatures) {
1376 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1377 }
1378 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1379 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1380 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001381 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001382 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001383 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001384 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001385 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001386 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1387 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1388 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001389 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001390 }
1391 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001392 if (implied) {
1393 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
1394 if (maxSdkVersion >= 0) {
1395 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
1396 }
1397 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1398 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1399 }
1400 printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001401 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001402 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001403
1404 void ToProto(pb::Badging* out_badging) override {
1405 if (!name.empty()) {
1406 auto permission = out_badging->add_uses_permissions();
1407 permission->set_name(name);
1408 if (maxSdkVersion > 0) {
1409 permission->set_max_sdk_version(maxSdkVersion);
1410 }
1411 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1412 permission->mutable_permission_flags()->set_never_for_location(true);
1413 }
1414 for (auto& requiredFeature : requiredFeatures) {
1415 permission->add_required_features(requiredFeature);
1416 }
1417 for (auto& requiredNotFeature : requiredNotFeatures) {
1418 permission->add_required_not_features(requiredNotFeature);
1419 }
1420 permission->set_required(required != 0);
1421 permission->set_implied(implied);
1422 }
1423 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001424};
1425
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001426/** Represents <required-feature> elements. **/
1427class RequiredFeature : public ManifestExtractor::Element {
1428 public:
1429 RequiredFeature() = default;
1430 std::string name;
1431
1432 void Extract(xml::Element* element) override {
1433 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1434 auto parent_stack = extractor()->parent_stack();
1435 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1436 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1437 uses_permission->requiredFeatures.push_back(name);
1438 }
1439 }
1440};
1441
1442/** Represents <required-not-feature> elements. **/
1443class RequiredNotFeature : public ManifestExtractor::Element {
1444 public:
1445 RequiredNotFeature() = default;
1446 std::string name;
1447
1448 void Extract(xml::Element* element) override {
1449 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1450 auto parent_stack = extractor()->parent_stack();
1451 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1452 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1453 uses_permission->requiredNotFeatures.push_back(name);
1454 }
1455 }
1456};
1457
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001458/** Represents <uses-permission-sdk-23> elements. **/
1459class UsesPermissionSdk23 : public ManifestExtractor::Element {
1460 public:
1461 UsesPermissionSdk23() = default;
1462 const std::string* name = nullptr;
1463 const int32_t* maxSdkVersion = nullptr;
1464
1465 void Extract(xml::Element* element) override {
1466 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1467 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1468
1469 if (name) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001470 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001471 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1472 }
1473 }
1474
Ryan Mitchell214846d2018-09-19 16:57:01 -07001475 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001476 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001477 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001478 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001479 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001480 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001481 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001482 }
1483 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001484
1485 void ToProto(pb::Badging* out_badging) override {
1486 if (name) {
1487 auto permission = out_badging->add_uses_permissions();
1488 permission->set_sdk23_and_above(true);
1489 permission->set_name(*name);
1490 if (maxSdkVersion) {
1491 permission->set_max_sdk_version(*maxSdkVersion);
1492 }
1493 }
1494 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001495};
1496
1497/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1498class Permission : public ManifestExtractor::Element {
1499 public:
1500 Permission() = default;
1501 std::string name;
1502
1503 void Extract(xml::Element* element) override {
1504 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1505 }
1506
Ryan Mitchell214846d2018-09-19 16:57:01 -07001507 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001508 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001509 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001510 }
1511 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001512
1513 void ToProto(pb::Badging* out_badging) override {
1514 if (!name.empty()) {
1515 out_badging->add_permissions()->set_name(name);
1516 }
1517 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001518};
1519
1520/** Represents <activity> elements. **/
1521class Activity : public ManifestExtractor::Element {
1522 public:
1523 Activity() = default;
1524 std::string name;
1525 std::string icon;
1526 std::string label;
1527 std::string banner;
1528
1529 bool has_component_ = false;
1530 bool has_launcher_category = false;
1531 bool has_leanback_launcher_category = false;
1532 bool has_main_action = false;
1533
1534 void Extract(xml::Element* element) override {
1535 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1536 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1537 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1538 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1539
1540 // Retrieve the package name from the manifest
1541 std::string package;
1542 for (auto& parent : extractor()->parent_stack()) {
1543 if (auto manifest = ElementCast<Manifest>(parent)) {
1544 package = manifest->package;
1545 break;
1546 }
1547 }
1548
1549 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001550 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001551 if (idx == 0) {
1552 name = package + name;
1553 } else if (idx < 0) {
1554 name = package + "." + name;
1555 }
1556
1557 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1558 if (orientation) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001559 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001560 int orien = *orientation;
1561 if (orien == 0 || orien == 6 || orien == 8) {
1562 // Requests landscape, sensorLandscape, or reverseLandscape.
1563 common->addImpliedFeature("android.hardware.screen.landscape",
1564 "one or more activities have specified a landscape orientation",
1565 false);
1566 } else if (orien == 1 || orien == 7 || orien == 9) {
1567 // Requests portrait, sensorPortrait, or reversePortrait.
1568 common->addImpliedFeature("android.hardware.screen.portrait",
1569 "one or more activities have specified a portrait orientation",
1570 false);
1571 }
1572 }
1573 }
1574
Ryan Mitchell214846d2018-09-19 16:57:01 -07001575 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001576 // Print whether the activity has the HOME category and a the MAIN action
1577 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001578 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001579 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001580 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001581 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001582 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001583 android::ResTable::normalizeForOutput(label.data()).c_str(),
1584 icon.data()));
1585 }
1586
1587 // Print wether the activity has the HOME category and a the MAIN action
1588 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001589 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001590 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001591 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001592 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001593 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001594 android::ResTable::normalizeForOutput(label.data()).c_str(),
1595 icon.data(), banner.data()));
1596 }
1597 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001598
1599 void ToProto(pb::Badging* out_badging) override {
1600 if (has_main_action && has_launcher_category) {
1601 auto activity = out_badging->mutable_launchable_activity();
1602 activity->set_name(name);
1603 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1604 activity->set_icon(icon);
1605 }
1606 if (has_leanback_launcher_category) {
1607 auto activity = out_badging->mutable_leanback_launchable_activity();
1608 activity->set_name(name);
1609 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1610 activity->set_icon(icon);
1611 activity->set_banner(banner);
1612 }
1613 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001614};
1615
1616/** Represents <intent-filter> elements. */
1617class IntentFilter : public ManifestExtractor::Element {
1618 public:
1619 IntentFilter() = default;
1620};
1621
1622/** Represents <category> elements. */
1623class Category : public ManifestExtractor::Element {
1624 public:
1625 Category() = default;
1626 std::string component = "";
1627
1628 void Extract(xml::Element* element) override {
1629 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1630
1631 auto parent_stack = extractor()->parent_stack();
1632 if (category && ElementCast<IntentFilter>(parent_stack[0])
1633 && ElementCast<Activity>(parent_stack[1])) {
1634 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1635
1636 if (*category == "android.intent.category.LAUNCHER") {
1637 activity->has_launcher_category = true;
1638 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1639 activity->has_leanback_launcher_category = true;
1640 } else if (*category == "android.intent.category.HOME") {
1641 component = "launcher";
1642 }
1643 }
1644 }
1645};
1646
1647/**
1648 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1649 * elements nested within.
1650 **/
1651class Provider : public ManifestExtractor::Element {
1652 public:
1653 Provider() = default;
1654 bool has_required_saf_attributes = false;
1655
1656 void Extract(xml::Element* element) override {
1657 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1658 const int32_t* grant_uri_permissions = GetAttributeInteger(
1659 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1660 const std::string* permission = GetAttributeString(
1661 FindAttribute(element, PERMISSION_ATTR));
1662
1663 has_required_saf_attributes = ((exported && *exported != 0)
1664 && (grant_uri_permissions && *grant_uri_permissions != 0)
1665 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1666 }
1667};
1668
1669/** Represents <receiver> elements. **/
1670class Receiver : public ManifestExtractor::Element {
1671 public:
1672 Receiver() = default;
1673 const std::string* permission = nullptr;
1674 bool has_component = false;
1675
1676 void Extract(xml::Element* element) override {
1677 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1678 }
1679};
1680
1681/**Represents <service> elements. **/
1682class Service : public ManifestExtractor::Element {
1683 public:
1684 Service() = default;
1685 const std::string* permission = nullptr;
1686 bool has_component = false;
1687
1688 void Extract(xml::Element* element) override {
1689 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1690 }
1691};
1692
1693/** Represents <uses-library> elements. **/
1694class UsesLibrary : public ManifestExtractor::Element {
1695 public:
1696 UsesLibrary() = default;
1697 std::string name;
1698 int required;
1699
1700 void Extract(xml::Element* element) override {
1701 auto parent_stack = extractor()->parent_stack();
1702 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1703 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1704 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1705 }
1706 }
1707
Ryan Mitchell214846d2018-09-19 16:57:01 -07001708 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001709 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001710 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001711 (required == 0) ? "-not-required" : "", name.data()));
1712 }
1713 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001714
1715 void ToProto(pb::Badging* out_badging) override {
1716 if (!name.empty()) {
1717 auto uses_library = out_badging->add_uses_libraries();
1718 uses_library->set_name(name);
1719 uses_library->set_required(required != 0);
1720 }
1721 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001722};
1723
Dianne Hackborn813d7502018-10-02 16:59:46 -07001724/** Represents <static-library> elements. **/
1725class StaticLibrary : public ManifestExtractor::Element {
1726 public:
1727 StaticLibrary() = default;
1728 std::string name;
1729 int version;
1730 int versionMajor;
1731
1732 void Extract(xml::Element* element) override {
1733 auto parent_stack = extractor()->parent_stack();
1734 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1735 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1736 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1737 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1738 }
1739 }
1740
Ryan Mitchell214846d2018-09-19 16:57:01 -07001741 void Print(text::Printer* printer) override {
1742 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001743 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1744 name.data(), version, versionMajor));
1745 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001746
1747 void ToProto(pb::Badging* out_badging) override {
1748 auto static_library = out_badging->mutable_static_library();
1749 static_library->set_name(name);
1750 static_library->set_version(version);
1751 static_library->set_version_major(versionMajor);
1752 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001753};
1754
1755/** Represents <uses-static-library> elements. **/
1756class UsesStaticLibrary : public ManifestExtractor::Element {
1757 public:
1758 UsesStaticLibrary() = default;
1759 std::string name;
1760 int version;
1761 int versionMajor;
1762 std::vector<std::string> certDigests;
1763
1764 void Extract(xml::Element* element) override {
1765 auto parent_stack = extractor()->parent_stack();
1766 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1767 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1768 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1769 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1770 AddCertDigest(element);
1771 }
1772 }
1773
1774 void AddCertDigest(xml::Element* element) {
1775 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1776 // We allow ":" delimiters in the SHA declaration as this is the format
1777 // emitted by the certtool making it easy for developers to copy/paste.
1778 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1779 if (!digest.empty()) {
1780 certDigests.push_back(digest);
1781 }
1782 }
1783
Ryan Mitchell214846d2018-09-19 16:57:01 -07001784 void Print(text::Printer* printer) override {
1785 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001786 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1787 name.data(), version, versionMajor));
1788 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001789 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001790 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001791 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001792 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001793
1794 void ToProto(pb::Badging* out_badging) override {
1795 auto uses_static_library = out_badging->add_uses_static_libraries();
1796 uses_static_library->set_name(name);
1797 uses_static_library->set_version(version);
1798 uses_static_library->set_version_major(versionMajor);
1799 for (auto& cert : certDigests) {
1800 uses_static_library->add_certificates(cert);
1801 }
1802 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001803};
1804
Alex Buynytskyya16137052021-12-02 13:26:54 +00001805/** Represents <sdk-library> elements. **/
1806class SdkLibrary : public ManifestExtractor::Element {
1807 public:
1808 SdkLibrary() = default;
1809 std::string name;
1810 int versionMajor;
1811
1812 void Extract(xml::Element* element) override {
1813 auto parent_stack = extractor()->parent_stack();
1814 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1815 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1816 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1817 }
1818 }
1819
1820 void Print(text::Printer* printer) override {
1821 printer->Print(
1822 StringPrintf("sdk-library: name='%s' versionMajor='%d'\n", name.data(), versionMajor));
1823 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001824
1825 void ToProto(pb::Badging* out_badging) override {
1826 auto sdk_library = out_badging->mutable_sdk_library();
1827 sdk_library->set_name(name);
1828 sdk_library->set_version_major(versionMajor);
1829 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001830};
1831
1832/** Represents <uses-sdk-library> elements. **/
1833class UsesSdkLibrary : public ManifestExtractor::Element {
1834 public:
1835 UsesSdkLibrary() = default;
1836 std::string name;
1837 int versionMajor;
1838 std::vector<std::string> certDigests;
1839
1840 void Extract(xml::Element* element) override {
1841 auto parent_stack = extractor()->parent_stack();
1842 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1843 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1844 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1845 AddCertDigest(element);
1846 }
1847 }
1848
1849 void AddCertDigest(xml::Element* element) {
1850 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1851 // We allow ":" delimiters in the SHA declaration as this is the format
1852 // emitted by the certtool making it easy for developers to copy/paste.
1853 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1854 if (!digest.empty()) {
1855 certDigests.push_back(digest);
1856 }
1857 }
1858
1859 void Print(text::Printer* printer) override {
1860 printer->Print(
1861 StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor));
1862 for (size_t i = 0; i < certDigests.size(); i++) {
1863 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
1864 }
1865 printer->Print("\n");
1866 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001867
1868 void ToProto(pb::Badging* out_badging) override {
1869 auto uses_sdk_library = out_badging->add_uses_sdk_libraries();
1870 uses_sdk_library->set_name(name);
1871 uses_sdk_library->set_version_major(versionMajor);
1872 for (auto& cert : certDigests) {
1873 uses_sdk_library->add_certificates(cert);
1874 }
1875 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001876};
1877
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001878/** Represents <uses-native-library> elements. **/
1879class UsesNativeLibrary : public ManifestExtractor::Element {
1880 public:
1881 UsesNativeLibrary() = default;
1882 std::string name;
1883 int required;
1884
1885 void Extract(xml::Element* element) override {
1886 auto parent_stack = extractor()->parent_stack();
1887 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1888 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1889 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1890 }
1891 }
1892
1893 void Print(text::Printer* printer) override {
1894 if (!name.empty()) {
1895 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1896 (required == 0) ? "-not-required" : "", name.data()));
1897 }
1898 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001899
1900 void ToProto(pb::Badging* out_badging) override {
1901 if (!name.empty()) {
1902 auto uses_native_library = out_badging->add_uses_native_libraries();
1903 uses_native_library->set_name(name);
1904 uses_native_library->set_required(required != 0);
1905 }
1906 }
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001907};
1908
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001909/**
1910 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1911 * explicitly enable meta data printing.
1912 **/
1913class MetaData : public ManifestExtractor::Element {
1914 public:
1915 MetaData() = default;
1916 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001917 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001918 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001919 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001920 const int* resource_int;
1921
1922 void Extract(xml::Element* element) override {
1923 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001924 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001925 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001926 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001927 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1928 }
1929
Ryan Mitchell214846d2018-09-19 16:57:01 -07001930 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001931 if (extractor()->options_.include_meta_data && !name.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001932 printer->Print(StringPrintf("meta-data: name='%s'", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001933 if (!value.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001934 printer->Print(StringPrintf(" value='%s'", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001935 } else if (value_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001936 printer->Print(StringPrintf(" value='%d'", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001937 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001938 if (!resource.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001939 printer->Print(StringPrintf(" resource='%s'", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001940 } else if (resource_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001941 printer->Print(StringPrintf(" resource='%d'", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001942 }
1943 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001944 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001945 }
1946 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001947
1948 void ToProto(pb::Badging* out_badging) override {
1949 if (!name.empty()) {
1950 auto metadata = out_badging->add_metadata();
1951 metadata->set_name(name);
1952 if (!value.empty()) {
1953 metadata->set_value_string(value);
1954 } else if (value_int) {
1955 metadata->set_value_int(*value_int);
1956 } else {
1957 if (!resource.empty()) {
1958 metadata->set_resource_string(resource);
1959 } else if (resource_int) {
1960 metadata->set_resource_int(*resource_int);
1961 }
1962 }
1963 }
1964 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001965};
1966
1967/**
1968 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1969 * service components.
1970 **/
1971class Action : public ManifestExtractor::Element {
1972 public:
1973 Action() = default;
1974 std::string component = "";
1975
1976 void Extract(xml::Element* element) override {
1977 auto parent_stack = extractor()->parent_stack();
1978 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1979
1980 if (ElementCast<IntentFilter>(parent_stack[0])) {
1981 if (ElementCast<Activity>(parent_stack[1])) {
1982 // Detects the presence of a particular type of activity.
1983 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1984 auto map = std::map<std::string, std::string>({
1985 { "android.intent.action.MAIN" , "main" },
1986 { "android.intent.action.VIDEO_CAMERA" , "camera" },
1987 { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
1988 });
1989
1990 auto entry = map.find(action);
1991 if (entry != map.end()) {
1992 component = entry->second;
1993 activity->has_component_ = true;
1994 }
1995
1996 if (action == "android.intent.action.MAIN") {
1997 activity->has_main_action = true;
1998 }
1999
2000 } else if (ElementCast<Receiver>(parent_stack[1])) {
2001 // Detects the presence of a particular type of receiver. If the action requires a
2002 // permission, then the receiver element is checked for the permission.
2003 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
2004 auto map = std::map<std::string, std::string>({
2005 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
2006 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
2007 });
2008
2009 auto permissions = std::map<std::string, std::string>({
2010 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
2011 });
2012
2013 auto entry = map.find(action);
2014 auto permission = permissions.find(action);
2015 if (entry != map.end() && (permission == permissions.end()
2016 || (receiver->permission && permission->second == *receiver->permission))) {
2017 receiver->has_component = true;
2018 component = entry->second;
2019 }
2020
2021 } else if (ElementCast<Service>(parent_stack[1])) {
2022 // Detects the presence of a particular type of service. If the action requires a
2023 // permission, then the service element is checked for the permission.
2024 Service* service = ElementCast<Service>(parent_stack[1]);
2025 auto map = std::map<std::string, std::string>({
2026 { "android.view.InputMethod" , "ime" },
2027 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
2028 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
2029 { "android.printservice.PrintService" , "print-service" },
2030 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
2031 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
2032 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
2033 { "android.service.dreams.DreamService" , "dream" },
2034 });
2035
2036 auto permissions = std::map<std::string, std::string>({
2037 { "android.accessibilityservice.AccessibilityService" ,
2038 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
2039 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
2040 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
2041 "android.permission.BIND_NFC_SERVICE" },
2042 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
2043 "android.permission.BIND_NFC_SERVICE" },
2044 { "android.service.notification.NotificationListenerService" ,
2045 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
2046 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
2047 });
2048
2049 auto entry = map.find(action);
2050 auto permission = permissions.find(action);
2051 if (entry != map.end() && (permission == permissions.end()
2052 || (service->permission && permission->second == *service->permission))) {
2053 service->has_component= true;
2054 component = entry->second;
2055 }
2056
2057 } else if (ElementCast<Provider>(parent_stack[1])) {
2058 // Detects the presence of a particular type of receiver. If the provider requires a
2059 // permission, then the provider element is checked for the permission.
2060 // Detect whether this action
2061 Provider* provider = ElementCast<Provider>(parent_stack[1]);
2062 if (action == "android.content.action.DOCUMENTS_PROVIDER"
2063 && provider->has_required_saf_attributes) {
2064 component = "document-provider";
2065 }
2066 }
2067 }
2068
2069 // Represents a searchable interface
2070 if (action == "android.intent.action.SEARCH") {
2071 component = "search";
2072 }
2073 }
2074};
2075
2076/**
2077 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
2078 **/
2079class SupportsInput : public ManifestExtractor::Element {
2080 public:
2081 SupportsInput() = default;
2082 std::vector<std::string> inputs;
2083
Ryan Mitchell214846d2018-09-19 16:57:01 -07002084 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002085 const size_t size = inputs.size();
2086 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002087 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002088 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002089 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002090 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002091 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002092 }
2093 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002094
2095 void ToProto(pb::Badging* out_badging) override {
2096 auto supports_input = out_badging->mutable_supports_input();
2097 for (auto& input : inputs) {
2098 supports_input->add_inputs(input);
2099 }
2100 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002101};
2102
2103/** Represents <input-type> elements. **/
2104class InputType : public ManifestExtractor::Element {
2105 public:
2106 InputType() = default;
2107 void Extract(xml::Element* element) override {
2108 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2109 auto parent_stack = extractor()->parent_stack();
2110
2111 // Add the input to the set of supported inputs
2112 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
2113 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
2114 supports->inputs.push_back(*name);
2115 }
2116 }
2117};
2118
2119/** Represents <original-package> elements. **/
2120class OriginalPackage : public ManifestExtractor::Element {
2121 public:
2122 OriginalPackage() = default;
2123 const std::string* name = nullptr;
2124
2125 void Extract(xml::Element* element) override {
2126 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2127 }
2128
Ryan Mitchell214846d2018-09-19 16:57:01 -07002129 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002130 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002131 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002132 }
2133 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002134
2135 void ToProto(pb::Badging* out_badging) override {
2136 if (name) {
2137 out_badging->mutable_package()->set_original_package(*name);
2138 }
2139 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002140};
2141
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002142
2143/** Represents <overlay> elements. **/
2144class Overlay : public ManifestExtractor::Element {
2145 public:
2146 Overlay() = default;
2147 const std::string* target_package = nullptr;
2148 int priority;
2149 bool is_static;
2150 const std::string* required_property_name = nullptr;
2151 const std::string* required_property_value = nullptr;
2152
2153 void Extract(xml::Element* element) override {
2154 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
2155 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
2156 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
2157 required_property_name = GetAttributeString(
2158 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
2159 required_property_value = GetAttributeString(
2160 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
2161 }
2162
2163 void Print(text::Printer* printer) override {
2164 printer->Print(StringPrintf("overlay:"));
2165 if (target_package) {
2166 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
2167 }
2168 printer->Print(StringPrintf(" priority='%d'", priority));
2169 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
2170 if (required_property_name) {
2171 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
2172 }
2173 if (required_property_value) {
2174 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
2175 }
2176 printer->Print("\n");
2177 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002178
2179 void ToProto(pb::Badging* out_badging) override {
2180 auto overlay = out_badging->mutable_overlay();
2181 if (target_package) {
2182 overlay->set_target_package(*target_package);
2183 }
2184 overlay->set_priority(priority);
2185 overlay->set_static_(is_static);
2186 if (required_property_name) {
2187 overlay->set_required_property_name(*required_property_name);
2188 }
2189 if (required_property_value) {
2190 overlay->set_required_property_value(*required_property_value);
2191 }
2192 }
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002193};
2194
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002195/** * Represents <package-verifier> elements. **/
2196class PackageVerifier : public ManifestExtractor::Element {
2197 public:
2198 PackageVerifier() = default;
2199 const std::string* name = nullptr;
2200 const std::string* public_key = nullptr;
2201
2202 void Extract(xml::Element* element) override {
2203 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2204 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
2205 }
2206
Ryan Mitchell214846d2018-09-19 16:57:01 -07002207 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002208 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002209 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002210 name->data(), public_key->data()));
2211 }
2212 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002213
2214 void ToProto(pb::Badging* out_badging) override {
2215 auto package_verifier = out_badging->mutable_package_verifier();
2216 if (name && public_key) {
2217 package_verifier->set_name(*name);
2218 package_verifier->set_public_key(*public_key);
2219 }
2220 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002221};
2222
2223/** Represents <uses-package> elements. **/
2224class UsesPackage : public ManifestExtractor::Element {
2225 public:
2226 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002227 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002228 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002229 int version;
2230 int versionMajor;
2231 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002232
2233 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002234 auto parent_stack = extractor()->parent_stack();
2235 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
2236 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
2237 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2238 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
2239 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
2240 AddCertDigest(element);
2241 }
2242 }
2243
2244 void AddCertDigest(xml::Element* element) {
2245 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
2246 // We allow ":" delimiters in the SHA declaration as this is the format
2247 // emitted by the certtool making it easy for developers to copy/paste.
2248 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
2249 if (!digest.empty()) {
2250 certDigests.push_back(digest);
2251 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002252 }
2253
Ryan Mitchell214846d2018-09-19 16:57:01 -07002254 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002255 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002256 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002257 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07002258 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
2259 packageType->data(), name->data(), version, versionMajor));
2260 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002261 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002262 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002263 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07002264 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002265 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002266 }
2267 }
2268 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002269
2270 void ToProto(pb::Badging* out_badging) override {
2271 if (name) {
2272 auto uses_package = out_badging->add_uses_packages();
2273 uses_package->set_name(*name);
2274 if (packageType) {
2275 uses_package->set_package_type(*packageType);
2276 uses_package->set_version(version);
2277 uses_package->set_version_major(versionMajor);
2278 for (auto& cert : certDigests) {
2279 uses_package->add_certificates(cert);
2280 }
2281 }
2282 }
2283 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07002284};
2285
2286/** Represents <additional-certificate> elements. **/
2287class AdditionalCertificate : public ManifestExtractor::Element {
2288 public:
2289 AdditionalCertificate() = default;
2290
2291 void Extract(xml::Element* element) override {
2292 auto parent_stack = extractor()->parent_stack();
2293 if (parent_stack.size() > 0) {
2294 if (ElementCast<UsesPackage>(parent_stack[0])) {
2295 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
2296 uses->AddCertDigest(element);
2297 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
2298 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
2299 uses->AddCertDigest(element);
2300 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002301 }
2302 }
2303};
2304
2305/** Represents <screen> elements found in <compatible-screens> elements. */
2306class Screen : public ManifestExtractor::Element {
2307 public:
2308 Screen() = default;
2309 const int32_t* size = nullptr;
2310 const int32_t* density = nullptr;
2311
2312 void Extract(xml::Element* element) override {
2313 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
2314 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
2315 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002316
2317 void ToProto(pb::Badging* out_badging) override {
2318 if (size && density) {
2319 auto screen = out_badging->mutable_compatible_screens()->add_screens();
2320 screen->set_density(*density);
2321 screen->set_size(*size);
2322 }
2323 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002324};
2325
2326/**
2327 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
2328 * that each denote a supported screen size and screen density.
2329 **/
2330class CompatibleScreens : public ManifestExtractor::Element {
2331 public:
2332 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07002333 void Print(text::Printer* printer) override {
2334 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002335
2336 bool first = true;
2337 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
2338 if (auto screen = ElementCast<Screen>(el)) {
2339 if (first) {
2340 first = false;
2341 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002342 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002343 }
2344
2345 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002346 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002347 }
2348 }
2349 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07002350 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002351 }
2352};
2353
2354/** Represents <supports-gl-texture> elements. **/
2355class SupportsGlTexture : public ManifestExtractor::Element {
2356 public:
2357 SupportsGlTexture() = default;
2358 const std::string* name = nullptr;
2359
2360 void Extract(xml::Element* element) override {
2361 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2362 }
2363
Ryan Mitchell214846d2018-09-19 16:57:01 -07002364 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002365 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002366 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002367 }
2368 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002369
2370 void ToProto(pb::Badging* out_badging) override {
2371 if (name) {
2372 out_badging->mutable_supports_gl_texture()->add_name(*name);
2373 }
2374 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002375};
2376
Todd Kennedyce3e1292020-10-29 17:14:24 -07002377/** Represents <property> elements. **/
2378class Property : public ManifestExtractor::Element {
2379 public:
2380 Property() = default;
2381 std::string name;
2382 std::string value;
2383 const int* value_int;
2384 std::string resource;
2385 const int* resource_int;
2386
2387 void Extract(xml::Element* element) override {
2388 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
2389 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
2390 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
2391 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
2392 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
2393 }
2394
2395 void Print(text::Printer* printer) override {
2396 printer->Print(StringPrintf("property: name='%s' ", name.data()));
2397 if (!value.empty()) {
2398 printer->Print(StringPrintf("value='%s' ", value.data()));
2399 } else if (value_int) {
2400 printer->Print(StringPrintf("value='%d' ", *value_int));
2401 } else {
2402 if (!resource.empty()) {
2403 printer->Print(StringPrintf("resource='%s' ", resource.data()));
2404 } else if (resource_int) {
2405 printer->Print(StringPrintf("resource='%d' ", *resource_int));
2406 }
2407 }
2408 printer->Print("\n");
2409 }
Todd Kennedyce3e1292020-10-29 17:14:24 -07002410
Iurii Makhno85875a82022-04-26 15:30:01 +00002411 void ToProto(pb::Badging* out_badging) override {
2412 if (!name.empty()) {
2413 auto property = out_badging->add_properties();
2414 property->set_name(name);
2415 if (!value.empty()) {
2416 property->set_value_string(value);
2417 } else if (value_int) {
2418 property->set_value_int(*value_int);
2419 } else {
2420 if (!resource.empty()) {
2421 property->set_resource_string(resource);
2422 } else if (resource_int) {
2423 property->set_resource_int(*resource_int);
2424 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002425 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002426 }
2427 }
2428};
2429
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002430/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07002431static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002432 el->Print(printer);
2433 for (auto &child : el->children()) {
2434 Print(child.get(), printer);
2435 }
2436}
2437
Iurii Makhno85875a82022-04-26 15:30:01 +00002438/** Recursively serializes extracted badging elements to proto. */
2439static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) {
2440 el->ToProto(out_badging);
2441 for (auto& child : el->children()) {
2442 ToProto(child.get(), out_badging);
2443 }
2444}
2445
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002446bool ManifestExtractor::Extract(IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002447 // Load the manifest
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002448 doc_ = apk_->LoadXml("AndroidManifest.xml", diag);
2449 if (doc_ == nullptr) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002450 diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
2451 return false;
2452 }
2453
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002454 xml::Element* element = doc_->root.get();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002455 if (element->name != "manifest") {
2456 diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
2457 return false;
2458 }
2459
2460 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
2461 // printing only permission elements is requested
2462 if (options_.only_permissions) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002463 root_element_ = ManifestExtractor::Element::Inflate(this, element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002464
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002465 if (auto manifest = ElementCast<Manifest>(root_element_.get())) {
2466 manifest->only_package_name = true;
2467
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002468 for (xml::Element* child : element->GetChildElements()) {
2469 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
2470 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002471 // Inflate the element and its descendants
2472 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002473 manifest->AddChild(permission_element);
2474 }
2475 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002476 return true;
2477 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002478 return false;
2479 }
2480
2481 // Collect information about the resource configurations
2482 if (apk_->GetResourceTable()) {
2483 for (auto &package : apk_->GetResourceTable()->packages) {
2484 for (auto &type : package->types) {
2485 for (auto &entry : type->entries) {
2486 for (auto &value : entry->values) {
2487 std::string locale_str = value->config.GetBcp47LanguageTag();
2488
2489 // Collect all the unique locales of the apk
2490 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002491 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002492 config.setBcp47Locale(locale_str.data());
2493 locales_.insert(std::make_pair(locale_str, config));
2494 }
2495
2496 // Collect all the unique density of the apk
2497 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
2498 : value->config.density;
2499 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002500 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002501 config.density = density;
2502 densities_.insert(std::make_pair(density, config));
2503 }
2504 }
2505 }
2506 }
2507 }
2508 }
2509
2510 // Extract badging information
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002511 root_element_ = Visit(element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002512
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002513 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
2514 // attribute values from the last defined tag.
2515 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002516 for (const auto& child : root_element_->children()) {
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002517 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
2518 filtered_uses_sdk_tags.emplace_back(uses_sdk);
2519 }
2520 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002521 if (filtered_uses_sdk_tags.size() >= 2U) {
2522 filtered_uses_sdk_tags.pop_back();
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002523 root_element_->Filter([&](const ManifestExtractor::Element* e) {
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002524 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
2525 filtered_uses_sdk_tags.end();
2526 });
2527 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002528
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002529 /** Recursively checks the extracted elements for the specified permission. **/
2530 auto FindPermission = [&](ManifestExtractor::Element* root,
2531 const std::string& name) -> ManifestExtractor::Element* {
2532 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
2533 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
2534 return permission->name == name;
2535 }
2536 return false;
2537 });
2538 };
2539
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002540 auto AddImpliedPermission = [&](const std::string& name, const std::string& reason,
2541 int32_t max_sdk_version) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002542 auto permission = util::make_unique<UsesPermission>();
2543 permission->name = name;
2544 permission->maxSdkVersion = max_sdk_version;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002545 permission->implied = true;
2546 permission->impliedReason = reason;
2547 implied_permissions_.push_back(std::move(permission));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002548 };
2549
2550 // Implied permissions
2551 // Pre-1.6 implicitly granted permission compatibility logic
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002552 bool insert_write_external = false;
2553 auto write_external_permission = ElementCast<UsesPermission>(
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002554 FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002555
Jackal Guo201a60a2021-08-31 12:37:30 +08002556 if (target_sdk() < SDK_DONUT) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002557 if (!write_external_permission) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002558 AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002559 insert_write_external = true;
2560 }
2561
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002562 if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) {
2563 AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002564 }
2565 }
2566
2567 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2568 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2569 // do this (regardless of target API version) because we can't have
2570 // an app with write permission but not read permission.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002571 auto read_external =
2572 FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002573 if (!read_external && (insert_write_external || write_external_permission)) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002574 AddImpliedPermission(
2575 "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE",
2576 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002577 }
2578
2579 // Pre-JellyBean call log permission compatibility.
Jackal Guo201a60a2021-08-31 12:37:30 +08002580 if (target_sdk() < SDK_JELLY_BEAN) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002581 if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") &&
2582 FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) {
2583 AddImpliedPermission("android.permission.READ_CALL_LOG",
2584 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002585 }
2586
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002587 if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") &&
2588 FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) {
2589 AddImpliedPermission("android.permission.WRITE_CALL_LOG",
2590 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002591 }
2592 }
2593
2594 // If the app hasn't declared the touchscreen as a feature requirement (either
2595 // directly or implied, required or not), then the faketouch feature is implied.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002596 if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) {
2597 common_feature_group()->addImpliedFeature("android.hardware.faketouch",
2598 "default feature for all apps", false);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002599 }
2600
2601 // Only print the common feature group if no feature group is defined
2602 std::vector<FeatureGroup*> feature_groups;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002603 ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002604 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2605 feature_groups.push_back(feature_group);
2606 }
2607 });
2608
2609 if (feature_groups.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002610 feature_groups_.push_back(common_feature_group());
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002611 } else {
2612 // Merge the common feature group into the feature group
2613 for (auto& feature_group : feature_groups) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002614 feature_group->Merge(common_feature_group());
2615 feature_groups_.push_back(feature_group);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002616 }
2617 };
2618
2619 // Collect the component types of the application
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002620 ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002621 if (ElementCast<Action>(el)) {
2622 auto action = ElementCast<Action>(el);
2623 if (!action->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002624 components_.discovered_components.insert(action->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002625 return;
2626 }
2627 }
2628
2629 if (ElementCast<Category>(el)) {
2630 auto category = ElementCast<Category>(el);
2631 if (!category->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002632 components_.discovered_components.insert(category->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002633 return;
2634 }
2635 }
2636 });
2637
2638 // Check for the payment component
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002639 ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002640 if (auto service = ElementCast<Service>(el)) {
2641 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2642 [&](ManifestExtractor::Element* el) -> bool {
2643 if (auto action = ElementCast<Action>(el)) {
2644 return (action->component == "host-apdu");
2645 }
2646 return false;
2647 }));
2648
2649 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2650 [&](ManifestExtractor::Element* el) -> bool {
2651 if (auto action = ElementCast<Action>(el)) {
2652 return (action->component == "offhost-apdu");
2653 }
2654 return false;
2655 }));
2656
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002657 ForEachChild(service,
2658 [this, &diag, &host_apdu_action,
2659 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2660 if (auto meta_data = ElementCast<MetaData>(el)) {
2661 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" &&
2662 host_apdu_action) ||
2663 (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" &&
2664 offhost_apdu_action)) {
2665 // Attempt to load the resource file
2666 if (!meta_data->resource.empty()) {
2667 return;
2668 }
2669 auto resource = this->apk_->LoadXml(meta_data->resource, diag);
2670 if (!resource) {
2671 return;
2672 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002673
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002674 // Look for the payment category on an <aid-group> element
2675 auto& root = resource.get()->root;
2676 if ((host_apdu_action && root->name == "host-apdu-service") ||
2677 (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2678 for (auto& child : root->GetChildElements()) {
2679 if (child->name == "aid-group") {
2680 auto category = FindAttribute(child, CATEGORY_ATTR);
2681 if (category && category->value == "payment") {
Iurii Makhno85875a82022-04-26 15:30:01 +00002682 this->components_.discovered_components.insert("payment");
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002683 return;
2684 }
2685 }
2686 }
2687 }
2688 }
2689 }
2690 });
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002691 }
2692 });
2693
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002694 // Print presence of activities, receivers, and services with no special components
2695 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2696 if (auto activity = ElementCast<Activity>(el)) {
2697 if (!activity->has_component_) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002698 components_.other_activities = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002699 return true;
2700 }
2701 }
2702 return false;
2703 });
2704
2705 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2706 if (auto receiver = ElementCast<Receiver>(el)) {
2707 if (!receiver->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002708 components_.other_receivers = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002709 return true;
2710 }
2711 }
2712 return false;
2713 });
2714
2715 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2716 if (auto service = ElementCast<Service>(el)) {
2717 if (!service->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002718 components_.other_services = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002719 return true;
2720 }
2721 }
2722 return false;
2723 });
2724
2725 // Gather the supported screens
2726 const static SupportsScreen default_screens{};
2727 SupportsScreen* screen = ElementCast<SupportsScreen>(
2728 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2729 return ElementCast<SupportsScreen>(el) != nullptr;
2730 }));
2731 supports_screen_ = screen ? screen : &default_screens;
2732
2733 // Gather the supported architectures_ of the app
2734 std::set<std::string> architectures_from_apk;
2735 auto it = apk_->GetFileCollection()->Iterator();
2736 while (it->HasNext()) {
2737 auto file_path = it->Next()->GetSource().path;
2738 size_t pos = file_path.find("lib/");
2739 if (pos != std::string::npos) {
2740 file_path = file_path.substr(pos + 4);
2741 pos = file_path.find('/');
2742 if (pos != std::string::npos) {
2743 file_path = file_path.substr(0, pos);
2744 }
2745
2746 architectures_from_apk.insert(file_path);
2747 }
2748 }
2749
2750 // Determine if the application has multiArch supports
2751 auto has_multi_arch =
2752 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2753 if (auto application = ElementCast<Application>(el)) {
2754 return application->has_multi_arch;
2755 }
2756 return false;
2757 });
2758
2759 bool output_alt_native_code = false;
2760 // A multiArch package is one that contains 64-bit and
2761 // 32-bit versions of native code and expects 3rd-party
2762 // apps to load these native code libraries. Since most
2763 // 64-bit systems also support 32-bit apps, the apps
2764 // loading this multiArch package's code may be either
2765 if (has_multi_arch) {
2766 // If this is a multiArch package, report the 64-bit
2767 // version only. Then as a separate entry, report the
2768 // rest.
2769 //
2770 // If we report the 32-bit architecture, this APK will
2771 // be installed on a 32-bit device, causing a large waste
2772 // of bandwidth and disk space. This assumes that
2773 // the developer of the multiArch package has also
2774 // made a version that is 32-bit only.
2775 const std::string kIntel64 = "x86_64";
2776 const std::string kArm64 = "arm64-v8a";
2777
2778 auto arch = architectures_from_apk.find(kIntel64);
2779 if (arch == architectures_from_apk.end()) {
2780 arch = architectures_from_apk.find(kArm64);
2781 }
2782
2783 if (arch != architectures_from_apk.end()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002784 architectures_.architectures.insert(*arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002785 architectures_from_apk.erase(arch);
2786 output_alt_native_code = true;
2787 }
2788 }
2789 for (auto& arch : architectures_from_apk) {
2790 if (output_alt_native_code) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002791 architectures_.alt_architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002792 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002793 architectures_.architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002794 }
2795 }
2796 return true;
2797}
2798
2799bool ManifestExtractor::Dump(text::Printer* printer) {
2800 Print(root_element_.get(), printer);
2801 if (options_.only_permissions) {
2802 return true;
2803 }
2804
2805 for (auto& implied_permission : implied_permissions_) {
2806 implied_permission->Print(printer);
2807 }
2808 for (auto& feature_group : feature_groups_) {
2809 feature_group->PrintGroup(printer);
2810 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002811 components_.Print(printer);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002812 supports_screen_->PrintScreens(printer, target_sdk_);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002813
2814 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002815 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002816 for (auto& config : locales_) {
2817 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002818 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002819 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002820 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002821 }
2822 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002823 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002824
2825 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002826 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002827 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002828 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002829 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002830 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002831
Iurii Makhno85875a82022-04-26 15:30:01 +00002832 architectures_.Print(printer);
2833 return true;
2834}
2835
2836bool ManifestExtractor::DumpProto(pb::Badging* out_badging) {
2837 ToProto(root_element_.get(), out_badging);
2838 for (auto& implied_permission : implied_permissions_) {
2839 implied_permission->ToProto(out_badging);
2840 }
2841 for (auto& feature_group : feature_groups_) {
2842 feature_group->GroupToProto(out_badging);
2843 }
2844 components_.ToProto(out_badging);
2845 supports_screen_->ToProtoScreens(out_badging, target_sdk_);
2846
2847 for (auto& config : locales_) {
2848 if (!config.first.empty()) {
2849 out_badging->add_locales(config.first);
2850 }
2851 }
2852 for (auto& config : densities_) {
2853 out_badging->add_densities(config.first);
2854 }
2855
2856 architectures_.ToProto(out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002857 return true;
2858}
2859
2860/**
2861 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2862 * pointer.
2863 **/
2864template<typename T>
2865T* ElementCast(ManifestExtractor::Element* element) {
2866 if (element == nullptr) {
2867 return nullptr;
2868 }
2869
2870 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002871 {"action", std::is_base_of<Action, T>::value},
2872 {"activity", std::is_base_of<Activity, T>::value},
2873 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2874 {"application", std::is_base_of<Application, T>::value},
2875 {"category", std::is_base_of<Category, T>::value},
2876 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2877 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2878 {"input-type", std::is_base_of<InputType, T>::value},
2879 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2880 {"meta-data", std::is_base_of<MetaData, T>::value},
2881 {"manifest", std::is_base_of<Manifest, T>::value},
2882 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2883 {"overlay", std::is_base_of<Overlay, T>::value},
2884 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2885 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002886 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002887 {"provider", std::is_base_of<Provider, T>::value},
2888 {"receiver", std::is_base_of<Receiver, T>::value},
2889 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2890 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2891 {"screen", std::is_base_of<Screen, T>::value},
2892 {"service", std::is_base_of<Service, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002893 {"sdk-library", std::is_base_of<SdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002894 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2895 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2896 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2897 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2898 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2899 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2900 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2901 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2902 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2903 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2904 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2905 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002906 {"uses-sdk-library", std::is_base_of<UsesSdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002907 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002908 };
2909
2910 auto check = kTagCheck.find(element->tag());
2911 if (check != kTagCheck.end() && check->second) {
2912 return static_cast<T*>(element);
2913 }
2914 return nullptr;
2915}
2916
2917template<typename T>
2918std::unique_ptr<T> CreateType() {
2919 return std::move(util::make_unique<T>());
2920}
2921
2922std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2923 ManifestExtractor* extractor, xml::Element* el) {
2924 const std::unordered_map<std::string,
2925 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2926 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002927 {"action", &CreateType<Action>},
2928 {"activity", &CreateType<Activity>},
2929 {"additional-certificate", &CreateType<AdditionalCertificate>},
2930 {"application", &CreateType<Application>},
2931 {"category", &CreateType<Category>},
2932 {"compatible-screens", &CreateType<CompatibleScreens>},
2933 {"feature-group", &CreateType<FeatureGroup>},
2934 {"input-type", &CreateType<InputType>},
2935 {"intent-filter", &CreateType<IntentFilter>},
2936 {"manifest", &CreateType<Manifest>},
2937 {"meta-data", &CreateType<MetaData>},
2938 {"original-package", &CreateType<OriginalPackage>},
2939 {"overlay", &CreateType<Overlay>},
2940 {"package-verifier", &CreateType<PackageVerifier>},
2941 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002942 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002943 {"provider", &CreateType<Provider>},
2944 {"receiver", &CreateType<Receiver>},
2945 {"required-feature", &CreateType<RequiredFeature>},
2946 {"required-not-feature", &CreateType<RequiredNotFeature>},
2947 {"screen", &CreateType<Screen>},
2948 {"service", &CreateType<Service>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002949 {"sdk-library", &CreateType<SdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002950 {"static-library", &CreateType<StaticLibrary>},
2951 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2952 {"supports-input", &CreateType<SupportsInput>},
2953 {"supports-screens", &CreateType<SupportsScreen>},
2954 {"uses-configuration", &CreateType<UsesConfiguarion>},
2955 {"uses-feature", &CreateType<UsesFeature>},
2956 {"uses-library", &CreateType<UsesLibrary>},
2957 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2958 {"uses-package", &CreateType<UsesPackage>},
2959 {"uses-permission", &CreateType<UsesPermission>},
2960 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2961 {"uses-sdk", &CreateType<UsesSdkBadging>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002962 {"uses-sdk-library", &CreateType<UsesSdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002963 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2964 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002965
2966 // Attempt to map the xml tag to a element inflater
2967 std::unique_ptr<ManifestExtractor::Element> element;
2968 auto check = kTagCheck.find(el->name);
2969 if (check != kTagCheck.end()) {
2970 element = check->second();
2971 } else {
2972 element = util::make_unique<ManifestExtractor::Element>();
2973 }
2974
2975 element->extractor_ = extractor;
2976 element->tag_ = el->name;
2977 element->Extract(el);
2978 return element;
2979}
2980
2981std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2982 auto element = ManifestExtractor::Element::Inflate(this, el);
2983 parent_stack_.insert(parent_stack_.begin(), element.get());
2984
2985 // Process the element and recursively visit the children
2986 for (xml::Element* child : el->GetChildElements()) {
2987 auto v = Visit(child);
2988 element->AddChild(v);
2989 }
2990
2991 parent_stack_.erase(parent_stack_.begin());
2992 return element;
2993}
2994
Ryan Mitchell214846d2018-09-19 16:57:01 -07002995int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
2996 IDiagnostics* diag) {
2997 ManifestExtractor extractor(apk, options);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002998 if (!extractor.Extract(diag)) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002999 return 1;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003000 }
3001 return extractor.Dump(printer) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003002}
3003
Iurii Makhno85875a82022-04-26 15:30:01 +00003004int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, IDiagnostics* diag) {
3005 DumpManifestOptions options{/* include_meta_data= */ true,
3006 /* only_permissions= */ false};
3007 ManifestExtractor extractor(apk, options);
3008 if (!extractor.Extract(diag)) {
3009 return 1;
3010 }
3011 return extractor.DumpProto(out_badging) ? 0 : 1;
3012}
3013
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02003014} // namespace aapt