Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 20 | #include <array> |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <set> |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 23 | #include <string_view> |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 24 | #include <vector> |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 25 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 26 | #include "LoadedApk.h" |
| 27 | #include "SdkConstants.h" |
| 28 | #include "ValueVisitor.h" |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 29 | #include "androidfw/ConfigDescription.h" |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 30 | #include "io/File.h" |
| 31 | #include "io/FileStream.h" |
| 32 | #include "process/IResourceTableConsumer.h" |
| 33 | #include "xml/XmlDom.h" |
| 34 | |
| 35 | using ::android::base::StringPrintf; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 36 | using ::android::ConfigDescription; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 37 | |
| 38 | namespace aapt { |
| 39 | |
| 40 | /** |
| 41 | * These are attribute resource constants for the platform, as found in android.R.attr. |
| 42 | */ |
| 43 | enum { |
| 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 Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 50 | PRIORITY_ATTR = 0x0101001c, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 51 | RESOURCE_ATTR = 0x01010025, |
| 52 | DEBUGGABLE_ATTR = 0x0101000f, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 53 | TARGET_PACKAGE_ATTR = 0x01010021, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 54 | 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 Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 84 | VERSION_ATTR = 0x01010519, |
| 85 | CERT_DIGEST_ATTR = 0x01010548, |
Sergey Nikolaienkov | 91331e5 | 2020-09-30 12:58:47 +0000 | [diff] [blame] | 86 | REQUIRED_FEATURE_ATTR = 0x01010554, |
| 87 | REQUIRED_NOT_FEATURE_ATTR = 0x01010555, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 88 | IS_STATIC_ATTR = 0x0101055a, |
| 89 | REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565, |
| 90 | REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 91 | COMPILE_SDK_VERSION_ATTR = 0x01010572, |
| 92 | COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 93 | VERSION_MAJOR_ATTR = 0x01010577, |
| 94 | PACKAGE_TYPE_ATTR = 0x01010587, |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 95 | USES_PERMISSION_FLAGS_ATTR = 0x01010644, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android"; |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 99 | constexpr int kNeverForLocation = 0x00010000; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 100 | |
| 101 | /** Retrieves the attribute of the element with the specified attribute resource id. */ |
| 102 | static 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. */ |
| 114 | static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package, |
| 115 | const std::string &name) { |
| 116 | return el->FindAttribute(package, name); |
| 117 | } |
| 118 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 119 | class 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 | |
| 152 | const 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 | |
| 167 | class 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 Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 211 | class CommonFeatureGroup; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 212 | class FeatureGroup; |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 213 | class SupportsScreen; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 214 | |
| 215 | class ManifestExtractor { |
| 216 | public: |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 217 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 218 | explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options) |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 219 | : apk_(apk), options_(options) { } |
| 220 | |
| 221 | class Element { |
| 222 | public: |
| 223 | Element() = default; |
| 224 | virtual ~Element() = default; |
| 225 | |
| 226 | static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el); |
| 227 | |
| 228 | /** Writes out the extracted contents of the element. */ |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 229 | virtual void Print(text::Printer* printer) { |
| 230 | } |
| 231 | |
| 232 | /** Saves extracted information into Badging proto. */ |
| 233 | virtual void ToProto(pb::Badging* out_badging) { |
| 234 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 235 | |
| 236 | /** Adds an element to the list of children of the element. */ |
| 237 | void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); } |
| 238 | |
Ryan Mitchell | 1966e1f | 2021-05-03 13:46:56 -0700 | [diff] [blame] | 239 | template <typename Predicate> |
| 240 | void Filter(Predicate&& func) { |
| 241 | children_.erase(std::remove_if(children_.begin(), children_.end(), |
Kelvin Zhang | 3965584d | 2021-05-10 12:17:14 -0400 | [diff] [blame] | 242 | [&](const auto& e) { return func(e.get()); }), |
| 243 | children_.end()); |
Ryan Mitchell | 1966e1f | 2021-05-03 13:46:56 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 246 | /** Retrieves the list of children of the element. */ |
| 247 | const std::vector<std::unique_ptr<Element>>& children() const { |
| 248 | return children_; |
| 249 | } |
| 250 | |
| 251 | /** Retrieves the extracted xml element tag. */ |
| 252 | const std::string tag() const { |
| 253 | return tag_; |
| 254 | } |
| 255 | |
| 256 | protected: |
| 257 | ManifestExtractor* extractor() const { |
| 258 | return extractor_; |
| 259 | } |
| 260 | |
| 261 | /** Retrieves and stores the information extracted from the xml element. */ |
| 262 | virtual void Extract(xml::Element* el) { } |
| 263 | |
| 264 | /* |
| 265 | * Retrieves a configuration value of the resource entry that best matches the specified |
| 266 | * configuration. |
| 267 | */ |
| 268 | static Value* BestConfigValue(ResourceEntry* entry, |
| 269 | const ConfigDescription& match) { |
| 270 | if (!entry) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | // Determine the config that best matches the desired config |
| 275 | ResourceConfigValue* best_value = nullptr; |
| 276 | for (auto& value : entry->values) { |
| 277 | if (!value->config.match(match)) { |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (best_value != nullptr) { |
| 282 | if (!value->config.isBetterThan(best_value->config, &match)) { |
| 283 | if (value->config.compare(best_value->config) != 0) { |
| 284 | continue; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | best_value = value.get(); |
| 290 | } |
| 291 | |
| 292 | // The entry has no values |
| 293 | if (!best_value) { |
| 294 | return nullptr; |
| 295 | } |
| 296 | |
| 297 | return best_value->value.get(); |
| 298 | } |
| 299 | |
| 300 | /** Retrieves the resource assigned to the specified resource id if one exists. */ |
| 301 | Value* FindValueById(const ResourceTable* table, const ResourceId& res_id, |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 302 | const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 303 | if (table) { |
| 304 | for (auto& package : table->packages) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 305 | for (auto& type : package->types) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 306 | for (auto& entry : type->entries) { |
| 307 | if (entry->id && entry->id.value() == res_id.id) { |
| 308 | if (auto value = BestConfigValue(entry.get(), config)) { |
| 309 | return value; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | return nullptr; |
| 317 | } |
| 318 | |
| 319 | /** Attempts to resolve the reference to a non-reference value. */ |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 320 | Value* ResolveReference(Reference* ref, const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 321 | const int kMaxIterations = 40; |
| 322 | int i = 0; |
| 323 | while (ref && ref->id && i++ < kMaxIterations) { |
| 324 | auto table = extractor_->apk_->GetResourceTable(); |
| 325 | if (auto value = FindValueById(table, ref->id.value(), config)) { |
| 326 | if (ValueCast<Reference>(value)) { |
| 327 | ref = ValueCast<Reference>(value); |
| 328 | } else { |
| 329 | return value; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Retrieves the integer value of the attribute . If the value of the attribute is a reference, |
| 338 | * this will attempt to resolve the reference to an integer value. |
| 339 | **/ |
| 340 | int32_t* GetAttributeInteger(xml::Attribute* attr, |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 341 | const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 342 | if (attr != nullptr) { |
| 343 | if (attr->compiled_value) { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 344 | // Resolve references using the configuration |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 345 | Value* value = attr->compiled_value.get(); |
| 346 | if (ValueCast<Reference>(value)) { |
| 347 | value = ResolveReference(ValueCast<Reference>(value), config); |
| 348 | } else { |
| 349 | value = attr->compiled_value.get(); |
| 350 | } |
| 351 | // Retrieve the integer data if possible |
| 352 | if (value != nullptr) { |
| 353 | if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) { |
| 354 | return (int32_t*) &intValue->value.data; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * A version of GetAttributeInteger that returns a default integer if the attribute does not |
| 364 | * exist or cannot be resolved to an integer value. |
| 365 | **/ |
| 366 | int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def, |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 367 | const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 368 | auto value = GetAttributeInteger(attr, config); |
| 369 | if (value) { |
| 370 | return *value; |
| 371 | } |
| 372 | return def; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Retrieves the string value of the attribute. If the value of the attribute is a reference, |
| 377 | * this will attempt to resolve the reference to a string value. |
| 378 | **/ |
| 379 | const std::string* GetAttributeString(xml::Attribute* attr, |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 380 | const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 381 | if (attr != nullptr) { |
| 382 | if (attr->compiled_value) { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 383 | // Resolve references using the configuration |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 384 | Value* value = attr->compiled_value.get(); |
| 385 | if (ValueCast<Reference>(value)) { |
| 386 | value = ResolveReference(ValueCast<Reference>(value), config); |
| 387 | } else { |
| 388 | value = attr->compiled_value.get(); |
| 389 | } |
| 390 | |
| 391 | // Retrieve the string data of the value if possible |
| 392 | if (value != nullptr) { |
| 393 | if (String* intValue = ValueCast<String>(value)) { |
| 394 | return &(*intValue->value); |
| 395 | } else if (RawString* rawValue = ValueCast<RawString>(value)) { |
| 396 | return &(*rawValue->value); |
| 397 | } else if (FileReference* strValue = ValueCast<FileReference>(value)) { |
| 398 | return &(*strValue->path); |
| 399 | } |
| 400 | } |
| 401 | } |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 402 | |
| 403 | if (!attr->value.empty()) { |
| 404 | return &attr->value; |
| 405 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 406 | } |
| 407 | return nullptr; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * A version of GetAttributeString that returns a default string if the attribute does not |
| 412 | * exist or cannot be resolved to an string value. |
| 413 | **/ |
| 414 | std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def, |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 415 | const ConfigDescription& config = DefaultConfig()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 416 | auto value = GetAttributeString(attr, config); |
| 417 | if (value) { |
| 418 | return *value; |
| 419 | } |
| 420 | return def; |
| 421 | } |
| 422 | |
| 423 | private: |
| 424 | ManifestExtractor* extractor_; |
| 425 | std::vector<std::unique_ptr<Element>> children_; |
| 426 | std::string tag_; |
| 427 | }; |
| 428 | |
| 429 | friend Element; |
| 430 | |
| 431 | /** Creates a default configuration used to retrieve resources. */ |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 432 | static ConfigDescription DefaultConfig() { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 433 | ConfigDescription config; |
| 434 | config.orientation = android::ResTable_config::ORIENTATION_PORT; |
| 435 | config.density = android::ResTable_config::DENSITY_MEDIUM; |
Jackal Guo | 201a60a | 2021-08-31 12:37:30 +0800 | [diff] [blame] | 436 | config.sdkVersion = SDK_CUR_DEVELOPMENT; // Very high. |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 437 | config.screenWidthDp = 320; |
| 438 | config.screenHeightDp = 480; |
| 439 | config.smallestScreenWidthDp = 320; |
| 440 | config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL; |
| 441 | return config; |
| 442 | } |
| 443 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 444 | bool Extract(android::IDiagnostics* diag); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 445 | bool Dump(text::Printer* printer); |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 446 | bool DumpProto(pb::Badging* out_badging); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 447 | |
| 448 | /** Recursively visit the xml element tree and return a processed badging element tree. */ |
| 449 | std::unique_ptr<Element> Visit(xml::Element* element); |
| 450 | |
Iurii Makhno | 05fd929 | 2022-08-24 18:56:12 +0000 | [diff] [blame] | 451 | /** Resets target SDK to 0. */ |
| 452 | void ResetTargetSdk() { |
| 453 | target_sdk_ = 0; |
| 454 | } |
| 455 | |
| 456 | /** Raises the target sdk value if the min target is greater than the current target. */ |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 457 | void RaiseTargetSdk(int32_t min_target) { |
| 458 | if (min_target > target_sdk_) { |
| 459 | target_sdk_ = min_target; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Retrieves the default feature group that features are added into when <uses-feature> |
| 465 | * are not in a <feature-group> element. |
| 466 | **/ |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 467 | CommonFeatureGroup* common_feature_group() { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 468 | return commonFeatureGroup_.get(); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Retrieves a mapping of density values to Configurations for retrieving resources that would be |
| 473 | * used for that density setting. |
| 474 | **/ |
| 475 | const std::map<uint16_t, ConfigDescription> densities() const { |
| 476 | return densities_; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that |
| 481 | * would be used for that locale setting. |
| 482 | **/ |
| 483 | const std::map<std::string, ConfigDescription> locales() const { |
| 484 | return locales_; |
| 485 | } |
| 486 | |
| 487 | /** Retrieves the current stack of parent during data extraction. */ |
| 488 | const std::vector<Element*> parent_stack() const { |
| 489 | return parent_stack_; |
| 490 | } |
| 491 | |
| 492 | int32_t target_sdk() const { |
| 493 | return target_sdk_; |
| 494 | } |
| 495 | |
| 496 | LoadedApk* const apk_; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 497 | DumpManifestOptions& options_; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 498 | |
| 499 | private: |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 500 | std::unique_ptr<xml::XmlResource> doc_; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 501 | std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>(); |
| 502 | std::map<std::string, ConfigDescription> locales_; |
| 503 | std::map<uint16_t, ConfigDescription> densities_; |
| 504 | std::vector<Element*> parent_stack_; |
| 505 | int32_t target_sdk_ = 0; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 506 | |
| 507 | std::unique_ptr<ManifestExtractor::Element> root_element_; |
| 508 | std::vector<std::unique_ptr<ManifestExtractor::Element>> implied_permissions_; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 509 | std::vector<FeatureGroup*> feature_groups_; |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 510 | Components components_; |
| 511 | Architectures architectures_; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 512 | const SupportsScreen* supports_screen_; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | template<typename T> T* ElementCast(ManifestExtractor::Element* element); |
| 516 | |
| 517 | /** Recurs through the children of the specified root in depth-first order. */ |
| 518 | static void ForEachChild(ManifestExtractor::Element* root, |
| 519 | std::function<void(ManifestExtractor::Element*)> f) { |
| 520 | for (auto& child : root->children()) { |
| 521 | f(child.get()); |
| 522 | ForEachChild(child.get(), f); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Checks the element and its recursive children for an element that makes the specified |
| 528 | * conditional function return true. Returns the first element that makes the conditional function |
| 529 | * return true. |
| 530 | **/ |
| 531 | static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root, |
| 532 | std::function<bool(ManifestExtractor::Element*)> f) { |
| 533 | if (f(root)) { |
| 534 | return root; |
| 535 | } |
| 536 | for (auto& child : root->children()) { |
| 537 | if (auto b2 = FindElement(child.get(), f)) { |
| 538 | return b2; |
| 539 | } |
| 540 | } |
| 541 | return nullptr; |
| 542 | } |
| 543 | |
| 544 | /** Represents the <manifest> elements **/ |
| 545 | class Manifest : public ManifestExtractor::Element { |
| 546 | public: |
| 547 | Manifest() = default; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 548 | bool only_package_name; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 549 | std::string package; |
| 550 | int32_t versionCode; |
| 551 | std::string versionName; |
| 552 | const std::string* split = nullptr; |
| 553 | const std::string* platformVersionName = nullptr; |
| 554 | const std::string* platformVersionCode = nullptr; |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 555 | const int32_t* platformVersionNameInt = nullptr; |
| 556 | const int32_t* platformVersionCodeInt = nullptr; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 557 | const int32_t* compilesdkVersion = nullptr; |
| 558 | const std::string* compilesdkVersionCodename = nullptr; |
| 559 | const int32_t* installLocation = nullptr; |
| 560 | |
| 561 | void Extract(xml::Element* manifest) override { |
| 562 | package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), ""); |
| 563 | versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0); |
| 564 | versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), ""); |
| 565 | split = GetAttributeString(FindAttribute(manifest, {}, "split")); |
| 566 | |
| 567 | // Extract the platform build info |
| 568 | platformVersionName = GetAttributeString(FindAttribute(manifest, {}, |
| 569 | "platformBuildVersionName")); |
| 570 | platformVersionCode = GetAttributeString(FindAttribute(manifest, {}, |
| 571 | "platformBuildVersionCode")); |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 572 | platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {}, |
| 573 | "platformBuildVersionName")); |
| 574 | platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {}, |
| 575 | "platformBuildVersionCode")); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 576 | |
| 577 | // Extract the compile sdk info |
| 578 | compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR)); |
| 579 | compilesdkVersionCodename = GetAttributeString( |
| 580 | FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR)); |
| 581 | installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR)); |
| 582 | } |
| 583 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 584 | void ToProto(pb::Badging* out_badging) override { |
| 585 | auto out_package = out_badging->mutable_package(); |
| 586 | out_package->set_package(package); |
| 587 | out_package->set_version_code(versionCode); |
| 588 | out_package->set_version_name(versionName); |
| 589 | if (compilesdkVersion) { |
| 590 | out_package->set_compile_sdk_version(*compilesdkVersion); |
| 591 | } |
| 592 | if (compilesdkVersionCodename) { |
| 593 | out_package->set_compile_sdk_version_codename(*compilesdkVersionCodename); |
| 594 | } |
| 595 | if (platformVersionName) { |
| 596 | out_package->set_platform_version_name(*platformVersionName); |
| 597 | } else if (platformVersionNameInt) { |
| 598 | out_package->set_platform_version_name(std::to_string(*platformVersionNameInt)); |
| 599 | } |
| 600 | if (platformVersionCode) { |
| 601 | out_package->set_platform_version_code(*platformVersionCode); |
| 602 | } else if (platformVersionCodeInt) { |
| 603 | out_package->set_platform_version_code(std::to_string(*platformVersionCodeInt)); |
| 604 | } |
| 605 | |
| 606 | if (installLocation) { |
| 607 | switch (*installLocation) { |
| 608 | case 0: |
| 609 | out_package->set_install_location(pb::PackageInfo_InstallLocation_AUTO); |
| 610 | break; |
| 611 | case 1: |
| 612 | out_package->set_install_location(pb::PackageInfo_InstallLocation_INTERNAL_ONLY); |
| 613 | break; |
| 614 | case 2: |
| 615 | out_package->set_install_location(pb::PackageInfo_InstallLocation_PREFER_EXTERNAL); |
| 616 | break; |
| 617 | default: |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 623 | void Print(text::Printer* printer) override { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 624 | if (only_package_name) { |
| 625 | printer->Println(StringPrintf("package: %s", package.data())); |
| 626 | } else { |
| 627 | PrintFull(printer); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void PrintFull(text::Printer* printer) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 632 | printer->Print(StringPrintf("package: name='%s' ", package.data())); |
| 633 | printer->Print(StringPrintf("versionCode='%s' ", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 634 | (versionCode > 0) ? std::to_string(versionCode).data() : "")); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 635 | printer->Print(StringPrintf("versionName='%s'", versionName.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 636 | |
| 637 | if (split) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 638 | printer->Print(StringPrintf(" split='%s'", split->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 639 | } |
| 640 | if (platformVersionName) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 641 | printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data())); |
Dave Ingram | 7e0e4c1 | 2019-08-01 15:11:41 -0700 | [diff] [blame] | 642 | } else if (platformVersionNameInt) { |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 643 | printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt)); |
| 644 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 645 | if (platformVersionCode) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 646 | printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data())); |
Dave Ingram | 7e0e4c1 | 2019-08-01 15:11:41 -0700 | [diff] [blame] | 647 | } else if (platformVersionCodeInt) { |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 648 | printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt)); |
| 649 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 650 | if (compilesdkVersion) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 651 | printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 652 | } |
| 653 | if (compilesdkVersionCodename) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 654 | printer->Print(StringPrintf(" compileSdkVersionCodename='%s'", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 655 | compilesdkVersionCodename->data())); |
| 656 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 657 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 658 | |
| 659 | if (installLocation) { |
| 660 | switch (*installLocation) { |
| 661 | case 0: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 662 | printer->Print("install-location:'auto'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 663 | break; |
| 664 | case 1: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 665 | printer->Print("install-location:'internalOnly'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 666 | break; |
| 667 | case 2: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 668 | printer->Print("install-location:'preferExternal'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 669 | break; |
| 670 | default: |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | }; |
| 676 | |
| 677 | /** Represents <application> elements. **/ |
| 678 | class Application : public ManifestExtractor::Element { |
| 679 | public: |
| 680 | Application() = default; |
| 681 | std::string label; |
| 682 | std::string icon; |
| 683 | std::string banner; |
| 684 | int32_t is_game; |
| 685 | int32_t debuggable; |
| 686 | int32_t test_only; |
| 687 | bool has_multi_arch; |
| 688 | |
| 689 | /** Mapping from locales to app names. */ |
| 690 | std::map<std::string, std::string> locale_labels; |
| 691 | |
| 692 | /** Mapping from densities to app icons. */ |
| 693 | std::map<uint16_t, std::string> density_icons; |
| 694 | |
| 695 | void Extract(xml::Element* element) override { |
| 696 | label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); |
| 697 | icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), ""); |
| 698 | test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0); |
| 699 | banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), ""); |
| 700 | is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0); |
| 701 | debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0); |
| 702 | |
| 703 | // We must search by name because the multiArch flag hasn't been API |
| 704 | // frozen yet. |
| 705 | has_multi_arch = (GetAttributeIntegerDefault( |
| 706 | FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0); |
| 707 | |
| 708 | // Retrieve the app names for every locale the app supports |
| 709 | auto attr = FindAttribute(element, LABEL_ATTR); |
| 710 | for (auto& config : extractor()->locales()) { |
| 711 | if (auto label = GetAttributeString(attr, config.second)) { |
| 712 | if (label) { |
| 713 | locale_labels.insert(std::make_pair(config.first, *label)); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Retrieve the icons for the densities the app supports |
| 719 | attr = FindAttribute(element, ICON_ATTR); |
| 720 | for (auto& config : extractor()->densities()) { |
| 721 | if (auto resource = GetAttributeString(attr, config.second)) { |
| 722 | if (resource) { |
| 723 | density_icons.insert(std::make_pair(config.first, *resource)); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 729 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 730 | // Print the labels for every locale |
| 731 | for (auto p : locale_labels) { |
| 732 | if (p.first.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 733 | printer->Print(StringPrintf("application-label:'%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 734 | android::ResTable::normalizeForOutput(p.second.data()) |
| 735 | .c_str())); |
| 736 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 737 | printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(), |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 738 | android::ResTable::normalizeForOutput(p.second.data()) |
| 739 | .c_str())); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // Print the icon paths for every density |
| 744 | for (auto p : density_icons) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 745 | printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // Print the application info |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 749 | printer->Print(StringPrintf("application: label='%s' ", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 750 | android::ResTable::normalizeForOutput(label.data()).c_str())); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 751 | printer->Print(StringPrintf("icon='%s'", icon.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 752 | if (!banner.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 753 | printer->Print(StringPrintf(" banner='%s'", banner.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 754 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 755 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 756 | |
| 757 | if (test_only != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 758 | printer->Print(StringPrintf("testOnly='%d'\n", test_only)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 759 | } |
| 760 | if (is_game != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 761 | printer->Print("application-isGame\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 762 | } |
| 763 | if (debuggable != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 764 | printer->Print("application-debuggable\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 767 | |
| 768 | void ToProto(pb::Badging* out_badging) override { |
| 769 | auto application = out_badging->mutable_application(); |
| 770 | application->set_label(android::ResTable::normalizeForOutput(label.data())); |
| 771 | application->set_icon(icon); |
| 772 | application->set_banner(banner); |
| 773 | application->set_test_only(test_only != 0); |
| 774 | application->set_game(is_game != 0); |
| 775 | application->set_debuggable(debuggable != 0); |
| 776 | |
| 777 | auto out_locale_labels = application->mutable_locale_labels(); |
| 778 | for (auto& p : locale_labels) { |
| 779 | if (!p.first.empty()) { |
| 780 | (*out_locale_labels)[p.first] = p.second; |
| 781 | } |
| 782 | } |
| 783 | auto out_density_icons = application->mutable_density_icons(); |
| 784 | for (auto& p : density_icons) { |
| 785 | (*out_density_icons)[p.first] = p.second; |
| 786 | } |
| 787 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 788 | }; |
| 789 | |
| 790 | /** Represents <uses-sdk> elements. **/ |
| 791 | class UsesSdkBadging : public ManifestExtractor::Element { |
| 792 | public: |
| 793 | UsesSdkBadging() = default; |
| 794 | const int32_t* min_sdk = nullptr; |
| 795 | const std::string* min_sdk_name = nullptr; |
| 796 | const int32_t* max_sdk = nullptr; |
| 797 | const int32_t* target_sdk = nullptr; |
| 798 | const std::string* target_sdk_name = nullptr; |
| 799 | |
| 800 | void Extract(xml::Element* element) override { |
| 801 | min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR)); |
| 802 | min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR)); |
| 803 | max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR)); |
| 804 | target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR)); |
| 805 | target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR)); |
| 806 | |
Iurii Makhno | 05fd929 | 2022-08-24 18:56:12 +0000 | [diff] [blame] | 807 | // Resets target SDK first. This is required if APK contains multiple <uses-sdk> elements, |
| 808 | // we only need to take the latest values. |
| 809 | extractor()->ResetTargetSdk(); |
| 810 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 811 | // Detect the target sdk of the element |
| 812 | if ((min_sdk_name && *min_sdk_name == "Donut") |
| 813 | || (target_sdk_name && *target_sdk_name == "Donut")) { |
Jackal Guo | 201a60a | 2021-08-31 12:37:30 +0800 | [diff] [blame] | 814 | extractor()->RaiseTargetSdk(SDK_DONUT); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 815 | } |
| 816 | if (min_sdk) { |
| 817 | extractor()->RaiseTargetSdk(*min_sdk); |
| 818 | } |
| 819 | if (target_sdk) { |
| 820 | extractor()->RaiseTargetSdk(*target_sdk); |
Ryan Mitchell | 95f0242 | 2020-04-30 10:25:53 -0700 | [diff] [blame] | 821 | } else if (target_sdk_name) { |
Jackal Guo | 201a60a | 2021-08-31 12:37:30 +0800 | [diff] [blame] | 822 | extractor()->RaiseTargetSdk(SDK_CUR_DEVELOPMENT); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 826 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 827 | if (min_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 828 | printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 829 | } else if (min_sdk_name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 830 | printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 831 | } |
| 832 | if (max_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 833 | printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 834 | } |
| 835 | if (target_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 836 | printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 837 | } else if (target_sdk_name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 838 | printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 841 | |
| 842 | void ToProto(pb::Badging* out_badging) override { |
| 843 | auto out_sdks = out_badging->mutable_uses_sdk(); |
| 844 | if (min_sdk) { |
| 845 | out_sdks->set_min_sdk_version(*min_sdk); |
| 846 | } else if (min_sdk_name) { |
| 847 | out_sdks->set_min_sdk_version_name(*min_sdk_name); |
| 848 | } |
| 849 | if (max_sdk) { |
| 850 | out_sdks->set_max_sdk_version(*max_sdk); |
| 851 | } |
| 852 | if (target_sdk) { |
| 853 | out_sdks->set_target_sdk_version(*target_sdk); |
| 854 | } else if (target_sdk_name) { |
| 855 | out_sdks->set_target_sdk_version_name(*target_sdk_name); |
| 856 | } |
| 857 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 858 | }; |
| 859 | |
| 860 | /** Represents <uses-configuration> elements. **/ |
| 861 | class UsesConfiguarion : public ManifestExtractor::Element { |
| 862 | public: |
| 863 | UsesConfiguarion() = default; |
| 864 | int32_t req_touch_screen = 0; |
| 865 | int32_t req_keyboard_type = 0; |
| 866 | int32_t req_hard_keyboard = 0; |
| 867 | int32_t req_navigation = 0; |
| 868 | int32_t req_five_way_nav = 0; |
| 869 | |
| 870 | void Extract(xml::Element* element) override { |
| 871 | req_touch_screen = GetAttributeIntegerDefault( |
| 872 | FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0); |
| 873 | req_keyboard_type = GetAttributeIntegerDefault( |
| 874 | FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0); |
| 875 | req_hard_keyboard = GetAttributeIntegerDefault( |
| 876 | FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0); |
| 877 | req_navigation = GetAttributeIntegerDefault( |
| 878 | FindAttribute(element, REQ_NAVIGATION_ATTR), 0); |
| 879 | req_five_way_nav = GetAttributeIntegerDefault( |
| 880 | FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0); |
| 881 | } |
| 882 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 883 | void Print(text::Printer* printer) override { |
| 884 | printer->Print("uses-configuration:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 885 | if (req_touch_screen != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 886 | printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 887 | } |
| 888 | if (req_keyboard_type != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 889 | printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 890 | } |
| 891 | if (req_hard_keyboard != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 892 | printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 893 | } |
| 894 | if (req_navigation != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 895 | printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 896 | } |
| 897 | if (req_five_way_nav != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 898 | printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 899 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 900 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 901 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 902 | |
| 903 | void ToProto(pb::Badging* out_badging) override { |
| 904 | auto out_configuration = out_badging->mutable_uses_configuration(); |
| 905 | out_configuration->set_req_touch_screen(req_touch_screen); |
| 906 | out_configuration->set_req_keyboard_type(req_keyboard_type); |
| 907 | out_configuration->set_req_hard_keyboard(req_hard_keyboard); |
| 908 | out_configuration->set_req_navigation(req_navigation); |
| 909 | out_configuration->set_req_five_way_nav(req_five_way_nav); |
| 910 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | /** Represents <supports-screen> elements. **/ |
| 914 | class SupportsScreen : public ManifestExtractor::Element { |
| 915 | public: |
| 916 | SupportsScreen() = default; |
| 917 | int32_t small_screen = 1; |
| 918 | int32_t normal_screen = 1; |
| 919 | int32_t large_screen = 1; |
| 920 | int32_t xlarge_screen = 1; |
| 921 | int32_t any_density = 1; |
| 922 | int32_t requires_smallest_width_dp = 0; |
| 923 | int32_t compatible_width_limit_dp = 0; |
| 924 | int32_t largest_width_limit_dp = 0; |
| 925 | |
| 926 | void Extract(xml::Element* element) override { |
| 927 | small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1); |
| 928 | normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1); |
| 929 | large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1); |
| 930 | xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1); |
| 931 | any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1); |
| 932 | |
| 933 | requires_smallest_width_dp = GetAttributeIntegerDefault( |
| 934 | FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0); |
| 935 | compatible_width_limit_dp = GetAttributeIntegerDefault( |
| 936 | FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0); |
| 937 | largest_width_limit_dp = GetAttributeIntegerDefault( |
| 938 | FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0); |
| 939 | |
| 940 | // For modern apps, if screen size buckets haven't been specified |
| 941 | // but the new width ranges have, then infer the buckets from them. |
| 942 | if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0 |
| 943 | && requires_smallest_width_dp > 0) { |
| 944 | int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp |
| 945 | : requires_smallest_width_dp; |
| 946 | small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0; |
| 947 | normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0; |
| 948 | large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0; |
| 949 | xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0; |
| 950 | } |
| 951 | } |
| 952 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 953 | void PrintScreens(text::Printer* printer, int32_t target_sdk) const { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 954 | // Print the formatted screen info |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 955 | printer->Print("supports-screens:"); |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 956 | if (IsSmallScreenSupported(target_sdk)) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 957 | printer->Print(" 'small'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 958 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 959 | if (normal_screen != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 960 | printer->Print(" 'normal'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 961 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 962 | if (IsLargeScreenSupported(target_sdk)) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 963 | printer->Print(" 'large'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 964 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 965 | if (IsXLargeScreenSupported(target_sdk)) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 966 | printer->Print(" 'xlarge'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 967 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 968 | printer->Print("\n"); |
| 969 | printer->Print(StringPrintf("supports-any-density: '%s'\n", |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 970 | (IsAnyDensitySupported(target_sdk)) ? "true" : "false")); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 971 | if (requires_smallest_width_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 972 | printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 973 | } |
| 974 | if (compatible_width_limit_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 975 | printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 976 | } |
| 977 | if (largest_width_limit_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 978 | printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 981 | |
| 982 | void ToProtoScreens(pb::Badging* out_badging, int32_t target_sdk) const { |
| 983 | auto supports_screen = out_badging->mutable_supports_screen(); |
| 984 | if (IsSmallScreenSupported(target_sdk)) { |
| 985 | supports_screen->add_screens(pb::SupportsScreen_ScreenType_SMALL); |
| 986 | } |
| 987 | if (normal_screen != 0) { |
| 988 | supports_screen->add_screens(pb::SupportsScreen_ScreenType_NORMAL); |
| 989 | } |
| 990 | if (IsLargeScreenSupported(target_sdk)) { |
| 991 | supports_screen->add_screens(pb::SupportsScreen_ScreenType_LARGE); |
| 992 | } |
| 993 | if (IsXLargeScreenSupported(target_sdk)) { |
| 994 | supports_screen->add_screens(pb::SupportsScreen_ScreenType_XLARGE); |
| 995 | } |
| 996 | supports_screen->set_supports_any_densities(IsAnyDensitySupported(target_sdk)); |
| 997 | supports_screen->set_requires_smallest_width_dp(requires_smallest_width_dp); |
| 998 | supports_screen->set_compatible_width_limit_dp(compatible_width_limit_dp); |
| 999 | supports_screen->set_largest_width_limit_dp(largest_width_limit_dp); |
| 1000 | } |
| 1001 | |
| 1002 | private: |
| 1003 | // Determine default values for any unspecified screen sizes, |
| 1004 | // based on the target SDK of the package. As of 4 (donut) |
| 1005 | // the screen size support was introduced, so all default to |
| 1006 | // enabled. |
| 1007 | bool IsSmallScreenSupported(int32_t target_sdk) const { |
| 1008 | if (small_screen > 0) { |
| 1009 | return target_sdk >= SDK_DONUT; |
| 1010 | } |
| 1011 | return small_screen != 0; |
| 1012 | } |
| 1013 | |
| 1014 | bool IsLargeScreenSupported(int32_t target_sdk) const { |
| 1015 | if (large_screen > 0) { |
| 1016 | return target_sdk >= SDK_DONUT; |
| 1017 | } |
| 1018 | return large_screen != 0; |
| 1019 | } |
| 1020 | |
| 1021 | bool IsXLargeScreenSupported(int32_t target_sdk) const { |
| 1022 | if (xlarge_screen > 0) { |
| 1023 | return target_sdk >= SDK_GINGERBREAD; |
| 1024 | } |
| 1025 | return xlarge_screen != 0; |
| 1026 | } |
| 1027 | |
| 1028 | bool IsAnyDensitySupported(int32_t target_sdk) const { |
| 1029 | if (any_density > 0) { |
| 1030 | return target_sdk >= SDK_DONUT || requires_smallest_width_dp > 0 || |
| 1031 | compatible_width_limit_dp > 0; |
| 1032 | } |
| 1033 | return any_density != 0; |
| 1034 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1035 | }; |
| 1036 | |
| 1037 | /** Represents <feature-group> elements. **/ |
| 1038 | class FeatureGroup : public ManifestExtractor::Element { |
| 1039 | public: |
| 1040 | FeatureGroup() = default; |
| 1041 | std::string label; |
| 1042 | int32_t open_gles_version = 0; |
| 1043 | |
| 1044 | void Extract(xml::Element* element) override { |
| 1045 | label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); |
| 1046 | } |
| 1047 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1048 | virtual void PrintGroup(text::Printer* printer) { |
| 1049 | printer->Print(StringPrintf("feature-group: label='%s'\n", label.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1050 | if (open_gles_version > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1051 | printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | for (auto feature : features_) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1055 | printer->Print(StringPrintf(" uses-feature%s: name='%s'", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1056 | (feature.second.required ? "" : "-not-required"), |
| 1057 | feature.first.data())); |
| 1058 | if (feature.second.version > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1059 | printer->Print(StringPrintf(" version='%d'", feature.second.version)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1060 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1061 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1065 | virtual void GroupToProto(pb::Badging* out_badging) { |
| 1066 | auto feature_group = out_badging->add_feature_groups(); |
| 1067 | feature_group->set_label(label); |
| 1068 | feature_group->set_open_gles_version(open_gles_version); |
| 1069 | for (auto& feature : features_) { |
| 1070 | auto out_feature = feature_group->add_features(); |
| 1071 | out_feature->set_name(feature.first); |
| 1072 | out_feature->set_required(feature.second.required); |
| 1073 | out_feature->set_version(feature.second.version); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1077 | /** Adds a feature to the feature group. */ |
| 1078 | void AddFeature(const std::string& name, bool required = true, int32_t version = -1) { |
Iurii Makhno | 0df4e6d | 2022-11-29 19:19:13 +0000 | [diff] [blame^] | 1079 | features_.insert_or_assign(name, Feature{required, version}); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1080 | if (required) { |
| 1081 | if (name == "android.hardware.camera.autofocus" || |
| 1082 | name == "android.hardware.camera.flash") { |
| 1083 | AddFeature("android.hardware.camera", true); |
| 1084 | } else if (name == "android.hardware.location.gps" || |
| 1085 | name == "android.hardware.location.network") { |
| 1086 | AddFeature("android.hardware.location", true); |
| 1087 | } else if (name == "android.hardware.faketouch.multitouch") { |
| 1088 | AddFeature("android.hardware.faketouch", true); |
| 1089 | } else if (name == "android.hardware.faketouch.multitouch.distinct" || |
| 1090 | name == "android.hardware.faketouch.multitouch.jazzhands") { |
| 1091 | AddFeature("android.hardware.faketouch.multitouch", true); |
| 1092 | AddFeature("android.hardware.faketouch", true); |
| 1093 | } else if (name == "android.hardware.touchscreen.multitouch") { |
| 1094 | AddFeature("android.hardware.touchscreen", true); |
| 1095 | } else if (name == "android.hardware.touchscreen.multitouch.distinct" || |
| 1096 | name == "android.hardware.touchscreen.multitouch.jazzhands") { |
| 1097 | AddFeature("android.hardware.touchscreen.multitouch", true); |
| 1098 | AddFeature("android.hardware.touchscreen", true); |
| 1099 | } else if (name == "android.hardware.opengles.aep") { |
| 1100 | const int kOpenGLESVersion31 = 0x00030001; |
| 1101 | if (kOpenGLESVersion31 > open_gles_version) { |
| 1102 | open_gles_version = kOpenGLESVersion31; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | /** Returns true if the feature group has the given feature. */ |
| 1109 | virtual bool HasFeature(const std::string& name) { |
| 1110 | return features_.find(name) != features_.end(); |
| 1111 | } |
| 1112 | |
| 1113 | /** Merges the features of another feature group into this group. */ |
| 1114 | void Merge(FeatureGroup* group) { |
| 1115 | open_gles_version = std::max(open_gles_version, group->open_gles_version); |
| 1116 | for (auto& feature : group->features_) { |
| 1117 | features_.insert(feature); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | protected: |
| 1122 | struct Feature { |
| 1123 | public: |
| 1124 | bool required = false; |
| 1125 | int32_t version = -1; |
| 1126 | }; |
| 1127 | |
| 1128 | /* Mapping of feature names to their properties. */ |
| 1129 | std::map<std::string, Feature> features_; |
| 1130 | }; |
| 1131 | |
| 1132 | /** |
| 1133 | * Represents the default feature group for the application if no <feature-group> elements are |
| 1134 | * present in the manifest. |
| 1135 | **/ |
| 1136 | class CommonFeatureGroup : public FeatureGroup { |
| 1137 | public: |
| 1138 | CommonFeatureGroup() = default; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1139 | void PrintGroup(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1140 | FeatureGroup::PrintGroup(printer); |
| 1141 | |
| 1142 | // Also print the implied features |
| 1143 | for (auto feature : implied_features_) { |
| 1144 | if (features_.find(feature.first) == features_.end()) { |
| 1145 | const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : ""; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1146 | printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data())); |
| 1147 | printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1148 | feature.first.data())); |
| 1149 | |
| 1150 | // Print the reasons as a sentence |
| 1151 | size_t count = 0; |
| 1152 | for (auto reason : feature.second.reasons) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1153 | printer->Print(reason); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1154 | if (count + 2 < feature.second.reasons.size()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1155 | printer->Print(", "); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1156 | } else if (count + 1 < feature.second.reasons.size()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1157 | printer->Print(", and "); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1158 | } |
| 1159 | count++; |
| 1160 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1161 | printer->Print("'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1166 | virtual void GroupToProto(pb::Badging* out_badging) override { |
| 1167 | FeatureGroup::GroupToProto(out_badging); |
| 1168 | auto feature_group = |
| 1169 | out_badging->mutable_feature_groups(out_badging->feature_groups_size() - 1); |
| 1170 | for (auto& feature : implied_features_) { |
| 1171 | if (features_.find(feature.first) == features_.end()) { |
| 1172 | auto out_feature = feature_group->add_features(); |
| 1173 | out_feature->set_name(feature.first); |
| 1174 | auto implied_data = out_feature->mutable_implied_data(); |
| 1175 | implied_data->set_from_sdk_23_permission(feature.second.implied_from_sdk_k23); |
| 1176 | for (auto& reason : feature.second.reasons) { |
| 1177 | implied_data->add_reasons(reason); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1183 | /** Returns true if the feature group has the given feature. */ |
| 1184 | bool HasFeature(const std::string& name) override { |
| 1185 | return FeatureGroup::HasFeature(name) |
| 1186 | || implied_features_.find(name) != implied_features_.end(); |
| 1187 | } |
| 1188 | |
| 1189 | /** Adds a feature to a set of implied features not explicitly requested in the manifest. */ |
| 1190 | void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) { |
| 1191 | auto entry = implied_features_.find(name); |
| 1192 | if (entry == implied_features_.end()) { |
| 1193 | implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23))); |
| 1194 | entry = implied_features_.find(name); |
| 1195 | } |
| 1196 | |
| 1197 | // A non-sdk 23 implied feature takes precedence. |
| 1198 | if (entry->second.implied_from_sdk_k23 && !sdk23) { |
| 1199 | entry->second.implied_from_sdk_k23 = false; |
| 1200 | } |
| 1201 | |
| 1202 | entry->second.reasons.insert(reason); |
| 1203 | } |
| 1204 | |
| 1205 | /** |
| 1206 | * Adds a feature to a set of implied features for all features that are implied by the presence |
| 1207 | * of the permission. |
| 1208 | **/ |
| 1209 | void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) { |
| 1210 | if (name == "android.permission.CAMERA") { |
| 1211 | addImpliedFeature("android.hardware.camera", |
| 1212 | StringPrintf("requested %s permission", name.data()), |
| 1213 | sdk23); |
| 1214 | |
| 1215 | } else if (name == "android.permission.ACCESS_FINE_LOCATION") { |
| 1216 | if (targetSdk < SDK_LOLLIPOP) { |
| 1217 | addImpliedFeature("android.hardware.location.gps", |
| 1218 | StringPrintf("requested %s permission", name.data()), |
| 1219 | sdk23); |
| 1220 | addImpliedFeature("android.hardware.location.gps", |
| 1221 | StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP), |
| 1222 | sdk23); |
| 1223 | } |
| 1224 | addImpliedFeature("android.hardware.location", |
| 1225 | StringPrintf("requested %s permission", name.data()), |
| 1226 | sdk23); |
| 1227 | |
| 1228 | } else if (name == "android.permission.ACCESS_COARSE_LOCATION") { |
| 1229 | if (targetSdk < SDK_LOLLIPOP) { |
| 1230 | addImpliedFeature("android.hardware.location.network", |
| 1231 | StringPrintf("requested %s permission", name.data()), |
| 1232 | sdk23); |
| 1233 | addImpliedFeature("android.hardware.location.network", |
| 1234 | StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP), |
| 1235 | sdk23); |
| 1236 | } |
| 1237 | addImpliedFeature("android.hardware.location", |
| 1238 | StringPrintf("requested %s permission", name.data()), |
| 1239 | sdk23); |
| 1240 | |
| 1241 | } else if (name == "android.permission.ACCESS_MOCK_LOCATION" || |
| 1242 | name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" || |
| 1243 | name == "android.permission.INSTALL_LOCATION_PROVIDER") { |
| 1244 | addImpliedFeature("android.hardware.location", |
| 1245 | StringPrintf("requested %s permission", name.data()), |
| 1246 | sdk23); |
| 1247 | |
| 1248 | } else if (name == "android.permission.BLUETOOTH" || |
| 1249 | name == "android.permission.BLUETOOTH_ADMIN") { |
| 1250 | if (targetSdk > SDK_DONUT) { |
| 1251 | addImpliedFeature("android.hardware.bluetooth", |
| 1252 | StringPrintf("requested %s permission", name.data()), |
| 1253 | sdk23); |
| 1254 | addImpliedFeature("android.hardware.bluetooth", |
| 1255 | StringPrintf("targetSdkVersion > %d", SDK_DONUT), |
| 1256 | sdk23); |
| 1257 | } |
| 1258 | |
| 1259 | } else if (name == "android.permission.RECORD_AUDIO") { |
| 1260 | addImpliedFeature("android.hardware.microphone", |
| 1261 | StringPrintf("requested %s permission", name.data()), |
| 1262 | sdk23); |
| 1263 | |
| 1264 | } else if (name == "android.permission.ACCESS_WIFI_STATE" || |
| 1265 | name == "android.permission.CHANGE_WIFI_STATE" || |
| 1266 | name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") { |
| 1267 | addImpliedFeature("android.hardware.wifi", |
| 1268 | StringPrintf("requested %s permission", name.data()), |
| 1269 | sdk23); |
| 1270 | |
| 1271 | } else if (name == "android.permission.CALL_PHONE" || |
| 1272 | name == "android.permission.CALL_PRIVILEGED" || |
| 1273 | name == "android.permission.MODIFY_PHONE_STATE" || |
| 1274 | name == "android.permission.PROCESS_OUTGOING_CALLS" || |
| 1275 | name == "android.permission.READ_SMS" || |
| 1276 | name == "android.permission.RECEIVE_SMS" || |
| 1277 | name == "android.permission.RECEIVE_MMS" || |
| 1278 | name == "android.permission.RECEIVE_WAP_PUSH" || |
| 1279 | name == "android.permission.SEND_SMS" || |
| 1280 | name == "android.permission.WRITE_APN_SETTINGS" || |
| 1281 | name == "android.permission.WRITE_SMS") { |
| 1282 | addImpliedFeature("android.hardware.telephony", |
| 1283 | "requested a telephony permission", |
| 1284 | sdk23); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | private: |
| 1289 | /** |
| 1290 | * Represents a feature that has been automatically added due to a pre-requisite or for some |
| 1291 | * other reason. |
| 1292 | */ |
| 1293 | struct ImpliedFeature { |
| 1294 | explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {} |
| 1295 | |
| 1296 | /** List of human-readable reasons for why this feature was implied. */ |
| 1297 | std::set<std::string> reasons; |
| 1298 | |
| 1299 | // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />) |
| 1300 | bool implied_from_sdk_k23; |
| 1301 | }; |
| 1302 | |
| 1303 | /* Mapping of implied feature names to their properties. */ |
| 1304 | std::map<std::string, ImpliedFeature> implied_features_; |
| 1305 | }; |
| 1306 | |
| 1307 | /** Represents <uses-feature> elements. **/ |
| 1308 | class UsesFeature : public ManifestExtractor::Element { |
| 1309 | public: |
| 1310 | UsesFeature() = default; |
| 1311 | void Extract(xml::Element* element) override { |
| 1312 | const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1313 | int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR)); |
| 1314 | bool required = GetAttributeIntegerDefault( |
| 1315 | FindAttribute(element, REQUIRED_ATTR), true) != 0; |
| 1316 | int32_t version = GetAttributeIntegerDefault( |
| 1317 | FindAttribute(element, kAndroidNamespace, "version"), 0); |
| 1318 | |
| 1319 | // Add the feature to the parent feature group element if one exists; otherwise, add it to the |
| 1320 | // common feature group |
| 1321 | FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]); |
| 1322 | if (!feature_group) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1323 | feature_group = extractor()->common_feature_group(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1324 | } else { |
| 1325 | // All features in side of <feature-group> elements are required. |
| 1326 | required = true; |
| 1327 | } |
| 1328 | |
| 1329 | if (name) { |
| 1330 | feature_group->AddFeature(*name, required, version); |
| 1331 | } else if (gl) { |
| 1332 | feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl); |
| 1333 | } |
| 1334 | } |
| 1335 | }; |
| 1336 | |
| 1337 | /** Represents <uses-permission> elements. **/ |
| 1338 | class UsesPermission : public ManifestExtractor::Element { |
| 1339 | public: |
| 1340 | UsesPermission() = default; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1341 | bool implied; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1342 | std::string name; |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 1343 | std::vector<std::string> requiredFeatures; |
| 1344 | std::vector<std::string> requiredNotFeatures; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1345 | int32_t required = true; |
| 1346 | int32_t maxSdkVersion = -1; |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 1347 | int32_t usesPermissionFlags = 0; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1348 | std::string impliedReason; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1349 | |
| 1350 | void Extract(xml::Element* element) override { |
Iurii Makhno | 0df4e6d | 2022-11-29 19:19:13 +0000 | [diff] [blame^] | 1351 | const auto parent_stack = extractor()->parent_stack(); |
| 1352 | if (!extractor()->options_.only_permissions && |
| 1353 | (parent_stack.size() != 1 || !ElementCast<Manifest>(parent_stack[0]))) { |
| 1354 | return; |
| 1355 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1356 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 1357 | std::string feature = |
| 1358 | GetAttributeStringDefault(FindAttribute(element, REQUIRED_FEATURE_ATTR), ""); |
| 1359 | if (!feature.empty()) { |
| 1360 | requiredFeatures.push_back(feature); |
| 1361 | } |
| 1362 | feature = GetAttributeStringDefault(FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), ""); |
| 1363 | if (!feature.empty()) { |
| 1364 | requiredNotFeatures.push_back(feature); |
| 1365 | } |
| 1366 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1367 | required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1); |
| 1368 | maxSdkVersion = GetAttributeIntegerDefault( |
| 1369 | FindAttribute(element, MAX_SDK_VERSION_ATTR), -1); |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 1370 | usesPermissionFlags = GetAttributeIntegerDefault( |
| 1371 | FindAttribute(element, USES_PERMISSION_FLAGS_ATTR), 0); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (!name.empty()) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1374 | CommonFeatureGroup* common = extractor()->common_feature_group(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1375 | common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1379 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1380 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1381 | printer->Print(StringPrintf("uses-permission: name='%s'", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1382 | if (maxSdkVersion >= 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1383 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1384 | } |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 1385 | if ((usesPermissionFlags & kNeverForLocation) != 0) { |
| 1386 | printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'")); |
| 1387 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1388 | printer->Print("\n"); |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 1389 | for (const std::string& requiredFeature : requiredFeatures) { |
| 1390 | printer->Print(StringPrintf(" required-feature='%s'\n", requiredFeature.data())); |
| 1391 | } |
| 1392 | for (const std::string& requiredNotFeature : requiredNotFeatures) { |
| 1393 | printer->Print(StringPrintf(" required-not-feature='%s'\n", requiredNotFeature.data())); |
| 1394 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1395 | if (required == 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1396 | printer->Print(StringPrintf("optional-permission: name='%s'", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1397 | if (maxSdkVersion >= 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1398 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1399 | } |
Jeff Sharkey | abcddfd | 2021-03-29 10:05:21 -0600 | [diff] [blame] | 1400 | if ((usesPermissionFlags & kNeverForLocation) != 0) { |
| 1401 | printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'")); |
| 1402 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1403 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1404 | } |
| 1405 | } |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1406 | if (implied) { |
| 1407 | printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data())); |
| 1408 | if (maxSdkVersion >= 0) { |
| 1409 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
| 1410 | } |
| 1411 | if ((usesPermissionFlags & kNeverForLocation) != 0) { |
| 1412 | printer->Print(StringPrintf(" usesPermissionFlags='neverForLocation'")); |
| 1413 | } |
| 1414 | printer->Print(StringPrintf(" reason='%s'\n", impliedReason.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1415 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1416 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1417 | |
| 1418 | void ToProto(pb::Badging* out_badging) override { |
| 1419 | if (!name.empty()) { |
| 1420 | auto permission = out_badging->add_uses_permissions(); |
| 1421 | permission->set_name(name); |
| 1422 | if (maxSdkVersion > 0) { |
| 1423 | permission->set_max_sdk_version(maxSdkVersion); |
| 1424 | } |
| 1425 | if ((usesPermissionFlags & kNeverForLocation) != 0) { |
| 1426 | permission->mutable_permission_flags()->set_never_for_location(true); |
| 1427 | } |
| 1428 | for (auto& requiredFeature : requiredFeatures) { |
| 1429 | permission->add_required_features(requiredFeature); |
| 1430 | } |
| 1431 | for (auto& requiredNotFeature : requiredNotFeatures) { |
| 1432 | permission->add_required_not_features(requiredNotFeature); |
| 1433 | } |
| 1434 | permission->set_required(required != 0); |
| 1435 | permission->set_implied(implied); |
| 1436 | } |
| 1437 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1438 | }; |
| 1439 | |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 1440 | /** Represents <required-feature> elements. **/ |
| 1441 | class RequiredFeature : public ManifestExtractor::Element { |
| 1442 | public: |
| 1443 | RequiredFeature() = default; |
| 1444 | std::string name; |
| 1445 | |
| 1446 | void Extract(xml::Element* element) override { |
| 1447 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1448 | auto parent_stack = extractor()->parent_stack(); |
| 1449 | if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) { |
| 1450 | UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]); |
| 1451 | uses_permission->requiredFeatures.push_back(name); |
| 1452 | } |
| 1453 | } |
| 1454 | }; |
| 1455 | |
| 1456 | /** Represents <required-not-feature> elements. **/ |
| 1457 | class RequiredNotFeature : public ManifestExtractor::Element { |
| 1458 | public: |
| 1459 | RequiredNotFeature() = default; |
| 1460 | std::string name; |
| 1461 | |
| 1462 | void Extract(xml::Element* element) override { |
| 1463 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1464 | auto parent_stack = extractor()->parent_stack(); |
| 1465 | if (!name.empty() && ElementCast<UsesPermission>(parent_stack[0])) { |
| 1466 | UsesPermission* uses_permission = ElementCast<UsesPermission>(parent_stack[0]); |
| 1467 | uses_permission->requiredNotFeatures.push_back(name); |
| 1468 | } |
| 1469 | } |
| 1470 | }; |
| 1471 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1472 | /** Represents <uses-permission-sdk-23> elements. **/ |
| 1473 | class UsesPermissionSdk23 : public ManifestExtractor::Element { |
| 1474 | public: |
| 1475 | UsesPermissionSdk23() = default; |
| 1476 | const std::string* name = nullptr; |
| 1477 | const int32_t* maxSdkVersion = nullptr; |
| 1478 | |
| 1479 | void Extract(xml::Element* element) override { |
Iurii Makhno | 0df4e6d | 2022-11-29 19:19:13 +0000 | [diff] [blame^] | 1480 | const auto parent_stack = extractor()->parent_stack(); |
| 1481 | if (!extractor()->options_.only_permissions && |
| 1482 | (parent_stack.size() != 1 || !ElementCast<Manifest>(parent_stack[0]))) { |
| 1483 | return; |
| 1484 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1485 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1486 | maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR)); |
| 1487 | |
| 1488 | if (name) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1489 | CommonFeatureGroup* common = extractor()->common_feature_group(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1490 | common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true); |
| 1491 | } |
| 1492 | } |
| 1493 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1494 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1495 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1496 | printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1497 | if (maxSdkVersion) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1498 | printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1499 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1500 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1501 | } |
| 1502 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1503 | |
| 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 Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1514 | }; |
| 1515 | |
| 1516 | /** Represents <permission> elements. These elements are only printing when dumping permissions. **/ |
| 1517 | class 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 Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1526 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1527 | if (extractor()->options_.only_permissions && !name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1528 | printer->Print(StringPrintf("permission: %s\n", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1529 | } |
| 1530 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1531 | |
| 1532 | void ToProto(pb::Badging* out_badging) override { |
| 1533 | if (!name.empty()) { |
| 1534 | out_badging->add_permissions()->set_name(name); |
| 1535 | } |
| 1536 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1537 | }; |
| 1538 | |
| 1539 | /** Represents <activity> elements. **/ |
| 1540 | class 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 Hsieh | f2ef657 | 2020-02-11 14:27:11 -0800 | [diff] [blame] | 1569 | ssize_t idx = name.find('.'); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1570 | 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 Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 1578 | CommonFeatureGroup* common = extractor()->common_feature_group(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1579 | 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 Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1594 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1595 | // Print whether the activity has the HOME category and a the MAIN action |
| 1596 | if (has_main_action && has_launcher_category) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1597 | printer->Print("launchable-activity:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1598 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1599 | printer->Print(StringPrintf(" name='%s' ", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1600 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1601 | printer->Print(StringPrintf(" label='%s' icon='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1602 | 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 Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1608 | printer->Print("leanback-launchable-activity:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1609 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1610 | printer->Print(StringPrintf(" name='%s' ", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1611 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1612 | printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1613 | android::ResTable::normalizeForOutput(label.data()).c_str(), |
| 1614 | icon.data(), banner.data())); |
| 1615 | } |
| 1616 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1617 | |
| 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 Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1633 | }; |
| 1634 | |
| 1635 | /** Represents <intent-filter> elements. */ |
| 1636 | class IntentFilter : public ManifestExtractor::Element { |
| 1637 | public: |
| 1638 | IntentFilter() = default; |
| 1639 | }; |
| 1640 | |
| 1641 | /** Represents <category> elements. */ |
| 1642 | class 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 | **/ |
| 1670 | class 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. **/ |
| 1689 | class 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. **/ |
| 1701 | class 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. **/ |
| 1713 | class UsesLibrary : public ManifestExtractor::Element { |
| 1714 | public: |
| 1715 | UsesLibrary() = default; |
| 1716 | std::string name; |
| 1717 | int required; |
| 1718 | |
| 1719 | void Extract(xml::Element* element) override { |
| 1720 | auto parent_stack = extractor()->parent_stack(); |
| 1721 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1722 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1723 | required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1); |
| 1724 | } |
| 1725 | } |
| 1726 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1727 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1728 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1729 | printer->Print(StringPrintf("uses-library%s:'%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1730 | (required == 0) ? "-not-required" : "", name.data())); |
| 1731 | } |
| 1732 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1733 | |
| 1734 | void ToProto(pb::Badging* out_badging) override { |
| 1735 | if (!name.empty()) { |
| 1736 | auto uses_library = out_badging->add_uses_libraries(); |
| 1737 | uses_library->set_name(name); |
| 1738 | uses_library->set_required(required != 0); |
| 1739 | } |
| 1740 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1741 | }; |
| 1742 | |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1743 | /** Represents <static-library> elements. **/ |
| 1744 | class StaticLibrary : public ManifestExtractor::Element { |
| 1745 | public: |
| 1746 | StaticLibrary() = default; |
| 1747 | std::string name; |
| 1748 | int version; |
| 1749 | int versionMajor; |
| 1750 | |
| 1751 | void Extract(xml::Element* element) override { |
| 1752 | auto parent_stack = extractor()->parent_stack(); |
| 1753 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1754 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1755 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 1756 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1757 | } |
| 1758 | } |
| 1759 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1760 | void Print(text::Printer* printer) override { |
| 1761 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1762 | "static-library: name='%s' version='%d' versionMajor='%d'\n", |
| 1763 | name.data(), version, versionMajor)); |
| 1764 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1765 | |
| 1766 | void ToProto(pb::Badging* out_badging) override { |
| 1767 | auto static_library = out_badging->mutable_static_library(); |
| 1768 | static_library->set_name(name); |
| 1769 | static_library->set_version(version); |
| 1770 | static_library->set_version_major(versionMajor); |
| 1771 | } |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1772 | }; |
| 1773 | |
| 1774 | /** Represents <uses-static-library> elements. **/ |
| 1775 | class UsesStaticLibrary : public ManifestExtractor::Element { |
| 1776 | public: |
| 1777 | UsesStaticLibrary() = default; |
| 1778 | std::string name; |
| 1779 | int version; |
| 1780 | int versionMajor; |
| 1781 | std::vector<std::string> certDigests; |
| 1782 | |
| 1783 | void Extract(xml::Element* element) override { |
| 1784 | auto parent_stack = extractor()->parent_stack(); |
| 1785 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1786 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1787 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 1788 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1789 | AddCertDigest(element); |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | void AddCertDigest(xml::Element* element) { |
| 1794 | std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), ""); |
| 1795 | // We allow ":" delimiters in the SHA declaration as this is the format |
| 1796 | // emitted by the certtool making it easy for developers to copy/paste. |
| 1797 | digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end()); |
| 1798 | if (!digest.empty()) { |
| 1799 | certDigests.push_back(digest); |
| 1800 | } |
| 1801 | } |
| 1802 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1803 | void Print(text::Printer* printer) override { |
| 1804 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1805 | "uses-static-library: name='%s' version='%d' versionMajor='%d'", |
| 1806 | name.data(), version, versionMajor)); |
| 1807 | for (size_t i = 0; i < certDigests.size(); i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1808 | printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1809 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1810 | printer->Print("\n"); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1811 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1812 | |
| 1813 | void ToProto(pb::Badging* out_badging) override { |
| 1814 | auto uses_static_library = out_badging->add_uses_static_libraries(); |
| 1815 | uses_static_library->set_name(name); |
| 1816 | uses_static_library->set_version(version); |
| 1817 | uses_static_library->set_version_major(versionMajor); |
| 1818 | for (auto& cert : certDigests) { |
| 1819 | uses_static_library->add_certificates(cert); |
| 1820 | } |
| 1821 | } |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1822 | }; |
| 1823 | |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 1824 | /** Represents <sdk-library> elements. **/ |
| 1825 | class SdkLibrary : public ManifestExtractor::Element { |
| 1826 | public: |
| 1827 | SdkLibrary() = default; |
| 1828 | std::string name; |
| 1829 | int versionMajor; |
| 1830 | |
| 1831 | void Extract(xml::Element* element) override { |
| 1832 | auto parent_stack = extractor()->parent_stack(); |
| 1833 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1834 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1835 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | void Print(text::Printer* printer) override { |
| 1840 | printer->Print( |
| 1841 | StringPrintf("sdk-library: name='%s' versionMajor='%d'\n", name.data(), versionMajor)); |
| 1842 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1843 | |
| 1844 | void ToProto(pb::Badging* out_badging) override { |
| 1845 | auto sdk_library = out_badging->mutable_sdk_library(); |
| 1846 | sdk_library->set_name(name); |
| 1847 | sdk_library->set_version_major(versionMajor); |
| 1848 | } |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 1849 | }; |
| 1850 | |
| 1851 | /** Represents <uses-sdk-library> elements. **/ |
| 1852 | class UsesSdkLibrary : public ManifestExtractor::Element { |
| 1853 | public: |
| 1854 | UsesSdkLibrary() = default; |
| 1855 | std::string name; |
| 1856 | int versionMajor; |
| 1857 | std::vector<std::string> certDigests; |
| 1858 | |
| 1859 | void Extract(xml::Element* element) override { |
| 1860 | auto parent_stack = extractor()->parent_stack(); |
| 1861 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1862 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1863 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1864 | AddCertDigest(element); |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | void AddCertDigest(xml::Element* element) { |
| 1869 | std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), ""); |
| 1870 | // We allow ":" delimiters in the SHA declaration as this is the format |
| 1871 | // emitted by the certtool making it easy for developers to copy/paste. |
| 1872 | digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end()); |
| 1873 | if (!digest.empty()) { |
| 1874 | certDigests.push_back(digest); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | void Print(text::Printer* printer) override { |
| 1879 | printer->Print( |
| 1880 | StringPrintf("uses-sdk-library: name='%s' versionMajor='%d'", name.data(), versionMajor)); |
| 1881 | for (size_t i = 0; i < certDigests.size(); i++) { |
| 1882 | printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); |
| 1883 | } |
| 1884 | printer->Print("\n"); |
| 1885 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1886 | |
| 1887 | void ToProto(pb::Badging* out_badging) override { |
| 1888 | auto uses_sdk_library = out_badging->add_uses_sdk_libraries(); |
| 1889 | uses_sdk_library->set_name(name); |
| 1890 | uses_sdk_library->set_version_major(versionMajor); |
| 1891 | for (auto& cert : certDigests) { |
| 1892 | uses_sdk_library->add_certificates(cert); |
| 1893 | } |
| 1894 | } |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 1895 | }; |
| 1896 | |
Jiyong Park | 6a5b8b1 | 2020-06-30 13:23:36 +0900 | [diff] [blame] | 1897 | /** Represents <uses-native-library> elements. **/ |
| 1898 | class UsesNativeLibrary : public ManifestExtractor::Element { |
| 1899 | public: |
| 1900 | UsesNativeLibrary() = default; |
| 1901 | std::string name; |
| 1902 | int required; |
| 1903 | |
| 1904 | void Extract(xml::Element* element) override { |
| 1905 | auto parent_stack = extractor()->parent_stack(); |
| 1906 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1907 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1908 | required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | void Print(text::Printer* printer) override { |
| 1913 | if (!name.empty()) { |
| 1914 | printer->Print(StringPrintf("uses-native-library%s:'%s'\n", |
| 1915 | (required == 0) ? "-not-required" : "", name.data())); |
| 1916 | } |
| 1917 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1918 | |
| 1919 | void ToProto(pb::Badging* out_badging) override { |
| 1920 | if (!name.empty()) { |
| 1921 | auto uses_native_library = out_badging->add_uses_native_libraries(); |
| 1922 | uses_native_library->set_name(name); |
| 1923 | uses_native_library->set_required(required != 0); |
| 1924 | } |
| 1925 | } |
Jiyong Park | 6a5b8b1 | 2020-06-30 13:23:36 +0900 | [diff] [blame] | 1926 | }; |
| 1927 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1928 | /** |
| 1929 | * Represents <meta-data> elements. These tags are only printed when a flag is passed in to |
| 1930 | * explicitly enable meta data printing. |
| 1931 | **/ |
| 1932 | class MetaData : public ManifestExtractor::Element { |
| 1933 | public: |
| 1934 | MetaData() = default; |
| 1935 | std::string name; |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1936 | std::string value; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1937 | const int* value_int; |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1938 | std::string resource; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1939 | const int* resource_int; |
| 1940 | |
| 1941 | void Extract(xml::Element* element) override { |
| 1942 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1943 | value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), ""); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1944 | value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR)); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1945 | resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), ""); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1946 | resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR)); |
| 1947 | } |
| 1948 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1949 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1950 | if (extractor()->options_.include_meta_data && !name.empty()) { |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame] | 1951 | printer->Print(StringPrintf("meta-data: name='%s'", name.data())); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1952 | if (!value.empty()) { |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame] | 1953 | printer->Print(StringPrintf(" value='%s'", value.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1954 | } else if (value_int) { |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame] | 1955 | printer->Print(StringPrintf(" value='%d'", *value_int)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1956 | } else { |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1957 | if (!resource.empty()) { |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame] | 1958 | printer->Print(StringPrintf(" resource='%s'", resource.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1959 | } else if (resource_int) { |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame] | 1960 | printer->Print(StringPrintf(" resource='%d'", *resource_int)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1961 | } |
| 1962 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1963 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1964 | } |
| 1965 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 1966 | |
| 1967 | void ToProto(pb::Badging* out_badging) override { |
| 1968 | if (!name.empty()) { |
| 1969 | auto metadata = out_badging->add_metadata(); |
| 1970 | metadata->set_name(name); |
| 1971 | if (!value.empty()) { |
| 1972 | metadata->set_value_string(value); |
| 1973 | } else if (value_int) { |
| 1974 | metadata->set_value_int(*value_int); |
| 1975 | } else { |
| 1976 | if (!resource.empty()) { |
| 1977 | metadata->set_resource_string(resource); |
| 1978 | } else if (resource_int) { |
| 1979 | metadata->set_resource_int(*resource_int); |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1984 | }; |
| 1985 | |
| 1986 | /** |
| 1987 | * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and |
| 1988 | * service components. |
| 1989 | **/ |
| 1990 | class Action : public ManifestExtractor::Element { |
| 1991 | public: |
| 1992 | Action() = default; |
| 1993 | std::string component = ""; |
| 1994 | |
| 1995 | void Extract(xml::Element* element) override { |
| 1996 | auto parent_stack = extractor()->parent_stack(); |
| 1997 | std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1998 | |
| 1999 | if (ElementCast<IntentFilter>(parent_stack[0])) { |
| 2000 | if (ElementCast<Activity>(parent_stack[1])) { |
| 2001 | // Detects the presence of a particular type of activity. |
| 2002 | Activity* activity = ElementCast<Activity>(parent_stack[1]); |
Iurii Makhno | 499ecd3 | 2022-08-19 16:34:26 +0000 | [diff] [blame] | 2003 | static const auto map = std::map<std::string, std::string>({ |
| 2004 | {"android.intent.action.MAIN", "main"}, |
| 2005 | {"android.media.action.VIDEO_CAMERA", "camera"}, |
| 2006 | {"android.media.action.STILL_IMAGE_CAMERA", "camera"}, |
| 2007 | {"android.media.action.STILL_IMAGE_CAMERA_SECURE", "camera-secure"}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2008 | }); |
| 2009 | |
| 2010 | auto entry = map.find(action); |
| 2011 | if (entry != map.end()) { |
| 2012 | component = entry->second; |
| 2013 | activity->has_component_ = true; |
| 2014 | } |
| 2015 | |
| 2016 | if (action == "android.intent.action.MAIN") { |
| 2017 | activity->has_main_action = true; |
| 2018 | } |
| 2019 | |
| 2020 | } else if (ElementCast<Receiver>(parent_stack[1])) { |
| 2021 | // Detects the presence of a particular type of receiver. If the action requires a |
| 2022 | // permission, then the receiver element is checked for the permission. |
| 2023 | Receiver* receiver = ElementCast<Receiver>(parent_stack[1]); |
| 2024 | auto map = std::map<std::string, std::string>({ |
| 2025 | { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" }, |
| 2026 | { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" }, |
| 2027 | }); |
| 2028 | |
| 2029 | auto permissions = std::map<std::string, std::string>({ |
| 2030 | { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" }, |
| 2031 | }); |
| 2032 | |
| 2033 | auto entry = map.find(action); |
| 2034 | auto permission = permissions.find(action); |
| 2035 | if (entry != map.end() && (permission == permissions.end() |
| 2036 | || (receiver->permission && permission->second == *receiver->permission))) { |
| 2037 | receiver->has_component = true; |
| 2038 | component = entry->second; |
| 2039 | } |
| 2040 | |
| 2041 | } else if (ElementCast<Service>(parent_stack[1])) { |
| 2042 | // Detects the presence of a particular type of service. If the action requires a |
| 2043 | // permission, then the service element is checked for the permission. |
| 2044 | Service* service = ElementCast<Service>(parent_stack[1]); |
| 2045 | auto map = std::map<std::string, std::string>({ |
| 2046 | { "android.view.InputMethod" , "ime" }, |
| 2047 | { "android.service.wallpaper.WallpaperService" , "wallpaper" }, |
| 2048 | { "android.accessibilityservice.AccessibilityService" , "accessibility" }, |
| 2049 | { "android.printservice.PrintService" , "print-service" }, |
| 2050 | { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" }, |
| 2051 | { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" }, |
| 2052 | { "android.service.notification.NotificationListenerService" ,"notification-listener" }, |
| 2053 | { "android.service.dreams.DreamService" , "dream" }, |
| 2054 | }); |
| 2055 | |
| 2056 | auto permissions = std::map<std::string, std::string>({ |
| 2057 | { "android.accessibilityservice.AccessibilityService" , |
| 2058 | "android.permission.BIND_ACCESSIBILITY_SERVICE" }, |
| 2059 | { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" }, |
| 2060 | { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , |
| 2061 | "android.permission.BIND_NFC_SERVICE" }, |
| 2062 | { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , |
| 2063 | "android.permission.BIND_NFC_SERVICE" }, |
| 2064 | { "android.service.notification.NotificationListenerService" , |
| 2065 | "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" }, |
| 2066 | { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" }, |
| 2067 | }); |
| 2068 | |
| 2069 | auto entry = map.find(action); |
| 2070 | auto permission = permissions.find(action); |
| 2071 | if (entry != map.end() && (permission == permissions.end() |
| 2072 | || (service->permission && permission->second == *service->permission))) { |
| 2073 | service->has_component= true; |
| 2074 | component = entry->second; |
| 2075 | } |
| 2076 | |
| 2077 | } else if (ElementCast<Provider>(parent_stack[1])) { |
| 2078 | // Detects the presence of a particular type of receiver. If the provider requires a |
| 2079 | // permission, then the provider element is checked for the permission. |
| 2080 | // Detect whether this action |
| 2081 | Provider* provider = ElementCast<Provider>(parent_stack[1]); |
| 2082 | if (action == "android.content.action.DOCUMENTS_PROVIDER" |
| 2083 | && provider->has_required_saf_attributes) { |
| 2084 | component = "document-provider"; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | // Represents a searchable interface |
| 2090 | if (action == "android.intent.action.SEARCH") { |
| 2091 | component = "search"; |
| 2092 | } |
| 2093 | } |
| 2094 | }; |
| 2095 | |
| 2096 | /** |
| 2097 | * Represents <supports-input> elements. The element may have <input-type> elements nested within. |
| 2098 | **/ |
| 2099 | class SupportsInput : public ManifestExtractor::Element { |
| 2100 | public: |
| 2101 | SupportsInput() = default; |
| 2102 | std::vector<std::string> inputs; |
| 2103 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2104 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2105 | const size_t size = inputs.size(); |
| 2106 | if (size > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2107 | printer->Print("supports-input: '"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2108 | for (size_t i = 0; i < size; i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2109 | printer->Print(StringPrintf("value='%s' ", inputs[i].data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2110 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2111 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2112 | } |
| 2113 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2114 | |
| 2115 | void ToProto(pb::Badging* out_badging) override { |
| 2116 | auto supports_input = out_badging->mutable_supports_input(); |
| 2117 | for (auto& input : inputs) { |
| 2118 | supports_input->add_inputs(input); |
| 2119 | } |
| 2120 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2121 | }; |
| 2122 | |
| 2123 | /** Represents <input-type> elements. **/ |
| 2124 | class InputType : public ManifestExtractor::Element { |
| 2125 | public: |
| 2126 | InputType() = default; |
| 2127 | void Extract(xml::Element* element) override { |
| 2128 | auto name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 2129 | auto parent_stack = extractor()->parent_stack(); |
| 2130 | |
| 2131 | // Add the input to the set of supported inputs |
| 2132 | if (name && ElementCast<SupportsInput>(parent_stack[0])) { |
| 2133 | SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]); |
| 2134 | supports->inputs.push_back(*name); |
| 2135 | } |
| 2136 | } |
| 2137 | }; |
| 2138 | |
| 2139 | /** Represents <original-package> elements. **/ |
| 2140 | class OriginalPackage : public ManifestExtractor::Element { |
| 2141 | public: |
| 2142 | OriginalPackage() = default; |
| 2143 | const std::string* name = nullptr; |
| 2144 | |
| 2145 | void Extract(xml::Element* element) override { |
| 2146 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 2147 | } |
| 2148 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2149 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2150 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2151 | printer->Print(StringPrintf("original-package:'%s'\n", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2154 | |
| 2155 | void ToProto(pb::Badging* out_badging) override { |
| 2156 | if (name) { |
| 2157 | out_badging->mutable_package()->set_original_package(*name); |
| 2158 | } |
| 2159 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2160 | }; |
| 2161 | |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 2162 | |
| 2163 | /** Represents <overlay> elements. **/ |
| 2164 | class Overlay : public ManifestExtractor::Element { |
| 2165 | public: |
| 2166 | Overlay() = default; |
| 2167 | const std::string* target_package = nullptr; |
| 2168 | int priority; |
| 2169 | bool is_static; |
| 2170 | const std::string* required_property_name = nullptr; |
| 2171 | const std::string* required_property_value = nullptr; |
| 2172 | |
| 2173 | void Extract(xml::Element* element) override { |
| 2174 | target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR)); |
| 2175 | priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0); |
| 2176 | is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0; |
| 2177 | required_property_name = GetAttributeString( |
| 2178 | FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR)); |
| 2179 | required_property_value = GetAttributeString( |
| 2180 | FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR)); |
| 2181 | } |
| 2182 | |
| 2183 | void Print(text::Printer* printer) override { |
| 2184 | printer->Print(StringPrintf("overlay:")); |
| 2185 | if (target_package) { |
| 2186 | printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str())); |
| 2187 | } |
| 2188 | printer->Print(StringPrintf(" priority='%d'", priority)); |
| 2189 | printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false")); |
| 2190 | if (required_property_name) { |
| 2191 | printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str())); |
| 2192 | } |
| 2193 | if (required_property_value) { |
| 2194 | printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str())); |
| 2195 | } |
| 2196 | printer->Print("\n"); |
| 2197 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2198 | |
| 2199 | void ToProto(pb::Badging* out_badging) override { |
| 2200 | auto overlay = out_badging->mutable_overlay(); |
| 2201 | if (target_package) { |
| 2202 | overlay->set_target_package(*target_package); |
| 2203 | } |
| 2204 | overlay->set_priority(priority); |
| 2205 | overlay->set_static_(is_static); |
| 2206 | if (required_property_name) { |
| 2207 | overlay->set_required_property_name(*required_property_name); |
| 2208 | } |
| 2209 | if (required_property_value) { |
| 2210 | overlay->set_required_property_value(*required_property_value); |
| 2211 | } |
| 2212 | } |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 2213 | }; |
| 2214 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2215 | /** * Represents <package-verifier> elements. **/ |
| 2216 | class PackageVerifier : public ManifestExtractor::Element { |
| 2217 | public: |
| 2218 | PackageVerifier() = default; |
| 2219 | const std::string* name = nullptr; |
| 2220 | const std::string* public_key = nullptr; |
| 2221 | |
| 2222 | void Extract(xml::Element* element) override { |
| 2223 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 2224 | public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR)); |
| 2225 | } |
| 2226 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2227 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2228 | if (name && public_key) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2229 | printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2230 | name->data(), public_key->data())); |
| 2231 | } |
| 2232 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2233 | |
| 2234 | void ToProto(pb::Badging* out_badging) override { |
| 2235 | auto package_verifier = out_badging->mutable_package_verifier(); |
| 2236 | if (name && public_key) { |
| 2237 | package_verifier->set_name(*name); |
| 2238 | package_verifier->set_public_key(*public_key); |
| 2239 | } |
| 2240 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2241 | }; |
| 2242 | |
| 2243 | /** Represents <uses-package> elements. **/ |
| 2244 | class UsesPackage : public ManifestExtractor::Element { |
| 2245 | public: |
| 2246 | UsesPackage() = default; |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2247 | const std::string* packageType = nullptr; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2248 | const std::string* name = nullptr; |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2249 | int version; |
| 2250 | int versionMajor; |
| 2251 | std::vector<std::string> certDigests; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2252 | |
| 2253 | void Extract(xml::Element* element) override { |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2254 | auto parent_stack = extractor()->parent_stack(); |
| 2255 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 2256 | packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR)); |
| 2257 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 2258 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 2259 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 2260 | AddCertDigest(element); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | void AddCertDigest(xml::Element* element) { |
| 2265 | std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), ""); |
| 2266 | // We allow ":" delimiters in the SHA declaration as this is the format |
| 2267 | // emitted by the certtool making it easy for developers to copy/paste. |
| 2268 | digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end()); |
| 2269 | if (!digest.empty()) { |
| 2270 | certDigests.push_back(digest); |
| 2271 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2274 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2275 | if (name) { |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2276 | if (packageType) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2277 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2278 | "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'", |
| 2279 | packageType->data(), name->data(), version, versionMajor)); |
| 2280 | for (size_t i = 0; i < certDigests.size(); i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2281 | printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2282 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2283 | printer->Print("\n"); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2284 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2285 | printer->Print(StringPrintf("uses-package:'%s'\n", name->data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2289 | |
| 2290 | void ToProto(pb::Badging* out_badging) override { |
| 2291 | if (name) { |
| 2292 | auto uses_package = out_badging->add_uses_packages(); |
| 2293 | uses_package->set_name(*name); |
| 2294 | if (packageType) { |
| 2295 | uses_package->set_package_type(*packageType); |
| 2296 | uses_package->set_version(version); |
| 2297 | uses_package->set_version_major(versionMajor); |
| 2298 | for (auto& cert : certDigests) { |
| 2299 | uses_package->add_certificates(cert); |
| 2300 | } |
| 2301 | } |
| 2302 | } |
| 2303 | } |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2304 | }; |
| 2305 | |
| 2306 | /** Represents <additional-certificate> elements. **/ |
| 2307 | class AdditionalCertificate : public ManifestExtractor::Element { |
| 2308 | public: |
| 2309 | AdditionalCertificate() = default; |
| 2310 | |
| 2311 | void Extract(xml::Element* element) override { |
| 2312 | auto parent_stack = extractor()->parent_stack(); |
| 2313 | if (parent_stack.size() > 0) { |
| 2314 | if (ElementCast<UsesPackage>(parent_stack[0])) { |
| 2315 | UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]); |
| 2316 | uses->AddCertDigest(element); |
| 2317 | } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) { |
| 2318 | UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]); |
| 2319 | uses->AddCertDigest(element); |
| 2320 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | }; |
| 2324 | |
| 2325 | /** Represents <screen> elements found in <compatible-screens> elements. */ |
| 2326 | class Screen : public ManifestExtractor::Element { |
| 2327 | public: |
| 2328 | Screen() = default; |
| 2329 | const int32_t* size = nullptr; |
| 2330 | const int32_t* density = nullptr; |
| 2331 | |
| 2332 | void Extract(xml::Element* element) override { |
| 2333 | size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR)); |
| 2334 | density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR)); |
| 2335 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2336 | |
| 2337 | void ToProto(pb::Badging* out_badging) override { |
| 2338 | if (size && density) { |
| 2339 | auto screen = out_badging->mutable_compatible_screens()->add_screens(); |
| 2340 | screen->set_density(*density); |
| 2341 | screen->set_size(*size); |
| 2342 | } |
| 2343 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2344 | }; |
| 2345 | |
| 2346 | /** |
| 2347 | * Represents <compatible-screens> elements. These elements have <screen> elements nested within |
| 2348 | * that each denote a supported screen size and screen density. |
| 2349 | **/ |
| 2350 | class CompatibleScreens : public ManifestExtractor::Element { |
| 2351 | public: |
| 2352 | CompatibleScreens() = default; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2353 | void Print(text::Printer* printer) override { |
| 2354 | printer->Print("compatible-screens:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2355 | |
| 2356 | bool first = true; |
| 2357 | ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){ |
| 2358 | if (auto screen = ElementCast<Screen>(el)) { |
| 2359 | if (first) { |
| 2360 | first = false; |
| 2361 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2362 | printer->Print(","); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | if (screen->size && screen->density) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2366 | printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | }); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2370 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2371 | } |
| 2372 | }; |
| 2373 | |
| 2374 | /** Represents <supports-gl-texture> elements. **/ |
| 2375 | class SupportsGlTexture : public ManifestExtractor::Element { |
| 2376 | public: |
| 2377 | SupportsGlTexture() = default; |
| 2378 | const std::string* name = nullptr; |
| 2379 | |
| 2380 | void Extract(xml::Element* element) override { |
| 2381 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 2382 | } |
| 2383 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2384 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2385 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2386 | printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2387 | } |
| 2388 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2389 | |
| 2390 | void ToProto(pb::Badging* out_badging) override { |
| 2391 | if (name) { |
| 2392 | out_badging->mutable_supports_gl_texture()->add_name(*name); |
| 2393 | } |
| 2394 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2395 | }; |
| 2396 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 2397 | /** Represents <property> elements. **/ |
| 2398 | class Property : public ManifestExtractor::Element { |
| 2399 | public: |
| 2400 | Property() = default; |
| 2401 | std::string name; |
| 2402 | std::string value; |
| 2403 | const int* value_int; |
| 2404 | std::string resource; |
| 2405 | const int* resource_int; |
| 2406 | |
| 2407 | void Extract(xml::Element* element) override { |
| 2408 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 2409 | value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), ""); |
| 2410 | value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR)); |
| 2411 | resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), ""); |
| 2412 | resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR)); |
| 2413 | } |
| 2414 | |
| 2415 | void Print(text::Printer* printer) override { |
| 2416 | printer->Print(StringPrintf("property: name='%s' ", name.data())); |
| 2417 | if (!value.empty()) { |
| 2418 | printer->Print(StringPrintf("value='%s' ", value.data())); |
| 2419 | } else if (value_int) { |
| 2420 | printer->Print(StringPrintf("value='%d' ", *value_int)); |
| 2421 | } else { |
| 2422 | if (!resource.empty()) { |
| 2423 | printer->Print(StringPrintf("resource='%s' ", resource.data())); |
| 2424 | } else if (resource_int) { |
| 2425 | printer->Print(StringPrintf("resource='%d' ", *resource_int)); |
| 2426 | } |
| 2427 | } |
| 2428 | printer->Print("\n"); |
| 2429 | } |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 2430 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2431 | void ToProto(pb::Badging* out_badging) override { |
| 2432 | if (!name.empty()) { |
| 2433 | auto property = out_badging->add_properties(); |
| 2434 | property->set_name(name); |
| 2435 | if (!value.empty()) { |
| 2436 | property->set_value_string(value); |
| 2437 | } else if (value_int) { |
| 2438 | property->set_value_int(*value_int); |
| 2439 | } else { |
| 2440 | if (!resource.empty()) { |
| 2441 | property->set_resource_string(resource); |
| 2442 | } else if (resource_int) { |
| 2443 | property->set_resource_int(*resource_int); |
| 2444 | } |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2445 | } |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | }; |
| 2449 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2450 | /** Recursively prints the extracted badging element. */ |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2451 | static void Print(ManifestExtractor::Element* el, text::Printer* printer) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2452 | el->Print(printer); |
| 2453 | for (auto &child : el->children()) { |
| 2454 | Print(child.get(), printer); |
| 2455 | } |
| 2456 | } |
| 2457 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2458 | /** Recursively serializes extracted badging elements to proto. */ |
| 2459 | static void ToProto(ManifestExtractor::Element* el, pb::Badging* out_badging) { |
| 2460 | el->ToProto(out_badging); |
| 2461 | for (auto& child : el->children()) { |
| 2462 | ToProto(child.get(), out_badging); |
| 2463 | } |
| 2464 | } |
| 2465 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 2466 | bool ManifestExtractor::Extract(android::IDiagnostics* diag) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2467 | // Load the manifest |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2468 | doc_ = apk_->LoadXml("AndroidManifest.xml", diag); |
| 2469 | if (doc_ == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 2470 | diag->Error(android::DiagMessage() << "failed to find AndroidManifest.xml"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2471 | return false; |
| 2472 | } |
| 2473 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2474 | xml::Element* element = doc_->root.get(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2475 | if (element->name != "manifest") { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 2476 | diag->Error(android::DiagMessage() << "manifest does not start with <manifest> tag"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if |
| 2481 | // printing only permission elements is requested |
| 2482 | if (options_.only_permissions) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2483 | root_element_ = ManifestExtractor::Element::Inflate(this, element); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2484 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2485 | if (auto manifest = ElementCast<Manifest>(root_element_.get())) { |
| 2486 | manifest->only_package_name = true; |
| 2487 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2488 | for (xml::Element* child : element->GetChildElements()) { |
| 2489 | if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23" |
| 2490 | || child->name == "permission") { |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2491 | // Inflate the element and its descendants |
| 2492 | auto permission_element = Visit(child); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2493 | manifest->AddChild(permission_element); |
| 2494 | } |
| 2495 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2496 | return true; |
| 2497 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2498 | return false; |
| 2499 | } |
| 2500 | |
| 2501 | // Collect information about the resource configurations |
| 2502 | if (apk_->GetResourceTable()) { |
| 2503 | for (auto &package : apk_->GetResourceTable()->packages) { |
| 2504 | for (auto &type : package->types) { |
| 2505 | for (auto &entry : type->entries) { |
| 2506 | for (auto &value : entry->values) { |
| 2507 | std::string locale_str = value->config.GetBcp47LanguageTag(); |
| 2508 | |
| 2509 | // Collect all the unique locales of the apk |
| 2510 | if (locales_.find(locale_str) == locales_.end()) { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 2511 | ConfigDescription config = ManifestExtractor::DefaultConfig(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2512 | config.setBcp47Locale(locale_str.data()); |
| 2513 | locales_.insert(std::make_pair(locale_str, config)); |
| 2514 | } |
| 2515 | |
| 2516 | // Collect all the unique density of the apk |
| 2517 | uint16_t density = (value->config.density == 0) ? (uint16_t) 160 |
| 2518 | : value->config.density; |
| 2519 | if (densities_.find(density) == densities_.end()) { |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 2520 | ConfigDescription config = ManifestExtractor::DefaultConfig(); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2521 | config.density = density; |
| 2522 | densities_.insert(std::make_pair(density, config)); |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | // Extract badging information |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2531 | root_element_ = Visit(element); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2532 | |
Ryan Mitchell | 1966e1f | 2021-05-03 13:46:56 -0700 | [diff] [blame] | 2533 | // Filter out all "uses-sdk" tags besides the very last tag. The android runtime only uses the |
| 2534 | // attribute values from the last defined tag. |
| 2535 | std::vector<UsesSdkBadging*> filtered_uses_sdk_tags; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2536 | for (const auto& child : root_element_->children()) { |
Ryan Mitchell | 1966e1f | 2021-05-03 13:46:56 -0700 | [diff] [blame] | 2537 | if (auto uses_sdk = ElementCast<UsesSdkBadging>(child.get())) { |
| 2538 | filtered_uses_sdk_tags.emplace_back(uses_sdk); |
| 2539 | } |
| 2540 | } |
Ryan Mitchell | 6ea9ed3 | 2021-05-20 11:38:57 -0700 | [diff] [blame] | 2541 | if (filtered_uses_sdk_tags.size() >= 2U) { |
| 2542 | filtered_uses_sdk_tags.pop_back(); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2543 | root_element_->Filter([&](const ManifestExtractor::Element* e) { |
Ryan Mitchell | 6ea9ed3 | 2021-05-20 11:38:57 -0700 | [diff] [blame] | 2544 | return std::find(filtered_uses_sdk_tags.begin(), filtered_uses_sdk_tags.end(), e) != |
| 2545 | filtered_uses_sdk_tags.end(); |
| 2546 | }); |
| 2547 | } |
Ryan Mitchell | 1966e1f | 2021-05-03 13:46:56 -0700 | [diff] [blame] | 2548 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2549 | /** Recursively checks the extracted elements for the specified permission. **/ |
| 2550 | auto FindPermission = [&](ManifestExtractor::Element* root, |
| 2551 | const std::string& name) -> ManifestExtractor::Element* { |
| 2552 | return FindElement(root, [&](ManifestExtractor::Element* el) -> bool { |
| 2553 | if (UsesPermission* permission = ElementCast<UsesPermission>(el)) { |
| 2554 | return permission->name == name; |
| 2555 | } |
| 2556 | return false; |
| 2557 | }); |
| 2558 | }; |
| 2559 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2560 | auto AddImpliedPermission = [&](const std::string& name, const std::string& reason, |
| 2561 | int32_t max_sdk_version) -> void { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2562 | auto permission = util::make_unique<UsesPermission>(); |
| 2563 | permission->name = name; |
| 2564 | permission->maxSdkVersion = max_sdk_version; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2565 | permission->implied = true; |
| 2566 | permission->impliedReason = reason; |
| 2567 | implied_permissions_.push_back(std::move(permission)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2568 | }; |
| 2569 | |
| 2570 | // Implied permissions |
| 2571 | // Pre-1.6 implicitly granted permission compatibility logic |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2572 | bool insert_write_external = false; |
| 2573 | auto write_external_permission = ElementCast<UsesPermission>( |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2574 | FindPermission(root_element_.get(), "android.permission.WRITE_EXTERNAL_STORAGE")); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2575 | |
Jackal Guo | 201a60a | 2021-08-31 12:37:30 +0800 | [diff] [blame] | 2576 | if (target_sdk() < SDK_DONUT) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2577 | if (!write_external_permission) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2578 | AddImpliedPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2579 | insert_write_external = true; |
| 2580 | } |
| 2581 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2582 | if (!FindPermission(root_element_.get(), "android.permission.READ_PHONE_STATE")) { |
| 2583 | AddImpliedPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | // If the application has requested WRITE_EXTERNAL_STORAGE, we will |
| 2588 | // force them to always take READ_EXTERNAL_STORAGE as well. We always |
| 2589 | // do this (regardless of target API version) because we can't have |
| 2590 | // an app with write permission but not read permission. |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2591 | auto read_external = |
| 2592 | FindPermission(root_element_.get(), "android.permission.READ_EXTERNAL_STORAGE"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2593 | if (!read_external && (insert_write_external || write_external_permission)) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2594 | AddImpliedPermission( |
| 2595 | "android.permission.READ_EXTERNAL_STORAGE", "requested WRITE_EXTERNAL_STORAGE", |
| 2596 | (write_external_permission) ? write_external_permission->maxSdkVersion : -1); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | // Pre-JellyBean call log permission compatibility. |
Jackal Guo | 201a60a | 2021-08-31 12:37:30 +0800 | [diff] [blame] | 2600 | if (target_sdk() < SDK_JELLY_BEAN) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2601 | if (!FindPermission(root_element_.get(), "android.permission.READ_CALL_LOG") && |
| 2602 | FindPermission(root_element_.get(), "android.permission.READ_CONTACTS")) { |
| 2603 | AddImpliedPermission("android.permission.READ_CALL_LOG", |
| 2604 | "targetSdkVersion < 16 and requested READ_CONTACTS", -1); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2607 | if (!FindPermission(root_element_.get(), "android.permission.WRITE_CALL_LOG") && |
| 2608 | FindPermission(root_element_.get(), "android.permission.WRITE_CONTACTS")) { |
| 2609 | AddImpliedPermission("android.permission.WRITE_CALL_LOG", |
| 2610 | "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | // If the app hasn't declared the touchscreen as a feature requirement (either |
| 2615 | // directly or implied, required or not), then the faketouch feature is implied. |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2616 | if (!common_feature_group()->HasFeature("android.hardware.touchscreen")) { |
| 2617 | common_feature_group()->addImpliedFeature("android.hardware.faketouch", |
| 2618 | "default feature for all apps", false); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | // Only print the common feature group if no feature group is defined |
| 2622 | std::vector<FeatureGroup*> feature_groups; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2623 | ForEachChild(root_element_.get(), [&feature_groups](ManifestExtractor::Element* el) -> void { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2624 | if (auto feature_group = ElementCast<FeatureGroup>(el)) { |
| 2625 | feature_groups.push_back(feature_group); |
| 2626 | } |
| 2627 | }); |
| 2628 | |
| 2629 | if (feature_groups.empty()) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2630 | feature_groups_.push_back(common_feature_group()); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2631 | } else { |
| 2632 | // Merge the common feature group into the feature group |
| 2633 | for (auto& feature_group : feature_groups) { |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2634 | feature_group->Merge(common_feature_group()); |
| 2635 | feature_groups_.push_back(feature_group); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2636 | } |
| 2637 | }; |
| 2638 | |
| 2639 | // Collect the component types of the application |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2640 | ForEachChild(root_element_.get(), [&](ManifestExtractor::Element* el) -> void { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2641 | if (ElementCast<Action>(el)) { |
| 2642 | auto action = ElementCast<Action>(el); |
| 2643 | if (!action->component.empty()) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2644 | components_.discovered_components.insert(action->component); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2645 | return; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | if (ElementCast<Category>(el)) { |
| 2650 | auto category = ElementCast<Category>(el); |
| 2651 | if (!category->component.empty()) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2652 | components_.discovered_components.insert(category->component); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2653 | return; |
| 2654 | } |
| 2655 | } |
| 2656 | }); |
| 2657 | |
| 2658 | // Check for the payment component |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2659 | ForEachChild(root_element_.get(), [this, &diag](ManifestExtractor::Element* el) -> void { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2660 | if (auto service = ElementCast<Service>(el)) { |
| 2661 | auto host_apdu_action = ElementCast<Action>(FindElement(service, |
| 2662 | [&](ManifestExtractor::Element* el) -> bool { |
| 2663 | if (auto action = ElementCast<Action>(el)) { |
| 2664 | return (action->component == "host-apdu"); |
| 2665 | } |
| 2666 | return false; |
| 2667 | })); |
| 2668 | |
| 2669 | auto offhost_apdu_action = ElementCast<Action>(FindElement(service, |
| 2670 | [&](ManifestExtractor::Element* el) -> bool { |
| 2671 | if (auto action = ElementCast<Action>(el)) { |
| 2672 | return (action->component == "offhost-apdu"); |
| 2673 | } |
| 2674 | return false; |
| 2675 | })); |
| 2676 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2677 | ForEachChild(service, |
| 2678 | [this, &diag, &host_apdu_action, |
| 2679 | &offhost_apdu_action](ManifestExtractor::Element* el) -> void { |
| 2680 | if (auto meta_data = ElementCast<MetaData>(el)) { |
| 2681 | if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && |
| 2682 | host_apdu_action) || |
| 2683 | (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" && |
| 2684 | offhost_apdu_action)) { |
| 2685 | // Attempt to load the resource file |
| 2686 | if (!meta_data->resource.empty()) { |
| 2687 | return; |
| 2688 | } |
| 2689 | auto resource = this->apk_->LoadXml(meta_data->resource, diag); |
| 2690 | if (!resource) { |
| 2691 | return; |
| 2692 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2693 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2694 | // Look for the payment category on an <aid-group> element |
| 2695 | auto& root = resource.get()->root; |
| 2696 | if ((host_apdu_action && root->name == "host-apdu-service") || |
| 2697 | (offhost_apdu_action && root->name == "offhost-apdu-service")) { |
| 2698 | for (auto& child : root->GetChildElements()) { |
| 2699 | if (child->name == "aid-group") { |
| 2700 | auto category = FindAttribute(child, CATEGORY_ATTR); |
| 2701 | if (category && category->value == "payment") { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2702 | this->components_.discovered_components.insert("payment"); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2703 | return; |
| 2704 | } |
| 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | } |
| 2710 | }); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2711 | } |
| 2712 | }); |
| 2713 | |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2714 | // Print presence of activities, receivers, and services with no special components |
| 2715 | FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2716 | if (auto activity = ElementCast<Activity>(el)) { |
| 2717 | if (!activity->has_component_) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2718 | components_.other_activities = true; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2719 | return true; |
| 2720 | } |
| 2721 | } |
| 2722 | return false; |
| 2723 | }); |
| 2724 | |
| 2725 | FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2726 | if (auto receiver = ElementCast<Receiver>(el)) { |
| 2727 | if (!receiver->has_component) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2728 | components_.other_receivers = true; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2729 | return true; |
| 2730 | } |
| 2731 | } |
| 2732 | return false; |
| 2733 | }); |
| 2734 | |
| 2735 | FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2736 | if (auto service = ElementCast<Service>(el)) { |
| 2737 | if (!service->has_component) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2738 | components_.other_services = true; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2739 | return true; |
| 2740 | } |
| 2741 | } |
| 2742 | return false; |
| 2743 | }); |
| 2744 | |
| 2745 | // Gather the supported screens |
| 2746 | const static SupportsScreen default_screens{}; |
| 2747 | SupportsScreen* screen = ElementCast<SupportsScreen>( |
| 2748 | FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2749 | return ElementCast<SupportsScreen>(el) != nullptr; |
| 2750 | })); |
| 2751 | supports_screen_ = screen ? screen : &default_screens; |
| 2752 | |
| 2753 | // Gather the supported architectures_ of the app |
| 2754 | std::set<std::string> architectures_from_apk; |
| 2755 | auto it = apk_->GetFileCollection()->Iterator(); |
| 2756 | while (it->HasNext()) { |
| 2757 | auto file_path = it->Next()->GetSource().path; |
Iurii Makhno | 499ecd3 | 2022-08-19 16:34:26 +0000 | [diff] [blame] | 2758 | if (file_path.starts_with("lib/")) { |
| 2759 | file_path = file_path.substr(4); |
| 2760 | size_t pos = file_path.find('/'); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2761 | if (pos != std::string::npos) { |
| 2762 | file_path = file_path.substr(0, pos); |
| 2763 | } |
| 2764 | |
| 2765 | architectures_from_apk.insert(file_path); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | // Determine if the application has multiArch supports |
| 2770 | auto has_multi_arch = |
| 2771 | FindElement(root_element_.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2772 | if (auto application = ElementCast<Application>(el)) { |
| 2773 | return application->has_multi_arch; |
| 2774 | } |
| 2775 | return false; |
| 2776 | }); |
| 2777 | |
| 2778 | bool output_alt_native_code = false; |
| 2779 | // A multiArch package is one that contains 64-bit and |
| 2780 | // 32-bit versions of native code and expects 3rd-party |
| 2781 | // apps to load these native code libraries. Since most |
| 2782 | // 64-bit systems also support 32-bit apps, the apps |
| 2783 | // loading this multiArch package's code may be either |
| 2784 | if (has_multi_arch) { |
| 2785 | // If this is a multiArch package, report the 64-bit |
| 2786 | // version only. Then as a separate entry, report the |
| 2787 | // rest. |
| 2788 | // |
| 2789 | // If we report the 32-bit architecture, this APK will |
| 2790 | // be installed on a 32-bit device, causing a large waste |
| 2791 | // of bandwidth and disk space. This assumes that |
| 2792 | // the developer of the multiArch package has also |
| 2793 | // made a version that is 32-bit only. |
| 2794 | const std::string kIntel64 = "x86_64"; |
| 2795 | const std::string kArm64 = "arm64-v8a"; |
| 2796 | |
| 2797 | auto arch = architectures_from_apk.find(kIntel64); |
| 2798 | if (arch == architectures_from_apk.end()) { |
| 2799 | arch = architectures_from_apk.find(kArm64); |
| 2800 | } |
| 2801 | |
| 2802 | if (arch != architectures_from_apk.end()) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2803 | architectures_.architectures.insert(*arch); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2804 | architectures_from_apk.erase(arch); |
| 2805 | output_alt_native_code = true; |
| 2806 | } |
| 2807 | } |
| 2808 | for (auto& arch : architectures_from_apk) { |
| 2809 | if (output_alt_native_code) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2810 | architectures_.alt_architectures.insert(arch); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2811 | } else { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2812 | architectures_.architectures.insert(arch); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | return true; |
| 2816 | } |
| 2817 | |
| 2818 | bool ManifestExtractor::Dump(text::Printer* printer) { |
| 2819 | Print(root_element_.get(), printer); |
| 2820 | if (options_.only_permissions) { |
| 2821 | return true; |
| 2822 | } |
| 2823 | |
| 2824 | for (auto& implied_permission : implied_permissions_) { |
| 2825 | implied_permission->Print(printer); |
| 2826 | } |
| 2827 | for (auto& feature_group : feature_groups_) { |
| 2828 | feature_group->PrintGroup(printer); |
| 2829 | } |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2830 | components_.Print(printer); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 2831 | supports_screen_->PrintScreens(printer, target_sdk_); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2832 | |
| 2833 | // Print all the unique locales of the apk |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2834 | printer->Print("locales:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2835 | for (auto& config : locales_) { |
| 2836 | if (config.first.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2837 | printer->Print(" '--_--'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2838 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2839 | printer->Print(StringPrintf(" '%s'", config.first.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2840 | } |
| 2841 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2842 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2843 | |
| 2844 | // Print all the densities locales of the apk |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2845 | printer->Print("densities:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2846 | for (auto& config : densities_) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2847 | printer->Print(StringPrintf(" '%d'", config.first)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2848 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2849 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2850 | |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2851 | architectures_.Print(printer); |
| 2852 | return true; |
| 2853 | } |
| 2854 | |
| 2855 | bool ManifestExtractor::DumpProto(pb::Badging* out_badging) { |
| 2856 | ToProto(root_element_.get(), out_badging); |
| 2857 | for (auto& implied_permission : implied_permissions_) { |
| 2858 | implied_permission->ToProto(out_badging); |
| 2859 | } |
| 2860 | for (auto& feature_group : feature_groups_) { |
| 2861 | feature_group->GroupToProto(out_badging); |
| 2862 | } |
| 2863 | components_.ToProto(out_badging); |
| 2864 | supports_screen_->ToProtoScreens(out_badging, target_sdk_); |
| 2865 | |
| 2866 | for (auto& config : locales_) { |
Iurii Makhno | cfc559b | 2022-08-25 15:57:52 +0000 | [diff] [blame] | 2867 | if (config.first.empty()) { |
| 2868 | out_badging->add_locales("--_--"); |
| 2869 | } else { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 2870 | out_badging->add_locales(config.first); |
| 2871 | } |
| 2872 | } |
| 2873 | for (auto& config : densities_) { |
| 2874 | out_badging->add_densities(config.first); |
| 2875 | } |
| 2876 | |
| 2877 | architectures_.ToProto(out_badging); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2878 | return true; |
| 2879 | } |
| 2880 | |
| 2881 | /** |
| 2882 | * Returns the element casted to the type if the element is of that type. Otherwise, returns a null |
| 2883 | * pointer. |
| 2884 | **/ |
| 2885 | template<typename T> |
| 2886 | T* ElementCast(ManifestExtractor::Element* element) { |
| 2887 | if (element == nullptr) { |
| 2888 | return nullptr; |
| 2889 | } |
| 2890 | |
| 2891 | const std::unordered_map<std::string, bool> kTagCheck = { |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2892 | {"action", std::is_base_of<Action, T>::value}, |
| 2893 | {"activity", std::is_base_of<Activity, T>::value}, |
| 2894 | {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value}, |
| 2895 | {"application", std::is_base_of<Application, T>::value}, |
| 2896 | {"category", std::is_base_of<Category, T>::value}, |
| 2897 | {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value}, |
| 2898 | {"feature-group", std::is_base_of<FeatureGroup, T>::value}, |
| 2899 | {"input-type", std::is_base_of<InputType, T>::value}, |
| 2900 | {"intent-filter", std::is_base_of<IntentFilter, T>::value}, |
| 2901 | {"meta-data", std::is_base_of<MetaData, T>::value}, |
| 2902 | {"manifest", std::is_base_of<Manifest, T>::value}, |
| 2903 | {"original-package", std::is_base_of<OriginalPackage, T>::value}, |
| 2904 | {"overlay", std::is_base_of<Overlay, T>::value}, |
| 2905 | {"package-verifier", std::is_base_of<PackageVerifier, T>::value}, |
| 2906 | {"permission", std::is_base_of<Permission, T>::value}, |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 2907 | {"property", std::is_base_of<Property, T>::value}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2908 | {"provider", std::is_base_of<Provider, T>::value}, |
| 2909 | {"receiver", std::is_base_of<Receiver, T>::value}, |
| 2910 | {"required-feature", std::is_base_of<RequiredFeature, T>::value}, |
| 2911 | {"required-not-feature", std::is_base_of<RequiredNotFeature, T>::value}, |
| 2912 | {"screen", std::is_base_of<Screen, T>::value}, |
| 2913 | {"service", std::is_base_of<Service, T>::value}, |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 2914 | {"sdk-library", std::is_base_of<SdkLibrary, T>::value}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2915 | {"static-library", std::is_base_of<StaticLibrary, T>::value}, |
| 2916 | {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value}, |
| 2917 | {"supports-input", std::is_base_of<SupportsInput, T>::value}, |
| 2918 | {"supports-screens", std::is_base_of<SupportsScreen, T>::value}, |
| 2919 | {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value}, |
| 2920 | {"uses-feature", std::is_base_of<UsesFeature, T>::value}, |
| 2921 | {"uses-library", std::is_base_of<UsesLibrary, T>::value}, |
| 2922 | {"uses-native-library", std::is_base_of<UsesNativeLibrary, T>::value}, |
| 2923 | {"uses-package", std::is_base_of<UsesPackage, T>::value}, |
| 2924 | {"uses-permission", std::is_base_of<UsesPermission, T>::value}, |
| 2925 | {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value}, |
| 2926 | {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value}, |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 2927 | {"uses-sdk-library", std::is_base_of<UsesSdkLibrary, T>::value}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2928 | {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2929 | }; |
| 2930 | |
| 2931 | auto check = kTagCheck.find(element->tag()); |
| 2932 | if (check != kTagCheck.end() && check->second) { |
| 2933 | return static_cast<T*>(element); |
| 2934 | } |
| 2935 | return nullptr; |
| 2936 | } |
| 2937 | |
| 2938 | template<typename T> |
| 2939 | std::unique_ptr<T> CreateType() { |
| 2940 | return std::move(util::make_unique<T>()); |
| 2941 | } |
| 2942 | |
| 2943 | std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate( |
| 2944 | ManifestExtractor* extractor, xml::Element* el) { |
| 2945 | const std::unordered_map<std::string, |
| 2946 | std::function<std::unique_ptr<ManifestExtractor::Element>()>> |
| 2947 | kTagCheck = { |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2948 | {"action", &CreateType<Action>}, |
| 2949 | {"activity", &CreateType<Activity>}, |
| 2950 | {"additional-certificate", &CreateType<AdditionalCertificate>}, |
| 2951 | {"application", &CreateType<Application>}, |
| 2952 | {"category", &CreateType<Category>}, |
| 2953 | {"compatible-screens", &CreateType<CompatibleScreens>}, |
| 2954 | {"feature-group", &CreateType<FeatureGroup>}, |
| 2955 | {"input-type", &CreateType<InputType>}, |
| 2956 | {"intent-filter", &CreateType<IntentFilter>}, |
| 2957 | {"manifest", &CreateType<Manifest>}, |
| 2958 | {"meta-data", &CreateType<MetaData>}, |
| 2959 | {"original-package", &CreateType<OriginalPackage>}, |
| 2960 | {"overlay", &CreateType<Overlay>}, |
| 2961 | {"package-verifier", &CreateType<PackageVerifier>}, |
| 2962 | {"permission", &CreateType<Permission>}, |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 2963 | {"property", &CreateType<Property>}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2964 | {"provider", &CreateType<Provider>}, |
| 2965 | {"receiver", &CreateType<Receiver>}, |
| 2966 | {"required-feature", &CreateType<RequiredFeature>}, |
| 2967 | {"required-not-feature", &CreateType<RequiredNotFeature>}, |
| 2968 | {"screen", &CreateType<Screen>}, |
| 2969 | {"service", &CreateType<Service>}, |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 2970 | {"sdk-library", &CreateType<SdkLibrary>}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2971 | {"static-library", &CreateType<StaticLibrary>}, |
| 2972 | {"supports-gl-texture", &CreateType<SupportsGlTexture>}, |
| 2973 | {"supports-input", &CreateType<SupportsInput>}, |
| 2974 | {"supports-screens", &CreateType<SupportsScreen>}, |
| 2975 | {"uses-configuration", &CreateType<UsesConfiguarion>}, |
| 2976 | {"uses-feature", &CreateType<UsesFeature>}, |
| 2977 | {"uses-library", &CreateType<UsesLibrary>}, |
| 2978 | {"uses-native-library", &CreateType<UsesNativeLibrary>}, |
| 2979 | {"uses-package", &CreateType<UsesPackage>}, |
| 2980 | {"uses-permission", &CreateType<UsesPermission>}, |
| 2981 | {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>}, |
| 2982 | {"uses-sdk", &CreateType<UsesSdkBadging>}, |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 2983 | {"uses-sdk-library", &CreateType<UsesSdkLibrary>}, |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 2984 | {"uses-static-library", &CreateType<UsesStaticLibrary>}, |
| 2985 | }; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2986 | |
| 2987 | // Attempt to map the xml tag to a element inflater |
| 2988 | std::unique_ptr<ManifestExtractor::Element> element; |
| 2989 | auto check = kTagCheck.find(el->name); |
| 2990 | if (check != kTagCheck.end()) { |
| 2991 | element = check->second(); |
| 2992 | } else { |
| 2993 | element = util::make_unique<ManifestExtractor::Element>(); |
| 2994 | } |
| 2995 | |
| 2996 | element->extractor_ = extractor; |
| 2997 | element->tag_ = el->name; |
| 2998 | element->Extract(el); |
| 2999 | return element; |
| 3000 | } |
| 3001 | |
| 3002 | std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) { |
| 3003 | auto element = ManifestExtractor::Element::Inflate(this, el); |
| 3004 | parent_stack_.insert(parent_stack_.begin(), element.get()); |
| 3005 | |
| 3006 | // Process the element and recursively visit the children |
| 3007 | for (xml::Element* child : el->GetChildElements()) { |
| 3008 | auto v = Visit(child); |
| 3009 | element->AddChild(v); |
| 3010 | } |
| 3011 | |
| 3012 | parent_stack_.erase(parent_stack_.begin()); |
| 3013 | return element; |
| 3014 | } |
| 3015 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 3016 | int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 3017 | android::IDiagnostics* diag) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 3018 | ManifestExtractor extractor(apk, options); |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 3019 | if (!extractor.Extract(diag)) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 3020 | return 1; |
Iurii Makhno | a9c74c5 | 2022-04-12 15:04:12 +0000 | [diff] [blame] | 3021 | } |
| 3022 | return extractor.Dump(printer) ? 0 : 1; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 3025 | int DumpBadgingProto(LoadedApk* apk, pb::Badging* out_badging, android::IDiagnostics* diag) { |
Iurii Makhno | 85875a8 | 2022-04-26 15:30:01 +0000 | [diff] [blame] | 3026 | DumpManifestOptions options{/* include_meta_data= */ true, |
| 3027 | /* only_permissions= */ false}; |
| 3028 | ManifestExtractor extractor(apk, options); |
| 3029 | if (!extractor.Extract(diag)) { |
| 3030 | return 1; |
| 3031 | } |
| 3032 | return extractor.DumpProto(out_badging) ? 0 : 1; |
| 3033 | } |
| 3034 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 3035 | } // namespace aapt |