blob: d60869af28461893bbe2f62a41f1f889668ddfcd [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000444 bool Extract(android::IDiagnostics* diag);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000445 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
Iurii Makhno05fd9292022-08-24 18:56:12 +0000451 /** Resets target SDK to 0. */
452 void ResetTargetSdk() {
453 target_sdk_ = 0;
454 }
455
456 /** Raises the target sdk value if the min target is greater than the current target. */
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700457 void RaiseTargetSdk(int32_t min_target) {
458 if (min_target > target_sdk_) {
459 target_sdk_ = min_target;
460 }
461 }
462
463 /**
464 * Retrieves the default feature group that features are added into when <uses-feature>
465 * are not in a <feature-group> element.
466 **/
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000467 CommonFeatureGroup* common_feature_group() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700468 return commonFeatureGroup_.get();
469 }
470
471 /**
472 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
473 * used for that density setting.
474 **/
475 const std::map<uint16_t, ConfigDescription> densities() const {
476 return densities_;
477 }
478
479 /**
480 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
481 * would be used for that locale setting.
482 **/
483 const std::map<std::string, ConfigDescription> locales() const {
484 return locales_;
485 }
486
487 /** Retrieves the current stack of parent during data extraction. */
488 const std::vector<Element*> parent_stack() const {
489 return parent_stack_;
490 }
491
492 int32_t target_sdk() const {
493 return target_sdk_;
494 }
495
496 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700497 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700498
499 private:
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000500 std::unique_ptr<xml::XmlResource> doc_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700501 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
502 std::map<std::string, ConfigDescription> locales_;
503 std::map<uint16_t, ConfigDescription> densities_;
504 std::vector<Element*> parent_stack_;
505 int32_t target_sdk_ = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000506
507 std::unique_ptr<ManifestExtractor::Element> root_element_;
508 std::vector<std::unique_ptr<ManifestExtractor::Element>> implied_permissions_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000509 std::vector<FeatureGroup*> feature_groups_;
Iurii Makhno85875a82022-04-26 15:30:01 +0000510 Components components_;
511 Architectures architectures_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000512 const SupportsScreen* supports_screen_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700513};
514
515template<typename T> T* ElementCast(ManifestExtractor::Element* element);
516
517/** Recurs through the children of the specified root in depth-first order. */
518static void ForEachChild(ManifestExtractor::Element* root,
519 std::function<void(ManifestExtractor::Element*)> f) {
520 for (auto& child : root->children()) {
521 f(child.get());
522 ForEachChild(child.get(), f);
523 }
524}
525
526/**
527 * Checks the element and its recursive children for an element that makes the specified
528 * conditional function return true. Returns the first element that makes the conditional function
529 * return true.
530 **/
531static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
532 std::function<bool(ManifestExtractor::Element*)> f) {
533 if (f(root)) {
534 return root;
535 }
536 for (auto& child : root->children()) {
537 if (auto b2 = FindElement(child.get(), f)) {
538 return b2;
539 }
540 }
541 return nullptr;
542}
543
544/** Represents the <manifest> elements **/
545class Manifest : public ManifestExtractor::Element {
546 public:
547 Manifest() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000548 bool only_package_name;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700549 std::string package;
550 int32_t versionCode;
551 std::string versionName;
552 const std::string* split = nullptr;
553 const std::string* platformVersionName = nullptr;
554 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700555 const int32_t* platformVersionNameInt = nullptr;
556 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700557 const int32_t* compilesdkVersion = nullptr;
558 const std::string* compilesdkVersionCodename = nullptr;
559 const int32_t* installLocation = nullptr;
560
561 void Extract(xml::Element* manifest) override {
562 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
563 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
564 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
565 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
566
567 // Extract the platform build info
568 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
569 "platformBuildVersionName"));
570 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
571 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700572 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
573 "platformBuildVersionName"));
574 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
575 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700576
577 // Extract the compile sdk info
578 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
579 compilesdkVersionCodename = GetAttributeString(
580 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
581 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
582 }
583
Iurii Makhno85875a82022-04-26 15:30:01 +0000584 void ToProto(pb::Badging* out_badging) override {
585 auto out_package = out_badging->mutable_package();
586 out_package->set_package(package);
587 out_package->set_version_code(versionCode);
588 out_package->set_version_name(versionName);
589 if (compilesdkVersion) {
590 out_package->set_compile_sdk_version(*compilesdkVersion);
591 }
592 if (compilesdkVersionCodename) {
593 out_package->set_compile_sdk_version_codename(*compilesdkVersionCodename);
594 }
595 if (platformVersionName) {
596 out_package->set_platform_version_name(*platformVersionName);
597 } else if (platformVersionNameInt) {
598 out_package->set_platform_version_name(std::to_string(*platformVersionNameInt));
599 }
600 if (platformVersionCode) {
601 out_package->set_platform_version_code(*platformVersionCode);
602 } else if (platformVersionCodeInt) {
603 out_package->set_platform_version_code(std::to_string(*platformVersionCodeInt));
604 }
605
606 if (installLocation) {
607 switch (*installLocation) {
608 case 0:
609 out_package->set_install_location(pb::PackageInfo_InstallLocation_AUTO);
610 break;
611 case 1:
612 out_package->set_install_location(pb::PackageInfo_InstallLocation_INTERNAL_ONLY);
613 break;
614 case 2:
615 out_package->set_install_location(pb::PackageInfo_InstallLocation_PREFER_EXTERNAL);
616 break;
617 default:
618 break;
619 }
620 }
621 }
622
Ryan Mitchell214846d2018-09-19 16:57:01 -0700623 void Print(text::Printer* printer) override {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000624 if (only_package_name) {
625 printer->Println(StringPrintf("package: %s", package.data()));
626 } else {
627 PrintFull(printer);
628 }
629 }
630
631 void PrintFull(text::Printer* printer) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700632 printer->Print(StringPrintf("package: name='%s' ", package.data()));
633 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700634 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700636
637 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700638 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700639 }
640 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700641 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700642 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700643 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
644 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700645 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700646 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700647 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700648 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
649 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700650 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700651 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700652 }
653 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700654 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700655 compilesdkVersionCodename->data()));
656 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700657 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700658
659 if (installLocation) {
660 switch (*installLocation) {
661 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700662 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700663 break;
664 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700665 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700666 break;
667 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700668 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700669 break;
670 default:
671 break;
672 }
673 }
674 }
675};
676
677/** Represents <application> elements. **/
678class Application : public ManifestExtractor::Element {
679 public:
680 Application() = default;
681 std::string label;
682 std::string icon;
683 std::string banner;
684 int32_t is_game;
685 int32_t debuggable;
686 int32_t test_only;
687 bool has_multi_arch;
688
689 /** Mapping from locales to app names. */
690 std::map<std::string, std::string> locale_labels;
691
692 /** Mapping from densities to app icons. */
693 std::map<uint16_t, std::string> density_icons;
694
695 void Extract(xml::Element* element) override {
696 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
697 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
698 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
699 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
700 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
701 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
702
703 // We must search by name because the multiArch flag hasn't been API
704 // frozen yet.
705 has_multi_arch = (GetAttributeIntegerDefault(
706 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
707
708 // Retrieve the app names for every locale the app supports
709 auto attr = FindAttribute(element, LABEL_ATTR);
710 for (auto& config : extractor()->locales()) {
711 if (auto label = GetAttributeString(attr, config.second)) {
712 if (label) {
713 locale_labels.insert(std::make_pair(config.first, *label));
714 }
715 }
716 }
717
718 // Retrieve the icons for the densities the app supports
719 attr = FindAttribute(element, ICON_ATTR);
720 for (auto& config : extractor()->densities()) {
721 if (auto resource = GetAttributeString(attr, config.second)) {
722 if (resource) {
723 density_icons.insert(std::make_pair(config.first, *resource));
724 }
725 }
726 }
727 }
728
Ryan Mitchell214846d2018-09-19 16:57:01 -0700729 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700730 // Print the labels for every locale
731 for (auto p : locale_labels) {
732 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700733 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700734 android::ResTable::normalizeForOutput(p.second.data())
735 .c_str()));
736 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700737 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700738 android::ResTable::normalizeForOutput(p.second.data())
739 .c_str()));
740 }
741 }
742
743 // Print the icon paths for every density
744 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700745 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700746 }
747
748 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700749 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700750 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700751 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700752 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700753 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700754 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700755 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700756
757 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700758 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700759 }
760 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700761 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700762 }
763 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700764 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700765 }
766 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000767
768 void ToProto(pb::Badging* out_badging) override {
769 auto application = out_badging->mutable_application();
770 application->set_label(android::ResTable::normalizeForOutput(label.data()));
771 application->set_icon(icon);
772 application->set_banner(banner);
773 application->set_test_only(test_only != 0);
774 application->set_game(is_game != 0);
775 application->set_debuggable(debuggable != 0);
776
777 auto out_locale_labels = application->mutable_locale_labels();
778 for (auto& p : locale_labels) {
779 if (!p.first.empty()) {
780 (*out_locale_labels)[p.first] = p.second;
781 }
782 }
783 auto out_density_icons = application->mutable_density_icons();
784 for (auto& p : density_icons) {
785 (*out_density_icons)[p.first] = p.second;
786 }
787 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700788};
789
790/** Represents <uses-sdk> elements. **/
791class UsesSdkBadging : public ManifestExtractor::Element {
792 public:
793 UsesSdkBadging() = default;
794 const int32_t* min_sdk = nullptr;
795 const std::string* min_sdk_name = nullptr;
796 const int32_t* max_sdk = nullptr;
797 const int32_t* target_sdk = nullptr;
798 const std::string* target_sdk_name = nullptr;
799
800 void Extract(xml::Element* element) override {
801 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
802 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
803 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
804 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
805 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
806
Iurii Makhno05fd9292022-08-24 18:56:12 +0000807 // Resets target SDK first. This is required if APK contains multiple <uses-sdk> elements,
808 // we only need to take the latest values.
809 extractor()->ResetTargetSdk();
810
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700811 // Detect the target sdk of the element
812 if ((min_sdk_name && *min_sdk_name == "Donut")
813 || (target_sdk_name && *target_sdk_name == "Donut")) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800814 extractor()->RaiseTargetSdk(SDK_DONUT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700815 }
816 if (min_sdk) {
817 extractor()->RaiseTargetSdk(*min_sdk);
818 }
819 if (target_sdk) {
820 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700821 } else if (target_sdk_name) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800822 extractor()->RaiseTargetSdk(SDK_CUR_DEVELOPMENT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700823 }
824 }
825
Ryan Mitchell214846d2018-09-19 16:57:01 -0700826 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700827 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700828 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700829 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700830 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700831 }
832 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700833 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700834 }
835 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700836 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700837 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700838 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700839 }
840 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000841
842 void ToProto(pb::Badging* out_badging) override {
843 auto out_sdks = out_badging->mutable_uses_sdk();
844 if (min_sdk) {
845 out_sdks->set_min_sdk_version(*min_sdk);
846 } else if (min_sdk_name) {
847 out_sdks->set_min_sdk_version_name(*min_sdk_name);
848 }
849 if (max_sdk) {
850 out_sdks->set_max_sdk_version(*max_sdk);
851 }
852 if (target_sdk) {
853 out_sdks->set_target_sdk_version(*target_sdk);
854 } else if (target_sdk_name) {
855 out_sdks->set_target_sdk_version_name(*target_sdk_name);
856 }
857 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700858};
859
860/** Represents <uses-configuration> elements. **/
861class UsesConfiguarion : public ManifestExtractor::Element {
862 public:
863 UsesConfiguarion() = default;
864 int32_t req_touch_screen = 0;
865 int32_t req_keyboard_type = 0;
866 int32_t req_hard_keyboard = 0;
867 int32_t req_navigation = 0;
868 int32_t req_five_way_nav = 0;
869
870 void Extract(xml::Element* element) override {
871 req_touch_screen = GetAttributeIntegerDefault(
872 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
873 req_keyboard_type = GetAttributeIntegerDefault(
874 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
875 req_hard_keyboard = GetAttributeIntegerDefault(
876 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
877 req_navigation = GetAttributeIntegerDefault(
878 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
879 req_five_way_nav = GetAttributeIntegerDefault(
880 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
881 }
882
Ryan Mitchell214846d2018-09-19 16:57:01 -0700883 void Print(text::Printer* printer) override {
884 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700885 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700886 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700887 }
888 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700889 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700890 }
891 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700892 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700893 }
894 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700895 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700896 }
897 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700900 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700901 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000902
903 void ToProto(pb::Badging* out_badging) override {
904 auto out_configuration = out_badging->mutable_uses_configuration();
905 out_configuration->set_req_touch_screen(req_touch_screen);
906 out_configuration->set_req_keyboard_type(req_keyboard_type);
907 out_configuration->set_req_hard_keyboard(req_hard_keyboard);
908 out_configuration->set_req_navigation(req_navigation);
909 out_configuration->set_req_five_way_nav(req_five_way_nav);
910 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700911};
912
913/** Represents <supports-screen> elements. **/
914class SupportsScreen : public ManifestExtractor::Element {
915 public:
916 SupportsScreen() = default;
917 int32_t small_screen = 1;
918 int32_t normal_screen = 1;
919 int32_t large_screen = 1;
920 int32_t xlarge_screen = 1;
921 int32_t any_density = 1;
922 int32_t requires_smallest_width_dp = 0;
923 int32_t compatible_width_limit_dp = 0;
924 int32_t largest_width_limit_dp = 0;
925
926 void Extract(xml::Element* element) override {
927 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
928 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
929 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
930 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
931 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
932
933 requires_smallest_width_dp = GetAttributeIntegerDefault(
934 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
935 compatible_width_limit_dp = GetAttributeIntegerDefault(
936 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
937 largest_width_limit_dp = GetAttributeIntegerDefault(
938 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
939
940 // For modern apps, if screen size buckets haven't been specified
941 // but the new width ranges have, then infer the buckets from them.
942 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
943 && requires_smallest_width_dp > 0) {
944 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
945 : requires_smallest_width_dp;
946 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
947 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
948 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
949 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
950 }
951 }
952
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000953 void PrintScreens(text::Printer* printer, int32_t target_sdk) const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700954 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700955 printer->Print("supports-screens:");
Iurii Makhno85875a82022-04-26 15:30:01 +0000956 if (IsSmallScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700957 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700958 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000959 if (normal_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700960 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700961 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000962 if (IsLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700963 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700964 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000965 if (IsXLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700966 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700967 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700968 printer->Print("\n");
969 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Iurii Makhno85875a82022-04-26 15:30:01 +0000970 (IsAnyDensitySupported(target_sdk)) ? "true" : "false"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700971 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700972 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700973 }
974 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700975 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700976 }
977 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700978 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700979 }
980 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000981
982 void ToProtoScreens(pb::Badging* out_badging, int32_t target_sdk) const {
983 auto supports_screen = out_badging->mutable_supports_screen();
984 if (IsSmallScreenSupported(target_sdk)) {
985 supports_screen->add_screens(pb::SupportsScreen_ScreenType_SMALL);
986 }
987 if (normal_screen != 0) {
988 supports_screen->add_screens(pb::SupportsScreen_ScreenType_NORMAL);
989 }
990 if (IsLargeScreenSupported(target_sdk)) {
991 supports_screen->add_screens(pb::SupportsScreen_ScreenType_LARGE);
992 }
993 if (IsXLargeScreenSupported(target_sdk)) {
994 supports_screen->add_screens(pb::SupportsScreen_ScreenType_XLARGE);
995 }
996 supports_screen->set_supports_any_densities(IsAnyDensitySupported(target_sdk));
997 supports_screen->set_requires_smallest_width_dp(requires_smallest_width_dp);
998 supports_screen->set_compatible_width_limit_dp(compatible_width_limit_dp);
999 supports_screen->set_largest_width_limit_dp(largest_width_limit_dp);
1000 }
1001
1002 private:
1003 // Determine default values for any unspecified screen sizes,
1004 // based on the target SDK of the package. As of 4 (donut)
1005 // the screen size support was introduced, so all default to
1006 // enabled.
1007 bool IsSmallScreenSupported(int32_t target_sdk) const {
1008 if (small_screen > 0) {
1009 return target_sdk >= SDK_DONUT;
1010 }
1011 return small_screen != 0;
1012 }
1013
1014 bool IsLargeScreenSupported(int32_t target_sdk) const {
1015 if (large_screen > 0) {
1016 return target_sdk >= SDK_DONUT;
1017 }
1018 return large_screen != 0;
1019 }
1020
1021 bool IsXLargeScreenSupported(int32_t target_sdk) const {
1022 if (xlarge_screen > 0) {
1023 return target_sdk >= SDK_GINGERBREAD;
1024 }
1025 return xlarge_screen != 0;
1026 }
1027
1028 bool IsAnyDensitySupported(int32_t target_sdk) const {
1029 if (any_density > 0) {
1030 return target_sdk >= SDK_DONUT || requires_smallest_width_dp > 0 ||
1031 compatible_width_limit_dp > 0;
1032 }
1033 return any_density != 0;
1034 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001035};
1036
1037/** Represents <feature-group> elements. **/
1038class FeatureGroup : public ManifestExtractor::Element {
1039 public:
1040 FeatureGroup() = default;
1041 std::string label;
1042 int32_t open_gles_version = 0;
1043
1044 void Extract(xml::Element* element) override {
1045 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1046 }
1047
Ryan Mitchell214846d2018-09-19 16:57:01 -07001048 virtual void PrintGroup(text::Printer* printer) {
1049 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001050 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001051 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001052 }
1053
1054 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001055 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001056 (feature.second.required ? "" : "-not-required"),
1057 feature.first.data()));
1058 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001059 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001060 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001061 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001062 }
1063 }
1064
Iurii Makhno85875a82022-04-26 15:30:01 +00001065 virtual void GroupToProto(pb::Badging* out_badging) {
1066 auto feature_group = out_badging->add_feature_groups();
1067 feature_group->set_label(label);
1068 feature_group->set_open_gles_version(open_gles_version);
1069 for (auto& feature : features_) {
1070 auto out_feature = feature_group->add_features();
1071 out_feature->set_name(feature.first);
1072 out_feature->set_required(feature.second.required);
1073 out_feature->set_version(feature.second.version);
1074 }
1075 }
1076
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001077 /** Adds a feature to the feature group. */
1078 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
Iurii Makhno0df4e6d2022-11-29 19:19:13 +00001079 features_.insert_or_assign(name, Feature{required, version});
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001080 if (required) {
1081 if (name == "android.hardware.camera.autofocus" ||
1082 name == "android.hardware.camera.flash") {
1083 AddFeature("android.hardware.camera", true);
1084 } else if (name == "android.hardware.location.gps" ||
1085 name == "android.hardware.location.network") {
1086 AddFeature("android.hardware.location", true);
1087 } else if (name == "android.hardware.faketouch.multitouch") {
1088 AddFeature("android.hardware.faketouch", true);
1089 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
1090 name == "android.hardware.faketouch.multitouch.jazzhands") {
1091 AddFeature("android.hardware.faketouch.multitouch", true);
1092 AddFeature("android.hardware.faketouch", true);
1093 } else if (name == "android.hardware.touchscreen.multitouch") {
1094 AddFeature("android.hardware.touchscreen", true);
1095 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
1096 name == "android.hardware.touchscreen.multitouch.jazzhands") {
1097 AddFeature("android.hardware.touchscreen.multitouch", true);
1098 AddFeature("android.hardware.touchscreen", true);
1099 } else if (name == "android.hardware.opengles.aep") {
1100 const int kOpenGLESVersion31 = 0x00030001;
1101 if (kOpenGLESVersion31 > open_gles_version) {
1102 open_gles_version = kOpenGLESVersion31;
1103 }
1104 }
1105 }
1106 }
1107
1108 /** Returns true if the feature group has the given feature. */
1109 virtual bool HasFeature(const std::string& name) {
1110 return features_.find(name) != features_.end();
1111 }
1112
1113 /** Merges the features of another feature group into this group. */
1114 void Merge(FeatureGroup* group) {
1115 open_gles_version = std::max(open_gles_version, group->open_gles_version);
1116 for (auto& feature : group->features_) {
1117 features_.insert(feature);
1118 }
1119 }
1120
1121 protected:
1122 struct Feature {
1123 public:
1124 bool required = false;
1125 int32_t version = -1;
1126 };
1127
1128 /* Mapping of feature names to their properties. */
1129 std::map<std::string, Feature> features_;
1130};
1131
1132/**
1133 * Represents the default feature group for the application if no <feature-group> elements are
1134 * present in the manifest.
1135 **/
1136class CommonFeatureGroup : public FeatureGroup {
1137 public:
1138 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001139 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001140 FeatureGroup::PrintGroup(printer);
1141
1142 // Also print the implied features
1143 for (auto feature : implied_features_) {
1144 if (features_.find(feature.first) == features_.end()) {
1145 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -07001146 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
1147 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001148 feature.first.data()));
1149
1150 // Print the reasons as a sentence
1151 size_t count = 0;
1152 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001153 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001154 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001155 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001156 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001157 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001158 }
1159 count++;
1160 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001161 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001162 }
1163 }
1164 }
1165
Iurii Makhno85875a82022-04-26 15:30:01 +00001166 virtual void GroupToProto(pb::Badging* out_badging) override {
1167 FeatureGroup::GroupToProto(out_badging);
1168 auto feature_group =
1169 out_badging->mutable_feature_groups(out_badging->feature_groups_size() - 1);
1170 for (auto& feature : implied_features_) {
1171 if (features_.find(feature.first) == features_.end()) {
1172 auto out_feature = feature_group->add_features();
1173 out_feature->set_name(feature.first);
1174 auto implied_data = out_feature->mutable_implied_data();
1175 implied_data->set_from_sdk_23_permission(feature.second.implied_from_sdk_k23);
1176 for (auto& reason : feature.second.reasons) {
1177 implied_data->add_reasons(reason);
1178 }
1179 }
1180 }
1181 }
1182
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001183 /** Returns true if the feature group has the given feature. */
1184 bool HasFeature(const std::string& name) override {
1185 return FeatureGroup::HasFeature(name)
1186 || implied_features_.find(name) != implied_features_.end();
1187 }
1188
1189 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
1190 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
1191 auto entry = implied_features_.find(name);
1192 if (entry == implied_features_.end()) {
1193 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
1194 entry = implied_features_.find(name);
1195 }
1196
1197 // A non-sdk 23 implied feature takes precedence.
1198 if (entry->second.implied_from_sdk_k23 && !sdk23) {
1199 entry->second.implied_from_sdk_k23 = false;
1200 }
1201
1202 entry->second.reasons.insert(reason);
1203 }
1204
1205 /**
1206 * Adds a feature to a set of implied features for all features that are implied by the presence
1207 * of the permission.
1208 **/
1209 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
1210 if (name == "android.permission.CAMERA") {
1211 addImpliedFeature("android.hardware.camera",
1212 StringPrintf("requested %s permission", name.data()),
1213 sdk23);
1214
1215 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
1216 if (targetSdk < SDK_LOLLIPOP) {
1217 addImpliedFeature("android.hardware.location.gps",
1218 StringPrintf("requested %s permission", name.data()),
1219 sdk23);
1220 addImpliedFeature("android.hardware.location.gps",
1221 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1222 sdk23);
1223 }
1224 addImpliedFeature("android.hardware.location",
1225 StringPrintf("requested %s permission", name.data()),
1226 sdk23);
1227
1228 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
1229 if (targetSdk < SDK_LOLLIPOP) {
1230 addImpliedFeature("android.hardware.location.network",
1231 StringPrintf("requested %s permission", name.data()),
1232 sdk23);
1233 addImpliedFeature("android.hardware.location.network",
1234 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1235 sdk23);
1236 }
1237 addImpliedFeature("android.hardware.location",
1238 StringPrintf("requested %s permission", name.data()),
1239 sdk23);
1240
1241 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
1242 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
1243 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
1244 addImpliedFeature("android.hardware.location",
1245 StringPrintf("requested %s permission", name.data()),
1246 sdk23);
1247
1248 } else if (name == "android.permission.BLUETOOTH" ||
1249 name == "android.permission.BLUETOOTH_ADMIN") {
1250 if (targetSdk > SDK_DONUT) {
1251 addImpliedFeature("android.hardware.bluetooth",
1252 StringPrintf("requested %s permission", name.data()),
1253 sdk23);
1254 addImpliedFeature("android.hardware.bluetooth",
1255 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
1256 sdk23);
1257 }
1258
1259 } else if (name == "android.permission.RECORD_AUDIO") {
1260 addImpliedFeature("android.hardware.microphone",
1261 StringPrintf("requested %s permission", name.data()),
1262 sdk23);
1263
1264 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
1265 name == "android.permission.CHANGE_WIFI_STATE" ||
1266 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
1267 addImpliedFeature("android.hardware.wifi",
1268 StringPrintf("requested %s permission", name.data()),
1269 sdk23);
1270
1271 } else if (name == "android.permission.CALL_PHONE" ||
1272 name == "android.permission.CALL_PRIVILEGED" ||
1273 name == "android.permission.MODIFY_PHONE_STATE" ||
1274 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1275 name == "android.permission.READ_SMS" ||
1276 name == "android.permission.RECEIVE_SMS" ||
1277 name == "android.permission.RECEIVE_MMS" ||
1278 name == "android.permission.RECEIVE_WAP_PUSH" ||
1279 name == "android.permission.SEND_SMS" ||
1280 name == "android.permission.WRITE_APN_SETTINGS" ||
1281 name == "android.permission.WRITE_SMS") {
1282 addImpliedFeature("android.hardware.telephony",
1283 "requested a telephony permission",
1284 sdk23);
1285 }
1286 }
1287
1288 private:
1289 /**
1290 * Represents a feature that has been automatically added due to a pre-requisite or for some
1291 * other reason.
1292 */
1293 struct ImpliedFeature {
1294 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1295
1296 /** List of human-readable reasons for why this feature was implied. */
1297 std::set<std::string> reasons;
1298
1299 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1300 bool implied_from_sdk_k23;
1301 };
1302
1303 /* Mapping of implied feature names to their properties. */
1304 std::map<std::string, ImpliedFeature> implied_features_;
1305};
1306
1307/** Represents <uses-feature> elements. **/
1308class UsesFeature : public ManifestExtractor::Element {
1309 public:
1310 UsesFeature() = default;
1311 void Extract(xml::Element* element) override {
1312 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1313 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1314 bool required = GetAttributeIntegerDefault(
1315 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1316 int32_t version = GetAttributeIntegerDefault(
1317 FindAttribute(element, kAndroidNamespace, "version"), 0);
1318
1319 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1320 // common feature group
1321 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1322 if (!feature_group) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001323 feature_group = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001324 } else {
1325 // All features in side of <feature-group> elements are required.
1326 required = true;
1327 }
1328
1329 if (name) {
1330 feature_group->AddFeature(*name, required, version);
1331 } else if (gl) {
1332 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1333 }
1334 }
1335};
1336
1337/** Represents <uses-permission> elements. **/
1338class UsesPermission : public ManifestExtractor::Element {
1339 public:
1340 UsesPermission() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001341 bool implied;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001342 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001343 std::vector<std::string> requiredFeatures;
1344 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001345 int32_t required = true;
1346 int32_t maxSdkVersion = -1;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001347 int32_t usesPermissionFlags = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001348 std::string impliedReason;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001349
1350 void Extract(xml::Element* element) override {
Iurii Makhno0df4e6d2022-11-29 19:19:13 +00001351 const auto parent_stack = extractor()->parent_stack();
1352 if (!extractor()->options_.only_permissions &&
1353 (parent_stack.size() != 1 || !ElementCast<Manifest>(parent_stack[0]))) {
1354 return;
1355 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001356 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001357 std::string feature =
1358 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1359 if (!feature.empty()) {
1360 requiredFeatures.push_back(feature);
1361 }
1362 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1363 if (!feature.empty()) {
1364 requiredNotFeatures.push_back(feature);
1365 }
1366
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001367 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1368 maxSdkVersion = GetAttributeIntegerDefault(
1369 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001370 usesPermissionFlags = GetAttributeIntegerDefault(
1371 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001372
1373 if (!name.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001374 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001375 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1376 }
1377 }
1378
Ryan Mitchell214846d2018-09-19 16:57:01 -07001379 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001380 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001381 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001382 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001383 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001384 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001385 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1386 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1387 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001388 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001389 for (const std::string& requiredFeature : requiredFeatures) {
1390 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1391 }
1392 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1393 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1394 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001395 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001396 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001397 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001398 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001399 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001400 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1401 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1402 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001403 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001404 }
1405 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001406 if (implied) {
1407 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
1408 if (maxSdkVersion >= 0) {
1409 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
1410 }
1411 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1412 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1413 }
1414 printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001415 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001416 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001417
1418 void ToProto(pb::Badging* out_badging) override {
1419 if (!name.empty()) {
1420 auto permission = out_badging->add_uses_permissions();
1421 permission->set_name(name);
1422 if (maxSdkVersion > 0) {
1423 permission->set_max_sdk_version(maxSdkVersion);
1424 }
1425 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1426 permission->mutable_permission_flags()->set_never_for_location(true);
1427 }
1428 for (auto& requiredFeature : requiredFeatures) {
1429 permission->add_required_features(requiredFeature);
1430 }
1431 for (auto& requiredNotFeature : requiredNotFeatures) {
1432 permission->add_required_not_features(requiredNotFeature);
1433 }
1434 permission->set_required(required != 0);
1435 permission->set_implied(implied);
1436 }
1437 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001438};
1439
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001440/** Represents <required-feature> elements. **/
1441class RequiredFeature : public ManifestExtractor::Element {
1442 public:
1443 RequiredFeature() = default;
1444 std::string name;
1445
1446 void Extract(xml::Element* element) override {
1447 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1448 auto parent_stack = extractor()->parent_stack();
1449 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1450 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1451 uses_permission->requiredFeatures.push_back(name);
1452 }
1453 }
1454};
1455
1456/** Represents <required-not-feature> elements. **/
1457class RequiredNotFeature : public ManifestExtractor::Element {
1458 public:
1459 RequiredNotFeature() = default;
1460 std::string name;
1461
1462 void Extract(xml::Element* element) override {
1463 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1464 auto parent_stack = extractor()->parent_stack();
1465 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1466 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1467 uses_permission->requiredNotFeatures.push_back(name);
1468 }
1469 }
1470};
1471
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001472/** Represents <uses-permission-sdk-23> elements. **/
1473class UsesPermissionSdk23 : public ManifestExtractor::Element {
1474 public:
1475 UsesPermissionSdk23() = default;
1476 const std::string* name = nullptr;
1477 const int32_t* maxSdkVersion = nullptr;
1478
1479 void Extract(xml::Element* element) override {
Iurii Makhno0df4e6d2022-11-29 19:19:13 +00001480 const auto parent_stack = extractor()->parent_stack();
1481 if (!extractor()->options_.only_permissions &&
1482 (parent_stack.size() != 1 || !ElementCast<Manifest>(parent_stack[0]))) {
1483 return;
1484 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001485 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1486 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1487
1488 if (name) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001489 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001490 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1491 }
1492 }
1493
Ryan Mitchell214846d2018-09-19 16:57:01 -07001494 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001495 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001496 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001497 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001498 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001499 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001500 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001501 }
1502 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001503
1504 void ToProto(pb::Badging* out_badging) override {
1505 if (name) {
1506 auto permission = out_badging->add_uses_permissions();
1507 permission->set_sdk23_and_above(true);
1508 permission->set_name(*name);
1509 if (maxSdkVersion) {
1510 permission->set_max_sdk_version(*maxSdkVersion);
1511 }
1512 }
1513 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001514};
1515
1516/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1517class Permission : public ManifestExtractor::Element {
1518 public:
1519 Permission() = default;
1520 std::string name;
1521
1522 void Extract(xml::Element* element) override {
1523 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1524 }
1525
Ryan Mitchell214846d2018-09-19 16:57:01 -07001526 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001527 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001528 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001529 }
1530 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001531
1532 void ToProto(pb::Badging* out_badging) override {
1533 if (!name.empty()) {
1534 out_badging->add_permissions()->set_name(name);
1535 }
1536 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001537};
1538
1539/** Represents <activity> elements. **/
1540class Activity : public ManifestExtractor::Element {
1541 public:
1542 Activity() = default;
1543 std::string name;
1544 std::string icon;
1545 std::string label;
1546 std::string banner;
1547
1548 bool has_component_ = false;
1549 bool has_launcher_category = false;
1550 bool has_leanback_launcher_category = false;
1551 bool has_main_action = false;
1552
1553 void Extract(xml::Element* element) override {
1554 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1555 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1556 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1557 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1558
1559 // Retrieve the package name from the manifest
1560 std::string package;
1561 for (auto& parent : extractor()->parent_stack()) {
1562 if (auto manifest = ElementCast<Manifest>(parent)) {
1563 package = manifest->package;
1564 break;
1565 }
1566 }
1567
1568 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001569 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001570 if (idx == 0) {
1571 name = package + name;
1572 } else if (idx < 0) {
1573 name = package + "." + name;
1574 }
1575
1576 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1577 if (orientation) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001578 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001579 int orien = *orientation;
1580 if (orien == 0 || orien == 6 || orien == 8) {
1581 // Requests landscape, sensorLandscape, or reverseLandscape.
1582 common->addImpliedFeature("android.hardware.screen.landscape",
1583 "one or more activities have specified a landscape orientation",
1584 false);
1585 } else if (orien == 1 || orien == 7 || orien == 9) {
1586 // Requests portrait, sensorPortrait, or reversePortrait.
1587 common->addImpliedFeature("android.hardware.screen.portrait",
1588 "one or more activities have specified a portrait orientation",
1589 false);
1590 }
1591 }
1592 }
1593
Ryan Mitchell214846d2018-09-19 16:57:01 -07001594 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001595 // Print whether the activity has the HOME category and a the MAIN action
1596 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001597 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001598 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001599 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001600 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001601 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001602 android::ResTable::normalizeForOutput(label.data()).c_str(),
1603 icon.data()));
1604 }
1605
1606 // Print wether the activity has the HOME category and a the MAIN action
1607 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001608 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001609 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001610 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001611 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001612 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001613 android::ResTable::normalizeForOutput(label.data()).c_str(),
1614 icon.data(), banner.data()));
1615 }
1616 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001617
1618 void ToProto(pb::Badging* out_badging) override {
1619 if (has_main_action && has_launcher_category) {
1620 auto activity = out_badging->mutable_launchable_activity();
1621 activity->set_name(name);
1622 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1623 activity->set_icon(icon);
1624 }
1625 if (has_leanback_launcher_category) {
1626 auto activity = out_badging->mutable_leanback_launchable_activity();
1627 activity->set_name(name);
1628 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1629 activity->set_icon(icon);
1630 activity->set_banner(banner);
1631 }
1632 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001633};
1634
1635/** Represents <intent-filter> elements. */
1636class IntentFilter : public ManifestExtractor::Element {
1637 public:
1638 IntentFilter() = default;
1639};
1640
1641/** Represents <category> elements. */
1642class Category : public ManifestExtractor::Element {
1643 public:
1644 Category() = default;
1645 std::string component = "";
1646
1647 void Extract(xml::Element* element) override {
1648 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1649
1650 auto parent_stack = extractor()->parent_stack();
1651 if (category && ElementCast<IntentFilter>(parent_stack[0])
1652 && ElementCast<Activity>(parent_stack[1])) {
1653 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1654
1655 if (*category == "android.intent.category.LAUNCHER") {
1656 activity->has_launcher_category = true;
1657 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1658 activity->has_leanback_launcher_category = true;
1659 } else if (*category == "android.intent.category.HOME") {
1660 component = "launcher";
1661 }
1662 }
1663 }
1664};
1665
1666/**
1667 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1668 * elements nested within.
1669 **/
1670class Provider : public ManifestExtractor::Element {
1671 public:
1672 Provider() = default;
1673 bool has_required_saf_attributes = false;
1674
1675 void Extract(xml::Element* element) override {
1676 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1677 const int32_t* grant_uri_permissions = GetAttributeInteger(
1678 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1679 const std::string* permission = GetAttributeString(
1680 FindAttribute(element, PERMISSION_ATTR));
1681
1682 has_required_saf_attributes = ((exported && *exported != 0)
1683 && (grant_uri_permissions && *grant_uri_permissions != 0)
1684 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1685 }
1686};
1687
1688/** Represents <receiver> elements. **/
1689class Receiver : public ManifestExtractor::Element {
1690 public:
1691 Receiver() = default;
1692 const std::string* permission = nullptr;
1693 bool has_component = false;
1694
1695 void Extract(xml::Element* element) override {
1696 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1697 }
1698};
1699
1700/**Represents <service> elements. **/
1701class Service : public ManifestExtractor::Element {
1702 public:
1703 Service() = default;
1704 const std::string* permission = nullptr;
1705 bool has_component = false;
1706
1707 void Extract(xml::Element* element) override {
1708 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1709 }
1710};
1711
1712/** Represents <uses-library> elements. **/
1713class UsesLibrary : public ManifestExtractor::Element {
1714 public:
1715 UsesLibrary() = default;
1716 std::string name;
1717 int required;
1718
1719 void Extract(xml::Element* element) override {
1720 auto parent_stack = extractor()->parent_stack();
1721 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1722 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1723 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1724 }
1725 }
1726
Ryan Mitchell214846d2018-09-19 16:57:01 -07001727 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001728 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001729 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001730 (required == 0) ? "-not-required" : "", name.data()));
1731 }
1732 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001733
1734 void ToProto(pb::Badging* out_badging) override {
1735 if (!name.empty()) {
1736 auto uses_library = out_badging->add_uses_libraries();
1737 uses_library->set_name(name);
1738 uses_library->set_required(required != 0);
1739 }
1740 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001741};
1742
Dianne Hackborn813d7502018-10-02 16:59:46 -07001743/** Represents <static-library> elements. **/
1744class StaticLibrary : public ManifestExtractor::Element {
1745 public:
1746 StaticLibrary() = default;
1747 std::string name;
1748 int version;
1749 int versionMajor;
1750
1751 void Extract(xml::Element* element) override {
1752 auto parent_stack = extractor()->parent_stack();
1753 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1754 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1755 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1756 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1757 }
1758 }
1759
Ryan Mitchell214846d2018-09-19 16:57:01 -07001760 void Print(text::Printer* printer) override {
1761 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001762 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1763 name.data(), version, versionMajor));
1764 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001765
1766 void ToProto(pb::Badging* out_badging) override {
1767 auto static_library = out_badging->mutable_static_library();
1768 static_library->set_name(name);
1769 static_library->set_version(version);
1770 static_library->set_version_major(versionMajor);
1771 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001772};
1773
1774/** Represents <uses-static-library> elements. **/
1775class UsesStaticLibrary : public ManifestExtractor::Element {
1776 public:
1777 UsesStaticLibrary() = default;
1778 std::string name;
1779 int version;
1780 int versionMajor;
1781 std::vector<std::string> certDigests;
1782
1783 void Extract(xml::Element* element) override {
1784 auto parent_stack = extractor()->parent_stack();
1785 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1786 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1787 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1788 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1789 AddCertDigest(element);
1790 }
1791 }
1792
1793 void AddCertDigest(xml::Element* element) {
1794 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1795 // We allow ":" delimiters in the SHA declaration as this is the format
1796 // emitted by the certtool making it easy for developers to copy/paste.
1797 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1798 if (!digest.empty()) {
1799 certDigests.push_back(digest);
1800 }
1801 }
1802
Ryan Mitchell214846d2018-09-19 16:57:01 -07001803 void Print(text::Printer* printer) override {
1804 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001805 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1806 name.data(), version, versionMajor));
1807 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001808 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001809 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001810 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001811 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001812
1813 void ToProto(pb::Badging* out_badging) override {
1814 auto uses_static_library = out_badging->add_uses_static_libraries();
1815 uses_static_library->set_name(name);
1816 uses_static_library->set_version(version);
1817 uses_static_library->set_version_major(versionMajor);
1818 for (auto& cert : certDigests) {
1819 uses_static_library->add_certificates(cert);
1820 }
1821 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001822};
1823
Alex Buynytskyya16137052021-12-02 13:26:54 +00001824/** Represents <sdk-library> elements. **/
1825class SdkLibrary : public ManifestExtractor::Element {
1826 public:
1827 SdkLibrary() = default;
1828 std::string name;
1829 int versionMajor;
1830
1831 void Extract(xml::Element* element) override {
1832 auto parent_stack = extractor()->parent_stack();
1833 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1834 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1835 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1836 }
1837 }
1838
1839 void Print(text::Printer* printer) override {
1840 printer->Print(
1841 StringPrintf("sdk-library: name='%s' versionMajor='%d'\n", name.data(), versionMajor));
1842 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001843
1844 void ToProto(pb::Badging* out_badging) override {
1845 auto sdk_library = out_badging->mutable_sdk_library();
1846 sdk_library->set_name(name);
1847 sdk_library->set_version_major(versionMajor);
1848 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001849};
1850
1851/** Represents <uses-sdk-library> elements. **/
1852class UsesSdkLibrary : public ManifestExtractor::Element {
1853 public:
1854 UsesSdkLibrary() = default;
1855 std::string name;
1856 int versionMajor;
1857 std::vector<std::string> certDigests;
1858
1859 void Extract(xml::Element* element) override {
1860 auto parent_stack = extractor()->parent_stack();
1861 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1862 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1863 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1864 AddCertDigest(element);
1865 }
1866 }
1867
1868 void AddCertDigest(xml::Element* element) {
1869 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1870 // We allow ":" delimiters in the SHA declaration as this is the format
1871 // emitted by the certtool making it easy for developers to copy/paste.
1872 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1873 if (!digest.empty()) {
1874 certDigests.push_back(digest);
1875 }
1876 }
1877
1878 void Print(text::Printer* printer) override {
1879 printer->Print(
1880 StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor));
1881 for (size_t i = 0; i < certDigests.size(); i++) {
1882 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
1883 }
1884 printer->Print("\n");
1885 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001886
1887 void ToProto(pb::Badging* out_badging) override {
1888 auto uses_sdk_library = out_badging->add_uses_sdk_libraries();
1889 uses_sdk_library->set_name(name);
1890 uses_sdk_library->set_version_major(versionMajor);
1891 for (auto& cert : certDigests) {
1892 uses_sdk_library->add_certificates(cert);
1893 }
1894 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001895};
1896
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001897/** Represents <uses-native-library> elements. **/
1898class UsesNativeLibrary : public ManifestExtractor::Element {
1899 public:
1900 UsesNativeLibrary() = default;
1901 std::string name;
1902 int required;
1903
1904 void Extract(xml::Element* element) override {
1905 auto parent_stack = extractor()->parent_stack();
1906 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1907 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1908 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1909 }
1910 }
1911
1912 void Print(text::Printer* printer) override {
1913 if (!name.empty()) {
1914 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1915 (required == 0) ? "-not-required" : "", name.data()));
1916 }
1917 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001918
1919 void ToProto(pb::Badging* out_badging) override {
1920 if (!name.empty()) {
1921 auto uses_native_library = out_badging->add_uses_native_libraries();
1922 uses_native_library->set_name(name);
1923 uses_native_library->set_required(required != 0);
1924 }
1925 }
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001926};
1927
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001928/**
1929 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1930 * explicitly enable meta data printing.
1931 **/
1932class MetaData : public ManifestExtractor::Element {
1933 public:
1934 MetaData() = default;
1935 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001936 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001937 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001938 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001939 const int* resource_int;
1940
1941 void Extract(xml::Element* element) override {
1942 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001943 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001944 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001945 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001946 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1947 }
1948
Ryan Mitchell214846d2018-09-19 16:57:01 -07001949 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001950 if (extractor()->options_.include_meta_data && !name.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001951 printer->Print(StringPrintf("meta-data: name='%s'", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001952 if (!value.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001953 printer->Print(StringPrintf(" value='%s'", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001954 } else if (value_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001955 printer->Print(StringPrintf(" value='%d'", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001956 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001957 if (!resource.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001958 printer->Print(StringPrintf(" resource='%s'", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001959 } else if (resource_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001960 printer->Print(StringPrintf(" resource='%d'", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001961 }
1962 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001963 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001964 }
1965 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001966
1967 void ToProto(pb::Badging* out_badging) override {
1968 if (!name.empty()) {
1969 auto metadata = out_badging->add_metadata();
1970 metadata->set_name(name);
1971 if (!value.empty()) {
1972 metadata->set_value_string(value);
1973 } else if (value_int) {
1974 metadata->set_value_int(*value_int);
1975 } else {
1976 if (!resource.empty()) {
1977 metadata->set_resource_string(resource);
1978 } else if (resource_int) {
1979 metadata->set_resource_int(*resource_int);
1980 }
1981 }
1982 }
1983 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001984};
1985
1986/**
1987 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1988 * service components.
1989 **/
1990class Action : public ManifestExtractor::Element {
1991 public:
1992 Action() = default;
1993 std::string component = "";
1994
1995 void Extract(xml::Element* element) override {
1996 auto parent_stack = extractor()->parent_stack();
1997 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1998
1999 if (ElementCast<IntentFilter>(parent_stack[0])) {
2000 if (ElementCast<Activity>(parent_stack[1])) {
2001 // Detects the presence of a particular type of activity.
2002 Activity* activity = ElementCast<Activity>(parent_stack[1]);
Iurii Makhno499ecd32022-08-19 16:34:26 +00002003 static const auto map = std::map<std::string, std::string>({
2004 {"android.intent.action.MAIN", "main"},
2005 {"android.media.action.VIDEO_CAMERA", "camera"},
2006 {"android.media.action.STILL_IMAGE_CAMERA", "camera"},
2007 {"android.media.action.STILL_IMAGE_CAMERA_SECURE", "camera-secure"},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002008 });
2009
2010 auto entry = map.find(action);
2011 if (entry != map.end()) {
2012 component = entry->second;
2013 activity->has_component_ = true;
2014 }
2015
2016 if (action == "android.intent.action.MAIN") {
2017 activity->has_main_action = true;
2018 }
2019
2020 } else if (ElementCast<Receiver>(parent_stack[1])) {
2021 // Detects the presence of a particular type of receiver. If the action requires a
2022 // permission, then the receiver element is checked for the permission.
2023 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
2024 auto map = std::map<std::string, std::string>({
2025 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
2026 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
2027 });
2028
2029 auto permissions = std::map<std::string, std::string>({
2030 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
2031 });
2032
2033 auto entry = map.find(action);
2034 auto permission = permissions.find(action);
2035 if (entry != map.end() && (permission == permissions.end()
2036 || (receiver->permission && permission->second == *receiver->permission))) {
2037 receiver->has_component = true;
2038 component = entry->second;
2039 }
2040
2041 } else if (ElementCast<Service>(parent_stack[1])) {
2042 // Detects the presence of a particular type of service. If the action requires a
2043 // permission, then the service element is checked for the permission.
2044 Service* service = ElementCast<Service>(parent_stack[1]);
2045 auto map = std::map<std::string, std::string>({
2046 { "android.view.InputMethod" , "ime" },
2047 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
2048 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
2049 { "android.printservice.PrintService" , "print-service" },
2050 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
2051 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
2052 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
2053 { "android.service.dreams.DreamService" , "dream" },
2054 });
2055
2056 auto permissions = std::map<std::string, std::string>({
2057 { "android.accessibilityservice.AccessibilityService" ,
2058 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
2059 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
2060 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
2061 "android.permission.BIND_NFC_SERVICE" },
2062 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
2063 "android.permission.BIND_NFC_SERVICE" },
2064 { "android.service.notification.NotificationListenerService" ,
2065 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
2066 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
2067 });
2068
2069 auto entry = map.find(action);
2070 auto permission = permissions.find(action);
2071 if (entry != map.end() && (permission == permissions.end()
2072 || (service->permission && permission->second == *service->permission))) {
2073 service->has_component= true;
2074 component = entry->second;
2075 }
2076
2077 } else if (ElementCast<Provider>(parent_stack[1])) {
2078 // Detects the presence of a particular type of receiver. If the provider requires a
2079 // permission, then the provider element is checked for the permission.
2080 // Detect whether this action
2081 Provider* provider = ElementCast<Provider>(parent_stack[1]);
2082 if (action == "android.content.action.DOCUMENTS_PROVIDER"
2083 && provider->has_required_saf_attributes) {
2084 component = "document-provider";
2085 }
2086 }
2087 }
2088
2089 // Represents a searchable interface
2090 if (action == "android.intent.action.SEARCH") {
2091 component = "search";
2092 }
2093 }
2094};
2095
2096/**
2097 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
2098 **/
2099class SupportsInput : public ManifestExtractor::Element {
2100 public:
2101 SupportsInput() = default;
2102 std::vector<std::string> inputs;
2103
Ryan Mitchell214846d2018-09-19 16:57:01 -07002104 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002105 const size_t size = inputs.size();
2106 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002107 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002108 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002109 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002110 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002111 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002112 }
2113 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002114
2115 void ToProto(pb::Badging* out_badging) override {
2116 auto supports_input = out_badging->mutable_supports_input();
2117 for (auto& input : inputs) {
2118 supports_input->add_inputs(input);
2119 }
2120 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002121};
2122
2123/** Represents <input-type> elements. **/
2124class InputType : public ManifestExtractor::Element {
2125 public:
2126 InputType() = default;
2127 void Extract(xml::Element* element) override {
2128 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2129 auto parent_stack = extractor()->parent_stack();
2130
2131 // Add the input to the set of supported inputs
2132 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
2133 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
2134 supports->inputs.push_back(*name);
2135 }
2136 }
2137};
2138
2139/** Represents <original-package> elements. **/
2140class OriginalPackage : public ManifestExtractor::Element {
2141 public:
2142 OriginalPackage() = default;
2143 const std::string* name = nullptr;
2144
2145 void Extract(xml::Element* element) override {
2146 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2147 }
2148
Ryan Mitchell214846d2018-09-19 16:57:01 -07002149 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002150 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002151 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002152 }
2153 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002154
2155 void ToProto(pb::Badging* out_badging) override {
2156 if (name) {
2157 out_badging->mutable_package()->set_original_package(*name);
2158 }
2159 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002160};
2161
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002162
2163/** Represents <overlay> elements. **/
2164class Overlay : public ManifestExtractor::Element {
2165 public:
2166 Overlay() = default;
2167 const std::string* target_package = nullptr;
2168 int priority;
2169 bool is_static;
2170 const std::string* required_property_name = nullptr;
2171 const std::string* required_property_value = nullptr;
2172
2173 void Extract(xml::Element* element) override {
2174 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
2175 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
2176 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
2177 required_property_name = GetAttributeString(
2178 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
2179 required_property_value = GetAttributeString(
2180 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
2181 }
2182
2183 void Print(text::Printer* printer) override {
2184 printer->Print(StringPrintf("overlay:"));
2185 if (target_package) {
2186 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
2187 }
2188 printer->Print(StringPrintf(" priority='%d'", priority));
2189 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
2190 if (required_property_name) {
2191 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
2192 }
2193 if (required_property_value) {
2194 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
2195 }
2196 printer->Print("\n");
2197 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002198
2199 void ToProto(pb::Badging* out_badging) override {
2200 auto overlay = out_badging->mutable_overlay();
2201 if (target_package) {
2202 overlay->set_target_package(*target_package);
2203 }
2204 overlay->set_priority(priority);
2205 overlay->set_static_(is_static);
2206 if (required_property_name) {
2207 overlay->set_required_property_name(*required_property_name);
2208 }
2209 if (required_property_value) {
2210 overlay->set_required_property_value(*required_property_value);
2211 }
2212 }
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002213};
2214
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002215/** * Represents <package-verifier> elements. **/
2216class PackageVerifier : public ManifestExtractor::Element {
2217 public:
2218 PackageVerifier() = default;
2219 const std::string* name = nullptr;
2220 const std::string* public_key = nullptr;
2221
2222 void Extract(xml::Element* element) override {
2223 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2224 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
2225 }
2226
Ryan Mitchell214846d2018-09-19 16:57:01 -07002227 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002228 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002229 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002230 name->data(), public_key->data()));
2231 }
2232 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002233
2234 void ToProto(pb::Badging* out_badging) override {
2235 auto package_verifier = out_badging->mutable_package_verifier();
2236 if (name && public_key) {
2237 package_verifier->set_name(*name);
2238 package_verifier->set_public_key(*public_key);
2239 }
2240 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002241};
2242
2243/** Represents <uses-package> elements. **/
2244class UsesPackage : public ManifestExtractor::Element {
2245 public:
2246 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002247 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002248 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002249 int version;
2250 int versionMajor;
2251 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002252
2253 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002254 auto parent_stack = extractor()->parent_stack();
2255 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
2256 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
2257 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2258 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
2259 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
2260 AddCertDigest(element);
2261 }
2262 }
2263
2264 void AddCertDigest(xml::Element* element) {
2265 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
2266 // We allow ":" delimiters in the SHA declaration as this is the format
2267 // emitted by the certtool making it easy for developers to copy/paste.
2268 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
2269 if (!digest.empty()) {
2270 certDigests.push_back(digest);
2271 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002272 }
2273
Ryan Mitchell214846d2018-09-19 16:57:01 -07002274 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002275 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002276 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002277 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07002278 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
2279 packageType->data(), name->data(), version, versionMajor));
2280 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002281 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002282 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002283 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07002284 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002285 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002286 }
2287 }
2288 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002289
2290 void ToProto(pb::Badging* out_badging) override {
2291 if (name) {
2292 auto uses_package = out_badging->add_uses_packages();
2293 uses_package->set_name(*name);
2294 if (packageType) {
2295 uses_package->set_package_type(*packageType);
2296 uses_package->set_version(version);
2297 uses_package->set_version_major(versionMajor);
2298 for (auto& cert : certDigests) {
2299 uses_package->add_certificates(cert);
2300 }
2301 }
2302 }
2303 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07002304};
2305
2306/** Represents <additional-certificate> elements. **/
2307class AdditionalCertificate : public ManifestExtractor::Element {
2308 public:
2309 AdditionalCertificate() = default;
2310
2311 void Extract(xml::Element* element) override {
2312 auto parent_stack = extractor()->parent_stack();
2313 if (parent_stack.size() > 0) {
2314 if (ElementCast<UsesPackage>(parent_stack[0])) {
2315 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
2316 uses->AddCertDigest(element);
2317 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
2318 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
2319 uses->AddCertDigest(element);
2320 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002321 }
2322 }
2323};
2324
2325/** Represents <screen> elements found in <compatible-screens> elements. */
2326class Screen : public ManifestExtractor::Element {
2327 public:
2328 Screen() = default;
2329 const int32_t* size = nullptr;
2330 const int32_t* density = nullptr;
2331
2332 void Extract(xml::Element* element) override {
2333 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
2334 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
2335 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002336
2337 void ToProto(pb::Badging* out_badging) override {
2338 if (size && density) {
2339 auto screen = out_badging->mutable_compatible_screens()->add_screens();
2340 screen->set_density(*density);
2341 screen->set_size(*size);
2342 }
2343 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002344};
2345
2346/**
2347 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
2348 * that each denote a supported screen size and screen density.
2349 **/
2350class CompatibleScreens : public ManifestExtractor::Element {
2351 public:
2352 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07002353 void Print(text::Printer* printer) override {
2354 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002355
2356 bool first = true;
2357 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
2358 if (auto screen = ElementCast<Screen>(el)) {
2359 if (first) {
2360 first = false;
2361 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002362 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002363 }
2364
2365 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002366 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002367 }
2368 }
2369 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07002370 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002371 }
2372};
2373
2374/** Represents <supports-gl-texture> elements. **/
2375class SupportsGlTexture : public ManifestExtractor::Element {
2376 public:
2377 SupportsGlTexture() = default;
2378 const std::string* name = nullptr;
2379
2380 void Extract(xml::Element* element) override {
2381 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2382 }
2383
Ryan Mitchell214846d2018-09-19 16:57:01 -07002384 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002385 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002386 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002387 }
2388 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002389
2390 void ToProto(pb::Badging* out_badging) override {
2391 if (name) {
2392 out_badging->mutable_supports_gl_texture()->add_name(*name);
2393 }
2394 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002395};
2396
Todd Kennedyce3e1292020-10-29 17:14:24 -07002397/** Represents <property> elements. **/
2398class Property : public ManifestExtractor::Element {
2399 public:
2400 Property() = default;
2401 std::string name;
2402 std::string value;
2403 const int* value_int;
2404 std::string resource;
2405 const int* resource_int;
2406
2407 void Extract(xml::Element* element) override {
2408 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
2409 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
2410 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
2411 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
2412 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
2413 }
2414
2415 void Print(text::Printer* printer) override {
2416 printer->Print(StringPrintf("property: name='%s' ", name.data()));
2417 if (!value.empty()) {
2418 printer->Print(StringPrintf("value='%s' ", value.data()));
2419 } else if (value_int) {
2420 printer->Print(StringPrintf("value='%d' ", *value_int));
2421 } else {
2422 if (!resource.empty()) {
2423 printer->Print(StringPrintf("resource='%s' ", resource.data()));
2424 } else if (resource_int) {
2425 printer->Print(StringPrintf("resource='%d' ", *resource_int));
2426 }
2427 }
2428 printer->Print("\n");
2429 }
Todd Kennedyce3e1292020-10-29 17:14:24 -07002430
Iurii Makhno85875a82022-04-26 15:30:01 +00002431 void ToProto(pb::Badging* out_badging) override {
2432 if (!name.empty()) {
2433 auto property = out_badging->add_properties();
2434 property->set_name(name);
2435 if (!value.empty()) {
2436 property->set_value_string(value);
2437 } else if (value_int) {
2438 property->set_value_int(*value_int);
2439 } else {
2440 if (!resource.empty()) {
2441 property->set_resource_string(resource);
2442 } else if (resource_int) {
2443 property->set_resource_int(*resource_int);
2444 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002445 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002446 }
2447 }
2448};
2449
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002450/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07002451static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002452 el->Print(printer);
2453 for (auto &child : el->children()) {
2454 Print(child.get(), printer);
2455 }
2456}
2457
Iurii Makhno85875a82022-04-26 15:30:01 +00002458/** Recursively serializes extracted badging elements to proto. */
2459static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) {
2460 el->ToProto(out_badging);
2461 for (auto& child : el->children()) {
2462 ToProto(child.get(), out_badging);
2463 }
2464}
2465
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002466bool ManifestExtractor::Extract(android::IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002467 // Load the manifest
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002468 doc_ = apk_->LoadXml("AndroidManifest.xml", diag);
2469 if (doc_ == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002470 diag->Error(android::DiagMessage() << "failed to find AndroidManifest.xml");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002471 return false;
2472 }
2473
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002474 xml::Element* element = doc_->root.get();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002475 if (element->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002476 diag->Error(android::DiagMessage() << "manifest does not start with <manifest> tag");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002477 return false;
2478 }
2479
2480 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
2481 // printing only permission elements is requested
2482 if (options_.only_permissions) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002483 root_element_ = ManifestExtractor::Element::Inflate(this, element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002484
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002485 if (auto manifest = ElementCast<Manifest>(root_element_.get())) {
2486 manifest->only_package_name = true;
2487
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002488 for (xml::Element* child : element->GetChildElements()) {
2489 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
2490 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002491 // Inflate the element and its descendants
2492 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002493 manifest->AddChild(permission_element);
2494 }
2495 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002496 return true;
2497 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002498 return false;
2499 }
2500
2501 // Collect information about the resource configurations
2502 if (apk_->GetResourceTable()) {
2503 for (auto &package : apk_->GetResourceTable()->packages) {
2504 for (auto &type : package->types) {
2505 for (auto &entry : type->entries) {
2506 for (auto &value : entry->values) {
2507 std::string locale_str = value->config.GetBcp47LanguageTag();
2508
2509 // Collect all the unique locales of the apk
2510 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002511 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002512 config.setBcp47Locale(locale_str.data());
2513 locales_.insert(std::make_pair(locale_str, config));
2514 }
2515
2516 // Collect all the unique density of the apk
2517 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
2518 : value->config.density;
2519 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002520 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002521 config.density = density;
2522 densities_.insert(std::make_pair(density, config));
2523 }
2524 }
2525 }
2526 }
2527 }
2528 }
2529
2530 // Extract badging information
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002531 root_element_ = Visit(element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002532
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002533 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
2534 // attribute values from the last defined tag.
2535 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002536 for (const auto& child : root_element_->children()) {
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002537 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
2538 filtered_uses_sdk_tags.emplace_back(uses_sdk);
2539 }
2540 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002541 if (filtered_uses_sdk_tags.size() >= 2U) {
2542 filtered_uses_sdk_tags.pop_back();
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002543 root_element_->Filter([&](const ManifestExtractor::Element* e) {
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002544 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
2545 filtered_uses_sdk_tags.end();
2546 });
2547 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002548
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002549 /** Recursively checks the extracted elements for the specified permission. **/
2550 auto FindPermission = [&](ManifestExtractor::Element* root,
2551 const std::string& name) -> ManifestExtractor::Element* {
2552 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
2553 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
2554 return permission->name == name;
2555 }
2556 return false;
2557 });
2558 };
2559
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002560 auto AddImpliedPermission = [&](const std::string& name, const std::string& reason,
2561 int32_t max_sdk_version) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002562 auto permission = util::make_unique<UsesPermission>();
2563 permission->name = name;
2564 permission->maxSdkVersion = max_sdk_version;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002565 permission->implied = true;
2566 permission->impliedReason = reason;
2567 implied_permissions_.push_back(std::move(permission));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002568 };
2569
2570 // Implied permissions
2571 // Pre-1.6 implicitly granted permission compatibility logic
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002572 bool insert_write_external = false;
2573 auto write_external_permission = ElementCast<UsesPermission>(
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002574 FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002575
Jackal Guo201a60a2021-08-31 12:37:30 +08002576 if (target_sdk() < SDK_DONUT) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002577 if (!write_external_permission) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002578 AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002579 insert_write_external = true;
2580 }
2581
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002582 if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) {
2583 AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002584 }
2585 }
2586
2587 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2588 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2589 // do this (regardless of target API version) because we can't have
2590 // an app with write permission but not read permission.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002591 auto read_external =
2592 FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002593 if (!read_external && (insert_write_external || write_external_permission)) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002594 AddImpliedPermission(
2595 "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE",
2596 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002597 }
2598
2599 // Pre-JellyBean call log permission compatibility.
Jackal Guo201a60a2021-08-31 12:37:30 +08002600 if (target_sdk() < SDK_JELLY_BEAN) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002601 if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") &&
2602 FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) {
2603 AddImpliedPermission("android.permission.READ_CALL_LOG",
2604 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002605 }
2606
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002607 if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") &&
2608 FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) {
2609 AddImpliedPermission("android.permission.WRITE_CALL_LOG",
2610 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002611 }
2612 }
2613
2614 // If the app hasn't declared the touchscreen as a feature requirement (either
2615 // directly or implied, required or not), then the faketouch feature is implied.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002616 if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) {
2617 common_feature_group()->addImpliedFeature("android.hardware.faketouch",
2618 "default feature for all apps", false);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002619 }
2620
2621 // Only print the common feature group if no feature group is defined
2622 std::vector<FeatureGroup*> feature_groups;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002623 ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002624 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2625 feature_groups.push_back(feature_group);
2626 }
2627 });
2628
2629 if (feature_groups.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002630 feature_groups_.push_back(common_feature_group());
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002631 } else {
2632 // Merge the common feature group into the feature group
2633 for (auto& feature_group : feature_groups) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002634 feature_group->Merge(common_feature_group());
2635 feature_groups_.push_back(feature_group);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002636 }
2637 };
2638
2639 // Collect the component types of the application
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002640 ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002641 if (ElementCast<Action>(el)) {
2642 auto action = ElementCast<Action>(el);
2643 if (!action->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002644 components_.discovered_components.insert(action->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002645 return;
2646 }
2647 }
2648
2649 if (ElementCast<Category>(el)) {
2650 auto category = ElementCast<Category>(el);
2651 if (!category->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002652 components_.discovered_components.insert(category->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002653 return;
2654 }
2655 }
2656 });
2657
2658 // Check for the payment component
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002659 ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002660 if (auto service = ElementCast<Service>(el)) {
2661 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2662 [&](ManifestExtractor::Element* el) -> bool {
2663 if (auto action = ElementCast<Action>(el)) {
2664 return (action->component == "host-apdu");
2665 }
2666 return false;
2667 }));
2668
2669 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2670 [&](ManifestExtractor::Element* el) -> bool {
2671 if (auto action = ElementCast<Action>(el)) {
2672 return (action->component == "offhost-apdu");
2673 }
2674 return false;
2675 }));
2676
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002677 ForEachChild(service,
2678 [this, &diag, &host_apdu_action,
2679 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2680 if (auto meta_data = ElementCast<MetaData>(el)) {
2681 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" &&
2682 host_apdu_action) ||
2683 (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" &&
2684 offhost_apdu_action)) {
2685 // Attempt to load the resource file
2686 if (!meta_data->resource.empty()) {
2687 return;
2688 }
2689 auto resource = this->apk_->LoadXml(meta_data->resource, diag);
2690 if (!resource) {
2691 return;
2692 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002693
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002694 // Look for the payment category on an <aid-group> element
2695 auto& root = resource.get()->root;
2696 if ((host_apdu_action && root->name == "host-apdu-service") ||
2697 (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2698 for (auto& child : root->GetChildElements()) {
2699 if (child->name == "aid-group") {
2700 auto category = FindAttribute(child, CATEGORY_ATTR);
2701 if (category && category->value == "payment") {
Iurii Makhno85875a82022-04-26 15:30:01 +00002702 this->components_.discovered_components.insert("payment");
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002703 return;
2704 }
2705 }
2706 }
2707 }
2708 }
2709 }
2710 });
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002711 }
2712 });
2713
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002714 // Print presence of activities, receivers, and services with no special components
2715 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2716 if (auto activity = ElementCast<Activity>(el)) {
2717 if (!activity->has_component_) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002718 components_.other_activities = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002719 return true;
2720 }
2721 }
2722 return false;
2723 });
2724
2725 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2726 if (auto receiver = ElementCast<Receiver>(el)) {
2727 if (!receiver->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002728 components_.other_receivers = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002729 return true;
2730 }
2731 }
2732 return false;
2733 });
2734
2735 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2736 if (auto service = ElementCast<Service>(el)) {
2737 if (!service->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002738 components_.other_services = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002739 return true;
2740 }
2741 }
2742 return false;
2743 });
2744
2745 // Gather the supported screens
2746 const static SupportsScreen default_screens{};
2747 SupportsScreen* screen = ElementCast<SupportsScreen>(
2748 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2749 return ElementCast<SupportsScreen>(el) != nullptr;
2750 }));
2751 supports_screen_ = screen ? screen : &default_screens;
2752
2753 // Gather the supported architectures_ of the app
2754 std::set<std::string> architectures_from_apk;
2755 auto it = apk_->GetFileCollection()->Iterator();
2756 while (it->HasNext()) {
2757 auto file_path = it->Next()->GetSource().path;
Iurii Makhno499ecd32022-08-19 16:34:26 +00002758 if (file_path.starts_with("lib/")) {
2759 file_path = file_path.substr(4);
2760 size_t pos = file_path.find('/');
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002761 if (pos != std::string::npos) {
2762 file_path = file_path.substr(0, pos);
2763 }
2764
2765 architectures_from_apk.insert(file_path);
2766 }
2767 }
2768
2769 // Determine if the application has multiArch supports
2770 auto has_multi_arch =
2771 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2772 if (auto application = ElementCast<Application>(el)) {
2773 return application->has_multi_arch;
2774 }
2775 return false;
2776 });
2777
2778 bool output_alt_native_code = false;
2779 // A multiArch package is one that contains 64-bit and
2780 // 32-bit versions of native code and expects 3rd-party
2781 // apps to load these native code libraries. Since most
2782 // 64-bit systems also support 32-bit apps, the apps
2783 // loading this multiArch package's code may be either
2784 if (has_multi_arch) {
2785 // If this is a multiArch package, report the 64-bit
2786 // version only. Then as a separate entry, report the
2787 // rest.
2788 //
2789 // If we report the 32-bit architecture, this APK will
2790 // be installed on a 32-bit device, causing a large waste
2791 // of bandwidth and disk space. This assumes that
2792 // the developer of the multiArch package has also
2793 // made a version that is 32-bit only.
2794 const std::string kIntel64 = "x86_64";
2795 const std::string kArm64 = "arm64-v8a";
2796
2797 auto arch = architectures_from_apk.find(kIntel64);
2798 if (arch == architectures_from_apk.end()) {
2799 arch = architectures_from_apk.find(kArm64);
2800 }
2801
2802 if (arch != architectures_from_apk.end()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002803 architectures_.architectures.insert(*arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002804 architectures_from_apk.erase(arch);
2805 output_alt_native_code = true;
2806 }
2807 }
2808 for (auto& arch : architectures_from_apk) {
2809 if (output_alt_native_code) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002810 architectures_.alt_architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002811 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002812 architectures_.architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002813 }
2814 }
2815 return true;
2816}
2817
2818bool ManifestExtractor::Dump(text::Printer* printer) {
2819 Print(root_element_.get(), printer);
2820 if (options_.only_permissions) {
2821 return true;
2822 }
2823
2824 for (auto& implied_permission : implied_permissions_) {
2825 implied_permission->Print(printer);
2826 }
2827 for (auto& feature_group : feature_groups_) {
2828 feature_group->PrintGroup(printer);
2829 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002830 components_.Print(printer);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002831 supports_screen_->PrintScreens(printer, target_sdk_);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002832
2833 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002834 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002835 for (auto& config : locales_) {
2836 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002837 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002838 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002839 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002840 }
2841 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002842 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002843
2844 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002845 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002846 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002847 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002848 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002849 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002850
Iurii Makhno85875a82022-04-26 15:30:01 +00002851 architectures_.Print(printer);
2852 return true;
2853}
2854
2855bool ManifestExtractor::DumpProto(pb::Badging* out_badging) {
2856 ToProto(root_element_.get(), out_badging);
2857 for (auto& implied_permission : implied_permissions_) {
2858 implied_permission->ToProto(out_badging);
2859 }
2860 for (auto& feature_group : feature_groups_) {
2861 feature_group->GroupToProto(out_badging);
2862 }
2863 components_.ToProto(out_badging);
2864 supports_screen_->ToProtoScreens(out_badging, target_sdk_);
2865
2866 for (auto& config : locales_) {
Iurii Makhnocfc559b2022-08-25 15:57:52 +00002867 if (config.first.empty()) {
2868 out_badging->add_locales("--_--");
2869 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002870 out_badging->add_locales(config.first);
2871 }
2872 }
2873 for (auto& config : densities_) {
2874 out_badging->add_densities(config.first);
2875 }
2876
2877 architectures_.ToProto(out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002878 return true;
2879}
2880
2881/**
2882 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2883 * pointer.
2884 **/
2885template<typename T>
2886T* ElementCast(ManifestExtractor::Element* element) {
2887 if (element == nullptr) {
2888 return nullptr;
2889 }
2890
2891 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002892 {"action", std::is_base_of<Action, T>::value},
2893 {"activity", std::is_base_of<Activity, T>::value},
2894 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2895 {"application", std::is_base_of<Application, T>::value},
2896 {"category", std::is_base_of<Category, T>::value},
2897 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2898 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2899 {"input-type", std::is_base_of<InputType, T>::value},
2900 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2901 {"meta-data", std::is_base_of<MetaData, T>::value},
2902 {"manifest", std::is_base_of<Manifest, T>::value},
2903 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2904 {"overlay", std::is_base_of<Overlay, T>::value},
2905 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2906 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002907 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002908 {"provider", std::is_base_of<Provider, T>::value},
2909 {"receiver", std::is_base_of<Receiver, T>::value},
2910 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2911 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2912 {"screen", std::is_base_of<Screen, T>::value},
2913 {"service", std::is_base_of<Service, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002914 {"sdk-library", std::is_base_of<SdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002915 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2916 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2917 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2918 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2919 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2920 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2921 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2922 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2923 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2924 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2925 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2926 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002927 {"uses-sdk-library", std::is_base_of<UsesSdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002928 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002929 };
2930
2931 auto check = kTagCheck.find(element->tag());
2932 if (check != kTagCheck.end() && check->second) {
2933 return static_cast<T*>(element);
2934 }
2935 return nullptr;
2936}
2937
2938template<typename T>
2939std::unique_ptr<T> CreateType() {
2940 return std::move(util::make_unique<T>());
2941}
2942
2943std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2944 ManifestExtractor* extractor, xml::Element* el) {
2945 const std::unordered_map<std::string,
2946 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2947 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002948 {"action", &CreateType<Action>},
2949 {"activity", &CreateType<Activity>},
2950 {"additional-certificate", &CreateType<AdditionalCertificate>},
2951 {"application", &CreateType<Application>},
2952 {"category", &CreateType<Category>},
2953 {"compatible-screens", &CreateType<CompatibleScreens>},
2954 {"feature-group", &CreateType<FeatureGroup>},
2955 {"input-type", &CreateType<InputType>},
2956 {"intent-filter", &CreateType<IntentFilter>},
2957 {"manifest", &CreateType<Manifest>},
2958 {"meta-data", &CreateType<MetaData>},
2959 {"original-package", &CreateType<OriginalPackage>},
2960 {"overlay", &CreateType<Overlay>},
2961 {"package-verifier", &CreateType<PackageVerifier>},
2962 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002963 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002964 {"provider", &CreateType<Provider>},
2965 {"receiver", &CreateType<Receiver>},
2966 {"required-feature", &CreateType<RequiredFeature>},
2967 {"required-not-feature", &CreateType<RequiredNotFeature>},
2968 {"screen", &CreateType<Screen>},
2969 {"service", &CreateType<Service>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002970 {"sdk-library", &CreateType<SdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002971 {"static-library", &CreateType<StaticLibrary>},
2972 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2973 {"supports-input", &CreateType<SupportsInput>},
2974 {"supports-screens", &CreateType<SupportsScreen>},
2975 {"uses-configuration", &CreateType<UsesConfiguarion>},
2976 {"uses-feature", &CreateType<UsesFeature>},
2977 {"uses-library", &CreateType<UsesLibrary>},
2978 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2979 {"uses-package", &CreateType<UsesPackage>},
2980 {"uses-permission", &CreateType<UsesPermission>},
2981 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2982 {"uses-sdk", &CreateType<UsesSdkBadging>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002983 {"uses-sdk-library", &CreateType<UsesSdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002984 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2985 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002986
2987 // Attempt to map the xml tag to a element inflater
2988 std::unique_ptr<ManifestExtractor::Element> element;
2989 auto check = kTagCheck.find(el->name);
2990 if (check != kTagCheck.end()) {
2991 element = check->second();
2992 } else {
2993 element = util::make_unique<ManifestExtractor::Element>();
2994 }
2995
2996 element->extractor_ = extractor;
2997 element->tag_ = el->name;
2998 element->Extract(el);
2999 return element;
3000}
3001
3002std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
3003 auto element = ManifestExtractor::Element::Inflate(this, el);
3004 parent_stack_.insert(parent_stack_.begin(), element.get());
3005
3006 // Process the element and recursively visit the children
3007 for (xml::Element* child : el->GetChildElements()) {
3008 auto v = Visit(child);
3009 element->AddChild(v);
3010 }
3011
3012 parent_stack_.erase(parent_stack_.begin());
3013 return element;
3014}
3015
Ryan Mitchell214846d2018-09-19 16:57:01 -07003016int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003017 android::IDiagnostics* diag) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07003018 ManifestExtractor extractor(apk, options);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003019 if (!extractor.Extract(diag)) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003020 return 1;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003021 }
3022 return extractor.Dump(printer) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003023}
3024
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003025int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, android::IDiagnostics* diag) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003026 DumpManifestOptions options{/* include_meta_data= */ true,
3027 /* only_permissions= */ false};
3028 ManifestExtractor extractor(apk, options);
3029 if (!extractor.Extract(diag)) {
3030 return 1;
3031 }
3032 return extractor.DumpProto(out_badging) ? 0 : 1;
3033}
3034
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02003035} // namespace aapt