blob: f0b0e867b35b80306fa1c6429a3cd1f2bf1c46d5 [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) {
1079 features_.insert(std::make_pair(name, Feature{ required, version }));
1080 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 {
1351 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001352 std::string feature =
1353 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1354 if (!feature.empty()) {
1355 requiredFeatures.push_back(feature);
1356 }
1357 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1358 if (!feature.empty()) {
1359 requiredNotFeatures.push_back(feature);
1360 }
1361
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001362 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1363 maxSdkVersion = GetAttributeIntegerDefault(
1364 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001365 usesPermissionFlags = GetAttributeIntegerDefault(
1366 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001367
1368 if (!name.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001369 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001370 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1371 }
1372 }
1373
Ryan Mitchell214846d2018-09-19 16:57:01 -07001374 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001375 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001376 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001377 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001378 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001379 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001380 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1381 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1382 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001383 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001384 for (const std::string& requiredFeature : requiredFeatures) {
1385 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1386 }
1387 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1388 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1389 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001390 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001391 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001392 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001393 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001394 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001395 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1396 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1397 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001398 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001399 }
1400 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001401 if (implied) {
1402 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
1403 if (maxSdkVersion >= 0) {
1404 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
1405 }
1406 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1407 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1408 }
1409 printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001410 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001411 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001412
1413 void ToProto(pb::Badging* out_badging) override {
1414 if (!name.empty()) {
1415 auto permission = out_badging->add_uses_permissions();
1416 permission->set_name(name);
1417 if (maxSdkVersion > 0) {
1418 permission->set_max_sdk_version(maxSdkVersion);
1419 }
1420 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1421 permission->mutable_permission_flags()->set_never_for_location(true);
1422 }
1423 for (auto& requiredFeature : requiredFeatures) {
1424 permission->add_required_features(requiredFeature);
1425 }
1426 for (auto& requiredNotFeature : requiredNotFeatures) {
1427 permission->add_required_not_features(requiredNotFeature);
1428 }
1429 permission->set_required(required != 0);
1430 permission->set_implied(implied);
1431 }
1432 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001433};
1434
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001435/** Represents <required-feature> elements. **/
1436class RequiredFeature : public ManifestExtractor::Element {
1437 public:
1438 RequiredFeature() = default;
1439 std::string name;
1440
1441 void Extract(xml::Element* element) override {
1442 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1443 auto parent_stack = extractor()->parent_stack();
1444 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1445 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1446 uses_permission->requiredFeatures.push_back(name);
1447 }
1448 }
1449};
1450
1451/** Represents <required-not-feature> elements. **/
1452class RequiredNotFeature : public ManifestExtractor::Element {
1453 public:
1454 RequiredNotFeature() = default;
1455 std::string name;
1456
1457 void Extract(xml::Element* element) override {
1458 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1459 auto parent_stack = extractor()->parent_stack();
1460 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1461 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1462 uses_permission->requiredNotFeatures.push_back(name);
1463 }
1464 }
1465};
1466
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001467/** Represents <uses-permission-sdk-23> elements. **/
1468class UsesPermissionSdk23 : public ManifestExtractor::Element {
1469 public:
1470 UsesPermissionSdk23() = default;
1471 const std::string* name = nullptr;
1472 const int32_t* maxSdkVersion = nullptr;
1473
1474 void Extract(xml::Element* element) override {
1475 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1476 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1477
1478 if (name) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001479 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001480 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1481 }
1482 }
1483
Ryan Mitchell214846d2018-09-19 16:57:01 -07001484 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001485 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001486 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001487 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001488 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001489 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001490 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001491 }
1492 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001493
1494 void ToProto(pb::Badging* out_badging) override {
1495 if (name) {
1496 auto permission = out_badging->add_uses_permissions();
1497 permission->set_sdk23_and_above(true);
1498 permission->set_name(*name);
1499 if (maxSdkVersion) {
1500 permission->set_max_sdk_version(*maxSdkVersion);
1501 }
1502 }
1503 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001504};
1505
1506/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1507class Permission : public ManifestExtractor::Element {
1508 public:
1509 Permission() = default;
1510 std::string name;
1511
1512 void Extract(xml::Element* element) override {
1513 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1514 }
1515
Ryan Mitchell214846d2018-09-19 16:57:01 -07001516 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001517 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001518 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001519 }
1520 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001521
1522 void ToProto(pb::Badging* out_badging) override {
1523 if (!name.empty()) {
1524 out_badging->add_permissions()->set_name(name);
1525 }
1526 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001527};
1528
1529/** Represents <activity> elements. **/
1530class Activity : public ManifestExtractor::Element {
1531 public:
1532 Activity() = default;
1533 std::string name;
1534 std::string icon;
1535 std::string label;
1536 std::string banner;
1537
1538 bool has_component_ = false;
1539 bool has_launcher_category = false;
1540 bool has_leanback_launcher_category = false;
1541 bool has_main_action = false;
1542
1543 void Extract(xml::Element* element) override {
1544 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1545 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1546 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1547 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1548
1549 // Retrieve the package name from the manifest
1550 std::string package;
1551 for (auto& parent : extractor()->parent_stack()) {
1552 if (auto manifest = ElementCast<Manifest>(parent)) {
1553 package = manifest->package;
1554 break;
1555 }
1556 }
1557
1558 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001559 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001560 if (idx == 0) {
1561 name = package + name;
1562 } else if (idx < 0) {
1563 name = package + "." + name;
1564 }
1565
1566 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1567 if (orientation) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001568 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001569 int orien = *orientation;
1570 if (orien == 0 || orien == 6 || orien == 8) {
1571 // Requests landscape, sensorLandscape, or reverseLandscape.
1572 common->addImpliedFeature("android.hardware.screen.landscape",
1573 "one or more activities have specified a landscape orientation",
1574 false);
1575 } else if (orien == 1 || orien == 7 || orien == 9) {
1576 // Requests portrait, sensorPortrait, or reversePortrait.
1577 common->addImpliedFeature("android.hardware.screen.portrait",
1578 "one or more activities have specified a portrait orientation",
1579 false);
1580 }
1581 }
1582 }
1583
Ryan Mitchell214846d2018-09-19 16:57:01 -07001584 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001585 // Print whether the activity has the HOME category and a the MAIN action
1586 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001587 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001588 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001589 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001590 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001591 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001592 android::ResTable::normalizeForOutput(label.data()).c_str(),
1593 icon.data()));
1594 }
1595
1596 // Print wether the activity has the HOME category and a the MAIN action
1597 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001598 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001599 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001600 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001601 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001602 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001603 android::ResTable::normalizeForOutput(label.data()).c_str(),
1604 icon.data(), banner.data()));
1605 }
1606 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001607
1608 void ToProto(pb::Badging* out_badging) override {
1609 if (has_main_action && has_launcher_category) {
1610 auto activity = out_badging->mutable_launchable_activity();
1611 activity->set_name(name);
1612 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1613 activity->set_icon(icon);
1614 }
1615 if (has_leanback_launcher_category) {
1616 auto activity = out_badging->mutable_leanback_launchable_activity();
1617 activity->set_name(name);
1618 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1619 activity->set_icon(icon);
1620 activity->set_banner(banner);
1621 }
1622 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001623};
1624
1625/** Represents <intent-filter> elements. */
1626class IntentFilter : public ManifestExtractor::Element {
1627 public:
1628 IntentFilter() = default;
1629};
1630
1631/** Represents <category> elements. */
1632class Category : public ManifestExtractor::Element {
1633 public:
1634 Category() = default;
1635 std::string component = "";
1636
1637 void Extract(xml::Element* element) override {
1638 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1639
1640 auto parent_stack = extractor()->parent_stack();
1641 if (category && ElementCast<IntentFilter>(parent_stack[0])
1642 && ElementCast<Activity>(parent_stack[1])) {
1643 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1644
1645 if (*category == "android.intent.category.LAUNCHER") {
1646 activity->has_launcher_category = true;
1647 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1648 activity->has_leanback_launcher_category = true;
1649 } else if (*category == "android.intent.category.HOME") {
1650 component = "launcher";
1651 }
1652 }
1653 }
1654};
1655
1656/**
1657 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1658 * elements nested within.
1659 **/
1660class Provider : public ManifestExtractor::Element {
1661 public:
1662 Provider() = default;
1663 bool has_required_saf_attributes = false;
1664
1665 void Extract(xml::Element* element) override {
1666 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1667 const int32_t* grant_uri_permissions = GetAttributeInteger(
1668 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1669 const std::string* permission = GetAttributeString(
1670 FindAttribute(element, PERMISSION_ATTR));
1671
1672 has_required_saf_attributes = ((exported && *exported != 0)
1673 && (grant_uri_permissions && *grant_uri_permissions != 0)
1674 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1675 }
1676};
1677
1678/** Represents <receiver> elements. **/
1679class Receiver : public ManifestExtractor::Element {
1680 public:
1681 Receiver() = default;
1682 const std::string* permission = nullptr;
1683 bool has_component = false;
1684
1685 void Extract(xml::Element* element) override {
1686 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1687 }
1688};
1689
1690/**Represents <service> elements. **/
1691class Service : public ManifestExtractor::Element {
1692 public:
1693 Service() = default;
1694 const std::string* permission = nullptr;
1695 bool has_component = false;
1696
1697 void Extract(xml::Element* element) override {
1698 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1699 }
1700};
1701
1702/** Represents <uses-library> elements. **/
1703class UsesLibrary : public ManifestExtractor::Element {
1704 public:
1705 UsesLibrary() = default;
1706 std::string name;
1707 int required;
1708
1709 void Extract(xml::Element* element) override {
1710 auto parent_stack = extractor()->parent_stack();
1711 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1712 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1713 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1714 }
1715 }
1716
Ryan Mitchell214846d2018-09-19 16:57:01 -07001717 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001718 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001719 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001720 (required == 0) ? "-not-required" : "", name.data()));
1721 }
1722 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001723
1724 void ToProto(pb::Badging* out_badging) override {
1725 if (!name.empty()) {
1726 auto uses_library = out_badging->add_uses_libraries();
1727 uses_library->set_name(name);
1728 uses_library->set_required(required != 0);
1729 }
1730 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001731};
1732
Dianne Hackborn813d7502018-10-02 16:59:46 -07001733/** Represents <static-library> elements. **/
1734class StaticLibrary : public ManifestExtractor::Element {
1735 public:
1736 StaticLibrary() = default;
1737 std::string name;
1738 int version;
1739 int versionMajor;
1740
1741 void Extract(xml::Element* element) override {
1742 auto parent_stack = extractor()->parent_stack();
1743 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1744 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1745 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1746 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1747 }
1748 }
1749
Ryan Mitchell214846d2018-09-19 16:57:01 -07001750 void Print(text::Printer* printer) override {
1751 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001752 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1753 name.data(), version, versionMajor));
1754 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001755
1756 void ToProto(pb::Badging* out_badging) override {
1757 auto static_library = out_badging->mutable_static_library();
1758 static_library->set_name(name);
1759 static_library->set_version(version);
1760 static_library->set_version_major(versionMajor);
1761 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001762};
1763
1764/** Represents <uses-static-library> elements. **/
1765class UsesStaticLibrary : public ManifestExtractor::Element {
1766 public:
1767 UsesStaticLibrary() = default;
1768 std::string name;
1769 int version;
1770 int versionMajor;
1771 std::vector<std::string> certDigests;
1772
1773 void Extract(xml::Element* element) override {
1774 auto parent_stack = extractor()->parent_stack();
1775 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1776 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1777 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1778 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1779 AddCertDigest(element);
1780 }
1781 }
1782
1783 void AddCertDigest(xml::Element* element) {
1784 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1785 // We allow ":" delimiters in the SHA declaration as this is the format
1786 // emitted by the certtool making it easy for developers to copy/paste.
1787 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1788 if (!digest.empty()) {
1789 certDigests.push_back(digest);
1790 }
1791 }
1792
Ryan Mitchell214846d2018-09-19 16:57:01 -07001793 void Print(text::Printer* printer) override {
1794 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001795 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1796 name.data(), version, versionMajor));
1797 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001798 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001799 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001800 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001801 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001802
1803 void ToProto(pb::Badging* out_badging) override {
1804 auto uses_static_library = out_badging->add_uses_static_libraries();
1805 uses_static_library->set_name(name);
1806 uses_static_library->set_version(version);
1807 uses_static_library->set_version_major(versionMajor);
1808 for (auto& cert : certDigests) {
1809 uses_static_library->add_certificates(cert);
1810 }
1811 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001812};
1813
Alex Buynytskyya16137052021-12-02 13:26:54 +00001814/** Represents <sdk-library> elements. **/
1815class SdkLibrary : public ManifestExtractor::Element {
1816 public:
1817 SdkLibrary() = default;
1818 std::string name;
1819 int versionMajor;
1820
1821 void Extract(xml::Element* element) override {
1822 auto parent_stack = extractor()->parent_stack();
1823 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1824 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1825 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1826 }
1827 }
1828
1829 void Print(text::Printer* printer) override {
1830 printer->Print(
1831 StringPrintf("sdk-library: name='%s' versionMajor='%d'\n", name.data(), versionMajor));
1832 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001833
1834 void ToProto(pb::Badging* out_badging) override {
1835 auto sdk_library = out_badging->mutable_sdk_library();
1836 sdk_library->set_name(name);
1837 sdk_library->set_version_major(versionMajor);
1838 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001839};
1840
1841/** Represents <uses-sdk-library> elements. **/
1842class UsesSdkLibrary : public ManifestExtractor::Element {
1843 public:
1844 UsesSdkLibrary() = default;
1845 std::string name;
1846 int versionMajor;
1847 std::vector<std::string> certDigests;
1848
1849 void Extract(xml::Element* element) override {
1850 auto parent_stack = extractor()->parent_stack();
1851 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1852 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1853 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1854 AddCertDigest(element);
1855 }
1856 }
1857
1858 void AddCertDigest(xml::Element* element) {
1859 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1860 // We allow ":" delimiters in the SHA declaration as this is the format
1861 // emitted by the certtool making it easy for developers to copy/paste.
1862 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1863 if (!digest.empty()) {
1864 certDigests.push_back(digest);
1865 }
1866 }
1867
1868 void Print(text::Printer* printer) override {
1869 printer->Print(
1870 StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor));
1871 for (size_t i = 0; i < certDigests.size(); i++) {
1872 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
1873 }
1874 printer->Print("\n");
1875 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001876
1877 void ToProto(pb::Badging* out_badging) override {
1878 auto uses_sdk_library = out_badging->add_uses_sdk_libraries();
1879 uses_sdk_library->set_name(name);
1880 uses_sdk_library->set_version_major(versionMajor);
1881 for (auto& cert : certDigests) {
1882 uses_sdk_library->add_certificates(cert);
1883 }
1884 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001885};
1886
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001887/** Represents <uses-native-library> elements. **/
1888class UsesNativeLibrary : public ManifestExtractor::Element {
1889 public:
1890 UsesNativeLibrary() = default;
1891 std::string name;
1892 int required;
1893
1894 void Extract(xml::Element* element) override {
1895 auto parent_stack = extractor()->parent_stack();
1896 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
1897 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1898 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1899 }
1900 }
1901
1902 void Print(text::Printer* printer) override {
1903 if (!name.empty()) {
1904 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1905 (required == 0) ? "-not-required" : "", name.data()));
1906 }
1907 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001908
1909 void ToProto(pb::Badging* out_badging) override {
1910 if (!name.empty()) {
1911 auto uses_native_library = out_badging->add_uses_native_libraries();
1912 uses_native_library->set_name(name);
1913 uses_native_library->set_required(required != 0);
1914 }
1915 }
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001916};
1917
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001918/**
1919 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1920 * explicitly enable meta data printing.
1921 **/
1922class MetaData : public ManifestExtractor::Element {
1923 public:
1924 MetaData() = default;
1925 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001926 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001927 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001928 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001929 const int* resource_int;
1930
1931 void Extract(xml::Element* element) override {
1932 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001933 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001934 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001935 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001936 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1937 }
1938
Ryan Mitchell214846d2018-09-19 16:57:01 -07001939 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001940 if (extractor()->options_.include_meta_data && !name.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001941 printer->Print(StringPrintf("meta-data: name='%s'", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001942 if (!value.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001943 printer->Print(StringPrintf(" value='%s'", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001944 } else if (value_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001945 printer->Print(StringPrintf(" value='%d'", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001946 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001947 if (!resource.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001948 printer->Print(StringPrintf(" resource='%s'", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001949 } else if (resource_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001950 printer->Print(StringPrintf(" resource='%d'", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001951 }
1952 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001953 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001954 }
1955 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001956
1957 void ToProto(pb::Badging* out_badging) override {
1958 if (!name.empty()) {
1959 auto metadata = out_badging->add_metadata();
1960 metadata->set_name(name);
1961 if (!value.empty()) {
1962 metadata->set_value_string(value);
1963 } else if (value_int) {
1964 metadata->set_value_int(*value_int);
1965 } else {
1966 if (!resource.empty()) {
1967 metadata->set_resource_string(resource);
1968 } else if (resource_int) {
1969 metadata->set_resource_int(*resource_int);
1970 }
1971 }
1972 }
1973 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001974};
1975
1976/**
1977 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1978 * service components.
1979 **/
1980class Action : public ManifestExtractor::Element {
1981 public:
1982 Action() = default;
1983 std::string component = "";
1984
1985 void Extract(xml::Element* element) override {
1986 auto parent_stack = extractor()->parent_stack();
1987 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1988
1989 if (ElementCast<IntentFilter>(parent_stack[0])) {
1990 if (ElementCast<Activity>(parent_stack[1])) {
1991 // Detects the presence of a particular type of activity.
1992 Activity* activity = ElementCast<Activity>(parent_stack[1]);
Iurii Makhno499ecd32022-08-19 16:34:26 +00001993 static const auto map = std::map<std::string, std::string>({
1994 {"android.intent.action.MAIN", "main"},
1995 {"android.media.action.VIDEO_CAMERA", "camera"},
1996 {"android.media.action.STILL_IMAGE_CAMERA", "camera"},
1997 {"android.media.action.STILL_IMAGE_CAMERA_SECURE", "camera-secure"},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001998 });
1999
2000 auto entry = map.find(action);
2001 if (entry != map.end()) {
2002 component = entry->second;
2003 activity->has_component_ = true;
2004 }
2005
2006 if (action == "android.intent.action.MAIN") {
2007 activity->has_main_action = true;
2008 }
2009
2010 } else if (ElementCast<Receiver>(parent_stack[1])) {
2011 // Detects the presence of a particular type of receiver. If the action requires a
2012 // permission, then the receiver element is checked for the permission.
2013 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
2014 auto map = std::map<std::string, std::string>({
2015 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
2016 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
2017 });
2018
2019 auto permissions = std::map<std::string, std::string>({
2020 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
2021 });
2022
2023 auto entry = map.find(action);
2024 auto permission = permissions.find(action);
2025 if (entry != map.end() && (permission == permissions.end()
2026 || (receiver->permission && permission->second == *receiver->permission))) {
2027 receiver->has_component = true;
2028 component = entry->second;
2029 }
2030
2031 } else if (ElementCast<Service>(parent_stack[1])) {
2032 // Detects the presence of a particular type of service. If the action requires a
2033 // permission, then the service element is checked for the permission.
2034 Service* service = ElementCast<Service>(parent_stack[1]);
2035 auto map = std::map<std::string, std::string>({
2036 { "android.view.InputMethod" , "ime" },
2037 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
2038 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
2039 { "android.printservice.PrintService" , "print-service" },
2040 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
2041 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
2042 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
2043 { "android.service.dreams.DreamService" , "dream" },
2044 });
2045
2046 auto permissions = std::map<std::string, std::string>({
2047 { "android.accessibilityservice.AccessibilityService" ,
2048 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
2049 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
2050 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
2051 "android.permission.BIND_NFC_SERVICE" },
2052 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
2053 "android.permission.BIND_NFC_SERVICE" },
2054 { "android.service.notification.NotificationListenerService" ,
2055 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
2056 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
2057 });
2058
2059 auto entry = map.find(action);
2060 auto permission = permissions.find(action);
2061 if (entry != map.end() && (permission == permissions.end()
2062 || (service->permission && permission->second == *service->permission))) {
2063 service->has_component= true;
2064 component = entry->second;
2065 }
2066
2067 } else if (ElementCast<Provider>(parent_stack[1])) {
2068 // Detects the presence of a particular type of receiver. If the provider requires a
2069 // permission, then the provider element is checked for the permission.
2070 // Detect whether this action
2071 Provider* provider = ElementCast<Provider>(parent_stack[1]);
2072 if (action == "android.content.action.DOCUMENTS_PROVIDER"
2073 && provider->has_required_saf_attributes) {
2074 component = "document-provider";
2075 }
2076 }
2077 }
2078
2079 // Represents a searchable interface
2080 if (action == "android.intent.action.SEARCH") {
2081 component = "search";
2082 }
2083 }
2084};
2085
2086/**
2087 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
2088 **/
2089class SupportsInput : public ManifestExtractor::Element {
2090 public:
2091 SupportsInput() = default;
2092 std::vector<std::string> inputs;
2093
Ryan Mitchell214846d2018-09-19 16:57:01 -07002094 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002095 const size_t size = inputs.size();
2096 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002097 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002098 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002099 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002100 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002101 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002102 }
2103 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002104
2105 void ToProto(pb::Badging* out_badging) override {
2106 auto supports_input = out_badging->mutable_supports_input();
2107 for (auto& input : inputs) {
2108 supports_input->add_inputs(input);
2109 }
2110 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002111};
2112
2113/** Represents <input-type> elements. **/
2114class InputType : public ManifestExtractor::Element {
2115 public:
2116 InputType() = default;
2117 void Extract(xml::Element* element) override {
2118 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2119 auto parent_stack = extractor()->parent_stack();
2120
2121 // Add the input to the set of supported inputs
2122 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
2123 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
2124 supports->inputs.push_back(*name);
2125 }
2126 }
2127};
2128
2129/** Represents <original-package> elements. **/
2130class OriginalPackage : public ManifestExtractor::Element {
2131 public:
2132 OriginalPackage() = default;
2133 const std::string* name = nullptr;
2134
2135 void Extract(xml::Element* element) override {
2136 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2137 }
2138
Ryan Mitchell214846d2018-09-19 16:57:01 -07002139 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002140 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002141 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002142 }
2143 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002144
2145 void ToProto(pb::Badging* out_badging) override {
2146 if (name) {
2147 out_badging->mutable_package()->set_original_package(*name);
2148 }
2149 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002150};
2151
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002152
2153/** Represents <overlay> elements. **/
2154class Overlay : public ManifestExtractor::Element {
2155 public:
2156 Overlay() = default;
2157 const std::string* target_package = nullptr;
2158 int priority;
2159 bool is_static;
2160 const std::string* required_property_name = nullptr;
2161 const std::string* required_property_value = nullptr;
2162
2163 void Extract(xml::Element* element) override {
2164 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
2165 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
2166 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
2167 required_property_name = GetAttributeString(
2168 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
2169 required_property_value = GetAttributeString(
2170 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
2171 }
2172
2173 void Print(text::Printer* printer) override {
2174 printer->Print(StringPrintf("overlay:"));
2175 if (target_package) {
2176 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
2177 }
2178 printer->Print(StringPrintf(" priority='%d'", priority));
2179 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
2180 if (required_property_name) {
2181 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
2182 }
2183 if (required_property_value) {
2184 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
2185 }
2186 printer->Print("\n");
2187 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002188
2189 void ToProto(pb::Badging* out_badging) override {
2190 auto overlay = out_badging->mutable_overlay();
2191 if (target_package) {
2192 overlay->set_target_package(*target_package);
2193 }
2194 overlay->set_priority(priority);
2195 overlay->set_static_(is_static);
2196 if (required_property_name) {
2197 overlay->set_required_property_name(*required_property_name);
2198 }
2199 if (required_property_value) {
2200 overlay->set_required_property_value(*required_property_value);
2201 }
2202 }
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002203};
2204
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002205/** * Represents <package-verifier> elements. **/
2206class PackageVerifier : public ManifestExtractor::Element {
2207 public:
2208 PackageVerifier() = default;
2209 const std::string* name = nullptr;
2210 const std::string* public_key = nullptr;
2211
2212 void Extract(xml::Element* element) override {
2213 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2214 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
2215 }
2216
Ryan Mitchell214846d2018-09-19 16:57:01 -07002217 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002218 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002219 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002220 name->data(), public_key->data()));
2221 }
2222 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002223
2224 void ToProto(pb::Badging* out_badging) override {
2225 auto package_verifier = out_badging->mutable_package_verifier();
2226 if (name && public_key) {
2227 package_verifier->set_name(*name);
2228 package_verifier->set_public_key(*public_key);
2229 }
2230 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002231};
2232
2233/** Represents <uses-package> elements. **/
2234class UsesPackage : public ManifestExtractor::Element {
2235 public:
2236 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002237 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002238 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002239 int version;
2240 int versionMajor;
2241 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002242
2243 void Extract(xml::Element* element) override {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002244 auto parent_stack = extractor()->parent_stack();
2245 if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
2246 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
2247 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2248 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
2249 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
2250 AddCertDigest(element);
2251 }
2252 }
2253
2254 void AddCertDigest(xml::Element* element) {
2255 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
2256 // We allow ":" delimiters in the SHA declaration as this is the format
2257 // emitted by the certtool making it easy for developers to copy/paste.
2258 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
2259 if (!digest.empty()) {
2260 certDigests.push_back(digest);
2261 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002262 }
2263
Ryan Mitchell214846d2018-09-19 16:57:01 -07002264 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002265 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002266 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002267 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07002268 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
2269 packageType->data(), name->data(), version, versionMajor));
2270 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002271 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002272 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002273 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07002274 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002275 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002276 }
2277 }
2278 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002279
2280 void ToProto(pb::Badging* out_badging) override {
2281 if (name) {
2282 auto uses_package = out_badging->add_uses_packages();
2283 uses_package->set_name(*name);
2284 if (packageType) {
2285 uses_package->set_package_type(*packageType);
2286 uses_package->set_version(version);
2287 uses_package->set_version_major(versionMajor);
2288 for (auto& cert : certDigests) {
2289 uses_package->add_certificates(cert);
2290 }
2291 }
2292 }
2293 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07002294};
2295
2296/** Represents <additional-certificate> elements. **/
2297class AdditionalCertificate : public ManifestExtractor::Element {
2298 public:
2299 AdditionalCertificate() = default;
2300
2301 void Extract(xml::Element* element) override {
2302 auto parent_stack = extractor()->parent_stack();
2303 if (parent_stack.size() > 0) {
2304 if (ElementCast<UsesPackage>(parent_stack[0])) {
2305 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
2306 uses->AddCertDigest(element);
2307 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
2308 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
2309 uses->AddCertDigest(element);
2310 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002311 }
2312 }
2313};
2314
2315/** Represents <screen> elements found in <compatible-screens> elements. */
2316class Screen : public ManifestExtractor::Element {
2317 public:
2318 Screen() = default;
2319 const int32_t* size = nullptr;
2320 const int32_t* density = nullptr;
2321
2322 void Extract(xml::Element* element) override {
2323 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
2324 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
2325 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002326
2327 void ToProto(pb::Badging* out_badging) override {
2328 if (size && density) {
2329 auto screen = out_badging->mutable_compatible_screens()->add_screens();
2330 screen->set_density(*density);
2331 screen->set_size(*size);
2332 }
2333 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002334};
2335
2336/**
2337 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
2338 * that each denote a supported screen size and screen density.
2339 **/
2340class CompatibleScreens : public ManifestExtractor::Element {
2341 public:
2342 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07002343 void Print(text::Printer* printer) override {
2344 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002345
2346 bool first = true;
2347 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
2348 if (auto screen = ElementCast<Screen>(el)) {
2349 if (first) {
2350 first = false;
2351 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002352 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002353 }
2354
2355 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002356 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002357 }
2358 }
2359 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07002360 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002361 }
2362};
2363
2364/** Represents <supports-gl-texture> elements. **/
2365class SupportsGlTexture : public ManifestExtractor::Element {
2366 public:
2367 SupportsGlTexture() = default;
2368 const std::string* name = nullptr;
2369
2370 void Extract(xml::Element* element) override {
2371 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2372 }
2373
Ryan Mitchell214846d2018-09-19 16:57:01 -07002374 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002375 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002376 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002377 }
2378 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002379
2380 void ToProto(pb::Badging* out_badging) override {
2381 if (name) {
2382 out_badging->mutable_supports_gl_texture()->add_name(*name);
2383 }
2384 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002385};
2386
Todd Kennedyce3e1292020-10-29 17:14:24 -07002387/** Represents <property> elements. **/
2388class Property : public ManifestExtractor::Element {
2389 public:
2390 Property() = default;
2391 std::string name;
2392 std::string value;
2393 const int* value_int;
2394 std::string resource;
2395 const int* resource_int;
2396
2397 void Extract(xml::Element* element) override {
2398 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
2399 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
2400 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
2401 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
2402 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
2403 }
2404
2405 void Print(text::Printer* printer) override {
2406 printer->Print(StringPrintf("property: name='%s' ", name.data()));
2407 if (!value.empty()) {
2408 printer->Print(StringPrintf("value='%s' ", value.data()));
2409 } else if (value_int) {
2410 printer->Print(StringPrintf("value='%d' ", *value_int));
2411 } else {
2412 if (!resource.empty()) {
2413 printer->Print(StringPrintf("resource='%s' ", resource.data()));
2414 } else if (resource_int) {
2415 printer->Print(StringPrintf("resource='%d' ", *resource_int));
2416 }
2417 }
2418 printer->Print("\n");
2419 }
Todd Kennedyce3e1292020-10-29 17:14:24 -07002420
Iurii Makhno85875a82022-04-26 15:30:01 +00002421 void ToProto(pb::Badging* out_badging) override {
2422 if (!name.empty()) {
2423 auto property = out_badging->add_properties();
2424 property->set_name(name);
2425 if (!value.empty()) {
2426 property->set_value_string(value);
2427 } else if (value_int) {
2428 property->set_value_int(*value_int);
2429 } else {
2430 if (!resource.empty()) {
2431 property->set_resource_string(resource);
2432 } else if (resource_int) {
2433 property->set_resource_int(*resource_int);
2434 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002435 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002436 }
2437 }
2438};
2439
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002440/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07002441static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002442 el->Print(printer);
2443 for (auto &child : el->children()) {
2444 Print(child.get(), printer);
2445 }
2446}
2447
Iurii Makhno85875a82022-04-26 15:30:01 +00002448/** Recursively serializes extracted badging elements to proto. */
2449static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) {
2450 el->ToProto(out_badging);
2451 for (auto& child : el->children()) {
2452 ToProto(child.get(), out_badging);
2453 }
2454}
2455
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002456bool ManifestExtractor::Extract(android::IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002457 // Load the manifest
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002458 doc_ = apk_->LoadXml("AndroidManifest.xml", diag);
2459 if (doc_ == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002460 diag->Error(android::DiagMessage() << "failed to find AndroidManifest.xml");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002461 return false;
2462 }
2463
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002464 xml::Element* element = doc_->root.get();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002465 if (element->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002466 diag->Error(android::DiagMessage() << "manifest does not start with <manifest> tag");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002467 return false;
2468 }
2469
2470 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
2471 // printing only permission elements is requested
2472 if (options_.only_permissions) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002473 root_element_ = ManifestExtractor::Element::Inflate(this, element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002474
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002475 if (auto manifest = ElementCast<Manifest>(root_element_.get())) {
2476 manifest->only_package_name = true;
2477
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002478 for (xml::Element* child : element->GetChildElements()) {
2479 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
2480 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002481 // Inflate the element and its descendants
2482 auto permission_element = Visit(child);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002483 manifest->AddChild(permission_element);
2484 }
2485 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002486 return true;
2487 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002488 return false;
2489 }
2490
2491 // Collect information about the resource configurations
2492 if (apk_->GetResourceTable()) {
2493 for (auto &package : apk_->GetResourceTable()->packages) {
2494 for (auto &type : package->types) {
2495 for (auto &entry : type->entries) {
2496 for (auto &value : entry->values) {
2497 std::string locale_str = value->config.GetBcp47LanguageTag();
2498
2499 // Collect all the unique locales of the apk
2500 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002501 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002502 config.setBcp47Locale(locale_str.data());
2503 locales_.insert(std::make_pair(locale_str, config));
2504 }
2505
2506 // Collect all the unique density of the apk
2507 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
2508 : value->config.density;
2509 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002510 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002511 config.density = density;
2512 densities_.insert(std::make_pair(density, config));
2513 }
2514 }
2515 }
2516 }
2517 }
2518 }
2519
2520 // Extract badging information
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002521 root_element_ = Visit(element);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002522
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002523 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
2524 // attribute values from the last defined tag.
2525 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002526 for (const auto& child : root_element_->children()) {
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002527 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
2528 filtered_uses_sdk_tags.emplace_back(uses_sdk);
2529 }
2530 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002531 if (filtered_uses_sdk_tags.size() >= 2U) {
2532 filtered_uses_sdk_tags.pop_back();
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002533 root_element_->Filter([&](const ManifestExtractor::Element* e) {
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002534 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
2535 filtered_uses_sdk_tags.end();
2536 });
2537 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002538
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002539 /** Recursively checks the extracted elements for the specified permission. **/
2540 auto FindPermission = [&](ManifestExtractor::Element* root,
2541 const std::string& name) -> ManifestExtractor::Element* {
2542 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
2543 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
2544 return permission->name == name;
2545 }
2546 return false;
2547 });
2548 };
2549
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002550 auto AddImpliedPermission = [&](const std::string& name, const std::string& reason,
2551 int32_t max_sdk_version) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002552 auto permission = util::make_unique<UsesPermission>();
2553 permission->name = name;
2554 permission->maxSdkVersion = max_sdk_version;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002555 permission->implied = true;
2556 permission->impliedReason = reason;
2557 implied_permissions_.push_back(std::move(permission));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002558 };
2559
2560 // Implied permissions
2561 // Pre-1.6 implicitly granted permission compatibility logic
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002562 bool insert_write_external = false;
2563 auto write_external_permission = ElementCast<UsesPermission>(
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002564 FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002565
Jackal Guo201a60a2021-08-31 12:37:30 +08002566 if (target_sdk() < SDK_DONUT) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002567 if (!write_external_permission) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002568 AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002569 insert_write_external = true;
2570 }
2571
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002572 if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) {
2573 AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002574 }
2575 }
2576
2577 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2578 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2579 // do this (regardless of target API version) because we can't have
2580 // an app with write permission but not read permission.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002581 auto read_external =
2582 FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002583 if (!read_external && (insert_write_external || write_external_permission)) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002584 AddImpliedPermission(
2585 "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE",
2586 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002587 }
2588
2589 // Pre-JellyBean call log permission compatibility.
Jackal Guo201a60a2021-08-31 12:37:30 +08002590 if (target_sdk() < SDK_JELLY_BEAN) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002591 if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") &&
2592 FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) {
2593 AddImpliedPermission("android.permission.READ_CALL_LOG",
2594 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002595 }
2596
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002597 if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") &&
2598 FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) {
2599 AddImpliedPermission("android.permission.WRITE_CALL_LOG",
2600 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002601 }
2602 }
2603
2604 // If the app hasn't declared the touchscreen as a feature requirement (either
2605 // directly or implied, required or not), then the faketouch feature is implied.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002606 if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) {
2607 common_feature_group()->addImpliedFeature("android.hardware.faketouch",
2608 "default feature for all apps", false);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002609 }
2610
2611 // Only print the common feature group if no feature group is defined
2612 std::vector<FeatureGroup*> feature_groups;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002613 ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002614 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2615 feature_groups.push_back(feature_group);
2616 }
2617 });
2618
2619 if (feature_groups.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002620 feature_groups_.push_back(common_feature_group());
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002621 } else {
2622 // Merge the common feature group into the feature group
2623 for (auto& feature_group : feature_groups) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002624 feature_group->Merge(common_feature_group());
2625 feature_groups_.push_back(feature_group);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002626 }
2627 };
2628
2629 // Collect the component types of the application
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002630 ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002631 if (ElementCast<Action>(el)) {
2632 auto action = ElementCast<Action>(el);
2633 if (!action->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002634 components_.discovered_components.insert(action->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002635 return;
2636 }
2637 }
2638
2639 if (ElementCast<Category>(el)) {
2640 auto category = ElementCast<Category>(el);
2641 if (!category->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002642 components_.discovered_components.insert(category->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002643 return;
2644 }
2645 }
2646 });
2647
2648 // Check for the payment component
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002649 ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002650 if (auto service = ElementCast<Service>(el)) {
2651 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2652 [&](ManifestExtractor::Element* el) -> bool {
2653 if (auto action = ElementCast<Action>(el)) {
2654 return (action->component == "host-apdu");
2655 }
2656 return false;
2657 }));
2658
2659 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2660 [&](ManifestExtractor::Element* el) -> bool {
2661 if (auto action = ElementCast<Action>(el)) {
2662 return (action->component == "offhost-apdu");
2663 }
2664 return false;
2665 }));
2666
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002667 ForEachChild(service,
2668 [this, &diag, &host_apdu_action,
2669 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2670 if (auto meta_data = ElementCast<MetaData>(el)) {
2671 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" &&
2672 host_apdu_action) ||
2673 (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" &&
2674 offhost_apdu_action)) {
2675 // Attempt to load the resource file
2676 if (!meta_data->resource.empty()) {
2677 return;
2678 }
2679 auto resource = this->apk_->LoadXml(meta_data->resource, diag);
2680 if (!resource) {
2681 return;
2682 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002683
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002684 // Look for the payment category on an <aid-group> element
2685 auto& root = resource.get()->root;
2686 if ((host_apdu_action && root->name == "host-apdu-service") ||
2687 (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2688 for (auto& child : root->GetChildElements()) {
2689 if (child->name == "aid-group") {
2690 auto category = FindAttribute(child, CATEGORY_ATTR);
2691 if (category && category->value == "payment") {
Iurii Makhno85875a82022-04-26 15:30:01 +00002692 this->components_.discovered_components.insert("payment");
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002693 return;
2694 }
2695 }
2696 }
2697 }
2698 }
2699 }
2700 });
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002701 }
2702 });
2703
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002704 // Print presence of activities, receivers, and services with no special components
2705 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2706 if (auto activity = ElementCast<Activity>(el)) {
2707 if (!activity->has_component_) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002708 components_.other_activities = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002709 return true;
2710 }
2711 }
2712 return false;
2713 });
2714
2715 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2716 if (auto receiver = ElementCast<Receiver>(el)) {
2717 if (!receiver->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002718 components_.other_receivers = 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 service = ElementCast<Service>(el)) {
2727 if (!service->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002728 components_.other_services = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002729 return true;
2730 }
2731 }
2732 return false;
2733 });
2734
2735 // Gather the supported screens
2736 const static SupportsScreen default_screens{};
2737 SupportsScreen* screen = ElementCast<SupportsScreen>(
2738 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2739 return ElementCast<SupportsScreen>(el) != nullptr;
2740 }));
2741 supports_screen_ = screen ? screen : &default_screens;
2742
2743 // Gather the supported architectures_ of the app
2744 std::set<std::string> architectures_from_apk;
2745 auto it = apk_->GetFileCollection()->Iterator();
2746 while (it->HasNext()) {
2747 auto file_path = it->Next()->GetSource().path;
Iurii Makhno499ecd32022-08-19 16:34:26 +00002748 if (file_path.starts_with("lib/")) {
2749 file_path = file_path.substr(4);
2750 size_t pos = file_path.find('/');
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002751 if (pos != std::string::npos) {
2752 file_path = file_path.substr(0, pos);
2753 }
2754
2755 architectures_from_apk.insert(file_path);
2756 }
2757 }
2758
2759 // Determine if the application has multiArch supports
2760 auto has_multi_arch =
2761 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2762 if (auto application = ElementCast<Application>(el)) {
2763 return application->has_multi_arch;
2764 }
2765 return false;
2766 });
2767
2768 bool output_alt_native_code = false;
2769 // A multiArch package is one that contains 64-bit and
2770 // 32-bit versions of native code and expects 3rd-party
2771 // apps to load these native code libraries. Since most
2772 // 64-bit systems also support 32-bit apps, the apps
2773 // loading this multiArch package's code may be either
2774 if (has_multi_arch) {
2775 // If this is a multiArch package, report the 64-bit
2776 // version only. Then as a separate entry, report the
2777 // rest.
2778 //
2779 // If we report the 32-bit architecture, this APK will
2780 // be installed on a 32-bit device, causing a large waste
2781 // of bandwidth and disk space. This assumes that
2782 // the developer of the multiArch package has also
2783 // made a version that is 32-bit only.
2784 const std::string kIntel64 = "x86_64";
2785 const std::string kArm64 = "arm64-v8a";
2786
2787 auto arch = architectures_from_apk.find(kIntel64);
2788 if (arch == architectures_from_apk.end()) {
2789 arch = architectures_from_apk.find(kArm64);
2790 }
2791
2792 if (arch != architectures_from_apk.end()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002793 architectures_.architectures.insert(*arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002794 architectures_from_apk.erase(arch);
2795 output_alt_native_code = true;
2796 }
2797 }
2798 for (auto& arch : architectures_from_apk) {
2799 if (output_alt_native_code) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002800 architectures_.alt_architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002801 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002802 architectures_.architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002803 }
2804 }
2805 return true;
2806}
2807
2808bool ManifestExtractor::Dump(text::Printer* printer) {
2809 Print(root_element_.get(), printer);
2810 if (options_.only_permissions) {
2811 return true;
2812 }
2813
2814 for (auto& implied_permission : implied_permissions_) {
2815 implied_permission->Print(printer);
2816 }
2817 for (auto& feature_group : feature_groups_) {
2818 feature_group->PrintGroup(printer);
2819 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002820 components_.Print(printer);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002821 supports_screen_->PrintScreens(printer, target_sdk_);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002822
2823 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002824 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002825 for (auto& config : locales_) {
2826 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002827 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002828 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002829 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002830 }
2831 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002832 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002833
2834 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002835 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002836 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002837 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002838 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002839 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002840
Iurii Makhno85875a82022-04-26 15:30:01 +00002841 architectures_.Print(printer);
2842 return true;
2843}
2844
2845bool ManifestExtractor::DumpProto(pb::Badging* out_badging) {
2846 ToProto(root_element_.get(), out_badging);
2847 for (auto& implied_permission : implied_permissions_) {
2848 implied_permission->ToProto(out_badging);
2849 }
2850 for (auto& feature_group : feature_groups_) {
2851 feature_group->GroupToProto(out_badging);
2852 }
2853 components_.ToProto(out_badging);
2854 supports_screen_->ToProtoScreens(out_badging, target_sdk_);
2855
2856 for (auto& config : locales_) {
2857 if (!config.first.empty()) {
2858 out_badging->add_locales(config.first);
2859 }
2860 }
2861 for (auto& config : densities_) {
2862 out_badging->add_densities(config.first);
2863 }
2864
2865 architectures_.ToProto(out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002866 return true;
2867}
2868
2869/**
2870 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2871 * pointer.
2872 **/
2873template<typename T>
2874T* ElementCast(ManifestExtractor::Element* element) {
2875 if (element == nullptr) {
2876 return nullptr;
2877 }
2878
2879 const std::unordered_map<std::string, bool> kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002880 {"action", std::is_base_of<Action, T>::value},
2881 {"activity", std::is_base_of<Activity, T>::value},
2882 {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
2883 {"application", std::is_base_of<Application, T>::value},
2884 {"category", std::is_base_of<Category, T>::value},
2885 {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
2886 {"feature-group", std::is_base_of<FeatureGroup, T>::value},
2887 {"input-type", std::is_base_of<InputType, T>::value},
2888 {"intent-filter", std::is_base_of<IntentFilter, T>::value},
2889 {"meta-data", std::is_base_of<MetaData, T>::value},
2890 {"manifest", std::is_base_of<Manifest, T>::value},
2891 {"original-package", std::is_base_of<OriginalPackage, T>::value},
2892 {"overlay", std::is_base_of<Overlay, T>::value},
2893 {"package-verifier", std::is_base_of<PackageVerifier, T>::value},
2894 {"permission", std::is_base_of<Permission, T>::value},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002895 {"property", std::is_base_of<Property, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002896 {"provider", std::is_base_of<Provider, T>::value},
2897 {"receiver", std::is_base_of<Receiver, T>::value},
2898 {"required-feature", std::is_base_of<RequiredFeature, T>::value},
2899 {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value},
2900 {"screen", std::is_base_of<Screen, T>::value},
2901 {"service", std::is_base_of<Service, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002902 {"sdk-library", std::is_base_of<SdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002903 {"static-library", std::is_base_of<StaticLibrary, T>::value},
2904 {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
2905 {"supports-input", std::is_base_of<SupportsInput, T>::value},
2906 {"supports-screens", std::is_base_of<SupportsScreen, T>::value},
2907 {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
2908 {"uses-feature", std::is_base_of<UsesFeature, T>::value},
2909 {"uses-library", std::is_base_of<UsesLibrary, T>::value},
2910 {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value},
2911 {"uses-package", std::is_base_of<UsesPackage, T>::value},
2912 {"uses-permission", std::is_base_of<UsesPermission, T>::value},
2913 {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
2914 {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002915 {"uses-sdk-library", std::is_base_of<UsesSdkLibrary, T>::value},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002916 {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002917 };
2918
2919 auto check = kTagCheck.find(element->tag());
2920 if (check != kTagCheck.end() && check->second) {
2921 return static_cast<T*>(element);
2922 }
2923 return nullptr;
2924}
2925
2926template<typename T>
2927std::unique_ptr<T> CreateType() {
2928 return std::move(util::make_unique<T>());
2929}
2930
2931std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
2932 ManifestExtractor* extractor, xml::Element* el) {
2933 const std::unordered_map<std::string,
2934 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
2935 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002936 {"action", &CreateType<Action>},
2937 {"activity", &CreateType<Activity>},
2938 {"additional-certificate", &CreateType<AdditionalCertificate>},
2939 {"application", &CreateType<Application>},
2940 {"category", &CreateType<Category>},
2941 {"compatible-screens", &CreateType<CompatibleScreens>},
2942 {"feature-group", &CreateType<FeatureGroup>},
2943 {"input-type", &CreateType<InputType>},
2944 {"intent-filter", &CreateType<IntentFilter>},
2945 {"manifest", &CreateType<Manifest>},
2946 {"meta-data", &CreateType<MetaData>},
2947 {"original-package", &CreateType<OriginalPackage>},
2948 {"overlay", &CreateType<Overlay>},
2949 {"package-verifier", &CreateType<PackageVerifier>},
2950 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002951 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002952 {"provider", &CreateType<Provider>},
2953 {"receiver", &CreateType<Receiver>},
2954 {"required-feature", &CreateType<RequiredFeature>},
2955 {"required-not-feature", &CreateType<RequiredNotFeature>},
2956 {"screen", &CreateType<Screen>},
2957 {"service", &CreateType<Service>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002958 {"sdk-library", &CreateType<SdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002959 {"static-library", &CreateType<StaticLibrary>},
2960 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2961 {"supports-input", &CreateType<SupportsInput>},
2962 {"supports-screens", &CreateType<SupportsScreen>},
2963 {"uses-configuration", &CreateType<UsesConfiguarion>},
2964 {"uses-feature", &CreateType<UsesFeature>},
2965 {"uses-library", &CreateType<UsesLibrary>},
2966 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2967 {"uses-package", &CreateType<UsesPackage>},
2968 {"uses-permission", &CreateType<UsesPermission>},
2969 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2970 {"uses-sdk", &CreateType<UsesSdkBadging>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002971 {"uses-sdk-library", &CreateType<UsesSdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002972 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2973 };
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002974
2975 // Attempt to map the xml tag to a element inflater
2976 std::unique_ptr<ManifestExtractor::Element> element;
2977 auto check = kTagCheck.find(el->name);
2978 if (check != kTagCheck.end()) {
2979 element = check->second();
2980 } else {
2981 element = util::make_unique<ManifestExtractor::Element>();
2982 }
2983
2984 element->extractor_ = extractor;
2985 element->tag_ = el->name;
2986 element->Extract(el);
2987 return element;
2988}
2989
2990std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
2991 auto element = ManifestExtractor::Element::Inflate(this, el);
2992 parent_stack_.insert(parent_stack_.begin(), element.get());
2993
2994 // Process the element and recursively visit the children
2995 for (xml::Element* child : el->GetChildElements()) {
2996 auto v = Visit(child);
2997 element->AddChild(v);
2998 }
2999
3000 parent_stack_.erase(parent_stack_.begin());
3001 return element;
3002}
3003
Ryan Mitchell214846d2018-09-19 16:57:01 -07003004int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003005 android::IDiagnostics* diag) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07003006 ManifestExtractor extractor(apk, options);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003007 if (!extractor.Extract(diag)) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003008 return 1;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003009 }
3010 return extractor.Dump(printer) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003011}
3012
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003013int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, android::IDiagnostics* diag) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003014 DumpManifestOptions options{/* include_meta_data= */ true,
3015 /* only_permissions= */ false};
3016 ManifestExtractor extractor(apk, options);
3017 if (!extractor.Extract(diag)) {
3018 return 1;
3019 }
3020 return extractor.DumpProto(out_badging) ? 0 : 1;
3021}
3022
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02003023} // namespace aapt