blob: 5a46a6691b6c997845432f1d615c8402406453e9 [file] [log] [blame]
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "DumpManifest.h"
18
Dianne Hackborn813d7502018-10-02 16:59:46 -070019#include <algorithm>
Iurii Makhno85875a82022-04-26 15:30:01 +000020#include <array>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000021#include <memory>
22#include <set>
Iurii Makhno85875a82022-04-26 15:30:01 +000023#include <string_view>
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000024#include <vector>
Dianne Hackborn813d7502018-10-02 16:59:46 -070025
Ryan Mitchellfc225b22018-08-21 14:52:51 -070026#include "LoadedApk.h"
27#include "SdkConstants.h"
28#include "ValueVisitor.h"
Iurii Makhnoa9c74c52022-04-12 15:04:12 +000029#include "androidfw/ConfigDescription.h"
Ryan Mitchellfc225b22018-08-21 14:52:51 -070030#include "io/File.h"
31#include "io/FileStream.h"
32#include "process/IResourceTableConsumer.h"
33#include "xml/XmlDom.h"
34
35using ::android::base::StringPrintf;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020036using ::android::ConfigDescription;
Ryan Mitchellfc225b22018-08-21 14:52:51 -070037
38namespace aapt {
39
40/**
41 * These are attribute resource constants for the platform, as found in android.R.attr.
42 */
43enum {
44 LABEL_ATTR = 0x01010001,
45 ICON_ATTR = 0x01010002,
46 NAME_ATTR = 0x01010003,
47 PERMISSION_ATTR = 0x01010006,
48 EXPORTED_ATTR = 0x01010010,
49 GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000050 PRIORITY_ATTR = 0x0101001c,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070051 RESOURCE_ATTR = 0x01010025,
52 DEBUGGABLE_ATTR = 0x0101000f,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000053 TARGET_PACKAGE_ATTR = 0x01010021,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070054 VALUE_ATTR = 0x01010024,
55 VERSION_CODE_ATTR = 0x0101021b,
56 VERSION_NAME_ATTR = 0x0101021c,
57 SCREEN_ORIENTATION_ATTR = 0x0101001e,
58 MIN_SDK_VERSION_ATTR = 0x0101020c,
59 MAX_SDK_VERSION_ATTR = 0x01010271,
60 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
61 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
62 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
63 REQ_NAVIGATION_ATTR = 0x0101022a,
64 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
65 TARGET_SDK_VERSION_ATTR = 0x01010270,
66 TEST_ONLY_ATTR = 0x01010272,
67 ANY_DENSITY_ATTR = 0x0101026c,
68 GL_ES_VERSION_ATTR = 0x01010281,
69 SMALL_SCREEN_ATTR = 0x01010284,
70 NORMAL_SCREEN_ATTR = 0x01010285,
71 LARGE_SCREEN_ATTR = 0x01010286,
72 XLARGE_SCREEN_ATTR = 0x010102bf,
73 REQUIRED_ATTR = 0x0101028e,
74 INSTALL_LOCATION_ATTR = 0x010102b7,
75 SCREEN_SIZE_ATTR = 0x010102ca,
76 SCREEN_DENSITY_ATTR = 0x010102cb,
77 REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
78 COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
79 LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
80 PUBLIC_KEY_ATTR = 0x010103a6,
81 CATEGORY_ATTR = 0x010103e8,
82 BANNER_ATTR = 0x10103f2,
83 ISGAME_ATTR = 0x10103f4,
Dianne Hackborn813d7502018-10-02 16:59:46 -070084 VERSION_ATTR = 0x01010519,
85 CERT_DIGEST_ATTR = 0x01010548,
Sergey Nikolaienkov91331e52020-09-30 12:58:47 +000086 REQUIRED_FEATURE_ATTR = 0x01010554,
87 REQUIRED_NOT_FEATURE_ATTR = 0x01010555,
Anton Hanssoncd2d8e22018-12-11 13:52:17 +000088 IS_STATIC_ATTR = 0x0101055a,
89 REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
90 REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070091 COMPILE_SDK_VERSION_ATTR = 0x01010572,
92 COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
Dianne Hackborn813d7502018-10-02 16:59:46 -070093 VERSION_MAJOR_ATTR = 0x01010577,
94 PACKAGE_TYPE_ATTR = 0x01010587,
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060095 USES_PERMISSION_FLAGS_ATTR = 0x01010644,
Ryan Mitchellfc225b22018-08-21 14:52:51 -070096};
97
98const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -060099constexpr int kNeverForLocation = 0x00010000;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700100
101/** Retrieves the attribute of the element with the specified attribute resource id. */
102static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
103 for (auto& a : el->attributes) {
104 if (a.compiled_attribute && a.compiled_attribute.value().id) {
105 if (a.compiled_attribute.value().id.value() == resd_id) {
106 return std::move(&a);
107 }
108 }
109 }
110 return nullptr;
111}
112
113/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
114static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
115 const std::string &name) {
116 return el->FindAttribute(package, name);
117}
118
Iurii Makhno85875a82022-04-26 15:30:01 +0000119class Architectures {
120 public:
121 std::set<std::string> architectures;
122 std::set<std::string> alt_architectures;
123
124 void Print(text::Printer* printer) {
125 if (!architectures.empty()) {
126 printer->Print("native-code:");
127 for (auto& arch : architectures) {
128 printer->Print(StringPrintf(" '%s'", arch.data()));
129 }
130 printer->Print("\n");
131 }
132 if (!alt_architectures.empty()) {
133 printer->Print("alt-native-code:");
134 for (auto& arch : alt_architectures) {
135 printer->Print(StringPrintf(" '%s'", arch.data()));
136 }
137 printer->Print("\n");
138 }
139 }
140
141 void ToProto(pb::Badging* out_badging) {
142 auto out_architectures = out_badging->mutable_architectures();
143 for (auto& arch : architectures) {
144 out_architectures->add_architectures(arch);
145 }
146 for (auto& arch : alt_architectures) {
147 out_architectures->add_alt_architectures(arch);
148 }
149 }
150};
151
152const static std::array<std::string_view, 14> printable_components{"app-widget",
153 "device-admin",
154 "ime",
155 "wallpaper",
156 "accessibility",
157 "print-service",
158 "payment",
159 "search",
160 "document-provider",
161 "launcher",
162 "notification-listener",
163 "dream",
164 "camera",
165 "camera-secure"};
166
167class Components {
168 public:
169 std::set<std::string, std::less<>> discovered_components;
170 bool other_activities = false;
171 bool other_receivers = false;
172 bool other_services = false;
173
174 void Print(text::Printer* printer) {
175 for (auto& component : printable_components) {
176 if (discovered_components.find(component) != discovered_components.end()) {
177 printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
178 }
179 }
180 // Print presence of main activity
181 if (discovered_components.find("main") != discovered_components.end()) {
182 printer->Print("main\n");
183 }
184
185 if (other_activities) {
186 printer->Print("other-activities\n");
187 }
188 if (other_receivers) {
189 printer->Print("other-receivers\n");
190 }
191 if (other_services) {
192 printer->Print("other-services\n");
193 }
194 }
195
196 void ToProto(pb::Badging* out_badging) {
197 auto out_components = out_badging->mutable_components();
198 for (auto& component : printable_components) {
199 auto discovered = discovered_components.find(component);
200 if (discovered != discovered_components.end()) {
201 out_components->add_provided_components(*discovered);
202 }
203 }
204 out_components->set_main(discovered_components.find("main") != discovered_components.end());
205 out_components->set_other_activities(other_activities);
206 out_components->set_other_receivers(other_receivers);
207 out_components->set_other_services(other_services);
208 }
209};
210
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700211class CommonFeatureGroup;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000212class FeatureGroup;
Iurii Makhno85875a82022-04-26 15:30:01 +0000213class SupportsScreen;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700214
215class ManifestExtractor {
216 public:
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700217
Ryan Mitchell214846d2018-09-19 16:57:01 -0700218 explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700219 : apk_(apk), options_(options) { }
220
221 class Element {
222 public:
223 Element() = default;
224 virtual ~Element() = default;
225
Iurii Makhno32c0c032022-12-08 18:07:58 +0000226 static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el,
227 const std::string& parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700228
229 /** Writes out the extracted contents of the element. */
Iurii Makhno85875a82022-04-26 15:30:01 +0000230 virtual void Print(text::Printer* printer) {
231 }
232
233 /** Saves extracted information into Badging proto. */
234 virtual void ToProto(pb::Badging* out_badging) {
235 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700236
237 /** Adds an element to the list of children of the element. */
238 void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
239
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700240 template <typename Predicate>
241 void Filter(Predicate&& func) {
242 children_.erase(std::remove_if(children_.begin(), children_.end(),
Kelvin Zhang3965584d2021-05-10 12:17:14 -0400243 [&](const auto& e) { return func(e.get()); }),
244 children_.end());
Ryan Mitchell1966e1f2021-05-03 13:46:56 -0700245 }
246
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700247 /** Retrieves the list of children of the element. */
248 const std::vector<std::unique_ptr<Element>>& children() const {
249 return children_;
250 }
251
252 /** Retrieves the extracted xml element tag. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000253 const std::string& tag() const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700254 return tag_;
255 }
256
Iurii Makhno32c0c032022-12-08 18:07:58 +0000257 /** Whether this element has special Extract/Print/ToProto logic. */
258 bool is_featured() const {
259 return featured_;
260 }
261
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700262 protected:
263 ManifestExtractor* extractor() const {
264 return extractor_;
265 }
266
267 /** Retrieves and stores the information extracted from the xml element. */
268 virtual void Extract(xml::Element* el) { }
269
270 /*
271 * Retrieves a configuration value of the resource entry that best matches the specified
272 * configuration.
273 */
274 static Value* BestConfigValue(ResourceEntry* entry,
275 const ConfigDescription& match) {
276 if (!entry) {
277 return nullptr;
278 }
279
280 // Determine the config that best matches the desired config
281 ResourceConfigValue* best_value = nullptr;
282 for (auto& value : entry->values) {
283 if (!value->config.match(match)) {
284 continue;
285 }
286
287 if (best_value != nullptr) {
288 if (!value->config.isBetterThan(best_value->config, &match)) {
289 if (value->config.compare(best_value->config) != 0) {
290 continue;
291 }
292 }
293 }
294
295 best_value = value.get();
296 }
297
298 // The entry has no values
299 if (!best_value) {
300 return nullptr;
301 }
302
303 return best_value->value.get();
304 }
305
306 /** Retrieves the resource assigned to the specified resource id if one exists. */
307 Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700308 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700309 if (table) {
310 for (auto& package : table->packages) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700311 for (auto& type : package->types) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700312 for (auto& entry : type->entries) {
313 if (entry->id && entry->id.value() == res_id.id) {
314 if (auto value = BestConfigValue(entry.get(), config)) {
315 return value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700316 }
317 }
318 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700319 }
320 }
321 }
322 return nullptr;
323 }
324
325 /** Attempts to resolve the reference to a non-reference value. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700326 Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700327 const int kMaxIterations = 40;
328 int i = 0;
329 while (ref && ref->id && i++ < kMaxIterations) {
330 auto table = extractor_->apk_->GetResourceTable();
331 if (auto value = FindValueById(table, ref->id.value(), config)) {
332 if (ValueCast<Reference>(value)) {
333 ref = ValueCast<Reference>(value);
334 } else {
335 return value;
336 }
337 }
338 }
339 return nullptr;
340 }
341
342 /**
343 * Retrieves the integer value of the attribute . If the value of the attribute is a reference,
344 * this will attempt to resolve the reference to an integer value.
345 **/
346 int32_t* GetAttributeInteger(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700347 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700348 if (attr != nullptr) {
349 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700350 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700351 Value* value = attr->compiled_value.get();
352 if (ValueCast<Reference>(value)) {
353 value = ResolveReference(ValueCast<Reference>(value), config);
354 } else {
355 value = attr->compiled_value.get();
356 }
357 // Retrieve the integer data if possible
358 if (value != nullptr) {
359 if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
360 return (int32_t*) &intValue->value.data;
361 }
362 }
363 }
364 }
365 return nullptr;
366 }
367
368 /**
369 * A version of GetAttributeInteger that returns a default integer if the attribute does not
370 * exist or cannot be resolved to an integer value.
371 **/
372 int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700373 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700374 auto value = GetAttributeInteger(attr, config);
375 if (value) {
376 return *value;
377 }
378 return def;
379 }
380
381 /**
382 * Retrieves the string value of the attribute. If the value of the attribute is a reference,
383 * this will attempt to resolve the reference to a string value.
384 **/
385 const std::string* GetAttributeString(xml::Attribute* attr,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700386 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700387 if (attr != nullptr) {
388 if (attr->compiled_value) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700389 // Resolve references using the configuration
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700390 Value* value = attr->compiled_value.get();
391 if (ValueCast<Reference>(value)) {
392 value = ResolveReference(ValueCast<Reference>(value), config);
393 } else {
394 value = attr->compiled_value.get();
395 }
396
397 // Retrieve the string data of the value if possible
398 if (value != nullptr) {
399 if (String* intValue = ValueCast<String>(value)) {
400 return &(*intValue->value);
401 } else if (RawString* rawValue = ValueCast<RawString>(value)) {
402 return &(*rawValue->value);
Iurii Makhno32c0c032022-12-08 18:07:58 +0000403 } else if (StyledString* styledStrValue = ValueCast<StyledString>(value)) {
404 return &(styledStrValue->value->value);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700405 } else if (FileReference* strValue = ValueCast<FileReference>(value)) {
406 return &(*strValue->path);
407 }
408 }
409 }
Ryan Mitchella36cc982019-06-05 10:13:41 -0700410
411 if (!attr->value.empty()) {
412 return &attr->value;
413 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700414 }
415 return nullptr;
416 }
417
418 /**
419 * A version of GetAttributeString that returns a default string if the attribute does not
420 * exist or cannot be resolved to an string value.
421 **/
422 std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700423 const ConfigDescription& config = DefaultConfig()) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700424 auto value = GetAttributeString(attr, config);
425 if (value) {
426 return *value;
427 }
428 return def;
429 }
430
431 private:
432 ManifestExtractor* extractor_;
433 std::vector<std::unique_ptr<Element>> children_;
434 std::string tag_;
Iurii Makhno32c0c032022-12-08 18:07:58 +0000435 bool featured_ = false;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700436 };
437
438 friend Element;
439
440 /** Creates a default configuration used to retrieve resources. */
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700441 static ConfigDescription DefaultConfig() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700442 ConfigDescription config;
443 config.orientation = android::ResTable_config::ORIENTATION_PORT;
444 config.density = android::ResTable_config::DENSITY_MEDIUM;
Jackal Guo201a60a2021-08-31 12:37:30 +0800445 config.sdkVersion = SDK_CUR_DEVELOPMENT; // Very high.
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700446 config.screenWidthDp = 320;
447 config.screenHeightDp = 480;
448 config.smallestScreenWidthDp = 320;
449 config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
450 return config;
451 }
452
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000453 bool Extract(android::IDiagnostics* diag);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000454 bool Dump(text::Printer* printer);
Iurii Makhno85875a82022-04-26 15:30:01 +0000455 bool DumpProto(pb::Badging* out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700456
457 /** Recursively visit the xml element tree and return a processed badging element tree. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000458 std::unique_ptr<Element> Visit(xml::Element* element, const std::string& parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700459
Iurii Makhno05fd9292022-08-24 18:56:12 +0000460 /** Resets target SDK to 0. */
461 void ResetTargetSdk() {
462 target_sdk_ = 0;
463 }
464
465 /** Raises the target sdk value if the min target is greater than the current target. */
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700466 void RaiseTargetSdk(int32_t min_target) {
467 if (min_target > target_sdk_) {
468 target_sdk_ = min_target;
469 }
470 }
471
472 /**
473 * Retrieves the default feature group that features are added into when <uses-feature>
474 * are not in a <feature-group> element.
475 **/
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000476 CommonFeatureGroup* common_feature_group() {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700477 return commonFeatureGroup_.get();
478 }
479
480 /**
481 * Retrieves a mapping of density values to Configurations for retrieving resources that would be
482 * used for that density setting.
483 **/
484 const std::map<uint16_t, ConfigDescription> densities() const {
485 return densities_;
486 }
487
488 /**
489 * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
490 * would be used for that locale setting.
491 **/
492 const std::map<std::string, ConfigDescription> locales() const {
493 return locales_;
494 }
495
496 /** Retrieves the current stack of parent during data extraction. */
Iurii Makhno32c0c032022-12-08 18:07:58 +0000497 const std::vector<Element*>& parent_stack() const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700498 return parent_stack_;
499 }
500
501 int32_t target_sdk() const {
502 return target_sdk_;
503 }
504
505 LoadedApk* const apk_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700506 DumpManifestOptions& options_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700507
508 private:
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000509 std::unique_ptr<xml::XmlResource> doc_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700510 std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
511 std::map<std::string, ConfigDescription> locales_;
512 std::map<uint16_t, ConfigDescription> densities_;
513 std::vector<Element*> parent_stack_;
514 int32_t target_sdk_ = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000515
516 std::unique_ptr<ManifestExtractor::Element> root_element_;
517 std::vector<std::unique_ptr<ManifestExtractor::Element>> implied_permissions_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000518 std::vector<FeatureGroup*> feature_groups_;
Iurii Makhno85875a82022-04-26 15:30:01 +0000519 Components components_;
520 Architectures architectures_;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000521 const SupportsScreen* supports_screen_;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700522};
523
524template<typename T> T* ElementCast(ManifestExtractor::Element* element);
525
526/** Recurs through the children of the specified root in depth-first order. */
527static void ForEachChild(ManifestExtractor::Element* root,
528 std::function<void(ManifestExtractor::Element*)> f) {
529 for (auto& child : root->children()) {
530 f(child.get());
531 ForEachChild(child.get(), f);
532 }
533}
534
535/**
536 * Checks the element and its recursive children for an element that makes the specified
537 * conditional function return true. Returns the first element that makes the conditional function
538 * return true.
539 **/
540static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
541 std::function<bool(ManifestExtractor::Element*)> f) {
542 if (f(root)) {
543 return root;
544 }
Iurii Makhno32c0c032022-12-08 18:07:58 +0000545 const auto& children = root->children();
546 for (auto it = children.rbegin(); it != children.rend(); ++it) {
547 if (auto b2 = FindElement(it->get(), f)) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700548 return b2;
549 }
550 }
551 return nullptr;
552}
553
554/** Represents the <manifest> elements **/
555class Manifest : public ManifestExtractor::Element {
556 public:
557 Manifest() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000558 bool only_package_name;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700559 std::string package;
560 int32_t versionCode;
561 std::string versionName;
562 const std::string* split = nullptr;
563 const std::string* platformVersionName = nullptr;
564 const std::string* platformVersionCode = nullptr;
Ryan Mitchella36cc982019-06-05 10:13:41 -0700565 const int32_t* platformVersionNameInt = nullptr;
566 const int32_t* platformVersionCodeInt = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700567 const int32_t* compilesdkVersion = nullptr;
568 const std::string* compilesdkVersionCodename = nullptr;
569 const int32_t* installLocation = nullptr;
570
571 void Extract(xml::Element* manifest) override {
572 package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
573 versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
574 versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
575 split = GetAttributeString(FindAttribute(manifest, {}, "split"));
576
577 // Extract the platform build info
578 platformVersionName = GetAttributeString(FindAttribute(manifest, {},
579 "platformBuildVersionName"));
580 platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
581 "platformBuildVersionCode"));
Ryan Mitchella36cc982019-06-05 10:13:41 -0700582 platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {},
583 "platformBuildVersionName"));
584 platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {},
585 "platformBuildVersionCode"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700586
587 // Extract the compile sdk info
588 compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
589 compilesdkVersionCodename = GetAttributeString(
590 FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
591 installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
592 }
593
Iurii Makhno85875a82022-04-26 15:30:01 +0000594 void ToProto(pb::Badging* out_badging) override {
595 auto out_package = out_badging->mutable_package();
596 out_package->set_package(package);
597 out_package->set_version_code(versionCode);
598 out_package->set_version_name(versionName);
599 if (compilesdkVersion) {
600 out_package->set_compile_sdk_version(*compilesdkVersion);
601 }
602 if (compilesdkVersionCodename) {
603 out_package->set_compile_sdk_version_codename(*compilesdkVersionCodename);
604 }
605 if (platformVersionName) {
606 out_package->set_platform_version_name(*platformVersionName);
607 } else if (platformVersionNameInt) {
608 out_package->set_platform_version_name(std::to_string(*platformVersionNameInt));
609 }
610 if (platformVersionCode) {
611 out_package->set_platform_version_code(*platformVersionCode);
612 } else if (platformVersionCodeInt) {
613 out_package->set_platform_version_code(std::to_string(*platformVersionCodeInt));
614 }
615
616 if (installLocation) {
617 switch (*installLocation) {
618 case 0:
619 out_package->set_install_location(pb::PackageInfo_InstallLocation_AUTO);
620 break;
621 case 1:
622 out_package->set_install_location(pb::PackageInfo_InstallLocation_INTERNAL_ONLY);
623 break;
624 case 2:
625 out_package->set_install_location(pb::PackageInfo_InstallLocation_PREFER_EXTERNAL);
626 break;
627 default:
628 break;
629 }
630 }
631 }
632
Ryan Mitchell214846d2018-09-19 16:57:01 -0700633 void Print(text::Printer* printer) override {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000634 if (only_package_name) {
635 printer->Println(StringPrintf("package: %s", package.data()));
636 } else {
637 PrintFull(printer);
638 }
639 }
640
641 void PrintFull(text::Printer* printer) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700642 printer->Print(StringPrintf("package: name='%s' ", package.data()));
643 printer->Print(StringPrintf("versionCode='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700644 (versionCode > 0) ? std::to_string(versionCode).data() : ""));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700645 printer->Print(StringPrintf("versionName='%s'", versionName.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700646
647 if (split) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700648 printer->Print(StringPrintf(" split='%s'", split->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700649 }
650 if (platformVersionName) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700651 printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700652 } else if (platformVersionNameInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700653 printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt));
654 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700655 if (platformVersionCode) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700656 printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
Dave Ingram7e0e4c12019-08-01 15:11:41 -0700657 } else if (platformVersionCodeInt) {
Ryan Mitchella36cc982019-06-05 10:13:41 -0700658 printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt));
659 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700660 if (compilesdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700661 printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700662 }
663 if (compilesdkVersionCodename) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700664 printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700665 compilesdkVersionCodename->data()));
666 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700667 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700668
669 if (installLocation) {
670 switch (*installLocation) {
671 case 0:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700672 printer->Print("install-location:'auto'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700673 break;
674 case 1:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700675 printer->Print("install-location:'internalOnly'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700676 break;
677 case 2:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700678 printer->Print("install-location:'preferExternal'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700679 break;
680 default:
681 break;
682 }
683 }
684 }
685};
686
687/** Represents <application> elements. **/
688class Application : public ManifestExtractor::Element {
689 public:
690 Application() = default;
691 std::string label;
692 std::string icon;
693 std::string banner;
694 int32_t is_game;
695 int32_t debuggable;
696 int32_t test_only;
697 bool has_multi_arch;
698
699 /** Mapping from locales to app names. */
700 std::map<std::string, std::string> locale_labels;
701
702 /** Mapping from densities to app icons. */
703 std::map<uint16_t, std::string> density_icons;
704
705 void Extract(xml::Element* element) override {
706 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
707 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
708 test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
709 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
710 is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
711 debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
712
713 // We must search by name because the multiArch flag hasn't been API
714 // frozen yet.
715 has_multi_arch = (GetAttributeIntegerDefault(
716 FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
717
718 // Retrieve the app names for every locale the app supports
719 auto attr = FindAttribute(element, LABEL_ATTR);
720 for (auto& config : extractor()->locales()) {
721 if (auto label = GetAttributeString(attr, config.second)) {
722 if (label) {
723 locale_labels.insert(std::make_pair(config.first, *label));
724 }
725 }
726 }
727
728 // Retrieve the icons for the densities the app supports
729 attr = FindAttribute(element, ICON_ATTR);
730 for (auto& config : extractor()->densities()) {
731 if (auto resource = GetAttributeString(attr, config.second)) {
732 if (resource) {
733 density_icons.insert(std::make_pair(config.first, *resource));
734 }
735 }
736 }
737 }
738
Ryan Mitchell214846d2018-09-19 16:57:01 -0700739 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700740 // Print the labels for every locale
741 for (auto p : locale_labels) {
742 if (p.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700743 printer->Print(StringPrintf("application-label:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700744 android::ResTable::normalizeForOutput(p.second.data())
745 .c_str()));
746 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700747 printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700748 android::ResTable::normalizeForOutput(p.second.data())
749 .c_str()));
750 }
751 }
752
753 // Print the icon paths for every density
754 for (auto p : density_icons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700755 printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700756 }
757
758 // Print the application info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700759 printer->Print(StringPrintf("application: label='%s' ",
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700760 android::ResTable::normalizeForOutput(label.data()).c_str()));
Ryan Mitchell214846d2018-09-19 16:57:01 -0700761 printer->Print(StringPrintf("icon='%s'", icon.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700762 if (!banner.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700763 printer->Print(StringPrintf(" banner='%s'", banner.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700764 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700765 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700766
767 if (test_only != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700768 printer->Print(StringPrintf("testOnly='%d'\n", test_only));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700769 }
770 if (is_game != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700771 printer->Print("application-isGame\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700772 }
773 if (debuggable != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700774 printer->Print("application-debuggable\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700775 }
776 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000777
778 void ToProto(pb::Badging* out_badging) override {
779 auto application = out_badging->mutable_application();
780 application->set_label(android::ResTable::normalizeForOutput(label.data()));
781 application->set_icon(icon);
782 application->set_banner(banner);
783 application->set_test_only(test_only != 0);
784 application->set_game(is_game != 0);
785 application->set_debuggable(debuggable != 0);
786
787 auto out_locale_labels = application->mutable_locale_labels();
788 for (auto& p : locale_labels) {
789 if (!p.first.empty()) {
790 (*out_locale_labels)[p.first] = p.second;
791 }
792 }
793 auto out_density_icons = application->mutable_density_icons();
794 for (auto& p : density_icons) {
795 (*out_density_icons)[p.first] = p.second;
796 }
797 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700798};
799
800/** Represents <uses-sdk> elements. **/
801class UsesSdkBadging : public ManifestExtractor::Element {
802 public:
803 UsesSdkBadging() = default;
804 const int32_t* min_sdk = nullptr;
805 const std::string* min_sdk_name = nullptr;
806 const int32_t* max_sdk = nullptr;
807 const int32_t* target_sdk = nullptr;
808 const std::string* target_sdk_name = nullptr;
809
810 void Extract(xml::Element* element) override {
811 min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
812 min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
813 max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
814 target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
815 target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
816
Iurii Makhno05fd9292022-08-24 18:56:12 +0000817 // Resets target SDK first. This is required if APK contains multiple <uses-sdk> elements,
818 // we only need to take the latest values.
819 extractor()->ResetTargetSdk();
820
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700821 // Detect the target sdk of the element
822 if ((min_sdk_name && *min_sdk_name == "Donut")
823 || (target_sdk_name && *target_sdk_name == "Donut")) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800824 extractor()->RaiseTargetSdk(SDK_DONUT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700825 }
826 if (min_sdk) {
827 extractor()->RaiseTargetSdk(*min_sdk);
828 }
829 if (target_sdk) {
830 extractor()->RaiseTargetSdk(*target_sdk);
Ryan Mitchell95f02422020-04-30 10:25:53 -0700831 } else if (target_sdk_name) {
Jackal Guo201a60a2021-08-31 12:37:30 +0800832 extractor()->RaiseTargetSdk(SDK_CUR_DEVELOPMENT);
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700833 }
834 }
835
Ryan Mitchell214846d2018-09-19 16:57:01 -0700836 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700837 if (min_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700838 printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700839 } else if (min_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700840 printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700841 }
842 if (max_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700843 printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700844 }
845 if (target_sdk) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700846 printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700847 } else if (target_sdk_name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700848 printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700849 }
850 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000851
852 void ToProto(pb::Badging* out_badging) override {
853 auto out_sdks = out_badging->mutable_uses_sdk();
854 if (min_sdk) {
855 out_sdks->set_min_sdk_version(*min_sdk);
856 } else if (min_sdk_name) {
857 out_sdks->set_min_sdk_version_name(*min_sdk_name);
858 }
859 if (max_sdk) {
860 out_sdks->set_max_sdk_version(*max_sdk);
861 }
862 if (target_sdk) {
863 out_sdks->set_target_sdk_version(*target_sdk);
864 } else if (target_sdk_name) {
865 out_sdks->set_target_sdk_version_name(*target_sdk_name);
866 }
867 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700868};
869
870/** Represents <uses-configuration> elements. **/
871class UsesConfiguarion : public ManifestExtractor::Element {
872 public:
873 UsesConfiguarion() = default;
874 int32_t req_touch_screen = 0;
875 int32_t req_keyboard_type = 0;
876 int32_t req_hard_keyboard = 0;
877 int32_t req_navigation = 0;
878 int32_t req_five_way_nav = 0;
879
880 void Extract(xml::Element* element) override {
881 req_touch_screen = GetAttributeIntegerDefault(
882 FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
883 req_keyboard_type = GetAttributeIntegerDefault(
884 FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
885 req_hard_keyboard = GetAttributeIntegerDefault(
886 FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
887 req_navigation = GetAttributeIntegerDefault(
888 FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
889 req_five_way_nav = GetAttributeIntegerDefault(
890 FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
891 }
892
Ryan Mitchell214846d2018-09-19 16:57:01 -0700893 void Print(text::Printer* printer) override {
894 printer->Print("uses-configuration:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700895 if (req_touch_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700896 printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700897 }
898 if (req_keyboard_type != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700899 printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700900 }
901 if (req_hard_keyboard != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700902 printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700903 }
904 if (req_navigation != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700905 printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700906 }
907 if (req_five_way_nav != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700908 printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700909 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700910 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700911 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000912
913 void ToProto(pb::Badging* out_badging) override {
914 auto out_configuration = out_badging->mutable_uses_configuration();
915 out_configuration->set_req_touch_screen(req_touch_screen);
916 out_configuration->set_req_keyboard_type(req_keyboard_type);
917 out_configuration->set_req_hard_keyboard(req_hard_keyboard);
918 out_configuration->set_req_navigation(req_navigation);
919 out_configuration->set_req_five_way_nav(req_five_way_nav);
920 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700921};
922
923/** Represents <supports-screen> elements. **/
924class SupportsScreen : public ManifestExtractor::Element {
925 public:
926 SupportsScreen() = default;
927 int32_t small_screen = 1;
928 int32_t normal_screen = 1;
929 int32_t large_screen = 1;
930 int32_t xlarge_screen = 1;
931 int32_t any_density = 1;
932 int32_t requires_smallest_width_dp = 0;
933 int32_t compatible_width_limit_dp = 0;
934 int32_t largest_width_limit_dp = 0;
935
936 void Extract(xml::Element* element) override {
937 small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
938 normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
939 large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
940 xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
941 any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
942
943 requires_smallest_width_dp = GetAttributeIntegerDefault(
944 FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
945 compatible_width_limit_dp = GetAttributeIntegerDefault(
946 FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
947 largest_width_limit_dp = GetAttributeIntegerDefault(
948 FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
949
950 // For modern apps, if screen size buckets haven't been specified
951 // but the new width ranges have, then infer the buckets from them.
952 if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
953 && requires_smallest_width_dp > 0) {
954 int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
955 : requires_smallest_width_dp;
956 small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
957 normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
958 large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
959 xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
960 }
961 }
962
Iurii Makhnoa9c74c52022-04-12 15:04:12 +0000963 void PrintScreens(text::Printer* printer, int32_t target_sdk) const {
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700964 // Print the formatted screen info
Ryan Mitchell214846d2018-09-19 16:57:01 -0700965 printer->Print("supports-screens:");
Iurii Makhno85875a82022-04-26 15:30:01 +0000966 if (IsSmallScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700967 printer->Print(" 'small'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700968 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000969 if (normal_screen != 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700970 printer->Print(" 'normal'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700971 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000972 if (IsLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700973 printer->Print(" 'large'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700974 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000975 if (IsXLargeScreenSupported(target_sdk)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700976 printer->Print(" 'xlarge'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700977 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700978 printer->Print("\n");
979 printer->Print(StringPrintf("supports-any-density: '%s'\n",
Iurii Makhno85875a82022-04-26 15:30:01 +0000980 (IsAnyDensitySupported(target_sdk)) ? "true" : "false"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700981 if (requires_smallest_width_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700982 printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700983 }
984 if (compatible_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700985 printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700986 }
987 if (largest_width_limit_dp > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700988 printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
Ryan Mitchellfc225b22018-08-21 14:52:51 -0700989 }
990 }
Iurii Makhno85875a82022-04-26 15:30:01 +0000991
992 void ToProtoScreens(pb::Badging* out_badging, int32_t target_sdk) const {
993 auto supports_screen = out_badging->mutable_supports_screen();
994 if (IsSmallScreenSupported(target_sdk)) {
995 supports_screen->add_screens(pb::SupportsScreen_ScreenType_SMALL);
996 }
997 if (normal_screen != 0) {
998 supports_screen->add_screens(pb::SupportsScreen_ScreenType_NORMAL);
999 }
1000 if (IsLargeScreenSupported(target_sdk)) {
1001 supports_screen->add_screens(pb::SupportsScreen_ScreenType_LARGE);
1002 }
1003 if (IsXLargeScreenSupported(target_sdk)) {
1004 supports_screen->add_screens(pb::SupportsScreen_ScreenType_XLARGE);
1005 }
1006 supports_screen->set_supports_any_densities(IsAnyDensitySupported(target_sdk));
1007 supports_screen->set_requires_smallest_width_dp(requires_smallest_width_dp);
1008 supports_screen->set_compatible_width_limit_dp(compatible_width_limit_dp);
1009 supports_screen->set_largest_width_limit_dp(largest_width_limit_dp);
1010 }
1011
1012 private:
1013 // Determine default values for any unspecified screen sizes,
1014 // based on the target SDK of the package. As of 4 (donut)
1015 // the screen size support was introduced, so all default to
1016 // enabled.
1017 bool IsSmallScreenSupported(int32_t target_sdk) const {
1018 if (small_screen > 0) {
1019 return target_sdk >= SDK_DONUT;
1020 }
1021 return small_screen != 0;
1022 }
1023
1024 bool IsLargeScreenSupported(int32_t target_sdk) const {
1025 if (large_screen > 0) {
1026 return target_sdk >= SDK_DONUT;
1027 }
1028 return large_screen != 0;
1029 }
1030
1031 bool IsXLargeScreenSupported(int32_t target_sdk) const {
1032 if (xlarge_screen > 0) {
1033 return target_sdk >= SDK_GINGERBREAD;
1034 }
1035 return xlarge_screen != 0;
1036 }
1037
1038 bool IsAnyDensitySupported(int32_t target_sdk) const {
1039 if (any_density > 0) {
1040 return target_sdk >= SDK_DONUT || requires_smallest_width_dp > 0 ||
1041 compatible_width_limit_dp > 0;
1042 }
1043 return any_density != 0;
1044 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001045};
1046
1047/** Represents <feature-group> elements. **/
1048class FeatureGroup : public ManifestExtractor::Element {
1049 public:
1050 FeatureGroup() = default;
1051 std::string label;
1052 int32_t open_gles_version = 0;
1053
1054 void Extract(xml::Element* element) override {
1055 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1056 }
1057
Ryan Mitchell214846d2018-09-19 16:57:01 -07001058 virtual void PrintGroup(text::Printer* printer) {
1059 printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001060 if (open_gles_version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001061 printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001062 }
1063
1064 for (auto feature : features_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001065 printer->Print(StringPrintf(" uses-feature%s: name='%s'",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001066 (feature.second.required ? "" : "-not-required"),
1067 feature.first.data()));
1068 if (feature.second.version > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001069 printer->Print(StringPrintf(" version='%d'", feature.second.version));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001070 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001071 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001072 }
1073 }
1074
Iurii Makhno85875a82022-04-26 15:30:01 +00001075 virtual void GroupToProto(pb::Badging* out_badging) {
1076 auto feature_group = out_badging->add_feature_groups();
1077 feature_group->set_label(label);
1078 feature_group->set_open_gles_version(open_gles_version);
1079 for (auto& feature : features_) {
1080 auto out_feature = feature_group->add_features();
1081 out_feature->set_name(feature.first);
1082 out_feature->set_required(feature.second.required);
1083 out_feature->set_version(feature.second.version);
1084 }
1085 }
1086
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001087 /** Adds a feature to the feature group. */
1088 void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
Iurii Makhno0df4e6d2022-11-29 19:19:13 +00001089 features_.insert_or_assign(name, Feature{required, version});
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001090 if (required) {
1091 if (name == "android.hardware.camera.autofocus" ||
1092 name == "android.hardware.camera.flash") {
1093 AddFeature("android.hardware.camera", true);
1094 } else if (name == "android.hardware.location.gps" ||
1095 name == "android.hardware.location.network") {
1096 AddFeature("android.hardware.location", true);
1097 } else if (name == "android.hardware.faketouch.multitouch") {
1098 AddFeature("android.hardware.faketouch", true);
1099 } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
1100 name == "android.hardware.faketouch.multitouch.jazzhands") {
1101 AddFeature("android.hardware.faketouch.multitouch", true);
1102 AddFeature("android.hardware.faketouch", true);
1103 } else if (name == "android.hardware.touchscreen.multitouch") {
1104 AddFeature("android.hardware.touchscreen", true);
1105 } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
1106 name == "android.hardware.touchscreen.multitouch.jazzhands") {
1107 AddFeature("android.hardware.touchscreen.multitouch", true);
1108 AddFeature("android.hardware.touchscreen", true);
1109 } else if (name == "android.hardware.opengles.aep") {
1110 const int kOpenGLESVersion31 = 0x00030001;
1111 if (kOpenGLESVersion31 > open_gles_version) {
1112 open_gles_version = kOpenGLESVersion31;
1113 }
1114 }
1115 }
1116 }
1117
1118 /** Returns true if the feature group has the given feature. */
1119 virtual bool HasFeature(const std::string& name) {
1120 return features_.find(name) != features_.end();
1121 }
1122
1123 /** Merges the features of another feature group into this group. */
1124 void Merge(FeatureGroup* group) {
1125 open_gles_version = std::max(open_gles_version, group->open_gles_version);
1126 for (auto& feature : group->features_) {
1127 features_.insert(feature);
1128 }
1129 }
1130
1131 protected:
1132 struct Feature {
1133 public:
1134 bool required = false;
1135 int32_t version = -1;
1136 };
1137
1138 /* Mapping of feature names to their properties. */
1139 std::map<std::string, Feature> features_;
1140};
1141
1142/**
1143 * Represents the default feature group for the application if no <feature-group> elements are
1144 * present in the manifest.
1145 **/
1146class CommonFeatureGroup : public FeatureGroup {
1147 public:
1148 CommonFeatureGroup() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07001149 void PrintGroup(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001150 FeatureGroup::PrintGroup(printer);
1151
1152 // Also print the implied features
1153 for (auto feature : implied_features_) {
1154 if (features_.find(feature.first) == features_.end()) {
1155 const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
Ryan Mitchell214846d2018-09-19 16:57:01 -07001156 printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
1157 printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001158 feature.first.data()));
1159
1160 // Print the reasons as a sentence
1161 size_t count = 0;
1162 for (auto reason : feature.second.reasons) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001163 printer->Print(reason);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001164 if (count + 2 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001165 printer->Print(", ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001166 } else if (count + 1 < feature.second.reasons.size()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001167 printer->Print(", and ");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001168 }
1169 count++;
1170 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001171 printer->Print("'\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001172 }
1173 }
1174 }
1175
Iurii Makhno85875a82022-04-26 15:30:01 +00001176 virtual void GroupToProto(pb::Badging* out_badging) override {
1177 FeatureGroup::GroupToProto(out_badging);
1178 auto feature_group =
1179 out_badging->mutable_feature_groups(out_badging->feature_groups_size() - 1);
1180 for (auto& feature : implied_features_) {
1181 if (features_.find(feature.first) == features_.end()) {
1182 auto out_feature = feature_group->add_features();
1183 out_feature->set_name(feature.first);
1184 auto implied_data = out_feature->mutable_implied_data();
1185 implied_data->set_from_sdk_23_permission(feature.second.implied_from_sdk_k23);
1186 for (auto& reason : feature.second.reasons) {
1187 implied_data->add_reasons(reason);
1188 }
1189 }
1190 }
1191 }
1192
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001193 /** Returns true if the feature group has the given feature. */
1194 bool HasFeature(const std::string& name) override {
1195 return FeatureGroup::HasFeature(name)
1196 || implied_features_.find(name) != implied_features_.end();
1197 }
1198
1199 /** Adds a feature to a set of implied features not explicitly requested in the manifest. */
1200 void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
1201 auto entry = implied_features_.find(name);
1202 if (entry == implied_features_.end()) {
1203 implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
1204 entry = implied_features_.find(name);
1205 }
1206
1207 // A non-sdk 23 implied feature takes precedence.
1208 if (entry->second.implied_from_sdk_k23 && !sdk23) {
1209 entry->second.implied_from_sdk_k23 = false;
1210 }
1211
1212 entry->second.reasons.insert(reason);
1213 }
1214
1215 /**
1216 * Adds a feature to a set of implied features for all features that are implied by the presence
1217 * of the permission.
1218 **/
1219 void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
1220 if (name == "android.permission.CAMERA") {
1221 addImpliedFeature("android.hardware.camera",
1222 StringPrintf("requested %s permission", name.data()),
1223 sdk23);
1224
1225 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
1226 if (targetSdk < SDK_LOLLIPOP) {
1227 addImpliedFeature("android.hardware.location.gps",
1228 StringPrintf("requested %s permission", name.data()),
1229 sdk23);
1230 addImpliedFeature("android.hardware.location.gps",
1231 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1232 sdk23);
1233 }
1234 addImpliedFeature("android.hardware.location",
1235 StringPrintf("requested %s permission", name.data()),
1236 sdk23);
1237
1238 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
1239 if (targetSdk < SDK_LOLLIPOP) {
1240 addImpliedFeature("android.hardware.location.network",
1241 StringPrintf("requested %s permission", name.data()),
1242 sdk23);
1243 addImpliedFeature("android.hardware.location.network",
1244 StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
1245 sdk23);
1246 }
1247 addImpliedFeature("android.hardware.location",
1248 StringPrintf("requested %s permission", name.data()),
1249 sdk23);
1250
1251 } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
1252 name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
1253 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
1254 addImpliedFeature("android.hardware.location",
1255 StringPrintf("requested %s permission", name.data()),
1256 sdk23);
1257
1258 } else if (name == "android.permission.BLUETOOTH" ||
1259 name == "android.permission.BLUETOOTH_ADMIN") {
1260 if (targetSdk > SDK_DONUT) {
1261 addImpliedFeature("android.hardware.bluetooth",
1262 StringPrintf("requested %s permission", name.data()),
1263 sdk23);
1264 addImpliedFeature("android.hardware.bluetooth",
1265 StringPrintf("targetSdkVersion > %d", SDK_DONUT),
1266 sdk23);
1267 }
1268
1269 } else if (name == "android.permission.RECORD_AUDIO") {
1270 addImpliedFeature("android.hardware.microphone",
1271 StringPrintf("requested %s permission", name.data()),
1272 sdk23);
1273
1274 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
1275 name == "android.permission.CHANGE_WIFI_STATE" ||
1276 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
1277 addImpliedFeature("android.hardware.wifi",
1278 StringPrintf("requested %s permission", name.data()),
1279 sdk23);
1280
1281 } else if (name == "android.permission.CALL_PHONE" ||
1282 name == "android.permission.CALL_PRIVILEGED" ||
1283 name == "android.permission.MODIFY_PHONE_STATE" ||
1284 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
1285 name == "android.permission.READ_SMS" ||
1286 name == "android.permission.RECEIVE_SMS" ||
1287 name == "android.permission.RECEIVE_MMS" ||
1288 name == "android.permission.RECEIVE_WAP_PUSH" ||
1289 name == "android.permission.SEND_SMS" ||
1290 name == "android.permission.WRITE_APN_SETTINGS" ||
1291 name == "android.permission.WRITE_SMS") {
1292 addImpliedFeature("android.hardware.telephony",
1293 "requested a telephony permission",
1294 sdk23);
1295 }
1296 }
1297
1298 private:
1299 /**
1300 * Represents a feature that has been automatically added due to a pre-requisite or for some
1301 * other reason.
1302 */
1303 struct ImpliedFeature {
1304 explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
1305
1306 /** List of human-readable reasons for why this feature was implied. */
1307 std::set<std::string> reasons;
1308
1309 // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
1310 bool implied_from_sdk_k23;
1311 };
1312
1313 /* Mapping of implied feature names to their properties. */
1314 std::map<std::string, ImpliedFeature> implied_features_;
1315};
1316
1317/** Represents <uses-feature> elements. **/
1318class UsesFeature : public ManifestExtractor::Element {
1319 public:
1320 UsesFeature() = default;
1321 void Extract(xml::Element* element) override {
1322 const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1323 int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
1324 bool required = GetAttributeIntegerDefault(
1325 FindAttribute(element, REQUIRED_ATTR), true) != 0;
1326 int32_t version = GetAttributeIntegerDefault(
1327 FindAttribute(element, kAndroidNamespace, "version"), 0);
1328
1329 // Add the feature to the parent feature group element if one exists; otherwise, add it to the
1330 // common feature group
1331 FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
1332 if (!feature_group) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001333 feature_group = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001334 } else {
1335 // All features in side of <feature-group> elements are required.
1336 required = true;
1337 }
1338
1339 if (name) {
1340 feature_group->AddFeature(*name, required, version);
1341 } else if (gl) {
1342 feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
1343 }
1344 }
1345};
1346
1347/** Represents <uses-permission> elements. **/
1348class UsesPermission : public ManifestExtractor::Element {
1349 public:
1350 UsesPermission() = default;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001351 bool implied;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001352 std::string name;
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001353 std::vector<std::string> requiredFeatures;
1354 std::vector<std::string> requiredNotFeatures;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001355 int32_t required = true;
1356 int32_t maxSdkVersion = -1;
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001357 int32_t usesPermissionFlags = 0;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001358 std::string impliedReason;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001359
1360 void Extract(xml::Element* element) override {
1361 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001362 std::string feature =
1363 GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
1364 if (!feature.empty()) {
1365 requiredFeatures.push_back(feature);
1366 }
1367 feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
1368 if (!feature.empty()) {
1369 requiredNotFeatures.push_back(feature);
1370 }
1371
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001372 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
1373 maxSdkVersion = GetAttributeIntegerDefault(
1374 FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001375 usesPermissionFlags = GetAttributeIntegerDefault(
1376 FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001377
1378 if (!name.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001379 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001380 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
1381 }
1382 }
1383
Ryan Mitchell214846d2018-09-19 16:57:01 -07001384 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001385 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001386 printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001387 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001388 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001389 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001390 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1391 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1392 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001393 printer->Print("\n");
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001394 for (const std::string& requiredFeature : requiredFeatures) {
1395 printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data()));
1396 }
1397 for (const std::string& requiredNotFeature : requiredNotFeatures) {
1398 printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data()));
1399 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001400 if (required == 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001401 printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001402 if (maxSdkVersion >= 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001403 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001404 }
Jeff Sharkeyabcddfd2021-03-29 10:05:21 -06001405 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1406 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1407 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001408 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001409 }
1410 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001411 if (implied) {
1412 printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
1413 if (maxSdkVersion >= 0) {
1414 printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
1415 }
1416 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1417 printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'"));
1418 }
1419 printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001420 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001421 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001422
1423 void ToProto(pb::Badging* out_badging) override {
1424 if (!name.empty()) {
1425 auto permission = out_badging->add_uses_permissions();
1426 permission->set_name(name);
1427 if (maxSdkVersion > 0) {
1428 permission->set_max_sdk_version(maxSdkVersion);
1429 }
1430 if ((usesPermissionFlags & kNeverForLocation) != 0) {
1431 permission->mutable_permission_flags()->set_never_for_location(true);
1432 }
1433 for (auto& requiredFeature : requiredFeatures) {
1434 permission->add_required_features(requiredFeature);
1435 }
1436 for (auto& requiredNotFeature : requiredNotFeatures) {
1437 permission->add_required_not_features(requiredNotFeature);
1438 }
1439 permission->set_required(required != 0);
1440 permission->set_implied(implied);
1441 }
1442 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001443};
1444
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00001445/** Represents <required-feature> elements. **/
1446class RequiredFeature : public ManifestExtractor::Element {
1447 public:
1448 RequiredFeature() = default;
1449 std::string name;
1450
1451 void Extract(xml::Element* element) override {
1452 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1453 auto parent_stack = extractor()->parent_stack();
1454 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1455 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1456 uses_permission->requiredFeatures.push_back(name);
1457 }
1458 }
1459};
1460
1461/** Represents <required-not-feature> elements. **/
1462class RequiredNotFeature : public ManifestExtractor::Element {
1463 public:
1464 RequiredNotFeature() = default;
1465 std::string name;
1466
1467 void Extract(xml::Element* element) override {
1468 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1469 auto parent_stack = extractor()->parent_stack();
1470 if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) {
1471 UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]);
1472 uses_permission->requiredNotFeatures.push_back(name);
1473 }
1474 }
1475};
1476
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001477/** Represents <uses-permission-sdk-23> elements. **/
1478class UsesPermissionSdk23 : public ManifestExtractor::Element {
1479 public:
1480 UsesPermissionSdk23() = default;
1481 const std::string* name = nullptr;
1482 const int32_t* maxSdkVersion = nullptr;
1483
1484 void Extract(xml::Element* element) override {
1485 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
1486 maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
1487
1488 if (name) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001489 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001490 common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
1491 }
1492 }
1493
Ryan Mitchell214846d2018-09-19 16:57:01 -07001494 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001495 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001496 printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001497 if (maxSdkVersion) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001498 printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001499 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001500 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001501 }
1502 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001503
1504 void ToProto(pb::Badging* out_badging) override {
1505 if (name) {
1506 auto permission = out_badging->add_uses_permissions();
1507 permission->set_sdk23_and_above(true);
1508 permission->set_name(*name);
1509 if (maxSdkVersion) {
1510 permission->set_max_sdk_version(*maxSdkVersion);
1511 }
1512 }
1513 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001514};
1515
1516/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
1517class Permission : public ManifestExtractor::Element {
1518 public:
1519 Permission() = default;
1520 std::string name;
1521
1522 void Extract(xml::Element* element) override {
1523 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1524 }
1525
Ryan Mitchell214846d2018-09-19 16:57:01 -07001526 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001527 if (extractor()->options_.only_permissions && !name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001528 printer->Print(StringPrintf("permission: %s\n", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001529 }
1530 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001531
1532 void ToProto(pb::Badging* out_badging) override {
1533 if (!name.empty()) {
1534 out_badging->add_permissions()->set_name(name);
1535 }
1536 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001537};
1538
1539/** Represents <activity> elements. **/
1540class Activity : public ManifestExtractor::Element {
1541 public:
1542 Activity() = default;
1543 std::string name;
1544 std::string icon;
1545 std::string label;
1546 std::string banner;
1547
1548 bool has_component_ = false;
1549 bool has_launcher_category = false;
1550 bool has_leanback_launcher_category = false;
1551 bool has_main_action = false;
1552
1553 void Extract(xml::Element* element) override {
1554 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1555 label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
1556 icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
1557 banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
1558
1559 // Retrieve the package name from the manifest
1560 std::string package;
1561 for (auto& parent : extractor()->parent_stack()) {
1562 if (auto manifest = ElementCast<Manifest>(parent)) {
1563 package = manifest->package;
1564 break;
1565 }
1566 }
1567
1568 // Fully qualify the activity name
Chih-Hung Hsiehf2ef6572020-02-11 14:27:11 -08001569 ssize_t idx = name.find('.');
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001570 if (idx == 0) {
1571 name = package + name;
1572 } else if (idx < 0) {
1573 name = package + "." + name;
1574 }
1575
1576 auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
1577 if (orientation) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00001578 CommonFeatureGroup* common = extractor()->common_feature_group();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001579 int orien = *orientation;
1580 if (orien == 0 || orien == 6 || orien == 8) {
1581 // Requests landscape, sensorLandscape, or reverseLandscape.
1582 common->addImpliedFeature("android.hardware.screen.landscape",
1583 "one or more activities have specified a landscape orientation",
1584 false);
1585 } else if (orien == 1 || orien == 7 || orien == 9) {
1586 // Requests portrait, sensorPortrait, or reversePortrait.
1587 common->addImpliedFeature("android.hardware.screen.portrait",
1588 "one or more activities have specified a portrait orientation",
1589 false);
1590 }
1591 }
1592 }
1593
Ryan Mitchell214846d2018-09-19 16:57:01 -07001594 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001595 // Print whether the activity has the HOME category and a the MAIN action
1596 if (has_main_action && has_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001597 printer->Print("launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001598 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001599 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001600 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001601 printer->Print(StringPrintf(" label='%s' icon='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001602 android::ResTable::normalizeForOutput(label.data()).c_str(),
1603 icon.data()));
1604 }
1605
1606 // Print wether the activity has the HOME category and a the MAIN action
1607 if (has_leanback_launcher_category) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001608 printer->Print("leanback-launchable-activity:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001609 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001610 printer->Print(StringPrintf(" name='%s' ", name.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001611 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001612 printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001613 android::ResTable::normalizeForOutput(label.data()).c_str(),
1614 icon.data(), banner.data()));
1615 }
1616 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001617
1618 void ToProto(pb::Badging* out_badging) override {
1619 if (has_main_action && has_launcher_category) {
1620 auto activity = out_badging->mutable_launchable_activity();
1621 activity->set_name(name);
1622 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1623 activity->set_icon(icon);
1624 }
1625 if (has_leanback_launcher_category) {
1626 auto activity = out_badging->mutable_leanback_launchable_activity();
1627 activity->set_name(name);
1628 activity->set_label(android::ResTable::normalizeForOutput(label.data()));
1629 activity->set_icon(icon);
1630 activity->set_banner(banner);
1631 }
1632 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001633};
1634
1635/** Represents <intent-filter> elements. */
1636class IntentFilter : public ManifestExtractor::Element {
1637 public:
1638 IntentFilter() = default;
1639};
1640
1641/** Represents <category> elements. */
1642class Category : public ManifestExtractor::Element {
1643 public:
1644 Category() = default;
1645 std::string component = "";
1646
1647 void Extract(xml::Element* element) override {
1648 const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
1649
1650 auto parent_stack = extractor()->parent_stack();
1651 if (category && ElementCast<IntentFilter>(parent_stack[0])
1652 && ElementCast<Activity>(parent_stack[1])) {
1653 Activity* activity = ElementCast<Activity>(parent_stack[1]);
1654
1655 if (*category == "android.intent.category.LAUNCHER") {
1656 activity->has_launcher_category = true;
1657 } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
1658 activity->has_leanback_launcher_category = true;
1659 } else if (*category == "android.intent.category.HOME") {
1660 component = "launcher";
1661 }
1662 }
1663 }
1664};
1665
1666/**
1667 * Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
1668 * elements nested within.
1669 **/
1670class Provider : public ManifestExtractor::Element {
1671 public:
1672 Provider() = default;
1673 bool has_required_saf_attributes = false;
1674
1675 void Extract(xml::Element* element) override {
1676 const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
1677 const int32_t* grant_uri_permissions = GetAttributeInteger(
1678 FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
1679 const std::string* permission = GetAttributeString(
1680 FindAttribute(element, PERMISSION_ATTR));
1681
1682 has_required_saf_attributes = ((exported && *exported != 0)
1683 && (grant_uri_permissions && *grant_uri_permissions != 0)
1684 && (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
1685 }
1686};
1687
1688/** Represents <receiver> elements. **/
1689class Receiver : public ManifestExtractor::Element {
1690 public:
1691 Receiver() = default;
1692 const std::string* permission = nullptr;
1693 bool has_component = false;
1694
1695 void Extract(xml::Element* element) override {
1696 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1697 }
1698};
1699
1700/**Represents <service> elements. **/
1701class Service : public ManifestExtractor::Element {
1702 public:
1703 Service() = default;
1704 const std::string* permission = nullptr;
1705 bool has_component = false;
1706
1707 void Extract(xml::Element* element) override {
1708 permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
1709 }
1710};
1711
1712/** Represents <uses-library> elements. **/
1713class UsesLibrary : public ManifestExtractor::Element {
1714 public:
1715 UsesLibrary() = default;
1716 std::string name;
1717 int required;
1718
1719 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001720 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1721 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001722 }
1723
Ryan Mitchell214846d2018-09-19 16:57:01 -07001724 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001725 if (!name.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001726 printer->Print(StringPrintf("uses-library%s:'%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001727 (required == 0) ? "-not-required" : "", name.data()));
1728 }
1729 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001730
1731 void ToProto(pb::Badging* out_badging) override {
1732 if (!name.empty()) {
1733 auto uses_library = out_badging->add_uses_libraries();
1734 uses_library->set_name(name);
1735 uses_library->set_required(required != 0);
1736 }
1737 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001738};
1739
Dianne Hackborn813d7502018-10-02 16:59:46 -07001740/** Represents <static-library> elements. **/
1741class StaticLibrary : public ManifestExtractor::Element {
1742 public:
1743 StaticLibrary() = default;
1744 std::string name;
1745 int version;
1746 int versionMajor;
1747
1748 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001749 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1750 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1751 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
Dianne Hackborn813d7502018-10-02 16:59:46 -07001752 }
1753
Ryan Mitchell214846d2018-09-19 16:57:01 -07001754 void Print(text::Printer* printer) override {
1755 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001756 "static-library: name='%s' version='%d' versionMajor='%d'\n",
1757 name.data(), version, versionMajor));
1758 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001759
1760 void ToProto(pb::Badging* out_badging) override {
1761 auto static_library = out_badging->mutable_static_library();
1762 static_library->set_name(name);
1763 static_library->set_version(version);
1764 static_library->set_version_major(versionMajor);
1765 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001766};
1767
1768/** Represents <uses-static-library> elements. **/
1769class UsesStaticLibrary : public ManifestExtractor::Element {
1770 public:
1771 UsesStaticLibrary() = default;
1772 std::string name;
1773 int version;
1774 int versionMajor;
1775 std::vector<std::string> certDigests;
1776
1777 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001778 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1779 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
1780 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1781 AddCertDigest(element);
Dianne Hackborn813d7502018-10-02 16:59:46 -07001782 }
1783
1784 void AddCertDigest(xml::Element* element) {
1785 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1786 // We allow ":" delimiters in the SHA declaration as this is the format
1787 // emitted by the certtool making it easy for developers to copy/paste.
1788 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1789 if (!digest.empty()) {
1790 certDigests.push_back(digest);
1791 }
1792 }
1793
Ryan Mitchell214846d2018-09-19 16:57:01 -07001794 void Print(text::Printer* printer) override {
1795 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07001796 "uses-static-library: name='%s' version='%d' versionMajor='%d'",
1797 name.data(), version, versionMajor));
1798 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07001799 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07001800 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001801 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07001802 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001803
1804 void ToProto(pb::Badging* out_badging) override {
1805 auto uses_static_library = out_badging->add_uses_static_libraries();
1806 uses_static_library->set_name(name);
1807 uses_static_library->set_version(version);
1808 uses_static_library->set_version_major(versionMajor);
1809 for (auto& cert : certDigests) {
1810 uses_static_library->add_certificates(cert);
1811 }
1812 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07001813};
1814
Alex Buynytskyya16137052021-12-02 13:26:54 +00001815/** Represents <sdk-library> elements. **/
1816class SdkLibrary : public ManifestExtractor::Element {
1817 public:
1818 SdkLibrary() = default;
1819 std::string name;
1820 int versionMajor;
1821
1822 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001823 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1824 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
Alex Buynytskyya16137052021-12-02 13:26:54 +00001825 }
1826
1827 void Print(text::Printer* printer) override {
1828 printer->Print(
1829 StringPrintf("sdk-library: name='%s' versionMajor='%d'\n", name.data(), versionMajor));
1830 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001831
1832 void ToProto(pb::Badging* out_badging) override {
1833 auto sdk_library = out_badging->mutable_sdk_library();
1834 sdk_library->set_name(name);
1835 sdk_library->set_version_major(versionMajor);
1836 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001837};
1838
1839/** Represents <uses-sdk-library> elements. **/
1840class UsesSdkLibrary : public ManifestExtractor::Element {
1841 public:
1842 UsesSdkLibrary() = default;
1843 std::string name;
1844 int versionMajor;
1845 std::vector<std::string> certDigests;
1846
1847 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001848 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1849 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
1850 AddCertDigest(element);
Alex Buynytskyya16137052021-12-02 13:26:54 +00001851 }
1852
1853 void AddCertDigest(xml::Element* element) {
1854 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
1855 // We allow ":" delimiters in the SHA declaration as this is the format
1856 // emitted by the certtool making it easy for developers to copy/paste.
1857 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
1858 if (!digest.empty()) {
1859 certDigests.push_back(digest);
1860 }
1861 }
1862
1863 void Print(text::Printer* printer) override {
1864 printer->Print(
1865 StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor));
1866 for (size_t i = 0; i < certDigests.size(); i++) {
1867 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
1868 }
1869 printer->Print("\n");
1870 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001871
1872 void ToProto(pb::Badging* out_badging) override {
1873 auto uses_sdk_library = out_badging->add_uses_sdk_libraries();
1874 uses_sdk_library->set_name(name);
1875 uses_sdk_library->set_version_major(versionMajor);
1876 for (auto& cert : certDigests) {
1877 uses_sdk_library->add_certificates(cert);
1878 }
1879 }
Alex Buynytskyya16137052021-12-02 13:26:54 +00001880};
1881
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001882/** Represents <uses-native-library> elements. **/
1883class UsesNativeLibrary : public ManifestExtractor::Element {
1884 public:
1885 UsesNativeLibrary() = default;
1886 std::string name;
1887 int required;
1888
1889 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00001890 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1891 required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001892 }
1893
1894 void Print(text::Printer* printer) override {
1895 if (!name.empty()) {
1896 printer->Print(StringPrintf("uses-native-library%s:'%s'\n",
1897 (required == 0) ? "-not-required" : "", name.data()));
1898 }
1899 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001900
1901 void ToProto(pb::Badging* out_badging) override {
1902 if (!name.empty()) {
1903 auto uses_native_library = out_badging->add_uses_native_libraries();
1904 uses_native_library->set_name(name);
1905 uses_native_library->set_required(required != 0);
1906 }
1907 }
Jiyong Park6a5b8b12020-06-30 13:23:36 +09001908};
1909
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001910/**
1911 * Represents <meta-data> elements. These tags are only printed when a flag is passed in to
1912 * explicitly enable meta data printing.
1913 **/
1914class MetaData : public ManifestExtractor::Element {
1915 public:
1916 MetaData() = default;
1917 std::string name;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001918 std::string value;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001919 const int* value_int;
Ryan Mitchell2250c932018-10-04 11:07:40 -07001920 std::string resource;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001921 const int* resource_int;
1922
1923 void Extract(xml::Element* element) override {
1924 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
Ryan Mitchell2250c932018-10-04 11:07:40 -07001925 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001926 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001927 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001928 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
1929 }
1930
Ryan Mitchell214846d2018-09-19 16:57:01 -07001931 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001932 if (extractor()->options_.include_meta_data && !name.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001933 printer->Print(StringPrintf("meta-data: name='%s'", name.data()));
Ryan Mitchell2250c932018-10-04 11:07:40 -07001934 if (!value.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001935 printer->Print(StringPrintf(" value='%s'", value.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001936 } else if (value_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001937 printer->Print(StringPrintf(" value='%d'", *value_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001938 } else {
Ryan Mitchell2250c932018-10-04 11:07:40 -07001939 if (!resource.empty()) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001940 printer->Print(StringPrintf(" resource='%s'", resource.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001941 } else if (resource_int) {
Iurii Makhnodcead622022-04-13 18:00:03 +00001942 printer->Print(StringPrintf(" resource='%d'", *resource_int));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001943 }
1944 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07001945 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001946 }
1947 }
Iurii Makhno85875a82022-04-26 15:30:01 +00001948
1949 void ToProto(pb::Badging* out_badging) override {
1950 if (!name.empty()) {
1951 auto metadata = out_badging->add_metadata();
1952 metadata->set_name(name);
1953 if (!value.empty()) {
1954 metadata->set_value_string(value);
1955 } else if (value_int) {
1956 metadata->set_value_int(*value_int);
1957 } else {
1958 if (!resource.empty()) {
1959 metadata->set_resource_string(resource);
1960 } else if (resource_int) {
1961 metadata->set_resource_int(*resource_int);
1962 }
1963 }
1964 }
1965 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001966};
1967
1968/**
1969 * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
1970 * service components.
1971 **/
1972class Action : public ManifestExtractor::Element {
1973 public:
1974 Action() = default;
1975 std::string component = "";
1976
1977 void Extract(xml::Element* element) override {
1978 auto parent_stack = extractor()->parent_stack();
1979 std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
1980
1981 if (ElementCast<IntentFilter>(parent_stack[0])) {
1982 if (ElementCast<Activity>(parent_stack[1])) {
1983 // Detects the presence of a particular type of activity.
1984 Activity* activity = ElementCast<Activity>(parent_stack[1]);
Iurii Makhno499ecd32022-08-19 16:34:26 +00001985 static const auto map = std::map<std::string, std::string>({
1986 {"android.intent.action.MAIN", "main"},
1987 {"android.media.action.VIDEO_CAMERA", "camera"},
1988 {"android.media.action.STILL_IMAGE_CAMERA", "camera"},
1989 {"android.media.action.STILL_IMAGE_CAMERA_SECURE", "camera-secure"},
Ryan Mitchellfc225b22018-08-21 14:52:51 -07001990 });
1991
1992 auto entry = map.find(action);
1993 if (entry != map.end()) {
1994 component = entry->second;
1995 activity->has_component_ = true;
1996 }
1997
1998 if (action == "android.intent.action.MAIN") {
1999 activity->has_main_action = true;
2000 }
2001
2002 } else if (ElementCast<Receiver>(parent_stack[1])) {
2003 // Detects the presence of a particular type of receiver. If the action requires a
2004 // permission, then the receiver element is checked for the permission.
2005 Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
2006 auto map = std::map<std::string, std::string>({
2007 { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
2008 { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
2009 });
2010
2011 auto permissions = std::map<std::string, std::string>({
2012 { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
2013 });
2014
2015 auto entry = map.find(action);
2016 auto permission = permissions.find(action);
2017 if (entry != map.end() && (permission == permissions.end()
2018 || (receiver->permission && permission->second == *receiver->permission))) {
2019 receiver->has_component = true;
2020 component = entry->second;
2021 }
2022
2023 } else if (ElementCast<Service>(parent_stack[1])) {
2024 // Detects the presence of a particular type of service. If the action requires a
2025 // permission, then the service element is checked for the permission.
2026 Service* service = ElementCast<Service>(parent_stack[1]);
2027 auto map = std::map<std::string, std::string>({
2028 { "android.view.InputMethod" , "ime" },
2029 { "android.service.wallpaper.WallpaperService" , "wallpaper" },
2030 { "android.accessibilityservice.AccessibilityService" , "accessibility" },
2031 { "android.printservice.PrintService" , "print-service" },
2032 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
2033 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
2034 { "android.service.notification.NotificationListenerService" ,"notification-listener" },
2035 { "android.service.dreams.DreamService" , "dream" },
2036 });
2037
2038 auto permissions = std::map<std::string, std::string>({
2039 { "android.accessibilityservice.AccessibilityService" ,
2040 "android.permission.BIND_ACCESSIBILITY_SERVICE" },
2041 { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
2042 { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
2043 "android.permission.BIND_NFC_SERVICE" },
2044 { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
2045 "android.permission.BIND_NFC_SERVICE" },
2046 { "android.service.notification.NotificationListenerService" ,
2047 "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
2048 { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
2049 });
2050
2051 auto entry = map.find(action);
2052 auto permission = permissions.find(action);
2053 if (entry != map.end() && (permission == permissions.end()
2054 || (service->permission && permission->second == *service->permission))) {
2055 service->has_component= true;
2056 component = entry->second;
2057 }
2058
2059 } else if (ElementCast<Provider>(parent_stack[1])) {
2060 // Detects the presence of a particular type of receiver. If the provider requires a
2061 // permission, then the provider element is checked for the permission.
2062 // Detect whether this action
2063 Provider* provider = ElementCast<Provider>(parent_stack[1]);
2064 if (action == "android.content.action.DOCUMENTS_PROVIDER"
2065 && provider->has_required_saf_attributes) {
2066 component = "document-provider";
2067 }
2068 }
2069 }
2070
2071 // Represents a searchable interface
2072 if (action == "android.intent.action.SEARCH") {
2073 component = "search";
2074 }
2075 }
2076};
2077
2078/**
2079 * Represents <supports-input> elements. The element may have <input-type> elements nested within.
2080 **/
2081class SupportsInput : public ManifestExtractor::Element {
2082 public:
2083 SupportsInput() = default;
2084 std::vector<std::string> inputs;
2085
Ryan Mitchell214846d2018-09-19 16:57:01 -07002086 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002087 const size_t size = inputs.size();
2088 if (size > 0) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002089 printer->Print("supports-input: '");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002090 for (size_t i = 0; i < size; i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002091 printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002092 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002093 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002094 }
2095 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002096
2097 void ToProto(pb::Badging* out_badging) override {
2098 auto supports_input = out_badging->mutable_supports_input();
2099 for (auto& input : inputs) {
2100 supports_input->add_inputs(input);
2101 }
2102 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002103};
2104
2105/** Represents <input-type> elements. **/
2106class InputType : public ManifestExtractor::Element {
2107 public:
2108 InputType() = default;
2109 void Extract(xml::Element* element) override {
2110 auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2111 auto parent_stack = extractor()->parent_stack();
2112
2113 // Add the input to the set of supported inputs
2114 if (name && ElementCast<SupportsInput>(parent_stack[0])) {
2115 SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
2116 supports->inputs.push_back(*name);
2117 }
2118 }
2119};
2120
2121/** Represents <original-package> elements. **/
2122class OriginalPackage : public ManifestExtractor::Element {
2123 public:
2124 OriginalPackage() = default;
2125 const std::string* name = nullptr;
2126
2127 void Extract(xml::Element* element) override {
2128 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2129 }
2130
Ryan Mitchell214846d2018-09-19 16:57:01 -07002131 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002132 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002133 printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002134 }
2135 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002136
2137 void ToProto(pb::Badging* out_badging) override {
2138 if (name) {
2139 out_badging->mutable_package()->set_original_package(*name);
2140 }
2141 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002142};
2143
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002144
2145/** Represents <overlay> elements. **/
2146class Overlay : public ManifestExtractor::Element {
2147 public:
2148 Overlay() = default;
2149 const std::string* target_package = nullptr;
2150 int priority;
2151 bool is_static;
2152 const std::string* required_property_name = nullptr;
2153 const std::string* required_property_value = nullptr;
2154
2155 void Extract(xml::Element* element) override {
2156 target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
2157 priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
2158 is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
2159 required_property_name = GetAttributeString(
2160 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
2161 required_property_value = GetAttributeString(
2162 FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
2163 }
2164
2165 void Print(text::Printer* printer) override {
2166 printer->Print(StringPrintf("overlay:"));
2167 if (target_package) {
2168 printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
2169 }
2170 printer->Print(StringPrintf(" priority='%d'", priority));
2171 printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
2172 if (required_property_name) {
2173 printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
2174 }
2175 if (required_property_value) {
2176 printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
2177 }
2178 printer->Print("\n");
2179 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002180
2181 void ToProto(pb::Badging* out_badging) override {
2182 auto overlay = out_badging->mutable_overlay();
2183 if (target_package) {
2184 overlay->set_target_package(*target_package);
2185 }
2186 overlay->set_priority(priority);
2187 overlay->set_static_(is_static);
2188 if (required_property_name) {
2189 overlay->set_required_property_name(*required_property_name);
2190 }
2191 if (required_property_value) {
2192 overlay->set_required_property_value(*required_property_value);
2193 }
2194 }
Anton Hanssoncd2d8e22018-12-11 13:52:17 +00002195};
2196
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002197/** * Represents <package-verifier> elements. **/
2198class PackageVerifier : public ManifestExtractor::Element {
2199 public:
2200 PackageVerifier() = default;
2201 const std::string* name = nullptr;
2202 const std::string* public_key = nullptr;
2203
2204 void Extract(xml::Element* element) override {
2205 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2206 public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
2207 }
2208
Ryan Mitchell214846d2018-09-19 16:57:01 -07002209 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002210 if (name && public_key) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002211 printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002212 name->data(), public_key->data()));
2213 }
2214 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002215
2216 void ToProto(pb::Badging* out_badging) override {
2217 auto package_verifier = out_badging->mutable_package_verifier();
2218 if (name && public_key) {
2219 package_verifier->set_name(*name);
2220 package_verifier->set_public_key(*public_key);
2221 }
2222 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002223};
2224
2225/** Represents <uses-package> elements. **/
2226class UsesPackage : public ManifestExtractor::Element {
2227 public:
2228 UsesPackage() = default;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002229 const std::string* packageType = nullptr;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002230 const std::string* name = nullptr;
Dianne Hackborn813d7502018-10-02 16:59:46 -07002231 int version;
2232 int versionMajor;
2233 std::vector<std::string> certDigests;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002234
2235 void Extract(xml::Element* element) override {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002236 packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
2237 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2238 version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
2239 versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
2240 AddCertDigest(element);
Dianne Hackborn813d7502018-10-02 16:59:46 -07002241 }
2242
2243 void AddCertDigest(xml::Element* element) {
2244 std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
2245 // We allow ":" delimiters in the SHA declaration as this is the format
2246 // emitted by the certtool making it easy for developers to copy/paste.
2247 digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
2248 if (!digest.empty()) {
2249 certDigests.push_back(digest);
2250 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002251 }
2252
Ryan Mitchell214846d2018-09-19 16:57:01 -07002253 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002254 if (name) {
Dianne Hackborn813d7502018-10-02 16:59:46 -07002255 if (packageType) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002256 printer->Print(StringPrintf(
Dianne Hackborn813d7502018-10-02 16:59:46 -07002257 "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
2258 packageType->data(), name->data(), version, versionMajor));
2259 for (size_t i = 0; i < certDigests.size(); i++) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002260 printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002261 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002262 printer->Print("\n");
Dianne Hackborn813d7502018-10-02 16:59:46 -07002263 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002264 printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
Dianne Hackborn813d7502018-10-02 16:59:46 -07002265 }
2266 }
2267 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002268
2269 void ToProto(pb::Badging* out_badging) override {
2270 if (name) {
2271 auto uses_package = out_badging->add_uses_packages();
2272 uses_package->set_name(*name);
2273 if (packageType) {
2274 uses_package->set_package_type(*packageType);
2275 uses_package->set_version(version);
2276 uses_package->set_version_major(versionMajor);
2277 for (auto& cert : certDigests) {
2278 uses_package->add_certificates(cert);
2279 }
2280 }
2281 }
2282 }
Dianne Hackborn813d7502018-10-02 16:59:46 -07002283};
2284
2285/** Represents <additional-certificate> elements. **/
2286class AdditionalCertificate : public ManifestExtractor::Element {
2287 public:
2288 AdditionalCertificate() = default;
2289
2290 void Extract(xml::Element* element) override {
2291 auto parent_stack = extractor()->parent_stack();
2292 if (parent_stack.size() > 0) {
2293 if (ElementCast<UsesPackage>(parent_stack[0])) {
2294 UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
2295 uses->AddCertDigest(element);
2296 } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
2297 UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
2298 uses->AddCertDigest(element);
2299 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002300 }
2301 }
2302};
2303
2304/** Represents <screen> elements found in <compatible-screens> elements. */
2305class Screen : public ManifestExtractor::Element {
2306 public:
2307 Screen() = default;
2308 const int32_t* size = nullptr;
2309 const int32_t* density = nullptr;
2310
2311 void Extract(xml::Element* element) override {
2312 size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
2313 density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
2314 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002315
2316 void ToProto(pb::Badging* out_badging) override {
2317 if (size && density) {
2318 auto screen = out_badging->mutable_compatible_screens()->add_screens();
2319 screen->set_density(*density);
2320 screen->set_size(*size);
2321 }
2322 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002323};
2324
2325/**
2326 * Represents <compatible-screens> elements. These elements have <screen> elements nested within
2327 * that each denote a supported screen size and screen density.
2328 **/
2329class CompatibleScreens : public ManifestExtractor::Element {
2330 public:
2331 CompatibleScreens() = default;
Ryan Mitchell214846d2018-09-19 16:57:01 -07002332 void Print(text::Printer* printer) override {
2333 printer->Print("compatible-screens:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002334
2335 bool first = true;
2336 ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
2337 if (auto screen = ElementCast<Screen>(el)) {
2338 if (first) {
2339 first = false;
2340 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002341 printer->Print(",");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002342 }
2343
2344 if (screen->size && screen->density) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002345 printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002346 }
2347 }
2348 });
Ryan Mitchell214846d2018-09-19 16:57:01 -07002349 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002350 }
2351};
2352
2353/** Represents <supports-gl-texture> elements. **/
2354class SupportsGlTexture : public ManifestExtractor::Element {
2355 public:
2356 SupportsGlTexture() = default;
2357 const std::string* name = nullptr;
2358
2359 void Extract(xml::Element* element) override {
2360 name = GetAttributeString(FindAttribute(element, NAME_ATTR));
2361 }
2362
Ryan Mitchell214846d2018-09-19 16:57:01 -07002363 void Print(text::Printer* printer) override {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002364 if (name) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002365 printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002366 }
2367 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002368
2369 void ToProto(pb::Badging* out_badging) override {
2370 if (name) {
2371 out_badging->mutable_supports_gl_texture()->add_name(*name);
2372 }
2373 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002374};
2375
Todd Kennedyce3e1292020-10-29 17:14:24 -07002376/** Represents <property> elements. **/
2377class Property : public ManifestExtractor::Element {
2378 public:
2379 Property() = default;
2380 std::string name;
2381 std::string value;
2382 const int* value_int;
2383 std::string resource;
2384 const int* resource_int;
2385
2386 void Extract(xml::Element* element) override {
2387 name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
2388 value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
2389 value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
2390 resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
2391 resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
2392 }
2393
2394 void Print(text::Printer* printer) override {
2395 printer->Print(StringPrintf("property: name='%s' ", name.data()));
2396 if (!value.empty()) {
2397 printer->Print(StringPrintf("value='%s' ", value.data()));
2398 } else if (value_int) {
2399 printer->Print(StringPrintf("value='%d' ", *value_int));
2400 } else {
2401 if (!resource.empty()) {
2402 printer->Print(StringPrintf("resource='%s' ", resource.data()));
2403 } else if (resource_int) {
2404 printer->Print(StringPrintf("resource='%d' ", *resource_int));
2405 }
2406 }
2407 printer->Print("\n");
2408 }
Todd Kennedyce3e1292020-10-29 17:14:24 -07002409
Iurii Makhno85875a82022-04-26 15:30:01 +00002410 void ToProto(pb::Badging* out_badging) override {
2411 if (!name.empty()) {
2412 auto property = out_badging->add_properties();
2413 property->set_name(name);
2414 if (!value.empty()) {
2415 property->set_value_string(value);
2416 } else if (value_int) {
2417 property->set_value_int(*value_int);
2418 } else {
2419 if (!resource.empty()) {
2420 property->set_resource_string(resource);
2421 } else if (resource_int) {
2422 property->set_resource_int(*resource_int);
2423 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002424 }
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002425 }
2426 }
2427};
2428
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002429/** Recursively prints the extracted badging element. */
Ryan Mitchell214846d2018-09-19 16:57:01 -07002430static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002431 el->Print(printer);
2432 for (auto &child : el->children()) {
2433 Print(child.get(), printer);
2434 }
2435}
2436
Iurii Makhno85875a82022-04-26 15:30:01 +00002437/** Recursively serializes extracted badging elements to proto. */
2438static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) {
2439 el->ToProto(out_badging);
2440 for (auto& child : el->children()) {
2441 ToProto(child.get(), out_badging);
2442 }
2443}
2444
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002445bool ManifestExtractor::Extract(android::IDiagnostics* diag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002446 // Load the manifest
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002447 doc_ = apk_->LoadXml("AndroidManifest.xml", diag);
2448 if (doc_ == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002449 diag->Error(android::DiagMessage() << "failed to find AndroidManifest.xml");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002450 return false;
2451 }
2452
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002453 xml::Element* element = doc_->root.get();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002454 if (element->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00002455 diag->Error(android::DiagMessage() << "manifest does not start with <manifest> tag");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002456 return false;
2457 }
2458
2459 // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
2460 // printing only permission elements is requested
2461 if (options_.only_permissions) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002462 root_element_ = ManifestExtractor::Element::Inflate(this, element, "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002463
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002464 if (auto manifest = ElementCast<Manifest>(root_element_.get())) {
2465 manifest->only_package_name = true;
2466
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002467 for (xml::Element* child : element->GetChildElements()) {
2468 if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
2469 || child->name == "permission") {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002470 // Inflate the element and its descendants
Iurii Makhno32c0c032022-12-08 18:07:58 +00002471 auto permission_element = Visit(child, "manifest");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002472 manifest->AddChild(permission_element);
2473 }
2474 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002475 return true;
2476 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002477 return false;
2478 }
2479
2480 // Collect information about the resource configurations
2481 if (apk_->GetResourceTable()) {
2482 for (auto &package : apk_->GetResourceTable()->packages) {
2483 for (auto &type : package->types) {
2484 for (auto &entry : type->entries) {
2485 for (auto &value : entry->values) {
2486 std::string locale_str = value->config.GetBcp47LanguageTag();
2487
2488 // Collect all the unique locales of the apk
2489 if (locales_.find(locale_str) == locales_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002490 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002491 config.setBcp47Locale(locale_str.data());
2492 locales_.insert(std::make_pair(locale_str, config));
2493 }
2494
2495 // Collect all the unique density of the apk
2496 uint16_t density = (value->config.density == 0) ? (uint16_t) 160
2497 : value->config.density;
2498 if (densities_.find(density) == densities_.end()) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -07002499 ConfigDescription config = ManifestExtractor::DefaultConfig();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002500 config.density = density;
2501 densities_.insert(std::make_pair(density, config));
2502 }
2503 }
2504 }
2505 }
2506 }
2507 }
2508
2509 // Extract badging information
Iurii Makhno32c0c032022-12-08 18:07:58 +00002510 root_element_ = Visit(element, "");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002511
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002512 // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the
2513 // attribute values from the last defined tag.
2514 std::vector<UsesSdkBadging*> filtered_uses_sdk_tags;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002515 for (const auto& child : root_element_->children()) {
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002516 if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) {
2517 filtered_uses_sdk_tags.emplace_back(uses_sdk);
2518 }
2519 }
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002520 if (filtered_uses_sdk_tags.size() >= 2U) {
2521 filtered_uses_sdk_tags.pop_back();
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002522 root_element_->Filter([&](const ManifestExtractor::Element* e) {
Ryan Mitchell6ea9ed32021-05-20 11:38:57 -07002523 return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) !=
2524 filtered_uses_sdk_tags.end();
2525 });
2526 }
Ryan Mitchell1966e1f2021-05-03 13:46:56 -07002527
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002528 /** Recursively checks the extracted elements for the specified permission. **/
2529 auto FindPermission = [&](ManifestExtractor::Element* root,
2530 const std::string& name) -> ManifestExtractor::Element* {
2531 return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
2532 if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
2533 return permission->name == name;
2534 }
2535 return false;
2536 });
2537 };
2538
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002539 auto AddImpliedPermission = [&](const std::string& name, const std::string& reason,
2540 int32_t max_sdk_version) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002541 auto permission = util::make_unique<UsesPermission>();
2542 permission->name = name;
2543 permission->maxSdkVersion = max_sdk_version;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002544 permission->implied = true;
2545 permission->impliedReason = reason;
2546 implied_permissions_.push_back(std::move(permission));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002547 };
2548
2549 // Implied permissions
2550 // Pre-1.6 implicitly granted permission compatibility logic
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002551 bool insert_write_external = false;
2552 auto write_external_permission = ElementCast<UsesPermission>(
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002553 FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002554
Jackal Guo201a60a2021-08-31 12:37:30 +08002555 if (target_sdk() < SDK_DONUT) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002556 if (!write_external_permission) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002557 AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002558 insert_write_external = true;
2559 }
2560
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002561 if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) {
2562 AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002563 }
2564 }
2565
2566 // If the application has requested WRITE_EXTERNAL_STORAGE, we will
2567 // force them to always take READ_EXTERNAL_STORAGE as well. We always
2568 // do this (regardless of target API version) because we can't have
2569 // an app with write permission but not read permission.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002570 auto read_external =
2571 FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002572 if (!read_external && (insert_write_external || write_external_permission)) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002573 AddImpliedPermission(
2574 "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE",
2575 (write_external_permission) ? write_external_permission->maxSdkVersion : -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002576 }
2577
2578 // Pre-JellyBean call log permission compatibility.
Jackal Guo201a60a2021-08-31 12:37:30 +08002579 if (target_sdk() < SDK_JELLY_BEAN) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002580 if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") &&
2581 FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) {
2582 AddImpliedPermission("android.permission.READ_CALL_LOG",
2583 "targetSdkVersion < 16 and requested READ_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002584 }
2585
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002586 if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") &&
2587 FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) {
2588 AddImpliedPermission("android.permission.WRITE_CALL_LOG",
2589 "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002590 }
2591 }
2592
2593 // If the app hasn't declared the touchscreen as a feature requirement (either
2594 // directly or implied, required or not), then the faketouch feature is implied.
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002595 if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) {
2596 common_feature_group()->addImpliedFeature("android.hardware.faketouch",
2597 "default feature for all apps", false);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002598 }
2599
2600 // Only print the common feature group if no feature group is defined
2601 std::vector<FeatureGroup*> feature_groups;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002602 ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002603 if (auto feature_group = ElementCast<FeatureGroup>(el)) {
2604 feature_groups.push_back(feature_group);
2605 }
2606 });
2607
2608 if (feature_groups.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002609 feature_groups_.push_back(common_feature_group());
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002610 } else {
2611 // Merge the common feature group into the feature group
2612 for (auto& feature_group : feature_groups) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002613 feature_group->Merge(common_feature_group());
2614 feature_groups_.push_back(feature_group);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002615 }
2616 };
2617
2618 // Collect the component types of the application
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002619 ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002620 if (ElementCast<Action>(el)) {
2621 auto action = ElementCast<Action>(el);
2622 if (!action->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002623 components_.discovered_components.insert(action->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002624 return;
2625 }
2626 }
2627
2628 if (ElementCast<Category>(el)) {
2629 auto category = ElementCast<Category>(el);
2630 if (!category->component.empty()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002631 components_.discovered_components.insert(category->component);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002632 return;
2633 }
2634 }
2635 });
2636
2637 // Check for the payment component
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002638 ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002639 if (auto service = ElementCast<Service>(el)) {
2640 auto host_apdu_action = ElementCast<Action>(FindElement(service,
2641 [&](ManifestExtractor::Element* el) -> bool {
2642 if (auto action = ElementCast<Action>(el)) {
2643 return (action->component == "host-apdu");
2644 }
2645 return false;
2646 }));
2647
2648 auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
2649 [&](ManifestExtractor::Element* el) -> bool {
2650 if (auto action = ElementCast<Action>(el)) {
2651 return (action->component == "offhost-apdu");
2652 }
2653 return false;
2654 }));
2655
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002656 ForEachChild(service,
2657 [this, &diag, &host_apdu_action,
2658 &offhost_apdu_action](ManifestExtractor::Element* el) -> void {
2659 if (auto meta_data = ElementCast<MetaData>(el)) {
2660 if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" &&
2661 host_apdu_action) ||
2662 (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" &&
2663 offhost_apdu_action)) {
2664 // Attempt to load the resource file
Iurii Makhno32c0c032022-12-08 18:07:58 +00002665 if (meta_data->resource.empty()) {
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002666 return;
2667 }
2668 auto resource = this->apk_->LoadXml(meta_data->resource, diag);
2669 if (!resource) {
2670 return;
2671 }
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002672
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002673 // Look for the payment category on an <aid-group> element
2674 auto& root = resource.get()->root;
2675 if ((host_apdu_action && root->name == "host-apdu-service") ||
2676 (offhost_apdu_action && root->name == "offhost-apdu-service")) {
2677 for (auto& child : root->GetChildElements()) {
2678 if (child->name == "aid-group") {
2679 auto category = FindAttribute(child, CATEGORY_ATTR);
2680 if (category && category->value == "payment") {
Iurii Makhno85875a82022-04-26 15:30:01 +00002681 this->components_.discovered_components.insert("payment");
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002682 return;
2683 }
2684 }
2685 }
2686 }
2687 }
2688 }
2689 });
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002690 }
2691 });
2692
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002693 // Print presence of activities, receivers, and services with no special components
2694 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2695 if (auto activity = ElementCast<Activity>(el)) {
2696 if (!activity->has_component_) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002697 components_.other_activities = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002698 return true;
2699 }
2700 }
2701 return false;
2702 });
2703
2704 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2705 if (auto receiver = ElementCast<Receiver>(el)) {
2706 if (!receiver->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002707 components_.other_receivers = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002708 return true;
2709 }
2710 }
2711 return false;
2712 });
2713
2714 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2715 if (auto service = ElementCast<Service>(el)) {
2716 if (!service->has_component) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002717 components_.other_services = true;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002718 return true;
2719 }
2720 }
2721 return false;
2722 });
2723
2724 // Gather the supported screens
2725 const static SupportsScreen default_screens{};
2726 SupportsScreen* screen = ElementCast<SupportsScreen>(
2727 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2728 return ElementCast<SupportsScreen>(el) != nullptr;
2729 }));
2730 supports_screen_ = screen ? screen : &default_screens;
2731
2732 // Gather the supported architectures_ of the app
2733 std::set<std::string> architectures_from_apk;
2734 auto it = apk_->GetFileCollection()->Iterator();
2735 while (it->HasNext()) {
2736 auto file_path = it->Next()->GetSource().path;
Iurii Makhno499ecd32022-08-19 16:34:26 +00002737 if (file_path.starts_with("lib/")) {
2738 file_path = file_path.substr(4);
2739 size_t pos = file_path.find('/');
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002740 if (pos != std::string::npos) {
2741 file_path = file_path.substr(0, pos);
2742 }
2743
2744 architectures_from_apk.insert(file_path);
2745 }
2746 }
2747
2748 // Determine if the application has multiArch supports
2749 auto has_multi_arch =
2750 FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool {
2751 if (auto application = ElementCast<Application>(el)) {
2752 return application->has_multi_arch;
2753 }
2754 return false;
2755 });
2756
2757 bool output_alt_native_code = false;
2758 // A multiArch package is one that contains 64-bit and
2759 // 32-bit versions of native code and expects 3rd-party
2760 // apps to load these native code libraries. Since most
2761 // 64-bit systems also support 32-bit apps, the apps
2762 // loading this multiArch package's code may be either
2763 if (has_multi_arch) {
2764 // If this is a multiArch package, report the 64-bit
2765 // version only. Then as a separate entry, report the
2766 // rest.
2767 //
2768 // If we report the 32-bit architecture, this APK will
2769 // be installed on a 32-bit device, causing a large waste
2770 // of bandwidth and disk space. This assumes that
2771 // the developer of the multiArch package has also
2772 // made a version that is 32-bit only.
2773 const std::string kIntel64 = "x86_64";
2774 const std::string kArm64 = "arm64-v8a";
2775
2776 auto arch = architectures_from_apk.find(kIntel64);
2777 if (arch == architectures_from_apk.end()) {
2778 arch = architectures_from_apk.find(kArm64);
2779 }
2780
2781 if (arch != architectures_from_apk.end()) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002782 architectures_.architectures.insert(*arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002783 architectures_from_apk.erase(arch);
2784 output_alt_native_code = true;
2785 }
2786 }
2787 for (auto& arch : architectures_from_apk) {
2788 if (output_alt_native_code) {
Iurii Makhno85875a82022-04-26 15:30:01 +00002789 architectures_.alt_architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002790 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002791 architectures_.architectures.insert(arch);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002792 }
2793 }
2794 return true;
2795}
2796
2797bool ManifestExtractor::Dump(text::Printer* printer) {
2798 Print(root_element_.get(), printer);
2799 if (options_.only_permissions) {
2800 return true;
2801 }
2802
2803 for (auto& implied_permission : implied_permissions_) {
2804 implied_permission->Print(printer);
2805 }
2806 for (auto& feature_group : feature_groups_) {
2807 feature_group->PrintGroup(printer);
2808 }
Iurii Makhno85875a82022-04-26 15:30:01 +00002809 components_.Print(printer);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00002810 supports_screen_->PrintScreens(printer, target_sdk_);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002811
2812 // Print all the unique locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002813 printer->Print("locales:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002814 for (auto& config : locales_) {
2815 if (config.first.empty()) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002816 printer->Print(" '--_--'");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002817 } else {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002818 printer->Print(StringPrintf(" '%s'", config.first.data()));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002819 }
2820 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002821 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002822
2823 // Print all the densities locales of the apk
Ryan Mitchell214846d2018-09-19 16:57:01 -07002824 printer->Print("densities:");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002825 for (auto& config : densities_) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07002826 printer->Print(StringPrintf(" '%d'", config.first));
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002827 }
Ryan Mitchell214846d2018-09-19 16:57:01 -07002828 printer->Print("\n");
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002829
Iurii Makhno85875a82022-04-26 15:30:01 +00002830 architectures_.Print(printer);
2831 return true;
2832}
2833
2834bool ManifestExtractor::DumpProto(pb::Badging* out_badging) {
2835 ToProto(root_element_.get(), out_badging);
2836 for (auto& implied_permission : implied_permissions_) {
2837 implied_permission->ToProto(out_badging);
2838 }
2839 for (auto& feature_group : feature_groups_) {
2840 feature_group->GroupToProto(out_badging);
2841 }
2842 components_.ToProto(out_badging);
2843 supports_screen_->ToProtoScreens(out_badging, target_sdk_);
2844
2845 for (auto& config : locales_) {
Iurii Makhnocfc559b2022-08-25 15:57:52 +00002846 if (config.first.empty()) {
2847 out_badging->add_locales("--_--");
2848 } else {
Iurii Makhno85875a82022-04-26 15:30:01 +00002849 out_badging->add_locales(config.first);
2850 }
2851 }
2852 for (auto& config : densities_) {
2853 out_badging->add_densities(config.first);
2854 }
2855
2856 architectures_.ToProto(out_badging);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002857 return true;
2858}
2859
Iurii Makhno32c0c032022-12-08 18:07:58 +00002860template <typename T>
2861constexpr const char* GetExpectedTagForType() {
2862 // This array does not appear at runtime, as GetExpectedTagForType function is used by compiler
2863 // to inject proper 'expected_tag' into ElementCast.
2864 std::array<std::pair<const char*, bool>, 37> tags = {
2865 std::make_pair("action", std::is_same<Action, T>::value),
2866 std::make_pair("activity", std::is_same<Activity, T>::value),
2867 std::make_pair("additional-certificate", std::is_same<AdditionalCertificate, T>::value),
2868 std::make_pair("application", std::is_same<Application, T>::value),
2869 std::make_pair("category", std::is_same<Category, T>::value),
2870 std::make_pair("compatible-screens", std::is_same<CompatibleScreens, T>::value),
2871 std::make_pair("feature-group", std::is_same<FeatureGroup, T>::value),
2872 std::make_pair("input-type", std::is_same<InputType, T>::value),
2873 std::make_pair("intent-filter", std::is_same<IntentFilter, T>::value),
2874 std::make_pair("meta-data", std::is_same<MetaData, T>::value),
2875 std::make_pair("manifest", std::is_same<Manifest, T>::value),
2876 std::make_pair("original-package", std::is_same<OriginalPackage, T>::value),
2877 std::make_pair("overlay", std::is_same<Overlay, T>::value),
2878 std::make_pair("package-verifier", std::is_same<PackageVerifier, T>::value),
2879 std::make_pair("permission", std::is_same<Permission, T>::value),
2880 std::make_pair("property", std::is_same<Property, T>::value),
2881 std::make_pair("provider", std::is_same<Provider, T>::value),
2882 std::make_pair("receiver", std::is_same<Receiver, T>::value),
2883 std::make_pair("required-feature", std::is_same<RequiredFeature, T>::value),
2884 std::make_pair("required-not-feature", std::is_same<RequiredNotFeature, T>::value),
2885 std::make_pair("screen", std::is_same<Screen, T>::value),
2886 std::make_pair("service", std::is_same<Service, T>::value),
2887 std::make_pair("sdk-library", std::is_same<SdkLibrary, T>::value),
2888 std::make_pair("static-library", std::is_same<StaticLibrary, T>::value),
2889 std::make_pair("supports-gl-texture", std::is_same<SupportsGlTexture, T>::value),
2890 std::make_pair("supports-input", std::is_same<SupportsInput, T>::value),
2891 std::make_pair("supports-screens", std::is_same<SupportsScreen, T>::value),
2892 std::make_pair("uses-configuration", std::is_same<UsesConfiguarion, T>::value),
2893 std::make_pair("uses-feature", std::is_same<UsesFeature, T>::value),
2894 std::make_pair("uses-library", std::is_same<UsesLibrary, T>::value),
2895 std::make_pair("uses-native-library", std::is_same<UsesNativeLibrary, T>::value),
2896 std::make_pair("uses-package", std::is_same<UsesPackage, T>::value),
2897 std::make_pair("uses-permission", std::is_same<UsesPermission, T>::value),
2898 std::make_pair("uses-permission-sdk-23", std::is_same<UsesPermissionSdk23, T>::value),
2899 std::make_pair("uses-sdk", std::is_same<UsesSdkBadging, T>::value),
2900 std::make_pair("uses-sdk-library", std::is_same<UsesSdkLibrary, T>::value),
2901 std::make_pair("uses-static-library", std::is_same<UsesStaticLibrary, T>::value),
2902 };
2903 for (const auto& pair : tags) {
2904 if (pair.second) {
2905 return pair.first;
2906 }
2907 }
2908 return nullptr;
2909}
2910
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002911/**
2912 * Returns the element casted to the type if the element is of that type. Otherwise, returns a null
2913 * pointer.
2914 **/
2915template<typename T>
2916T* ElementCast(ManifestExtractor::Element* element) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00002917 constexpr const char* expected_tag = GetExpectedTagForType<T>();
2918 if (element != nullptr && expected_tag != nullptr && element->is_featured() &&
2919 element->tag() == expected_tag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002920 return static_cast<T*>(element);
2921 }
2922 return nullptr;
2923}
2924
2925template<typename T>
2926std::unique_ptr<T> CreateType() {
2927 return std::move(util::make_unique<T>());
2928}
2929
2930std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
Iurii Makhno32c0c032022-12-08 18:07:58 +00002931 ManifestExtractor* extractor, xml::Element* el, const std::string& parent_tag) {
2932 static const std::unordered_map<std::string_view,
2933 std::function<std::unique_ptr<ManifestExtractor::Element>()>>
Ryan Mitchellfc225b22018-08-21 14:52:51 -07002934 kTagCheck = {
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002935 {"action", &CreateType<Action>},
2936 {"activity", &CreateType<Activity>},
2937 {"additional-certificate", &CreateType<AdditionalCertificate>},
2938 {"application", &CreateType<Application>},
2939 {"category", &CreateType<Category>},
2940 {"compatible-screens", &CreateType<CompatibleScreens>},
2941 {"feature-group", &CreateType<FeatureGroup>},
2942 {"input-type", &CreateType<InputType>},
2943 {"intent-filter", &CreateType<IntentFilter>},
2944 {"manifest", &CreateType<Manifest>},
2945 {"meta-data", &CreateType<MetaData>},
2946 {"original-package", &CreateType<OriginalPackage>},
2947 {"overlay", &CreateType<Overlay>},
2948 {"package-verifier", &CreateType<PackageVerifier>},
2949 {"permission", &CreateType<Permission>},
Todd Kennedyce3e1292020-10-29 17:14:24 -07002950 {"property", &CreateType<Property>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002951 {"provider", &CreateType<Provider>},
2952 {"receiver", &CreateType<Receiver>},
2953 {"required-feature", &CreateType<RequiredFeature>},
2954 {"required-not-feature", &CreateType<RequiredNotFeature>},
2955 {"screen", &CreateType<Screen>},
2956 {"service", &CreateType<Service>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002957 {"sdk-library", &CreateType<SdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002958 {"static-library", &CreateType<StaticLibrary>},
2959 {"supports-gl-texture", &CreateType<SupportsGlTexture>},
2960 {"supports-input", &CreateType<SupportsInput>},
2961 {"supports-screens", &CreateType<SupportsScreen>},
2962 {"uses-configuration", &CreateType<UsesConfiguarion>},
2963 {"uses-feature", &CreateType<UsesFeature>},
2964 {"uses-library", &CreateType<UsesLibrary>},
2965 {"uses-native-library", &CreateType<UsesNativeLibrary>},
2966 {"uses-package", &CreateType<UsesPackage>},
2967 {"uses-permission", &CreateType<UsesPermission>},
2968 {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
2969 {"uses-sdk", &CreateType<UsesSdkBadging>},
Alex Buynytskyya16137052021-12-02 13:26:54 +00002970 {"uses-sdk-library", &CreateType<UsesSdkLibrary>},
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +00002971 {"uses-static-library", &CreateType<UsesStaticLibrary>},
2972 };
Iurii Makhno32c0c032022-12-08 18:07:58 +00002973 static constexpr std::array<std::pair<std::string_view, std::string_view>, 53>
2974 kValidChildParentTags = {
2975 std::make_pair("action", "intent-filter"),
2976 std::make_pair("activity", "application"),
2977 std::make_pair("additional-certificate", "uses-package"),
2978 std::make_pair("additional-certificate", "uses-static-library"),
2979 std::make_pair("application", "manifest"),
2980 std::make_pair("category", "intent-filter"),
2981 std::make_pair("compatible-screens", "manifest"),
2982 std::make_pair("feature-group", "manifest"),
2983 std::make_pair("input-type", "supports-input"),
2984 std::make_pair("intent-filter", "activity"),
2985 std::make_pair("intent-filter", "activity-alias"),
2986 std::make_pair("intent-filter", "service"),
2987 std::make_pair("intent-filter", "receiver"),
2988 std::make_pair("intent-filter", "provider"),
2989 std::make_pair("manifest", ""),
2990 std::make_pair("meta-data", "activity"),
2991 std::make_pair("meta-data", "activity-alias"),
2992 std::make_pair("meta-data", "application"),
2993 std::make_pair("meta-data", "service"),
2994 std::make_pair("meta-data", "receiver"),
2995 std::make_pair("meta-data", "provider"),
2996 std::make_pair("original-package", "manifest"),
2997 std::make_pair("overlay", "manifest"),
2998 std::make_pair("package-verifier", "manifest"),
2999 std::make_pair("permission", "manifest"),
3000 std::make_pair("property", "activity"),
3001 std::make_pair("property", "activity-alias"),
3002 std::make_pair("property", "application"),
3003 std::make_pair("property", "service"),
3004 std::make_pair("property", "receiver"),
3005 std::make_pair("property", "provider"),
3006 std::make_pair("provider", "application"),
3007 std::make_pair("receiver", "application"),
3008 std::make_pair("required-feature", "uses-permission"),
3009 std::make_pair("required-not-feature", "uses-permission"),
3010 std::make_pair("screen", "compatible-screens"),
3011 std::make_pair("service", "application"),
3012 std::make_pair("sdk-library", "application"),
3013 std::make_pair("static-library", "application"),
3014 std::make_pair("supports-gl-texture", "manifest"),
3015 std::make_pair("supports-input", "manifest"),
3016 std::make_pair("supports-screens", "manifest"),
3017 std::make_pair("uses-configuration", "manifest"),
3018 std::make_pair("uses-feature", "feature-group"),
3019 std::make_pair("uses-feature", "manifest"),
3020 std::make_pair("uses-library", "application"),
3021 std::make_pair("uses-native-library", "application"),
3022 std::make_pair("uses-package", "application"),
3023 std::make_pair("uses-permission", "manifest"),
3024 std::make_pair("uses-permission-sdk-23", "manifest"),
3025 std::make_pair("uses-sdk", "manifest"),
3026 std::make_pair("uses-sdk-library", "application"),
3027 std::make_pair("uses-static-library", "application"),
3028 };
3029 bool is_valid_tag = std::find(kValidChildParentTags.begin(), kValidChildParentTags.end(),
3030 std::make_pair<std::string_view, std::string_view>(
3031 el->name, parent_tag)) != kValidChildParentTags.end();
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003032 // Attempt to map the xml tag to a element inflater
3033 std::unique_ptr<ManifestExtractor::Element> element;
3034 auto check = kTagCheck.find(el->name);
Iurii Makhno32c0c032022-12-08 18:07:58 +00003035 if (check != kTagCheck.end() && is_valid_tag) {
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003036 element = check->second();
Iurii Makhno32c0c032022-12-08 18:07:58 +00003037 element->featured_ = true;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003038 } else {
3039 element = util::make_unique<ManifestExtractor::Element>();
3040 }
3041
3042 element->extractor_ = extractor;
3043 element->tag_ = el->name;
3044 element->Extract(el);
3045 return element;
3046}
3047
Iurii Makhno32c0c032022-12-08 18:07:58 +00003048std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(
3049 xml::Element* el, const std::string& parent_tag) {
3050 auto element = ManifestExtractor::Element::Inflate(this, el, parent_tag);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003051 parent_stack_.insert(parent_stack_.begin(), element.get());
3052
3053 // Process the element and recursively visit the children
3054 for (xml::Element* child : el->GetChildElements()) {
Iurii Makhno32c0c032022-12-08 18:07:58 +00003055 auto v = Visit(child, el->name);
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003056 element->AddChild(v);
3057 }
3058
3059 parent_stack_.erase(parent_stack_.begin());
3060 return element;
3061}
3062
Ryan Mitchell214846d2018-09-19 16:57:01 -07003063int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003064 android::IDiagnostics* diag) {
Ryan Mitchell214846d2018-09-19 16:57:01 -07003065 ManifestExtractor extractor(apk, options);
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003066 if (!extractor.Extract(diag)) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003067 return 1;
Iurii Makhnoa9c74c52022-04-12 15:04:12 +00003068 }
3069 return extractor.Dump(printer) ? 0 : 1;
Ryan Mitchellfc225b22018-08-21 14:52:51 -07003070}
3071
Jeremy Meyer56f36e82022-05-20 20:35:42 +00003072int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, android::IDiagnostics* diag) {
Iurii Makhno85875a82022-04-26 15:30:01 +00003073 DumpManifestOptions options{/* include_meta_data= */ true,
3074 /* only_permissions= */ false};
3075 ManifestExtractor extractor(apk, options);
3076 if (!extractor.Extract(diag)) {
3077 return 1;
3078 }
3079 return extractor.DumpProto(out_badging) ? 0 : 1;
3080}
3081
Mårten Kongstad24c9aa62018-06-20 08:46:41 +02003082} // namespace aapt