blob: a43bf1b60f421b7c2ca83fd8d95fd1376a7d587e [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
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +000019#include <androidfw/ApkParsing.h>
20
Dianne Hackborn813d7502018-10-02 16:59:46 -070021#include <algorithm>
Iurii Makhno85875a82022-04-26 15:30:01 +000022#include <array>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000023#include <memory>
24#include <set>
Iurii Makhno85875a82022-04-26 15:30:01 +000025#include <string_view>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000026#include <vector>
Dianne Hackborn813d7502018-10-02 16:59:46 -070027
Ryan Mitchellfc225b22018-08-21 14:52:51 -070028#include "LoadedApk.h"
29#include "SdkConstants.h"
30#include "ValueVisitor.h"
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000031#include "androidfw/ConfigDescription.h"
Ryan Mitchellfc225b22018-08-21 14:52:51 -070032#include "io/File.h"
33#include "io/FileStream.h"
34#include "process/IResourceTableConsumer.h"
35#include "xml/XmlDom.h"
36
37using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020038using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070039
40namespace aapt {
41
42/**
43 * These are attribute resource constants for the platform, as found in android.R.attr.
44 */
45enum {
46 LABEL_ATTR = 0x01010001,
47 ICON_ATTR = 0x01010002,
48 NAME_ATTR = 0x01010003,
49 PERMISSION_ATTR = 0x01010006,
50 EXPORTED_ATTR = 0x01010010,
51 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000052 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070053 RESOURCE_ATTR = 0x01010025,
54 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000055 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070056 VALUE_ATTR = 0x01010024,
57 VERSION_CODE_ATTR = 0x0101021b,
58 VERSION_NAME_ATTR = 0x0101021c,
59 SCREEN_ORIENTATION_ATTR = 0x0101001e,
60 MIN_SDK_VERSION_ATTR = 0x0101020c,
61 MAX_SDK_VERSION_ATTR = 0x01010271,
62 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
63 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
64 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
65 REQ_NAVIGATION_ATTR = 0x0101022a,
66 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
67 TARGET_SDK_VERSION_ATTR = 0x01010270,
68 TEST_ONLY_ATTR = 0x01010272,
69 ANY_DENSITY_ATTR = 0x0101026c,
70 GL_ES_VERSION_ATTR = 0x01010281,
71 SMALL_SCREEN_ATTR = 0x01010284,
72 NORMAL_SCREEN_ATTR = 0x01010285,
73 LARGE_SCREEN_ATTR = 0x01010286,
74 XLARGE_SCREEN_ATTR = 0x010102bf,
75 REQUIRED_ATTR = 0x0101028e,
76 INSTALL_LOCATION_ATTR = 0x010102b7,
77 SCREEN_SIZE_ATTR = 0x010102ca,
78 SCREEN_DENSITY_ATTR = 0x010102cb,
79 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
80 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
81 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
82 PUBLIC_KEY_ATTR = 0x010103a6,
83 CATEGORY_ATTR = 0x010103e8,
84 BANNER_ATTR = 0x10103f2,
85 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070086 VERSION_ATTR = 0x01010519,
87 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000088 REQUIRED_FEATURE_ATTR = 0x01010554,
89 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000090 IS_STATIC_ATTR = 0x0101055a,
91 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
92 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070093 COMPILE_SDK_VERSION_ATTR = 0x01010572,
94 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070095 VERSION_MAJOR_ATTR = 0x01010577,
96 PACKAGE_TYPE_ATTR = 0x01010587,
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060097 USES_PERMISSION_FLAGS_ATTR = 0x01010644,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070098};
99
100const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -0600101constexpr int kNeverForLocation = 0x00010000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700102
103/** Retrieves the attribute of the element with the specified attribute resource id. */
104static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
105 for (auto& a : el->attributes) {
106 if (a.compiled_attribute && a.compiled_attribute.value().id) {
107 if (a.compiled_attribute.value().id.value() == resd_id) {
108 return std::move(&a);
109 }
110 }
111 }
112 return nullptr;
113}
114
115/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
116static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
117 const std::string &name) {
118 return el->FindAttribute(package, name);
119}
120
Iurii Makhno85875a82022-04-26 15:30:01 +0000121class Architectures {
122 public:
123 std::set<std::string> architectures;
124 std::set<std::string> alt_architectures;
125
126 void Print(text::Printer* printer) {
127 if (!architectures.empty()) {
128 printer->Print("native-code:");
129 for (auto& arch : architectures) {
130 printer->Print(StringPrintf(" '%s'", arch.data()));
131 }
132 printer->Print("\n");
133 }
134 if (!alt_architectures.empty()) {
135 printer->Print("alt-native-code:");
136 for (auto& arch : alt_architectures) {
137 printer->Print(StringPrintf(" '%s'", arch.data()));
138 }
139 printer->Print("\n");
140 }
141 }
142
143 void ToProto(pb::Badging* out_badging) {
144 auto out_architectures = out_badging->mutable_architectures();
145 for (auto& arch : architectures) {
146 out_architectures->add_architectures(arch);
147 }
148 for (auto& arch : alt_architectures) {
149 out_architectures->add_alt_architectures(arch);
150 }
151 }
152};
153
154const static std::array<std::string_view, 14> printable_components{"app-widget",
155 "device-admin",
156 "ime",
157 "wallpaper",
158 "accessibility",
159 "print-service",
160 "payment",
161 "search",
162 "document-provider",
163 "launcher",
164 "notification-listener",
165 "dream",
166 "camera",
167 "camera-secure"};
168
169class Components {
170 public:
171 std::set<std::string, std::less<>> discovered_components;
172 bool other_activities = false;
173 bool other_receivers = false;
174 bool other_services = false;
175
176 void Print(text::Printer* printer) {
177 for (auto& component : printable_components) {
178 if (discovered_components.find(component) != discovered_components.end()) {
179 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
180 }
181 }
182 // Print presence of main activity
183 if (discovered_components.find("main") != discovered_components.end()) {
184 printer->Print("main\n");
185 }
186
187 if (other_activities) {
188 printer->Print("other-activities\n");
189 }
190 if (other_receivers) {
191 printer->Print("other-receivers\n");
192 }
193 if (other_services) {
194 printer->Print("other-services\n");
195 }
196 }
197
198 void ToProto(pb::Badging* out_badging) {
199 auto out_components = out_badging->mutable_components();
200 for (auto& component : printable_components) {
201 auto discovered = discovered_components.find(component);
202 if (discovered != discovered_components.end()) {
203 out_components->add_provided_components(*discovered);
204 }
205 }
206 out_components->set_main(discovered_components.find("main") != discovered_components.end());
207 out_components->set_other_activities(other_activities);
208 out_components->set_other_receivers(other_receivers);
209 out_components->set_other_services(other_services);
210 }
211};
212
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700213class CommonFeatureGroup;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000214class FeatureGroup;
Iurii Makhno85875a82022-04-26 15:30:01 +0000215class SupportsScreen;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700216
217class ManifestExtractor {
218 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700219
Ryan Mitchell214846d2018-09-19 16:57:01 -0700220 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700221 : apk_(apk), options_(options) { }
222
223 class Element {
224 public:
225 Element() = default;
226 virtual ~Element() = default;
227
Iurii Makhno32c0c032022-12-08 18:07:58 +0000228 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el,
229 const std::string& parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700230
231 /** Writes out the extracted contents of the element. */
Iurii Makhno85875a82022-04-26 15:30:01 +0000232 virtual void Print(text::Printer* printer) {
233 }
234
235 /** Saves extracted information into Badging proto. */
236 virtual void ToProto(pb::Badging* out_badging) {
237 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700238
239 /** Adds an element to the list of children of the element. */
240 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
241
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700242 template <typename Predicate>
243 void Filter(Predicate&& func) {
244 children_.erase(std::remove_if(children_.begin(), children_.end(),
Kelvin Zhang3965584d2021-05-10 12:17:14 -0400245 [&](const auto& e) { return func(e.get()); }),
246 children_.end());
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700247 }
248
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700249 /** Retrieves the list of children of the element. */
250 const std::vector<std::unique_ptr<Element>>& children() const {
251 return children_;
252 }
253
254 /** Retrieves the extracted xml element tag. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000255 const std::string& tag() const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700256 return tag_;
257 }
258
Iurii Makhno32c0c032022-12-08 18:07:58 +0000259 /** Whether this element has special Extract/Print/ToProto logic. */
260 bool is_featured() const {
261 return featured_;
262 }
263
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700264 protected:
265 ManifestExtractor* extractor() const {
266 return extractor_;
267 }
268
269 /** Retrieves and stores the information extracted from the xml element. */
270 virtual void Extract(xml::Element* el) { }
271
272 /*
273 * Retrieves a configuration value of the resource entry that best matches the specified
274 * configuration.
275 */
276 static Value* BestConfigValue(ResourceEntry* entry,
277 const ConfigDescription& match) {
278 if (!entry) {
279 return nullptr;
280 }
281
282 // Determine the config that best matches the desired config
283 ResourceConfigValue* best_value = nullptr;
284 for (auto& value : entry->values) {
285 if (!value->config.match(match)) {
286 continue;
287 }
288
289 if (best_value != nullptr) {
290 if (!value->config.isBetterThan(best_value->config, &match)) {
291 if (value->config.compare(best_value->config) != 0) {
292 continue;
293 }
294 }
295 }
296
297 best_value = value.get();
298 }
299
300 // The entry has no values
301 if (!best_value) {
302 return nullptr;
303 }
304
305 return best_value->value.get();
306 }
307
308 /** Retrieves the resource assigned to the specified resource id if one exists. */
309 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700310 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700311 if (table) {
312 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700313 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700314 for (auto& entry : type->entries) {
315 if (entry->id && entry->id.value() == res_id.id) {
316 if (auto value = BestConfigValue(entry.get(), config)) {
317 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700318 }
319 }
320 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700321 }
322 }
323 }
324 return nullptr;
325 }
326
327 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700328 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700329 const int kMaxIterations = 40;
330 int i = 0;
331 while (ref && ref->id && i++ < kMaxIterations) {
332 auto table = extractor_->apk_->GetResourceTable();
333 if (auto value = FindValueById(table, ref->id.value(), config)) {
334 if (ValueCast<Reference>(value)) {
335 ref = ValueCast<Reference>(value);
336 } else {
337 return value;
338 }
339 }
340 }
341 return nullptr;
342 }
343
344 /**
345 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
346 * this will attempt to resolve the reference to an integer value.
347 **/
348 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700349 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700350 if (attr != nullptr) {
351 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700352 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700353 Value* value = attr->compiled_value.get();
354 if (ValueCast<Reference>(value)) {
355 value = ResolveReference(ValueCast<Reference>(value), config);
356 } else {
357 value = attr->compiled_value.get();
358 }
359 // Retrieve the integer data if possible
360 if (value != nullptr) {
361 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
362 return (int32_t*) &intValue->value.data;
363 }
364 }
365 }
366 }
367 return nullptr;
368 }
369
370 /**
371 * A version of GetAttributeInteger that returns a default integer if the attribute does not
372 * exist or cannot be resolved to an integer value.
373 **/
374 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700375 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700376 auto value = GetAttributeInteger(attr, config);
377 if (value) {
378 return *value;
379 }
380 return def;
381 }
382
383 /**
384 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
385 * this will attempt to resolve the reference to a string value.
386 **/
387 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700388 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700389 if (attr != nullptr) {
390 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700391 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700392 Value* value = attr->compiled_value.get();
393 if (ValueCast<Reference>(value)) {
394 value = ResolveReference(ValueCast<Reference>(value), config);
395 } else {
396 value = attr->compiled_value.get();
397 }
398
399 // Retrieve the string data of the value if possible
400 if (value != nullptr) {
401 if (String* intValue = ValueCast<String>(value)) {
402 return &(*intValue->value);
403 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
404 return &(*rawValue->value);
Iurii Makhno32c0c032022-12-08 18:07:58 +0000405 } else if (StyledString* styledStrValue = ValueCast<StyledString>(value)) {
406 return &(styledStrValue->value->value);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700407 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
408 return &(*strValue->path);
409 }
410 }
411 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700412
413 if (!attr->value.empty()) {
414 return &attr->value;
415 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700416 }
417 return nullptr;
418 }
419
420 /**
421 * A version of GetAttributeString that returns a default string if the attribute does not
422 * exist or cannot be resolved to an string value.
423 **/
424 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700425 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700426 auto value = GetAttributeString(attr, config);
427 if (value) {
428 return *value;
429 }
430 return def;
431 }
432
433 private:
434 ManifestExtractor* extractor_;
435 std::vector<std::unique_ptr<Element>> children_;
436 std::string tag_;
Iurii Makhno32c0c032022-12-08 18:07:58 +0000437 bool featured_ = false;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700438 };
439
440 friend Element;
441
442 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700443 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700444 ConfigDescription config;
445 config.orientation = android::ResTable_config::ORIENTATION_PORT;
446 config.density = android::ResTable_config::DENSITY_MEDIUM;
Jackal Guo201a60a2021-08-31 12:37:30 +0800447 config.sdkVersion = SDK_CUR_DEVELOPMENT; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700448 config.screenWidthDp = 320;
449 config.screenHeightDp = 480;
450 config.smallestScreenWidthDp = 320;
451 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
452 return config;
453 }
454
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000455 bool Extract(android::IDiagnostics* diag);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000456 bool Dump(text::Printer* printer);
Iurii Makhno85875a82022-04-26 15:30:01 +0000457 bool DumpProto(pb::Badging* out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700458
459 /** Recursively visit the xml element tree and return a processed badging element tree. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000460 std::unique_ptr<Element> Visit(xml::Element* element, const std::string& parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700461
Iurii Makhno05fd9292022-08-24 18:56:12 +0000462 /** Resets target SDK to 0. */
463 void ResetTargetSdk() {
464 target_sdk_ = 0;
465 }
466
467 /** Raises the target sdk value if the min target is greater than the current target. */
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700468 void RaiseTargetSdk(int32_t min_target) {
469 if (min_target > target_sdk_) {
470 target_sdk_ = min_target;
471 }
472 }
473
474 /**
475 * Retrieves the default feature group that features are added into when <uses-feature>
476 * are not in a <feature-group> element.
477 **/
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000478 CommonFeatureGroup* common_feature_group() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700479 return commonFeatureGroup_.get();
480 }
481
482 /**
483 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
484 * used for that density setting.
485 **/
486 const std::map<uint16_t, ConfigDescription> densities() const {
487 return densities_;
488 }
489
490 /**
491 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
492 * would be used for that locale setting.
493 **/
494 const std::map<std::string, ConfigDescription> locales() const {
495 return locales_;
496 }
497
498 /** Retrieves the current stack of parent during data extraction. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000499 const std::vector<Element*>& parent_stack() const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700500 return parent_stack_;
501 }
502
503 int32_t target_sdk() const {
504 return target_sdk_;
505 }
506
507 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700508 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700509
510 private:
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000511 std::unique_ptr<xml::XmlResource> doc_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700512 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
513 std::map<std::string, ConfigDescription> locales_;
514 std::map<uint16_t, ConfigDescription> densities_;
515 std::vector<Element*> parent_stack_;
516 int32_t target_sdk_ = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000517
518 std::unique_ptr<ManifestExtractor::Element> root_element_;
519 std::vector<std::unique_ptr<ManifestExtractor::Element>> implied_permissions_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000520 std::vector<FeatureGroup*> feature_groups_;
Iurii Makhno85875a82022-04-26 15:30:01 +0000521 Components components_;
522 Architectures architectures_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000523 const SupportsScreen* supports_screen_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700524};
525
526template<typename T> T* ElementCast(ManifestExtractor::Element* element);
527
528/** Recurs through the children of the specified root in depth-first order. */
529static void ForEachChild(ManifestExtractor::Element* root,
530 std::function<void(ManifestExtractor::Element*)> f) {
531 for (auto& child : root->children()) {
532 f(child.get());
533 ForEachChild(child.get(), f);
534 }
535}
536
537/**
538 * Checks the element and its recursive children for an element that makes the specified
539 * conditional function return true. Returns the first element that makes the conditional function
540 * return true.
541 **/
542static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
543 std::function<bool(ManifestExtractor::Element*)> f) {
544 if (f(root)) {
545 return root;
546 }
Iurii Makhno32c0c032022-12-08 18:07:58 +0000547 const auto& children = root->children();
548 for (auto it = children.rbegin(); it != children.rend(); ++it) {
549 if (auto b2 = FindElement(it->get(), f)) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700550 return b2;
551 }
552 }
553 return nullptr;
554}
555
556/** Represents the <manifest> elements **/
557class Manifest : public ManifestExtractor::Element {
558 public:
559 Manifest() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000560 bool only_package_name;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700561 std::string package;
562 int32_t versionCode;
563 std::string versionName;
564 const std::string* split = nullptr;
565 const std::string* platformVersionName = nullptr;
566 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700567 const int32_t* platformVersionNameInt = nullptr;
568 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700569 const int32_t* compilesdkVersion = nullptr;
570 const std::string* compilesdkVersionCodename = nullptr;
571 const int32_t* installLocation = nullptr;
572
573 void Extract(xml::Element* manifest) override {
574 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
575 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
576 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
577 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
578
579 // Extract the platform build info
580 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
581 "platformBuildVersionName"));
582 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
583 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700584 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
585 "platformBuildVersionName"));
586 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
587 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700588
589 // Extract the compile sdk info
590 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
591 compilesdkVersionCodename = GetAttributeString(
592 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
593 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
594 }
595
Iurii Makhno85875a82022-04-26 15:30:01 +0000596 void ToProto(pb::Badging* out_badging) override {
597 auto out_package = out_badging->mutable_package();
598 out_package->set_package(package);
599 out_package->set_version_code(versionCode);
600 out_package->set_version_name(versionName);
601 if (compilesdkVersion) {
602 out_package->set_compile_sdk_version(*compilesdkVersion);
603 }
604 if (compilesdkVersionCodename) {
605 out_package->set_compile_sdk_version_codename(*compilesdkVersionCodename);
606 }
607 if (platformVersionName) {
608 out_package->set_platform_version_name(*platformVersionName);
609 } else if (platformVersionNameInt) {
610 out_package->set_platform_version_name(std::to_string(*platformVersionNameInt));
611 }
612 if (platformVersionCode) {
613 out_package->set_platform_version_code(*platformVersionCode);
614 } else if (platformVersionCodeInt) {
615 out_package->set_platform_version_code(std::to_string(*platformVersionCodeInt));
616 }
617
618 if (installLocation) {
619 switch (*installLocation) {
620 case 0:
621 out_package->set_install_location(pb::PackageInfo_InstallLocation_AUTO);
622 break;
623 case 1:
624 out_package->set_install_location(pb::PackageInfo_InstallLocation_INTERNAL_ONLY);
625 break;
626 case 2:
627 out_package->set_install_location(pb::PackageInfo_InstallLocation_PREFER_EXTERNAL);
628 break;
629 default:
630 break;
631 }
632 }
633 }
634
Ryan Mitchell214846d2018-09-19 16:57:01 -0700635 void Print(text::Printer* printer) override {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000636 if (only_package_name) {
637 printer->Println(StringPrintf("package: %s", package.data()));
638 } else {
639 PrintFull(printer);
640 }
641 }
642
643 void PrintFull(text::Printer* printer) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700644 printer->Print(StringPrintf("package: name='%s' ", package.data()));
645 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700646 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700647 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700648
649 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700650 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700651 }
652 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700653 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700654 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700655 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
656 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700657 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700658 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700659 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700660 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
661 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700662 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700663 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700664 }
665 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700666 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700667 compilesdkVersionCodename->data()));
668 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700669 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700670
671 if (installLocation) {
672 switch (*installLocation) {
673 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700674 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700675 break;
676 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700677 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700678 break;
679 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700680 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700681 break;
682 default:
683 break;
684 }
685 }
686 }
687};
688
689/** Represents <application> elements. **/
690class Application : public ManifestExtractor::Element {
691 public:
692 Application() = default;
693 std::string label;
694 std::string icon;
695 std::string banner;
696 int32_t is_game;
697 int32_t debuggable;
698 int32_t test_only;
699 bool has_multi_arch;
700
701 /** Mapping from locales to app names. */
702 std::map<std::string, std::string> locale_labels;
703
704 /** Mapping from densities to app icons. */
705 std::map<uint16_t, std::string> density_icons;
706
707 void Extract(xml::Element* element) override {
708 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
709 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
710 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
711 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
712 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
713 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
714
715 // We must search by name because the multiArch flag hasn't been API
716 // frozen yet.
717 has_multi_arch = (GetAttributeIntegerDefault(
718 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
719
720 // Retrieve the app names for every locale the app supports
721 auto attr = FindAttribute(element, LABEL_ATTR);
722 for (auto& config : extractor()->locales()) {
723 if (auto label = GetAttributeString(attr, config.second)) {
724 if (label) {
725 locale_labels.insert(std::make_pair(config.first, *label));
726 }
727 }
728 }
729
730 // Retrieve the icons for the densities the app supports
731 attr = FindAttribute(element, ICON_ATTR);
732 for (auto& config : extractor()->densities()) {
733 if (auto resource = GetAttributeString(attr, config.second)) {
734 if (resource) {
735 density_icons.insert(std::make_pair(config.first, *resource));
736 }
737 }
738 }
739 }
740
Ryan Mitchell214846d2018-09-19 16:57:01 -0700741 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700742 // Print the labels for every locale
743 for (auto p : locale_labels) {
744 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700745 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700746 android::ResTable::normalizeForOutput(p.second.data())
747 .c_str()));
748 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700749 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700750 android::ResTable::normalizeForOutput(p.second.data())
751 .c_str()));
752 }
753 }
754
755 // Print the icon paths for every density
756 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700757 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700758 }
759
760 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700761 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700762 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700763 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700764 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700765 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700766 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700767 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700768
769 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700770 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700771 }
772 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700773 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700774 }
775 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700776 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700777 }
778 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000779
780 void ToProto(pb::Badging* out_badging) override {
781 auto application = out_badging->mutable_application();
782 application->set_label(android::ResTable::normalizeForOutput(label.data()));
783 application->set_icon(icon);
784 application->set_banner(banner);
785 application->set_test_only(test_only != 0);
786 application->set_game(is_game != 0);
787 application->set_debuggable(debuggable != 0);
788
789 auto out_locale_labels = application->mutable_locale_labels();
790 for (auto& p : locale_labels) {
791 if (!p.first.empty()) {
792 (*out_locale_labels)[p.first] = p.second;
793 }
794 }
795 auto out_density_icons = application->mutable_density_icons();
796 for (auto& p : density_icons) {
797 (*out_density_icons)[p.first] = p.second;
798 }
799 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700800};
801
802/** Represents <uses-sdk> elements. **/
803class UsesSdkBadging : public ManifestExtractor::Element {
804 public:
805 UsesSdkBadging() = default;
806 const int32_t* min_sdk = nullptr;
807 const std::string* min_sdk_name = nullptr;
808 const int32_t* max_sdk = nullptr;
809 const int32_t* target_sdk = nullptr;
810 const std::string* target_sdk_name = nullptr;
811
812 void Extract(xml::Element* element) override {
813 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
814 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
815 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
816 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
817 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
818
Iurii Makhno05fd9292022-08-24 18:56:12 +0000819 // Resets target SDK first. This is required if APK contains multiple <uses-sdk> elements,
820 // we only need to take the latest values.
821 extractor()->ResetTargetSdk();
822
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700823 // Detect the target sdk of the element
824 if ((min_sdk_name && *min_sdk_name == "Donut")
825 || (target_sdk_name && *target_sdk_name == "Donut")) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800826 extractor()->RaiseTargetSdk(SDK_DONUT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700827 }
828 if (min_sdk) {
829 extractor()->RaiseTargetSdk(*min_sdk);
830 }
831 if (target_sdk) {
832 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700833 } else if (target_sdk_name) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800834 extractor()->RaiseTargetSdk(SDK_CUR_DEVELOPMENT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700835 }
836 }
837
Ryan Mitchell214846d2018-09-19 16:57:01 -0700838 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700839 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700840 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700841 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700842 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700843 }
844 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700845 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700846 }
847 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700848 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700849 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700850 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700851 }
852 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000853
854 void ToProto(pb::Badging* out_badging) override {
855 auto out_sdks = out_badging->mutable_uses_sdk();
856 if (min_sdk) {
857 out_sdks->set_min_sdk_version(*min_sdk);
858 } else if (min_sdk_name) {
859 out_sdks->set_min_sdk_version_name(*min_sdk_name);
860 }
861 if (max_sdk) {
862 out_sdks->set_max_sdk_version(*max_sdk);
863 }
864 if (target_sdk) {
865 out_sdks->set_target_sdk_version(*target_sdk);
866 } else if (target_sdk_name) {
867 out_sdks->set_target_sdk_version_name(*target_sdk_name);
868 }
869 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700870};
871
872/** Represents <uses-configuration> elements. **/
873class UsesConfiguarion : public ManifestExtractor::Element {
874 public:
875 UsesConfiguarion() = default;
876 int32_t req_touch_screen = 0;
877 int32_t req_keyboard_type = 0;
878 int32_t req_hard_keyboard = 0;
879 int32_t req_navigation = 0;
880 int32_t req_five_way_nav = 0;
881
882 void Extract(xml::Element* element) override {
883 req_touch_screen = GetAttributeIntegerDefault(
884 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
885 req_keyboard_type = GetAttributeIntegerDefault(
886 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
887 req_hard_keyboard = GetAttributeIntegerDefault(
888 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
889 req_navigation = GetAttributeIntegerDefault(
890 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
891 req_five_way_nav = GetAttributeIntegerDefault(
892 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
893 }
894
Ryan Mitchell214846d2018-09-19 16:57:01 -0700895 void Print(text::Printer* printer) override {
896 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700897 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700898 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700899 }
900 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700901 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700902 }
903 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700904 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700905 }
906 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700907 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700908 }
909 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700910 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700911 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700912 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700913 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000914
915 void ToProto(pb::Badging* out_badging) override {
Iurii Makhno4480a662022-12-08 18:52:30 +0000916 auto out_configuration = out_badging->add_uses_configurations();
Iurii Makhno85875a82022-04-26 15:30:01 +0000917 out_configuration->set_req_touch_screen(req_touch_screen);
918 out_configuration->set_req_keyboard_type(req_keyboard_type);
919 out_configuration->set_req_hard_keyboard(req_hard_keyboard);
920 out_configuration->set_req_navigation(req_navigation);
921 out_configuration->set_req_five_way_nav(req_five_way_nav);
922 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700923};
924
925/** Represents <supports-screen> elements. **/
926class SupportsScreen : public ManifestExtractor::Element {
927 public:
928 SupportsScreen() = default;
929 int32_t small_screen = 1;
930 int32_t normal_screen = 1;
931 int32_t large_screen = 1;
932 int32_t xlarge_screen = 1;
933 int32_t any_density = 1;
934 int32_t requires_smallest_width_dp = 0;
935 int32_t compatible_width_limit_dp = 0;
936 int32_t largest_width_limit_dp = 0;
937
938 void Extract(xml::Element* element) override {
939 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
940 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
941 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
942 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
943 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
944
945 requires_smallest_width_dp = GetAttributeIntegerDefault(
946 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
947 compatible_width_limit_dp = GetAttributeIntegerDefault(
948 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
949 largest_width_limit_dp = GetAttributeIntegerDefault(
950 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
951
952 // For modern apps, if screen size buckets haven't been specified
953 // but the new width ranges have, then infer the buckets from them.
954 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
955 && requires_smallest_width_dp > 0) {
956 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
957 : requires_smallest_width_dp;
958 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
959 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
960 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
961 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
962 }
963 }
964
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000965 void PrintScreens(text::Printer* printer, int32_t target_sdk) const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700966 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700967 printer->Print("supports-screens:");
Iurii Makhno85875a82022-04-26 15:30:01 +0000968 if (IsSmallScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700969 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700970 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000971 if (normal_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700972 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700973 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000974 if (IsLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700975 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700976 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000977 if (IsXLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700978 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700979 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700980 printer->Print("\n");
981 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Iurii Makhno85875a82022-04-26 15:30:01 +0000982 (IsAnyDensitySupported(target_sdk)) ? "true" : "false"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700983 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700984 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700985 }
986 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700987 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700988 }
989 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700990 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700991 }
992 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000993
994 void ToProtoScreens(pb::Badging* out_badging, int32_t target_sdk) const {
995 auto supports_screen = out_badging->mutable_supports_screen();
996 if (IsSmallScreenSupported(target_sdk)) {
997 supports_screen->add_screens(pb::SupportsScreen_ScreenType_SMALL);
998 }
999 if (normal_screen != 0) {
1000 supports_screen->add_screens(pb::SupportsScreen_ScreenType_NORMAL);
1001 }
1002 if (IsLargeScreenSupported(target_sdk)) {
1003 supports_screen->add_screens(pb::SupportsScreen_ScreenType_LARGE);
1004 }
1005 if (IsXLargeScreenSupported(target_sdk)) {
1006 supports_screen->add_screens(pb::SupportsScreen_ScreenType_XLARGE);
1007 }
1008 supports_screen->set_supports_any_densities(IsAnyDensitySupported(target_sdk));
1009 supports_screen->set_requires_smallest_width_dp(requires_smallest_width_dp);
1010 supports_screen->set_compatible_width_limit_dp(compatible_width_limit_dp);
1011 supports_screen->set_largest_width_limit_dp(largest_width_limit_dp);
1012 }
1013
1014 private:
1015 // Determine default values for any unspecified screen sizes,
1016 // based on the target SDK of the package. As of 4 (donut)
1017 // the screen size support was introduced, so all default to
1018 // enabled.
1019 bool IsSmallScreenSupported(int32_t target_sdk) const {
1020 if (small_screen > 0) {
1021 return target_sdk >= SDK_DONUT;
1022 }
1023 return small_screen != 0;
1024 }
1025
1026 bool IsLargeScreenSupported(int32_t target_sdk) const {
1027 if (large_screen > 0) {
1028 return target_sdk >= SDK_DONUT;
1029 }
1030 return large_screen != 0;
1031 }
1032
1033 bool IsXLargeScreenSupported(int32_t target_sdk) const {
1034 if (xlarge_screen > 0) {
1035 return target_sdk >= SDK_GINGERBREAD;
1036 }
1037 return xlarge_screen != 0;
1038 }
1039
1040 bool IsAnyDensitySupported(int32_t target_sdk) const {
1041 if (any_density > 0) {
1042 return target_sdk >= SDK_DONUT || requires_smallest_width_dp > 0 ||
1043 compatible_width_limit_dp > 0;
1044 }
1045 return any_density != 0;
1046 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001047};
1048
1049/** Represents <feature-group> elements. **/
1050class FeatureGroup : public ManifestExtractor::Element {
1051 public:
1052 FeatureGroup() = default;
1053 std::string label;
1054 int32_t open_gles_version = 0;
1055
1056 void Extract(xml::Element* element) override {
1057 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1058 }
1059
Ryan Mitchell214846d2018-09-19 16:57:01 -07001060 virtual void PrintGroup(text::Printer* printer) {
1061 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001062 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001063 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001064 }
1065
1066 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001067 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001068 (feature.second.required ? "" : "-not-required"),
1069 feature.first.data()));
1070 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001071 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001072 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001073 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001074 }
1075 }
1076
Iurii Makhno85875a82022-04-26 15:30:01 +00001077 virtual void GroupToProto(pb::Badging* out_badging) {
1078 auto feature_group = out_badging->add_feature_groups();
1079 feature_group->set_label(label);
1080 feature_group->set_open_gles_version(open_gles_version);
1081 for (auto& feature : features_) {
1082 auto out_feature = feature_group->add_features();
1083 out_feature->set_name(feature.first);
1084 out_feature->set_required(feature.second.required);
1085 out_feature->set_version(feature.second.version);
1086 }
1087 }
1088
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001089 /** Adds a feature to the feature group. */
1090 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
Iurii Makhno0df4e6d2022-11-29 19:19:13 +00001091 features_.insert_or_assign(name, Feature{required, version});
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001092 if (required) {
1093 if (name == "android.hardware.camera.autofocus" ||
1094 name == "android.hardware.camera.flash") {
1095 AddFeature("android.hardware.camera", true);
1096 } else if (name == "android.hardware.location.gps" ||
1097 name == "android.hardware.location.network") {
1098 AddFeature("android.hardware.location", true);
1099 } else if (name == "android.hardware.faketouch.multitouch") {
1100 AddFeature("android.hardware.faketouch", true);
1101 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
1102 name == "android.hardware.faketouch.multitouch.jazzhands") {
1103 AddFeature("android.hardware.faketouch.multitouch", true);
1104 AddFeature("android.hardware.faketouch", true);
1105 } else if (name == "android.hardware.touchscreen.multitouch") {
1106 AddFeature("android.hardware.touchscreen", true);
1107 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
1108 name == "android.hardware.touchscreen.multitouch.jazzhands") {
1109 AddFeature("android.hardware.touchscreen.multitouch", true);
1110 AddFeature("android.hardware.touchscreen", true);
1111 } else if (name == "android.hardware.opengles.aep") {
1112 const int kOpenGLESVersion31 = 0x00030001;
1113 if (kOpenGLESVersion31 > open_gles_version) {
1114 open_gles_version = kOpenGLESVersion31;
1115 }
1116 }
1117 }
1118 }
1119
1120 /** Returns true if the feature group has the given feature. */
1121 virtual bool HasFeature(const std::string& name) {
1122 return features_.find(name) != features_.end();
1123 }
1124
1125 /** Merges the features of another feature group into this group. */
1126 void Merge(FeatureGroup* group) {
1127 open_gles_version = std::max(open_gles_version, group->open_gles_version);
1128 for (auto& feature : group->features_) {
1129 features_.insert(feature);
1130 }
1131 }
1132
1133 protected:
1134 struct Feature {
1135 public:
1136 bool required = false;
1137 int32_t version = -1;
1138 };
1139
1140 /* Mapping of feature names to their properties. */
1141 std::map<std::string, Feature> features_;
1142};
1143
1144/**
1145 * Represents the default feature group for the application if no <feature-group> elements are
1146 * present in the manifest.
1147 **/
1148class CommonFeatureGroup : public FeatureGroup {
1149 public:
1150 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001151 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001152 FeatureGroup::PrintGroup(printer);
1153
1154 // Also print the implied features
1155 for (auto feature : implied_features_) {
1156 if (features_.find(feature.first) == features_.end()) {
1157 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -07001158 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
1159 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001160 feature.first.data()));
1161
1162 // Print the reasons as a sentence
1163 size_t count = 0;
1164 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001165 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001166 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001167 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001168 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001169 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001170 }
1171 count++;
1172 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001173 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001174 }
1175 }
1176 }
1177
Iurii Makhno85875a82022-04-26 15:30:01 +00001178 virtual void GroupToProto(pb::Badging* out_badging) override {
1179 FeatureGroup::GroupToProto(out_badging);
1180 auto feature_group =
1181 out_badging->mutable_feature_groups(out_badging->feature_groups_size() - 1);
1182 for (auto& feature : implied_features_) {
1183 if (features_.find(feature.first) == features_.end()) {
1184 auto out_feature = feature_group->add_features();
1185 out_feature->set_name(feature.first);
1186 auto implied_data = out_feature->mutable_implied_data();
1187 implied_data->set_from_sdk_23_permission(feature.second.implied_from_sdk_k23);
1188 for (auto& reason : feature.second.reasons) {
1189 implied_data->add_reasons(reason);
1190 }
1191 }
1192 }
1193 }
1194
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001195 /** Returns true if the feature group has the given feature. */
1196 bool HasFeature(const std::string& name) override {
1197 return FeatureGroup::HasFeature(name)
1198 || implied_features_.find(name) != implied_features_.end();
1199 }
1200
1201 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
1202 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
1203 auto entry = implied_features_.find(name);
1204 if (entry == implied_features_.end()) {
1205 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
1206 entry = implied_features_.find(name);
1207 }
1208
1209 // A non-sdk 23 implied feature takes precedence.
1210 if (entry->second.implied_from_sdk_k23 && !sdk23) {
1211 entry->second.implied_from_sdk_k23 = false;
1212 }
1213
1214 entry->second.reasons.insert(reason);
1215 }
1216
1217 /**
1218 * Adds a feature to a set of implied features for all features that are implied by the presence
1219 * of the permission.
1220 **/
1221 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
1222 if (name == "android.permission.CAMERA") {
1223 addImpliedFeature("android.hardware.camera",
1224 StringPrintf("requested %s permission", name.data()),
1225 sdk23);
1226
1227 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
1228 if (targetSdk < SDK_LOLLIPOP) {
1229 addImpliedFeature("android.hardware.location.gps",
1230 StringPrintf("requested %s permission", name.data()),
1231 sdk23);
1232 addImpliedFeature("android.hardware.location.gps",
1233 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1234 sdk23);
1235 }
1236 addImpliedFeature("android.hardware.location",
1237 StringPrintf("requested %s permission", name.data()),
1238 sdk23);
1239
1240 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
1241 if (targetSdk < SDK_LOLLIPOP) {
1242 addImpliedFeature("android.hardware.location.network",
1243 StringPrintf("requested %s permission", name.data()),
1244 sdk23);
1245 addImpliedFeature("android.hardware.location.network",
1246 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1247 sdk23);
1248 }
1249 addImpliedFeature("android.hardware.location",
1250 StringPrintf("requested %s permission", name.data()),
1251 sdk23);
1252
1253 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
1254 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
1255 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
1256 addImpliedFeature("android.hardware.location",
1257 StringPrintf("requested %s permission", name.data()),
1258 sdk23);
1259
1260 } else if (name == "android.permission.BLUETOOTH" ||
1261 name == "android.permission.BLUETOOTH_ADMIN") {
1262 if (targetSdk > SDK_DONUT) {
1263 addImpliedFeature("android.hardware.bluetooth",
1264 StringPrintf("requested %s permission", name.data()),
1265 sdk23);
1266 addImpliedFeature("android.hardware.bluetooth",
1267 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
1268 sdk23);
1269 }
1270
1271 } else if (name == "android.permission.RECORD_AUDIO") {
1272 addImpliedFeature("android.hardware.microphone",
1273 StringPrintf("requested %s permission", name.data()),
1274 sdk23);
1275
1276 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
1277 name == "android.permission.CHANGE_WIFI_STATE" ||
1278 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
1279 addImpliedFeature("android.hardware.wifi",
1280 StringPrintf("requested %s permission", name.data()),
1281 sdk23);
1282
1283 } else if (name == "android.permission.CALL_PHONE" ||
1284 name == "android.permission.CALL_PRIVILEGED" ||
1285 name == "android.permission.MODIFY_PHONE_STATE" ||
1286 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1287 name == "android.permission.READ_SMS" ||
1288 name == "android.permission.RECEIVE_SMS" ||
1289 name == "android.permission.RECEIVE_MMS" ||
1290 name == "android.permission.RECEIVE_WAP_PUSH" ||
1291 name == "android.permission.SEND_SMS" ||
1292 name == "android.permission.WRITE_APN_SETTINGS" ||
1293 name == "android.permission.WRITE_SMS") {
1294 addImpliedFeature("android.hardware.telephony",
1295 "requested a telephony permission",
1296 sdk23);
1297 }
1298 }
1299
1300 private:
1301 /**
1302 * Represents a feature that has been automatically added due to a pre-requisite or for some
1303 * other reason.
1304 */
1305 struct ImpliedFeature {
1306 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1307
1308 /** List of human-readable reasons for why this feature was implied. */
1309 std::set<std::string> reasons;
1310
1311 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1312 bool implied_from_sdk_k23;
1313 };
1314
1315 /* Mapping of implied feature names to their properties. */
1316 std::map<std::string, ImpliedFeature> implied_features_;
1317};
1318
1319/** Represents <uses-feature> elements. **/
1320class UsesFeature : public ManifestExtractor::Element {
1321 public:
1322 UsesFeature() = default;
1323 void Extract(xml::Element* element) override {
1324 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1325 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1326 bool required = GetAttributeIntegerDefault(
1327 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1328 int32_t version = GetAttributeIntegerDefault(
1329 FindAttribute(element, kAndroidNamespace, "version"), 0);
1330
1331 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1332 // common feature group
1333 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1334 if (!feature_group) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001335 feature_group = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001336 } else {
1337 // All features in side of <feature-group> elements are required.
1338 required = true;
1339 }
1340
1341 if (name) {
1342 feature_group->AddFeature(*name, required, version);
1343 } else if (gl) {
1344 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1345 }
1346 }
1347};
1348
1349/** Represents <uses-permission> elements. **/
1350class UsesPermission : public ManifestExtractor::Element {
1351 public:
1352 UsesPermission() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001353 bool implied;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001354 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001355 std::vector<std::string> requiredFeatures;
1356 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001357 int32_t required = true;
1358 int32_t maxSdkVersion = -1;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001359 int32_t usesPermissionFlags = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001360 std::string impliedReason;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001361
1362 void Extract(xml::Element* element) override {
1363 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001364 std::string feature =
1365 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1366 if (!feature.empty()) {
1367 requiredFeatures.push_back(feature);
1368 }
1369 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1370 if (!feature.empty()) {
1371 requiredNotFeatures.push_back(feature);
1372 }
1373
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001374 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1375 maxSdkVersion = GetAttributeIntegerDefault(
1376 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001377 usesPermissionFlags = GetAttributeIntegerDefault(
1378 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001379
1380 if (!name.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001381 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001382 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1383 }
1384 }
1385
Ryan Mitchell214846d2018-09-19 16:57:01 -07001386 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001387 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001388 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001389 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001390 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001391 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001392 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1393 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1394 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001395 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001396 for (const std::string& requiredFeature : requiredFeatures) {
1397 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1398 }
1399 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1400 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1401 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001402 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001403 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001404 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001405 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001406 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001407 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1408 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1409 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001410 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001411 }
1412 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001413 if (implied) {
1414 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
1415 if (maxSdkVersion >= 0) {
1416 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
1417 }
1418 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1419 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1420 }
1421 printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001422 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001423 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001424
1425 void ToProto(pb::Badging* out_badging) override {
1426 if (!name.empty()) {
1427 auto permission = out_badging->add_uses_permissions();
1428 permission->set_name(name);
1429 if (maxSdkVersion > 0) {
1430 permission->set_max_sdk_version(maxSdkVersion);
1431 }
1432 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1433 permission->mutable_permission_flags()->set_never_for_location(true);
1434 }
1435 for (auto& requiredFeature : requiredFeatures) {
1436 permission->add_required_features(requiredFeature);
1437 }
1438 for (auto& requiredNotFeature : requiredNotFeatures) {
1439 permission->add_required_not_features(requiredNotFeature);
1440 }
1441 permission->set_required(required != 0);
1442 permission->set_implied(implied);
1443 }
1444 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001445};
1446
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001447/** Represents <required-feature> elements. **/
1448class RequiredFeature : public ManifestExtractor::Element {
1449 public:
1450 RequiredFeature() = default;
1451 std::string name;
1452
1453 void Extract(xml::Element* element) override {
1454 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1455 auto parent_stack = extractor()->parent_stack();
1456 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1457 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1458 uses_permission->requiredFeatures.push_back(name);
1459 }
1460 }
1461};
1462
1463/** Represents <required-not-feature> elements. **/
1464class RequiredNotFeature : public ManifestExtractor::Element {
1465 public:
1466 RequiredNotFeature() = default;
1467 std::string name;
1468
1469 void Extract(xml::Element* element) override {
1470 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1471 auto parent_stack = extractor()->parent_stack();
1472 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1473 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1474 uses_permission->requiredNotFeatures.push_back(name);
1475 }
1476 }
1477};
1478
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001479/** Represents <uses-permission-sdk-23> elements. **/
1480class UsesPermissionSdk23 : public ManifestExtractor::Element {
1481 public:
1482 UsesPermissionSdk23() = default;
1483 const std::string* name = nullptr;
1484 const int32_t* maxSdkVersion = nullptr;
1485
1486 void Extract(xml::Element* element) override {
1487 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1488 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1489
1490 if (name) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001491 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001492 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1493 }
1494 }
1495
Ryan Mitchell214846d2018-09-19 16:57:01 -07001496 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001497 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001498 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001499 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001500 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001501 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001502 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001503 }
1504 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001505
1506 void ToProto(pb::Badging* out_badging) override {
1507 if (name) {
1508 auto permission = out_badging->add_uses_permissions();
1509 permission->set_sdk23_and_above(true);
1510 permission->set_name(*name);
1511 if (maxSdkVersion) {
1512 permission->set_max_sdk_version(*maxSdkVersion);
1513 }
1514 }
1515 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001516};
1517
1518/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1519class Permission : public ManifestExtractor::Element {
1520 public:
1521 Permission() = default;
1522 std::string name;
1523
1524 void Extract(xml::Element* element) override {
1525 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1526 }
1527
Ryan Mitchell214846d2018-09-19 16:57:01 -07001528 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001529 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001530 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001531 }
1532 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001533
1534 void ToProto(pb::Badging* out_badging) override {
1535 if (!name.empty()) {
1536 out_badging->add_permissions()->set_name(name);
1537 }
1538 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001539};
1540
1541/** Represents <activity> elements. **/
1542class Activity : public ManifestExtractor::Element {
1543 public:
1544 Activity() = default;
1545 std::string name;
1546 std::string icon;
1547 std::string label;
1548 std::string banner;
1549
1550 bool has_component_ = false;
1551 bool has_launcher_category = false;
1552 bool has_leanback_launcher_category = false;
1553 bool has_main_action = false;
1554
1555 void Extract(xml::Element* element) override {
1556 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1557 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1558 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1559 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1560
1561 // Retrieve the package name from the manifest
1562 std::string package;
1563 for (auto& parent : extractor()->parent_stack()) {
1564 if (auto manifest = ElementCast<Manifest>(parent)) {
1565 package = manifest->package;
1566 break;
1567 }
1568 }
1569
1570 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001571 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001572 if (idx == 0) {
1573 name = package + name;
1574 } else if (idx < 0) {
1575 name = package + "." + name;
1576 }
1577
1578 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1579 if (orientation) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001580 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001581 int orien = *orientation;
1582 if (orien == 0 || orien == 6 || orien == 8) {
1583 // Requests landscape, sensorLandscape, or reverseLandscape.
1584 common->addImpliedFeature("android.hardware.screen.landscape",
1585 "one or more activities have specified a landscape orientation",
1586 false);
1587 } else if (orien == 1 || orien == 7 || orien == 9) {
1588 // Requests portrait, sensorPortrait, or reversePortrait.
1589 common->addImpliedFeature("android.hardware.screen.portrait",
1590 "one or more activities have specified a portrait orientation",
1591 false);
1592 }
1593 }
1594 }
1595
Ryan Mitchell214846d2018-09-19 16:57:01 -07001596 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001597 // Print whether the activity has the HOME category and a the MAIN action
1598 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001599 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001600 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001601 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001602 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001603 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001604 android::ResTable::normalizeForOutput(label.data()).c_str(),
1605 icon.data()));
1606 }
1607
1608 // Print wether the activity has the HOME category and a the MAIN action
1609 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001610 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001611 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001612 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001613 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001614 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001615 android::ResTable::normalizeForOutput(label.data()).c_str(),
1616 icon.data(), banner.data()));
1617 }
1618 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001619
1620 void ToProto(pb::Badging* out_badging) override {
1621 if (has_main_action && has_launcher_category) {
1622 auto activity = out_badging->mutable_launchable_activity();
1623 activity->set_name(name);
1624 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1625 activity->set_icon(icon);
1626 }
1627 if (has_leanback_launcher_category) {
1628 auto activity = out_badging->mutable_leanback_launchable_activity();
1629 activity->set_name(name);
1630 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1631 activity->set_icon(icon);
1632 activity->set_banner(banner);
1633 }
1634 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001635};
1636
1637/** Represents <intent-filter> elements. */
1638class IntentFilter : public ManifestExtractor::Element {
1639 public:
1640 IntentFilter() = default;
1641};
1642
1643/** Represents <category> elements. */
1644class Category : public ManifestExtractor::Element {
1645 public:
1646 Category() = default;
1647 std::string component = "";
1648
1649 void Extract(xml::Element* element) override {
1650 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1651
1652 auto parent_stack = extractor()->parent_stack();
1653 if (category && ElementCast<IntentFilter>(parent_stack[0])
1654 && ElementCast<Activity>(parent_stack[1])) {
1655 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1656
1657 if (*category == "android.intent.category.LAUNCHER") {
1658 activity->has_launcher_category = true;
1659 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1660 activity->has_leanback_launcher_category = true;
1661 } else if (*category == "android.intent.category.HOME") {
1662 component = "launcher";
1663 }
1664 }
1665 }
1666};
1667
1668/**
1669 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1670 * elements nested within.
1671 **/
1672class Provider : public ManifestExtractor::Element {
1673 public:
1674 Provider() = default;
1675 bool has_required_saf_attributes = false;
1676
1677 void Extract(xml::Element* element) override {
1678 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1679 const int32_t* grant_uri_permissions = GetAttributeInteger(
1680 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1681 const std::string* permission = GetAttributeString(
1682 FindAttribute(element, PERMISSION_ATTR));
1683
1684 has_required_saf_attributes = ((exported && *exported != 0)
1685 && (grant_uri_permissions && *grant_uri_permissions != 0)
1686 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1687 }
1688};
1689
1690/** Represents <receiver> elements. **/
1691class Receiver : public ManifestExtractor::Element {
1692 public:
1693 Receiver() = 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 <service> elements. **/
1703class Service : public ManifestExtractor::Element {
1704 public:
1705 Service() = default;
1706 const std::string* permission = nullptr;
1707 bool has_component = false;
1708
1709 void Extract(xml::Element* element) override {
1710 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1711 }
1712};
1713
1714/** Represents <uses-library> elements. **/
1715class UsesLibrary : public ManifestExtractor::Element {
1716 public:
1717 UsesLibrary() = default;
1718 std::string name;
1719 int required;
1720
1721 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001722 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1723 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001724 }
1725
Ryan Mitchell214846d2018-09-19 16:57:01 -07001726 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001727 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001728 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001729 (required == 0) ? "-not-required" : "", name.data()));
1730 }
1731 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001732
1733 void ToProto(pb::Badging* out_badging) override {
1734 if (!name.empty()) {
1735 auto uses_library = out_badging->add_uses_libraries();
1736 uses_library->set_name(name);
1737 uses_library->set_required(required != 0);
1738 }
1739 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001740};
1741
Dianne Hackborn813d7502018-10-02 16:59:46 -07001742/** Represents <static-library> elements. **/
1743class StaticLibrary : public ManifestExtractor::Element {
1744 public:
1745 StaticLibrary() = default;
1746 std::string name;
1747 int version;
1748 int versionMajor;
1749
1750 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001751 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1752 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1753 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
Dianne Hackborn813d7502018-10-02 16:59:46 -07001754 }
1755
Ryan Mitchell214846d2018-09-19 16:57:01 -07001756 void Print(text::Printer* printer) override {
1757 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001758 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1759 name.data(), version, versionMajor));
1760 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001761
1762 void ToProto(pb::Badging* out_badging) override {
1763 auto static_library = out_badging->mutable_static_library();
1764 static_library->set_name(name);
1765 static_library->set_version(version);
1766 static_library->set_version_major(versionMajor);
1767 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001768};
1769
1770/** Represents <uses-static-library> elements. **/
1771class UsesStaticLibrary : public ManifestExtractor::Element {
1772 public:
1773 UsesStaticLibrary() = default;
1774 std::string name;
1775 int version;
1776 int versionMajor;
1777 std::vector<std::string> certDigests;
1778
1779 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001780 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1781 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1782 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1783 AddCertDigest(element);
Dianne Hackborn813d7502018-10-02 16:59:46 -07001784 }
1785
1786 void AddCertDigest(xml::Element* element) {
1787 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1788 // We allow ":" delimiters in the SHA declaration as this is the format
1789 // emitted by the certtool making it easy for developers to copy/paste.
1790 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1791 if (!digest.empty()) {
1792 certDigests.push_back(digest);
1793 }
1794 }
1795
Ryan Mitchell214846d2018-09-19 16:57:01 -07001796 void Print(text::Printer* printer) override {
1797 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001798 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1799 name.data(), version, versionMajor));
1800 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001801 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001802 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001803 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001804 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001805
1806 void ToProto(pb::Badging* out_badging) override {
1807 auto uses_static_library = out_badging->add_uses_static_libraries();
1808 uses_static_library->set_name(name);
1809 uses_static_library->set_version(version);
1810 uses_static_library->set_version_major(versionMajor);
1811 for (auto& cert : certDigests) {
1812 uses_static_library->add_certificates(cert);
1813 }
1814 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001815};
1816
Alex Buynytskyya16137052021-12-02 13:26:54 +00001817/** Represents <sdk-library> elements. **/
1818class SdkLibrary : public ManifestExtractor::Element {
1819 public:
1820 SdkLibrary() = default;
1821 std::string name;
1822 int versionMajor;
1823
1824 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001825 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1826 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
Alex Buynytskyya16137052021-12-02 13:26:54 +00001827 }
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 {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001850 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1851 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1852 AddCertDigest(element);
Alex Buynytskyya16137052021-12-02 13:26:54 +00001853 }
1854
1855 void AddCertDigest(xml::Element* element) {
1856 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1857 // We allow ":" delimiters in the SHA declaration as this is the format
1858 // emitted by the certtool making it easy for developers to copy/paste.
1859 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1860 if (!digest.empty()) {
1861 certDigests.push_back(digest);
1862 }
1863 }
1864
1865 void Print(text::Printer* printer) override {
1866 printer->Print(
1867 StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor));
1868 for (size_t i = 0; i < certDigests.size(); i++) {
1869 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
1870 }
1871 printer->Print("\n");
1872 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001873
1874 void ToProto(pb::Badging* out_badging) override {
1875 auto uses_sdk_library = out_badging->add_uses_sdk_libraries();
1876 uses_sdk_library->set_name(name);
1877 uses_sdk_library->set_version_major(versionMajor);
1878 for (auto& cert : certDigests) {
1879 uses_sdk_library->add_certificates(cert);
1880 }
1881 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001882};
1883
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001884/** Represents <uses-native-library> elements. **/
1885class UsesNativeLibrary : public ManifestExtractor::Element {
1886 public:
1887 UsesNativeLibrary() = default;
1888 std::string name;
1889 int required;
1890
1891 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001892 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1893 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001894 }
1895
1896 void Print(text::Printer* printer) override {
1897 if (!name.empty()) {
1898 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1899 (required == 0) ? "-not-required" : "", name.data()));
1900 }
1901 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001902
1903 void ToProto(pb::Badging* out_badging) override {
1904 if (!name.empty()) {
1905 auto uses_native_library = out_badging->add_uses_native_libraries();
1906 uses_native_library->set_name(name);
1907 uses_native_library->set_required(required != 0);
1908 }
1909 }
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001910};
1911
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001912/**
1913 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1914 * explicitly enable meta data printing.
1915 **/
1916class MetaData : public ManifestExtractor::Element {
1917 public:
1918 MetaData() = default;
1919 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001920 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001921 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001922 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001923 const int* resource_int;
1924
1925 void Extract(xml::Element* element) override {
1926 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001927 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001928 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001929 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001930 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1931 }
1932
Ryan Mitchell214846d2018-09-19 16:57:01 -07001933 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001934 if (extractor()->options_.include_meta_data && !name.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001935 printer->Print(StringPrintf("meta-data: name='%s'", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001936 if (!value.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001937 printer->Print(StringPrintf(" value='%s'", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001938 } else if (value_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001939 printer->Print(StringPrintf(" value='%d'", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001940 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001941 if (!resource.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001942 printer->Print(StringPrintf(" resource='%s'", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001943 } else if (resource_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001944 printer->Print(StringPrintf(" resource='%d'", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001945 }
1946 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001947 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001948 }
1949 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001950
1951 void ToProto(pb::Badging* out_badging) override {
1952 if (!name.empty()) {
1953 auto metadata = out_badging->add_metadata();
1954 metadata->set_name(name);
1955 if (!value.empty()) {
1956 metadata->set_value_string(value);
1957 } else if (value_int) {
1958 metadata->set_value_int(*value_int);
1959 } else {
1960 if (!resource.empty()) {
1961 metadata->set_resource_string(resource);
1962 } else if (resource_int) {
1963 metadata->set_resource_int(*resource_int);
1964 }
1965 }
1966 }
1967 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001968};
1969
1970/**
1971 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1972 * service components.
1973 **/
1974class Action : public ManifestExtractor::Element {
1975 public:
1976 Action() = default;
1977 std::string component = "";
1978
1979 void Extract(xml::Element* element) override {
1980 auto parent_stack = extractor()->parent_stack();
1981 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1982
1983 if (ElementCast<IntentFilter>(parent_stack[0])) {
1984 if (ElementCast<Activity>(parent_stack[1])) {
1985 // Detects the presence of a particular type of activity.
1986 Activity* activity = ElementCast<Activity>(parent_stack[1]);
Iurii Makhno499ecd32022-08-19 16:34:26 +00001987 static const auto map = std::map<std::string, std::string>({
1988 {"android.intent.action.MAIN", "main"},
1989 {"android.media.action.VIDEO_CAMERA", "camera"},
1990 {"android.media.action.STILL_IMAGE_CAMERA", "camera"},
1991 {"android.media.action.STILL_IMAGE_CAMERA_SECURE", "camera-secure"},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001992 });
1993
1994 auto entry = map.find(action);
1995 if (entry != map.end()) {
1996 component = entry->second;
1997 activity->has_component_ = true;
1998 }
1999
2000 if (action == "android.intent.action.MAIN") {
2001 activity->has_main_action = true;
2002 }
2003
2004 } else if (ElementCast<Receiver>(parent_stack[1])) {
2005 // Detects the presence of a particular type of receiver. If the action requires a
2006 // permission, then the receiver element is checked for the permission.
2007 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
2008 auto map = std::map<std::string, std::string>({
2009 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
2010 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
2011 });
2012
2013 auto permissions = std::map<std::string, std::string>({
2014 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
2015 });
2016
2017 auto entry = map.find(action);
2018 auto permission = permissions.find(action);
2019 if (entry != map.end() && (permission == permissions.end()
2020 || (receiver->permission && permission->second == *receiver->permission))) {
2021 receiver->has_component = true;
2022 component = entry->second;
2023 }
2024
2025 } else if (ElementCast<Service>(parent_stack[1])) {
2026 // Detects the presence of a particular type of service. If the action requires a
2027 // permission, then the service element is checked for the permission.
2028 Service* service = ElementCast<Service>(parent_stack[1]);
2029 auto map = std::map<std::string, std::string>({
2030 { "android.view.InputMethod" , "ime" },
2031 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
2032 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
2033 { "android.printservice.PrintService" , "print-service" },
2034 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
2035 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
2036 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
2037 { "android.service.dreams.DreamService" , "dream" },
2038 });
2039
2040 auto permissions = std::map<std::string, std::string>({
2041 { "android.accessibilityservice.AccessibilityService" ,
2042 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
2043 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
2044 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
2045 "android.permission.BIND_NFC_SERVICE" },
2046 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
2047 "android.permission.BIND_NFC_SERVICE" },
2048 { "android.service.notification.NotificationListenerService" ,
2049 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
2050 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
2051 });
2052
2053 auto entry = map.find(action);
2054 auto permission = permissions.find(action);
2055 if (entry != map.end() && (permission == permissions.end()
2056 || (service->permission && permission->second == *service->permission))) {
2057 service->has_component= true;
2058 component = entry->second;
2059 }
2060
2061 } else if (ElementCast<Provider>(parent_stack[1])) {
2062 // Detects the presence of a particular type of receiver. If the provider requires a
2063 // permission, then the provider element is checked for the permission.
2064 // Detect whether this action
2065 Provider* provider = ElementCast<Provider>(parent_stack[1]);
2066 if (action == "android.content.action.DOCUMENTS_PROVIDER"
2067 && provider->has_required_saf_attributes) {
2068 component = "document-provider";
2069 }
2070 }
2071 }
2072
2073 // Represents a searchable interface
2074 if (action == "android.intent.action.SEARCH") {
2075 component = "search";
2076 }
2077 }
2078};
2079
2080/**
2081 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
2082 **/
2083class SupportsInput : public ManifestExtractor::Element {
2084 public:
2085 SupportsInput() = default;
2086 std::vector<std::string> inputs;
2087
Ryan Mitchell214846d2018-09-19 16:57:01 -07002088 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002089 const size_t size = inputs.size();
2090 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002091 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002092 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002093 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002094 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002095 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002096 }
2097 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002098
2099 void ToProto(pb::Badging* out_badging) override {
2100 auto supports_input = out_badging->mutable_supports_input();
2101 for (auto& input : inputs) {
2102 supports_input->add_inputs(input);
2103 }
2104 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002105};
2106
2107/** Represents <input-type> elements. **/
2108class InputType : public ManifestExtractor::Element {
2109 public:
2110 InputType() = default;
2111 void Extract(xml::Element* element) override {
2112 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2113 auto parent_stack = extractor()->parent_stack();
2114
2115 // Add the input to the set of supported inputs
2116 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
2117 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
2118 supports->inputs.push_back(*name);
2119 }
2120 }
2121};
2122
Andrei Oneaa10b8082023-02-23 17:43:25 +00002123/** Represents <install-constraints> elements. **/
2124class InstallConstraints : public ManifestExtractor::Element {
2125 public:
2126 InstallConstraints() = default;
2127 std::vector<std::string> fingerprint_prefixes;
2128
2129 void Extract(xml::Element* element) override {
2130 for (xml::Element* child : element->GetChildElements()) {
2131 if (child->name == "fingerprint-prefix") {
2132 xml::Attribute* attr = child->FindAttribute(kAndroidNamespace, "value");
2133 if (attr) {
2134 fingerprint_prefixes.push_back(attr->value);
2135 }
2136 }
2137 }
2138 }
2139
2140 void Print(text::Printer* printer) override {
2141 if (!fingerprint_prefixes.empty()) {
2142 printer->Print(StringPrintf("install-constraints:\n"));
2143 for (const auto& prefix : fingerprint_prefixes) {
2144 printer->Print(StringPrintf(" fingerprint-prefix='%s'\n", prefix.c_str()));
2145 }
2146 }
2147 }
2148};
2149
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002150/** Represents <original-package> elements. **/
2151class OriginalPackage : public ManifestExtractor::Element {
2152 public:
2153 OriginalPackage() = default;
2154 const std::string* name = nullptr;
2155
2156 void Extract(xml::Element* element) override {
2157 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2158 }
2159
Ryan Mitchell214846d2018-09-19 16:57:01 -07002160 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002161 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002162 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002163 }
2164 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002165
2166 void ToProto(pb::Badging* out_badging) override {
2167 if (name) {
2168 out_badging->mutable_package()->set_original_package(*name);
2169 }
2170 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002171};
2172
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002173
2174/** Represents <overlay> elements. **/
2175class Overlay : public ManifestExtractor::Element {
2176 public:
2177 Overlay() = default;
2178 const std::string* target_package = nullptr;
2179 int priority;
2180 bool is_static;
2181 const std::string* required_property_name = nullptr;
2182 const std::string* required_property_value = nullptr;
2183
2184 void Extract(xml::Element* element) override {
2185 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
2186 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
2187 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
2188 required_property_name = GetAttributeString(
2189 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
2190 required_property_value = GetAttributeString(
2191 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
2192 }
2193
2194 void Print(text::Printer* printer) override {
2195 printer->Print(StringPrintf("overlay:"));
2196 if (target_package) {
2197 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
2198 }
2199 printer->Print(StringPrintf(" priority='%d'", priority));
2200 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
2201 if (required_property_name) {
2202 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
2203 }
2204 if (required_property_value) {
2205 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
2206 }
2207 printer->Print("\n");
2208 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002209
2210 void ToProto(pb::Badging* out_badging) override {
2211 auto overlay = out_badging->mutable_overlay();
2212 if (target_package) {
2213 overlay->set_target_package(*target_package);
2214 }
2215 overlay->set_priority(priority);
2216 overlay->set_static_(is_static);
2217 if (required_property_name) {
2218 overlay->set_required_property_name(*required_property_name);
2219 }
2220 if (required_property_value) {
2221 overlay->set_required_property_value(*required_property_value);
2222 }
2223 }
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002224};
2225
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002226/** * Represents <package-verifier> elements. **/
2227class PackageVerifier : public ManifestExtractor::Element {
2228 public:
2229 PackageVerifier() = default;
2230 const std::string* name = nullptr;
2231 const std::string* public_key = nullptr;
2232
2233 void Extract(xml::Element* element) override {
2234 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2235 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
2236 }
2237
Ryan Mitchell214846d2018-09-19 16:57:01 -07002238 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002239 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002240 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002241 name->data(), public_key->data()));
2242 }
2243 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002244
2245 void ToProto(pb::Badging* out_badging) override {
2246 auto package_verifier = out_badging->mutable_package_verifier();
2247 if (name && public_key) {
2248 package_verifier->set_name(*name);
2249 package_verifier->set_public_key(*public_key);
2250 }
2251 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002252};
2253
2254/** Represents <uses-package> elements. **/
2255class UsesPackage : public ManifestExtractor::Element {
2256 public:
2257 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002258 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002259 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002260 int version;
2261 int versionMajor;
2262 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002263
2264 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002265 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
2266 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2267 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
2268 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
2269 AddCertDigest(element);
Dianne Hackborn813d7502018-10-02 16:59:46 -07002270 }
2271
2272 void AddCertDigest(xml::Element* element) {
2273 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
2274 // We allow ":" delimiters in the SHA declaration as this is the format
2275 // emitted by the certtool making it easy for developers to copy/paste.
2276 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
2277 if (!digest.empty()) {
2278 certDigests.push_back(digest);
2279 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002280 }
2281
Ryan Mitchell214846d2018-09-19 16:57:01 -07002282 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002283 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002284 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002285 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07002286 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
2287 packageType->data(), name->data(), version, versionMajor));
2288 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002289 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002290 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002291 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07002292 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002293 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002294 }
2295 }
2296 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002297
2298 void ToProto(pb::Badging* out_badging) override {
2299 if (name) {
2300 auto uses_package = out_badging->add_uses_packages();
2301 uses_package->set_name(*name);
2302 if (packageType) {
2303 uses_package->set_package_type(*packageType);
2304 uses_package->set_version(version);
2305 uses_package->set_version_major(versionMajor);
2306 for (auto& cert : certDigests) {
2307 uses_package->add_certificates(cert);
2308 }
2309 }
2310 }
2311 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07002312};
2313
2314/** Represents <additional-certificate> elements. **/
2315class AdditionalCertificate : public ManifestExtractor::Element {
2316 public:
2317 AdditionalCertificate() = default;
2318
2319 void Extract(xml::Element* element) override {
2320 auto parent_stack = extractor()->parent_stack();
2321 if (parent_stack.size() > 0) {
2322 if (ElementCast<UsesPackage>(parent_stack[0])) {
2323 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
2324 uses->AddCertDigest(element);
2325 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
2326 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
2327 uses->AddCertDigest(element);
2328 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002329 }
2330 }
2331};
2332
2333/** Represents <screen> elements found in <compatible-screens> elements. */
2334class Screen : public ManifestExtractor::Element {
2335 public:
2336 Screen() = default;
2337 const int32_t* size = nullptr;
2338 const int32_t* density = nullptr;
2339
2340 void Extract(xml::Element* element) override {
2341 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
2342 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
2343 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002344
2345 void ToProto(pb::Badging* out_badging) override {
2346 if (size && density) {
2347 auto screen = out_badging->mutable_compatible_screens()->add_screens();
2348 screen->set_density(*density);
2349 screen->set_size(*size);
2350 }
2351 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002352};
2353
2354/**
2355 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
2356 * that each denote a supported screen size and screen density.
2357 **/
2358class CompatibleScreens : public ManifestExtractor::Element {
2359 public:
2360 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07002361 void Print(text::Printer* printer) override {
2362 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002363
2364 bool first = true;
2365 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
2366 if (auto screen = ElementCast<Screen>(el)) {
2367 if (first) {
2368 first = false;
2369 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002370 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002371 }
2372
2373 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002374 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002375 }
2376 }
2377 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07002378 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002379 }
2380};
2381
2382/** Represents <supports-gl-texture> elements. **/
2383class SupportsGlTexture : public ManifestExtractor::Element {
2384 public:
2385 SupportsGlTexture() = default;
2386 const std::string* name = nullptr;
2387
2388 void Extract(xml::Element* element) override {
2389 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2390 }
2391
Ryan Mitchell214846d2018-09-19 16:57:01 -07002392 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002393 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002394 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002395 }
2396 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002397
2398 void ToProto(pb::Badging* out_badging) override {
2399 if (name) {
2400 out_badging->mutable_supports_gl_texture()->add_name(*name);
2401 }
2402 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002403};
2404
Todd Kennedyce3e1292020-10-29 17:14:24 -07002405/** Represents <property> elements. **/
2406class Property : public ManifestExtractor::Element {
2407 public:
2408 Property() = default;
2409 std::string name;
2410 std::string value;
2411 const int* value_int;
2412 std::string resource;
2413 const int* resource_int;
2414
2415 void Extract(xml::Element* element) override {
2416 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
2417 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
2418 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
2419 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
2420 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
2421 }
2422
2423 void Print(text::Printer* printer) override {
2424 printer->Print(StringPrintf("property: name='%s' ", name.data()));
2425 if (!value.empty()) {
2426 printer->Print(StringPrintf("value='%s' ", value.data()));
2427 } else if (value_int) {
2428 printer->Print(StringPrintf("value='%d' ", *value_int));
2429 } else {
2430 if (!resource.empty()) {
2431 printer->Print(StringPrintf("resource='%s' ", resource.data()));
2432 } else if (resource_int) {
2433 printer->Print(StringPrintf("resource='%d' ", *resource_int));
2434 }
2435 }
2436 printer->Print("\n");
2437 }
Todd Kennedyce3e1292020-10-29 17:14:24 -07002438
Iurii Makhno85875a82022-04-26 15:30:01 +00002439 void ToProto(pb::Badging* out_badging) override {
2440 if (!name.empty()) {
2441 auto property = out_badging->add_properties();
2442 property->set_name(name);
2443 if (!value.empty()) {
2444 property->set_value_string(value);
2445 } else if (value_int) {
2446 property->set_value_int(*value_int);
2447 } else {
2448 if (!resource.empty()) {
2449 property->set_resource_string(resource);
2450 } else if (resource_int) {
2451 property->set_resource_int(*resource_int);
2452 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002453 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002454 }
2455 }
2456};
2457
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002458/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07002459static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002460 el->Print(printer);
2461 for (auto &child : el->children()) {
2462 Print(child.get(), printer);
2463 }
2464}
2465
Iurii Makhno85875a82022-04-26 15:30:01 +00002466/** Recursively serializes extracted badging elements to proto. */
2467static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) {
2468 el->ToProto(out_badging);
2469 for (auto& child : el->children()) {
2470 ToProto(child.get(), out_badging);
2471 }
2472}
2473
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002474bool ManifestExtractor::Extract(android::IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002475 // Load the manifest
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002476 doc_ = apk_->LoadXml("AndroidManifest.xml", diag);
2477 if (doc_ == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002478 diag->Error(android::DiagMessage() << "failed to find AndroidManifest.xml");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002479 return false;
2480 }
2481
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002482 xml::Element* element = doc_->root.get();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002483 if (element->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002484 diag->Error(android::DiagMessage() << "manifest does not start with <manifest> tag");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002485 return false;
2486 }
2487
2488 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
2489 // printing only permission elements is requested
2490 if (options_.only_permissions) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002491 root_element_ = ManifestExtractor::Element::Inflate(this, element, "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002492
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002493 if (auto manifest = ElementCast<Manifest>(root_element_.get())) {
2494 manifest->only_package_name = true;
2495
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002496 for (xml::Element* child : element->GetChildElements()) {
2497 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
2498 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002499 // Inflate the element and its descendants
Iurii Makhno32c0c032022-12-08 18:07:58 +00002500 auto permission_element = Visit(child, "manifest");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002501 manifest->AddChild(permission_element);
2502 }
2503 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002504 return true;
2505 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002506 return false;
2507 }
2508
2509 // Collect information about the resource configurations
2510 if (apk_->GetResourceTable()) {
2511 for (auto &package : apk_->GetResourceTable()->packages) {
2512 for (auto &type : package->types) {
2513 for (auto &entry : type->entries) {
2514 for (auto &value : entry->values) {
2515 std::string locale_str = value->config.GetBcp47LanguageTag();
2516
2517 // Collect all the unique locales of the apk
2518 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002519 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002520 config.setBcp47Locale(locale_str.data());
2521 locales_.insert(std::make_pair(locale_str, config));
2522 }
2523
2524 // Collect all the unique density of the apk
2525 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
2526 : value->config.density;
2527 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002528 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002529 config.density = density;
2530 densities_.insert(std::make_pair(density, config));
2531 }
2532 }
2533 }
2534 }
2535 }
2536 }
2537
2538 // Extract badging information
Iurii Makhno32c0c032022-12-08 18:07:58 +00002539 root_element_ = Visit(element, "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002540
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002541 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
2542 // attribute values from the last defined tag.
2543 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002544 for (const auto& child : root_element_->children()) {
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002545 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
2546 filtered_uses_sdk_tags.emplace_back(uses_sdk);
2547 }
2548 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002549 if (filtered_uses_sdk_tags.size() >= 2U) {
2550 filtered_uses_sdk_tags.pop_back();
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002551 root_element_->Filter([&](const ManifestExtractor::Element* e) {
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002552 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
2553 filtered_uses_sdk_tags.end();
2554 });
2555 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002556
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002557 /** Recursively checks the extracted elements for the specified permission. **/
2558 auto FindPermission = [&](ManifestExtractor::Element* root,
2559 const std::string& name) -> ManifestExtractor::Element* {
2560 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
2561 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
2562 return permission->name == name;
2563 }
2564 return false;
2565 });
2566 };
2567
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002568 auto AddImpliedPermission = [&](const std::string& name, const std::string& reason,
2569 int32_t max_sdk_version) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002570 auto permission = util::make_unique<UsesPermission>();
2571 permission->name = name;
2572 permission->maxSdkVersion = max_sdk_version;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002573 permission->implied = true;
2574 permission->impliedReason = reason;
2575 implied_permissions_.push_back(std::move(permission));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002576 };
2577
2578 // Implied permissions
2579 // Pre-1.6 implicitly granted permission compatibility logic
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002580 bool insert_write_external = false;
2581 auto write_external_permission = ElementCast<UsesPermission>(
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002582 FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002583
Jackal Guo201a60a2021-08-31 12:37:30 +08002584 if (target_sdk() < SDK_DONUT) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002585 if (!write_external_permission) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002586 AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002587 insert_write_external = true;
2588 }
2589
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002590 if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) {
2591 AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002592 }
2593 }
2594
2595 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2596 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2597 // do this (regardless of target API version) because we can't have
2598 // an app with write permission but not read permission.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002599 auto read_external =
2600 FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002601 if (!read_external && (insert_write_external || write_external_permission)) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002602 AddImpliedPermission(
2603 "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE",
2604 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002605 }
2606
2607 // Pre-JellyBean call log permission compatibility.
Jackal Guo201a60a2021-08-31 12:37:30 +08002608 if (target_sdk() < SDK_JELLY_BEAN) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002609 if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") &&
2610 FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) {
2611 AddImpliedPermission("android.permission.READ_CALL_LOG",
2612 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002613 }
2614
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002615 if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") &&
2616 FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) {
2617 AddImpliedPermission("android.permission.WRITE_CALL_LOG",
2618 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002619 }
2620 }
2621
2622 // If the app hasn't declared the touchscreen as a feature requirement (either
2623 // directly or implied, required or not), then the faketouch feature is implied.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002624 if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) {
2625 common_feature_group()->addImpliedFeature("android.hardware.faketouch",
2626 "default feature for all apps", false);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002627 }
2628
2629 // Only print the common feature group if no feature group is defined
2630 std::vector<FeatureGroup*> feature_groups;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002631 ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002632 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2633 feature_groups.push_back(feature_group);
2634 }
2635 });
2636
2637 if (feature_groups.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002638 feature_groups_.push_back(common_feature_group());
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002639 } else {
2640 // Merge the common feature group into the feature group
2641 for (auto& feature_group : feature_groups) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002642 feature_group->Merge(common_feature_group());
2643 feature_groups_.push_back(feature_group);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002644 }
2645 };
2646
2647 // Collect the component types of the application
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002648 ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002649 if (ElementCast<Action>(el)) {
2650 auto action = ElementCast<Action>(el);
2651 if (!action->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002652 components_.discovered_components.insert(action->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002653 return;
2654 }
2655 }
2656
2657 if (ElementCast<Category>(el)) {
2658 auto category = ElementCast<Category>(el);
2659 if (!category->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002660 components_.discovered_components.insert(category->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002661 return;
2662 }
2663 }
2664 });
2665
2666 // Check for the payment component
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002667 ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002668 if (auto service = ElementCast<Service>(el)) {
2669 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2670 [&](ManifestExtractor::Element* el) -> bool {
2671 if (auto action = ElementCast<Action>(el)) {
2672 return (action->component == "host-apdu");
2673 }
2674 return false;
2675 }));
2676
2677 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2678 [&](ManifestExtractor::Element* el) -> bool {
2679 if (auto action = ElementCast<Action>(el)) {
2680 return (action->component == "offhost-apdu");
2681 }
2682 return false;
2683 }));
2684
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002685 ForEachChild(service,
2686 [this, &diag, &host_apdu_action,
2687 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2688 if (auto meta_data = ElementCast<MetaData>(el)) {
2689 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" &&
2690 host_apdu_action) ||
2691 (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" &&
2692 offhost_apdu_action)) {
2693 // Attempt to load the resource file
Iurii Makhno32c0c032022-12-08 18:07:58 +00002694 if (meta_data->resource.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002695 return;
2696 }
2697 auto resource = this->apk_->LoadXml(meta_data->resource, diag);
2698 if (!resource) {
2699 return;
2700 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002701
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002702 // Look for the payment category on an <aid-group> element
2703 auto& root = resource.get()->root;
2704 if ((host_apdu_action && root->name == "host-apdu-service") ||
2705 (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2706 for (auto& child : root->GetChildElements()) {
2707 if (child->name == "aid-group") {
2708 auto category = FindAttribute(child, CATEGORY_ATTR);
2709 if (category && category->value == "payment") {
Iurii Makhno85875a82022-04-26 15:30:01 +00002710 this->components_.discovered_components.insert("payment");
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002711 return;
2712 }
2713 }
2714 }
2715 }
2716 }
2717 }
2718 });
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002719 }
2720 });
2721
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002722 // Print presence of activities, receivers, and services with no special components
2723 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2724 if (auto activity = ElementCast<Activity>(el)) {
2725 if (!activity->has_component_) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002726 components_.other_activities = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002727 return true;
2728 }
2729 }
2730 return false;
2731 });
2732
2733 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2734 if (auto receiver = ElementCast<Receiver>(el)) {
2735 if (!receiver->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002736 components_.other_receivers = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002737 return true;
2738 }
2739 }
2740 return false;
2741 });
2742
2743 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2744 if (auto service = ElementCast<Service>(el)) {
2745 if (!service->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002746 components_.other_services = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002747 return true;
2748 }
2749 }
2750 return false;
2751 });
2752
2753 // Gather the supported screens
2754 const static SupportsScreen default_screens{};
2755 SupportsScreen* screen = ElementCast<SupportsScreen>(
2756 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2757 return ElementCast<SupportsScreen>(el) != nullptr;
2758 }));
2759 supports_screen_ = screen ? screen : &default_screens;
2760
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +00002761 bool has_renderscript_bitcode = false;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002762 auto it = apk_->GetFileCollection()->Iterator();
2763 while (it->HasNext()) {
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +00002764 if (it->Next()->GetSource().path.ends_with(".bc")) {
2765 has_renderscript_bitcode = true;
2766 break;
2767 }
2768 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002769
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +00002770 // Gather the supported architectures_ of the app
2771 std::set<std::string> architectures_from_apk;
2772 it = apk_->GetFileCollection()->Iterator();
2773 while (it->HasNext()) {
2774 auto file_path = it->Next()->GetSource().path.c_str();
2775
2776 const char* last_slash =
2777 android::util::ValidLibraryPathLastSlash(file_path, has_renderscript_bitcode, false);
2778 if (last_slash) {
2779 architectures_from_apk.insert(std::string(file_path + APK_LIB_LEN, last_slash));
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002780 }
2781 }
2782
2783 // Determine if the application has multiArch supports
2784 auto has_multi_arch =
2785 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2786 if (auto application = ElementCast<Application>(el)) {
2787 return application->has_multi_arch;
2788 }
2789 return false;
2790 });
2791
2792 bool output_alt_native_code = false;
2793 // A multiArch package is one that contains 64-bit and
2794 // 32-bit versions of native code and expects 3rd-party
2795 // apps to load these native code libraries. Since most
2796 // 64-bit systems also support 32-bit apps, the apps
2797 // loading this multiArch package's code may be either
2798 if (has_multi_arch) {
2799 // If this is a multiArch package, report the 64-bit
2800 // version only. Then as a separate entry, report the
2801 // rest.
2802 //
2803 // If we report the 32-bit architecture, this APK will
2804 // be installed on a 32-bit device, causing a large waste
2805 // of bandwidth and disk space. This assumes that
2806 // the developer of the multiArch package has also
2807 // made a version that is 32-bit only.
2808 const std::string kIntel64 = "x86_64";
2809 const std::string kArm64 = "arm64-v8a";
2810
2811 auto arch = architectures_from_apk.find(kIntel64);
2812 if (arch == architectures_from_apk.end()) {
2813 arch = architectures_from_apk.find(kArm64);
2814 }
2815
2816 if (arch != architectures_from_apk.end()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002817 architectures_.architectures.insert(*arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002818 architectures_from_apk.erase(arch);
2819 output_alt_native_code = true;
2820 }
2821 }
2822 for (auto& arch : architectures_from_apk) {
2823 if (output_alt_native_code) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002824 architectures_.alt_architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002825 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002826 architectures_.architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002827 }
2828 }
2829 return true;
2830}
2831
2832bool ManifestExtractor::Dump(text::Printer* printer) {
2833 Print(root_element_.get(), printer);
2834 if (options_.only_permissions) {
2835 return true;
2836 }
2837
2838 for (auto& implied_permission : implied_permissions_) {
2839 implied_permission->Print(printer);
2840 }
2841 for (auto& feature_group : feature_groups_) {
2842 feature_group->PrintGroup(printer);
2843 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002844 components_.Print(printer);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002845 supports_screen_->PrintScreens(printer, target_sdk_);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002846
2847 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002848 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002849 for (auto& config : locales_) {
2850 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002851 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002852 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002853 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002854 }
2855 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002856 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002857
2858 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002859 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002860 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002861 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002862 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002863 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002864
Iurii Makhno85875a82022-04-26 15:30:01 +00002865 architectures_.Print(printer);
2866 return true;
2867}
2868
2869bool ManifestExtractor::DumpProto(pb::Badging* out_badging) {
2870 ToProto(root_element_.get(), out_badging);
2871 for (auto& implied_permission : implied_permissions_) {
2872 implied_permission->ToProto(out_badging);
2873 }
2874 for (auto& feature_group : feature_groups_) {
2875 feature_group->GroupToProto(out_badging);
2876 }
2877 components_.ToProto(out_badging);
2878 supports_screen_->ToProtoScreens(out_badging, target_sdk_);
2879
2880 for (auto& config : locales_) {
Iurii Makhnocfc559b2022-08-25 15:57:52 +00002881 if (config.first.empty()) {
2882 out_badging->add_locales("--_--");
2883 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002884 out_badging->add_locales(config.first);
2885 }
2886 }
2887 for (auto& config : densities_) {
2888 out_badging->add_densities(config.first);
2889 }
2890
2891 architectures_.ToProto(out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002892 return true;
2893}
2894
Iurii Makhno32c0c032022-12-08 18:07:58 +00002895template <typename T>
2896constexpr const char* GetExpectedTagForType() {
2897 // This array does not appear at runtime, as GetExpectedTagForType function is used by compiler
2898 // to inject proper 'expected_tag' into ElementCast.
Andrei Oneaa10b8082023-02-23 17:43:25 +00002899 std::array<std::pair<const char*, bool>, 38> tags = {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002900 std::make_pair("action", std::is_same<Action, T>::value),
2901 std::make_pair("activity", std::is_same<Activity, T>::value),
2902 std::make_pair("additional-certificate", std::is_same<AdditionalCertificate, T>::value),
2903 std::make_pair("application", std::is_same<Application, T>::value),
2904 std::make_pair("category", std::is_same<Category, T>::value),
2905 std::make_pair("compatible-screens", std::is_same<CompatibleScreens, T>::value),
2906 std::make_pair("feature-group", std::is_same<FeatureGroup, T>::value),
2907 std::make_pair("input-type", std::is_same<InputType, T>::value),
Andrei Oneaa10b8082023-02-23 17:43:25 +00002908 std::make_pair("install-constraints", std::is_same<InstallConstraints, T>::value),
Iurii Makhno32c0c032022-12-08 18:07:58 +00002909 std::make_pair("intent-filter", std::is_same<IntentFilter, T>::value),
2910 std::make_pair("meta-data", std::is_same<MetaData, T>::value),
2911 std::make_pair("manifest", std::is_same<Manifest, T>::value),
2912 std::make_pair("original-package", std::is_same<OriginalPackage, T>::value),
2913 std::make_pair("overlay", std::is_same<Overlay, T>::value),
2914 std::make_pair("package-verifier", std::is_same<PackageVerifier, T>::value),
2915 std::make_pair("permission", std::is_same<Permission, T>::value),
2916 std::make_pair("property", std::is_same<Property, T>::value),
2917 std::make_pair("provider", std::is_same<Provider, T>::value),
2918 std::make_pair("receiver", std::is_same<Receiver, T>::value),
2919 std::make_pair("required-feature", std::is_same<RequiredFeature, T>::value),
2920 std::make_pair("required-not-feature", std::is_same<RequiredNotFeature, T>::value),
2921 std::make_pair("screen", std::is_same<Screen, T>::value),
2922 std::make_pair("service", std::is_same<Service, T>::value),
2923 std::make_pair("sdk-library", std::is_same<SdkLibrary, T>::value),
2924 std::make_pair("static-library", std::is_same<StaticLibrary, T>::value),
2925 std::make_pair("supports-gl-texture", std::is_same<SupportsGlTexture, T>::value),
2926 std::make_pair("supports-input", std::is_same<SupportsInput, T>::value),
2927 std::make_pair("supports-screens", std::is_same<SupportsScreen, T>::value),
2928 std::make_pair("uses-configuration", std::is_same<UsesConfiguarion, T>::value),
2929 std::make_pair("uses-feature", std::is_same<UsesFeature, T>::value),
2930 std::make_pair("uses-library", std::is_same<UsesLibrary, T>::value),
2931 std::make_pair("uses-native-library", std::is_same<UsesNativeLibrary, T>::value),
2932 std::make_pair("uses-package", std::is_same<UsesPackage, T>::value),
2933 std::make_pair("uses-permission", std::is_same<UsesPermission, T>::value),
2934 std::make_pair("uses-permission-sdk-23", std::is_same<UsesPermissionSdk23, T>::value),
2935 std::make_pair("uses-sdk", std::is_same<UsesSdkBadging, T>::value),
2936 std::make_pair("uses-sdk-library", std::is_same<UsesSdkLibrary, T>::value),
2937 std::make_pair("uses-static-library", std::is_same<UsesStaticLibrary, T>::value),
2938 };
2939 for (const auto& pair : tags) {
2940 if (pair.second) {
2941 return pair.first;
2942 }
2943 }
2944 return nullptr;
2945}
2946
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002947/**
2948 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2949 * pointer.
2950 **/
2951template<typename T>
2952T* ElementCast(ManifestExtractor::Element* element) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002953 constexpr const char* expected_tag = GetExpectedTagForType<T>();
2954 if (element != nullptr && expected_tag != nullptr && element->is_featured() &&
2955 element->tag() == expected_tag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002956 return static_cast<T*>(element);
2957 }
2958 return nullptr;
2959}
2960
2961template<typename T>
2962std::unique_ptr<T> CreateType() {
2963 return std::move(util::make_unique<T>());
2964}
2965
2966std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
Iurii Makhno32c0c032022-12-08 18:07:58 +00002967 ManifestExtractor* extractor, xml::Element* el, const std::string& parent_tag) {
2968 static const std::unordered_map<std::string_view,
2969 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002970 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002971 {"action", &CreateType<Action>},
2972 {"activity", &CreateType<Activity>},
2973 {"additional-certificate", &CreateType<AdditionalCertificate>},
2974 {"application", &CreateType<Application>},
2975 {"category", &CreateType<Category>},
2976 {"compatible-screens", &CreateType<CompatibleScreens>},
2977 {"feature-group", &CreateType<FeatureGroup>},
2978 {"input-type", &CreateType<InputType>},
Andrei Oneaa10b8082023-02-23 17:43:25 +00002979 {"install-constraints", &CreateType<InstallConstraints>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002980 {"intent-filter", &CreateType<IntentFilter>},
2981 {"manifest", &CreateType<Manifest>},
2982 {"meta-data", &CreateType<MetaData>},
2983 {"original-package", &CreateType<OriginalPackage>},
2984 {"overlay", &CreateType<Overlay>},
2985 {"package-verifier", &CreateType<PackageVerifier>},
2986 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002987 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002988 {"provider", &CreateType<Provider>},
2989 {"receiver", &CreateType<Receiver>},
2990 {"required-feature", &CreateType<RequiredFeature>},
2991 {"required-not-feature", &CreateType<RequiredNotFeature>},
2992 {"screen", &CreateType<Screen>},
2993 {"service", &CreateType<Service>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002994 {"sdk-library", &CreateType<SdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002995 {"static-library", &CreateType<StaticLibrary>},
2996 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2997 {"supports-input", &CreateType<SupportsInput>},
2998 {"supports-screens", &CreateType<SupportsScreen>},
2999 {"uses-configuration", &CreateType<UsesConfiguarion>},
3000 {"uses-feature", &CreateType<UsesFeature>},
3001 {"uses-library", &CreateType<UsesLibrary>},
3002 {"uses-native-library", &CreateType<UsesNativeLibrary>},
3003 {"uses-package", &CreateType<UsesPackage>},
3004 {"uses-permission", &CreateType<UsesPermission>},
3005 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
3006 {"uses-sdk", &CreateType<UsesSdkBadging>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00003007 {"uses-sdk-library", &CreateType<UsesSdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00003008 {"uses-static-library", &CreateType<UsesStaticLibrary>},
3009 };
Iurii Makhno32c0c032022-12-08 18:07:58 +00003010 static constexpr std::array<std::pair<std::string_view, std::string_view>, 53>
3011 kValidChildParentTags = {
3012 std::make_pair("action", "intent-filter"),
3013 std::make_pair("activity", "application"),
3014 std::make_pair("additional-certificate", "uses-package"),
3015 std::make_pair("additional-certificate", "uses-static-library"),
3016 std::make_pair("application", "manifest"),
3017 std::make_pair("category", "intent-filter"),
3018 std::make_pair("compatible-screens", "manifest"),
3019 std::make_pair("feature-group", "manifest"),
3020 std::make_pair("input-type", "supports-input"),
3021 std::make_pair("intent-filter", "activity"),
3022 std::make_pair("intent-filter", "activity-alias"),
3023 std::make_pair("intent-filter", "service"),
3024 std::make_pair("intent-filter", "receiver"),
3025 std::make_pair("intent-filter", "provider"),
3026 std::make_pair("manifest", ""),
3027 std::make_pair("meta-data", "activity"),
3028 std::make_pair("meta-data", "activity-alias"),
3029 std::make_pair("meta-data", "application"),
3030 std::make_pair("meta-data", "service"),
3031 std::make_pair("meta-data", "receiver"),
3032 std::make_pair("meta-data", "provider"),
3033 std::make_pair("original-package", "manifest"),
3034 std::make_pair("overlay", "manifest"),
3035 std::make_pair("package-verifier", "manifest"),
3036 std::make_pair("permission", "manifest"),
3037 std::make_pair("property", "activity"),
3038 std::make_pair("property", "activity-alias"),
3039 std::make_pair("property", "application"),
3040 std::make_pair("property", "service"),
3041 std::make_pair("property", "receiver"),
3042 std::make_pair("property", "provider"),
3043 std::make_pair("provider", "application"),
3044 std::make_pair("receiver", "application"),
3045 std::make_pair("required-feature", "uses-permission"),
3046 std::make_pair("required-not-feature", "uses-permission"),
3047 std::make_pair("screen", "compatible-screens"),
3048 std::make_pair("service", "application"),
3049 std::make_pair("sdk-library", "application"),
3050 std::make_pair("static-library", "application"),
3051 std::make_pair("supports-gl-texture", "manifest"),
3052 std::make_pair("supports-input", "manifest"),
3053 std::make_pair("supports-screens", "manifest"),
3054 std::make_pair("uses-configuration", "manifest"),
3055 std::make_pair("uses-feature", "feature-group"),
3056 std::make_pair("uses-feature", "manifest"),
3057 std::make_pair("uses-library", "application"),
3058 std::make_pair("uses-native-library", "application"),
3059 std::make_pair("uses-package", "application"),
3060 std::make_pair("uses-permission", "manifest"),
3061 std::make_pair("uses-permission-sdk-23", "manifest"),
3062 std::make_pair("uses-sdk", "manifest"),
3063 std::make_pair("uses-sdk-library", "application"),
3064 std::make_pair("uses-static-library", "application"),
3065 };
3066 bool is_valid_tag = std::find(kValidChildParentTags.begin(), kValidChildParentTags.end(),
3067 std::make_pair<std::string_view, std::string_view>(
3068 el->name, parent_tag)) != kValidChildParentTags.end();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003069 // Attempt to map the xml tag to a element inflater
3070 std::unique_ptr<ManifestExtractor::Element> element;
3071 auto check = kTagCheck.find(el->name);
Iurii Makhno32c0c032022-12-08 18:07:58 +00003072 if (check != kTagCheck.end() && is_valid_tag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003073 element = check->second();
Iurii Makhno32c0c032022-12-08 18:07:58 +00003074 element->featured_ = true;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003075 } else {
3076 element = util::make_unique<ManifestExtractor::Element>();
3077 }
3078
3079 element->extractor_ = extractor;
3080 element->tag_ = el->name;
3081 element->Extract(el);
3082 return element;
3083}
3084
Iurii Makhno32c0c032022-12-08 18:07:58 +00003085std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(
3086 xml::Element* el, const std::string& parent_tag) {
3087 auto element = ManifestExtractor::Element::Inflate(this, el, parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003088 parent_stack_.insert(parent_stack_.begin(), element.get());
3089
3090 // Process the element and recursively visit the children
3091 for (xml::Element* child : el->GetChildElements()) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00003092 auto v = Visit(child, el->name);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003093 element->AddChild(v);
3094 }
3095
3096 parent_stack_.erase(parent_stack_.begin());
3097 return element;
3098}
3099
Ryan Mitchell214846d2018-09-19 16:57:01 -07003100int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003101 android::IDiagnostics* diag) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07003102 ManifestExtractor extractor(apk, options);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003103 if (!extractor.Extract(diag)) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003104 return 1;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003105 }
3106 return extractor.Dump(printer) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003107}
3108
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003109int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, android::IDiagnostics* diag) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003110 DumpManifestOptions options{/* include_meta_data= */ true,
3111 /* only_permissions= */ false};
3112 ManifestExtractor extractor(apk, options);
3113 if (!extractor.Extract(diag)) {
3114 return 1;
3115 }
3116 return extractor.DumpProto(out_badging) ? 0 : 1;
3117}
3118
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02003119} // namespace aapt