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> |
| 20 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 21 | #include "LoadedApk.h" |
| 22 | #include "SdkConstants.h" |
| 23 | #include "ValueVisitor.h" |
| 24 | #include "io/File.h" |
| 25 | #include "io/FileStream.h" |
| 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "xml/XmlDom.h" |
| 28 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 29 | #include "androidfw/ConfigDescription.h" |
| 30 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 31 | using ::android::base::StringPrintf; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 32 | using ::android::ConfigDescription; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 33 | |
| 34 | namespace aapt { |
| 35 | |
| 36 | /** |
| 37 | * These are attribute resource constants for the platform, as found in android.R.attr. |
| 38 | */ |
| 39 | enum { |
| 40 | LABEL_ATTR = 0x01010001, |
| 41 | ICON_ATTR = 0x01010002, |
| 42 | NAME_ATTR = 0x01010003, |
| 43 | PERMISSION_ATTR = 0x01010006, |
| 44 | EXPORTED_ATTR = 0x01010010, |
| 45 | GRANT_URI_PERMISSIONS_ATTR = 0x0101001b, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 46 | PRIORITY_ATTR = 0x0101001c, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 47 | RESOURCE_ATTR = 0x01010025, |
| 48 | DEBUGGABLE_ATTR = 0x0101000f, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 49 | TARGET_PACKAGE_ATTR = 0x01010021, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 50 | VALUE_ATTR = 0x01010024, |
| 51 | VERSION_CODE_ATTR = 0x0101021b, |
| 52 | VERSION_NAME_ATTR = 0x0101021c, |
| 53 | SCREEN_ORIENTATION_ATTR = 0x0101001e, |
| 54 | MIN_SDK_VERSION_ATTR = 0x0101020c, |
| 55 | MAX_SDK_VERSION_ATTR = 0x01010271, |
| 56 | REQ_TOUCH_SCREEN_ATTR = 0x01010227, |
| 57 | REQ_KEYBOARD_TYPE_ATTR = 0x01010228, |
| 58 | REQ_HARD_KEYBOARD_ATTR = 0x01010229, |
| 59 | REQ_NAVIGATION_ATTR = 0x0101022a, |
| 60 | REQ_FIVE_WAY_NAV_ATTR = 0x01010232, |
| 61 | TARGET_SDK_VERSION_ATTR = 0x01010270, |
| 62 | TEST_ONLY_ATTR = 0x01010272, |
| 63 | ANY_DENSITY_ATTR = 0x0101026c, |
| 64 | GL_ES_VERSION_ATTR = 0x01010281, |
| 65 | SMALL_SCREEN_ATTR = 0x01010284, |
| 66 | NORMAL_SCREEN_ATTR = 0x01010285, |
| 67 | LARGE_SCREEN_ATTR = 0x01010286, |
| 68 | XLARGE_SCREEN_ATTR = 0x010102bf, |
| 69 | REQUIRED_ATTR = 0x0101028e, |
| 70 | INSTALL_LOCATION_ATTR = 0x010102b7, |
| 71 | SCREEN_SIZE_ATTR = 0x010102ca, |
| 72 | SCREEN_DENSITY_ATTR = 0x010102cb, |
| 73 | REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364, |
| 74 | COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365, |
| 75 | LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366, |
| 76 | PUBLIC_KEY_ATTR = 0x010103a6, |
| 77 | CATEGORY_ATTR = 0x010103e8, |
| 78 | BANNER_ATTR = 0x10103f2, |
| 79 | ISGAME_ATTR = 0x10103f4, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 80 | VERSION_ATTR = 0x01010519, |
| 81 | CERT_DIGEST_ATTR = 0x01010548, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 82 | REQUIRED_FEATURE_ATTR = 0x01010557, |
| 83 | REQUIRED_NOT_FEATURE_ATTR = 0x01010558, |
| 84 | IS_STATIC_ATTR = 0x0101055a, |
| 85 | REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565, |
| 86 | REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 87 | COMPILE_SDK_VERSION_ATTR = 0x01010572, |
| 88 | COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 89 | VERSION_MAJOR_ATTR = 0x01010577, |
| 90 | PACKAGE_TYPE_ATTR = 0x01010587, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android"; |
| 94 | |
| 95 | /** Retrieves the attribute of the element with the specified attribute resource id. */ |
| 96 | static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) { |
| 97 | for (auto& a : el->attributes) { |
| 98 | if (a.compiled_attribute && a.compiled_attribute.value().id) { |
| 99 | if (a.compiled_attribute.value().id.value() == resd_id) { |
| 100 | return std::move(&a); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | /** Retrieves the attribute of the element that has the specified namespace and attribute name. */ |
| 108 | static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package, |
| 109 | const std::string &name) { |
| 110 | return el->FindAttribute(package, name); |
| 111 | } |
| 112 | |
| 113 | class CommonFeatureGroup; |
| 114 | |
| 115 | class ManifestExtractor { |
| 116 | public: |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 117 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 118 | explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options) |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 119 | : apk_(apk), options_(options) { } |
| 120 | |
| 121 | class Element { |
| 122 | public: |
| 123 | Element() = default; |
| 124 | virtual ~Element() = default; |
| 125 | |
| 126 | static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el); |
| 127 | |
| 128 | /** Writes out the extracted contents of the element. */ |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 129 | virtual void Print(text::Printer* printer) { } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 130 | |
| 131 | /** Adds an element to the list of children of the element. */ |
| 132 | void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); } |
| 133 | |
| 134 | /** Retrieves the list of children of the element. */ |
| 135 | const std::vector<std::unique_ptr<Element>>& children() const { |
| 136 | return children_; |
| 137 | } |
| 138 | |
| 139 | /** Retrieves the extracted xml element tag. */ |
| 140 | const std::string tag() const { |
| 141 | return tag_; |
| 142 | } |
| 143 | |
| 144 | protected: |
| 145 | ManifestExtractor* extractor() const { |
| 146 | return extractor_; |
| 147 | } |
| 148 | |
| 149 | /** Retrieves and stores the information extracted from the xml element. */ |
| 150 | virtual void Extract(xml::Element* el) { } |
| 151 | |
| 152 | /* |
| 153 | * Retrieves a configuration value of the resource entry that best matches the specified |
| 154 | * configuration. |
| 155 | */ |
| 156 | static Value* BestConfigValue(ResourceEntry* entry, |
| 157 | const ConfigDescription& match) { |
| 158 | if (!entry) { |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | // Determine the config that best matches the desired config |
| 163 | ResourceConfigValue* best_value = nullptr; |
| 164 | for (auto& value : entry->values) { |
| 165 | if (!value->config.match(match)) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if (best_value != nullptr) { |
| 170 | if (!value->config.isBetterThan(best_value->config, &match)) { |
| 171 | if (value->config.compare(best_value->config) != 0) { |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | best_value = value.get(); |
| 178 | } |
| 179 | |
| 180 | // The entry has no values |
| 181 | if (!best_value) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
| 185 | return best_value->value.get(); |
| 186 | } |
| 187 | |
| 188 | /** Retrieves the resource assigned to the specified resource id if one exists. */ |
| 189 | Value* FindValueById(const ResourceTable* table, const ResourceId& res_id, |
| 190 | const ConfigDescription& config = DummyConfig()) { |
| 191 | if (table) { |
| 192 | for (auto& package : table->packages) { |
| 193 | if (package->id && package->id.value() == res_id.package_id()) { |
| 194 | for (auto& type : package->types) { |
| 195 | if (type->id && type->id.value() == res_id.type_id()) { |
| 196 | for (auto& entry : type->entries) { |
| 197 | if (entry->id && entry->id.value() == res_id.entry_id()) { |
| 198 | if (auto value = BestConfigValue(entry.get(), config)) { |
| 199 | return value; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | /** Attempts to resolve the reference to a non-reference value. */ |
| 212 | Value* ResolveReference(Reference* ref, const ConfigDescription& config = DummyConfig()) { |
| 213 | const int kMaxIterations = 40; |
| 214 | int i = 0; |
| 215 | while (ref && ref->id && i++ < kMaxIterations) { |
| 216 | auto table = extractor_->apk_->GetResourceTable(); |
| 217 | if (auto value = FindValueById(table, ref->id.value(), config)) { |
| 218 | if (ValueCast<Reference>(value)) { |
| 219 | ref = ValueCast<Reference>(value); |
| 220 | } else { |
| 221 | return value; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Retrieves the integer value of the attribute . If the value of the attribute is a reference, |
| 230 | * this will attempt to resolve the reference to an integer value. |
| 231 | **/ |
| 232 | int32_t* GetAttributeInteger(xml::Attribute* attr, |
| 233 | const ConfigDescription& config = DummyConfig()) { |
| 234 | if (attr != nullptr) { |
| 235 | if (attr->compiled_value) { |
| 236 | // Resolve references using the dummy configuration |
| 237 | Value* value = attr->compiled_value.get(); |
| 238 | if (ValueCast<Reference>(value)) { |
| 239 | value = ResolveReference(ValueCast<Reference>(value), config); |
| 240 | } else { |
| 241 | value = attr->compiled_value.get(); |
| 242 | } |
| 243 | // Retrieve the integer data if possible |
| 244 | if (value != nullptr) { |
| 245 | if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) { |
| 246 | return (int32_t*) &intValue->value.data; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * A version of GetAttributeInteger that returns a default integer if the attribute does not |
| 256 | * exist or cannot be resolved to an integer value. |
| 257 | **/ |
| 258 | int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def, |
| 259 | const ConfigDescription& config = DummyConfig()) { |
| 260 | auto value = GetAttributeInteger(attr, config); |
| 261 | if (value) { |
| 262 | return *value; |
| 263 | } |
| 264 | return def; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Retrieves the string value of the attribute. If the value of the attribute is a reference, |
| 269 | * this will attempt to resolve the reference to a string value. |
| 270 | **/ |
| 271 | const std::string* GetAttributeString(xml::Attribute* attr, |
| 272 | const ConfigDescription& config = DummyConfig()) { |
| 273 | if (attr != nullptr) { |
| 274 | if (attr->compiled_value) { |
| 275 | // Resolve references using the dummy configuration |
| 276 | Value* value = attr->compiled_value.get(); |
| 277 | if (ValueCast<Reference>(value)) { |
| 278 | value = ResolveReference(ValueCast<Reference>(value), config); |
| 279 | } else { |
| 280 | value = attr->compiled_value.get(); |
| 281 | } |
| 282 | |
| 283 | // Retrieve the string data of the value if possible |
| 284 | if (value != nullptr) { |
| 285 | if (String* intValue = ValueCast<String>(value)) { |
| 286 | return &(*intValue->value); |
| 287 | } else if (RawString* rawValue = ValueCast<RawString>(value)) { |
| 288 | return &(*rawValue->value); |
| 289 | } else if (FileReference* strValue = ValueCast<FileReference>(value)) { |
| 290 | return &(*strValue->path); |
| 291 | } |
| 292 | } |
| 293 | } |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 294 | |
| 295 | if (!attr->value.empty()) { |
| 296 | return &attr->value; |
| 297 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 298 | } |
| 299 | return nullptr; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * A version of GetAttributeString that returns a default string if the attribute does not |
| 304 | * exist or cannot be resolved to an string value. |
| 305 | **/ |
| 306 | std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def, |
| 307 | const ConfigDescription& config = DummyConfig()) { |
| 308 | auto value = GetAttributeString(attr, config); |
| 309 | if (value) { |
| 310 | return *value; |
| 311 | } |
| 312 | return def; |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | ManifestExtractor* extractor_; |
| 317 | std::vector<std::unique_ptr<Element>> children_; |
| 318 | std::string tag_; |
| 319 | }; |
| 320 | |
| 321 | friend Element; |
| 322 | |
| 323 | /** Creates a default configuration used to retrieve resources. */ |
| 324 | static ConfigDescription DummyConfig() { |
| 325 | ConfigDescription config; |
| 326 | config.orientation = android::ResTable_config::ORIENTATION_PORT; |
| 327 | config.density = android::ResTable_config::DENSITY_MEDIUM; |
| 328 | config.sdkVersion = 10000; // Very high. |
| 329 | config.screenWidthDp = 320; |
| 330 | config.screenHeightDp = 480; |
| 331 | config.smallestScreenWidthDp = 320; |
| 332 | config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL; |
| 333 | return config; |
| 334 | } |
| 335 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 336 | bool Dump(text::Printer* printer, IDiagnostics* diag); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 337 | |
| 338 | /** Recursively visit the xml element tree and return a processed badging element tree. */ |
| 339 | std::unique_ptr<Element> Visit(xml::Element* element); |
| 340 | |
| 341 | /** Raises the target sdk value if the min target is greater than the current target. */ |
| 342 | void RaiseTargetSdk(int32_t min_target) { |
| 343 | if (min_target > target_sdk_) { |
| 344 | target_sdk_ = min_target; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Retrieves the default feature group that features are added into when <uses-feature> |
| 350 | * are not in a <feature-group> element. |
| 351 | **/ |
| 352 | CommonFeatureGroup* GetCommonFeatureGroup() { |
| 353 | return commonFeatureGroup_.get(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Retrieves a mapping of density values to Configurations for retrieving resources that would be |
| 358 | * used for that density setting. |
| 359 | **/ |
| 360 | const std::map<uint16_t, ConfigDescription> densities() const { |
| 361 | return densities_; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that |
| 366 | * would be used for that locale setting. |
| 367 | **/ |
| 368 | const std::map<std::string, ConfigDescription> locales() const { |
| 369 | return locales_; |
| 370 | } |
| 371 | |
| 372 | /** Retrieves the current stack of parent during data extraction. */ |
| 373 | const std::vector<Element*> parent_stack() const { |
| 374 | return parent_stack_; |
| 375 | } |
| 376 | |
| 377 | int32_t target_sdk() const { |
| 378 | return target_sdk_; |
| 379 | } |
| 380 | |
| 381 | LoadedApk* const apk_; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 382 | DumpManifestOptions& options_; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 383 | |
| 384 | private: |
| 385 | std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>(); |
| 386 | std::map<std::string, ConfigDescription> locales_; |
| 387 | std::map<uint16_t, ConfigDescription> densities_; |
| 388 | std::vector<Element*> parent_stack_; |
| 389 | int32_t target_sdk_ = 0; |
| 390 | }; |
| 391 | |
| 392 | template<typename T> T* ElementCast(ManifestExtractor::Element* element); |
| 393 | |
| 394 | /** Recurs through the children of the specified root in depth-first order. */ |
| 395 | static void ForEachChild(ManifestExtractor::Element* root, |
| 396 | std::function<void(ManifestExtractor::Element*)> f) { |
| 397 | for (auto& child : root->children()) { |
| 398 | f(child.get()); |
| 399 | ForEachChild(child.get(), f); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Checks the element and its recursive children for an element that makes the specified |
| 405 | * conditional function return true. Returns the first element that makes the conditional function |
| 406 | * return true. |
| 407 | **/ |
| 408 | static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root, |
| 409 | std::function<bool(ManifestExtractor::Element*)> f) { |
| 410 | if (f(root)) { |
| 411 | return root; |
| 412 | } |
| 413 | for (auto& child : root->children()) { |
| 414 | if (auto b2 = FindElement(child.get(), f)) { |
| 415 | return b2; |
| 416 | } |
| 417 | } |
| 418 | return nullptr; |
| 419 | } |
| 420 | |
| 421 | /** Represents the <manifest> elements **/ |
| 422 | class Manifest : public ManifestExtractor::Element { |
| 423 | public: |
| 424 | Manifest() = default; |
| 425 | std::string package; |
| 426 | int32_t versionCode; |
| 427 | std::string versionName; |
| 428 | const std::string* split = nullptr; |
| 429 | const std::string* platformVersionName = nullptr; |
| 430 | const std::string* platformVersionCode = nullptr; |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 431 | const int32_t* platformVersionNameInt = nullptr; |
| 432 | const int32_t* platformVersionCodeInt = nullptr; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 433 | const int32_t* compilesdkVersion = nullptr; |
| 434 | const std::string* compilesdkVersionCodename = nullptr; |
| 435 | const int32_t* installLocation = nullptr; |
| 436 | |
| 437 | void Extract(xml::Element* manifest) override { |
| 438 | package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), ""); |
| 439 | versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0); |
| 440 | versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), ""); |
| 441 | split = GetAttributeString(FindAttribute(manifest, {}, "split")); |
| 442 | |
| 443 | // Extract the platform build info |
| 444 | platformVersionName = GetAttributeString(FindAttribute(manifest, {}, |
| 445 | "platformBuildVersionName")); |
| 446 | platformVersionCode = GetAttributeString(FindAttribute(manifest, {}, |
| 447 | "platformBuildVersionCode")); |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 448 | platformVersionNameInt = GetAttributeInteger(FindAttribute(manifest, {}, |
| 449 | "platformBuildVersionName")); |
| 450 | platformVersionCodeInt = GetAttributeInteger(FindAttribute(manifest, {}, |
| 451 | "platformBuildVersionCode")); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 452 | |
| 453 | // Extract the compile sdk info |
| 454 | compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR)); |
| 455 | compilesdkVersionCodename = GetAttributeString( |
| 456 | FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR)); |
| 457 | installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR)); |
| 458 | } |
| 459 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 460 | void Print(text::Printer* printer) override { |
| 461 | printer->Print(StringPrintf("package: name='%s' ", package.data())); |
| 462 | printer->Print(StringPrintf("versionCode='%s' ", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 463 | (versionCode > 0) ? std::to_string(versionCode).data() : "")); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 464 | printer->Print(StringPrintf("versionName='%s'", versionName.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 465 | |
| 466 | if (split) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 467 | printer->Print(StringPrintf(" split='%s'", split->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 468 | } |
| 469 | if (platformVersionName) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 470 | printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 471 | } |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 472 | if (platformVersionNameInt) { |
| 473 | printer->Print(StringPrintf(" platformBuildVersionName='%d'", *platformVersionNameInt)); |
| 474 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 475 | if (platformVersionCode) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 476 | printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 477 | } |
Ryan Mitchell | a36cc98 | 2019-06-05 10:13:41 -0700 | [diff] [blame] | 478 | if (platformVersionCodeInt) { |
| 479 | printer->Print(StringPrintf(" platformBuildVersionCode='%d'", *platformVersionCodeInt)); |
| 480 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 481 | if (compilesdkVersion) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 482 | printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 483 | } |
| 484 | if (compilesdkVersionCodename) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 485 | printer->Print(StringPrintf(" compileSdkVersionCodename='%s'", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 486 | compilesdkVersionCodename->data())); |
| 487 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 488 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 489 | |
| 490 | if (installLocation) { |
| 491 | switch (*installLocation) { |
| 492 | case 0: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 493 | printer->Print("install-location:'auto'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 494 | break; |
| 495 | case 1: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 496 | printer->Print("install-location:'internalOnly'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 497 | break; |
| 498 | case 2: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 499 | printer->Print("install-location:'preferExternal'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 500 | break; |
| 501 | default: |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | /** Represents <application> elements. **/ |
| 509 | class Application : public ManifestExtractor::Element { |
| 510 | public: |
| 511 | Application() = default; |
| 512 | std::string label; |
| 513 | std::string icon; |
| 514 | std::string banner; |
| 515 | int32_t is_game; |
| 516 | int32_t debuggable; |
| 517 | int32_t test_only; |
| 518 | bool has_multi_arch; |
| 519 | |
| 520 | /** Mapping from locales to app names. */ |
| 521 | std::map<std::string, std::string> locale_labels; |
| 522 | |
| 523 | /** Mapping from densities to app icons. */ |
| 524 | std::map<uint16_t, std::string> density_icons; |
| 525 | |
| 526 | void Extract(xml::Element* element) override { |
| 527 | label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); |
| 528 | icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), ""); |
| 529 | test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0); |
| 530 | banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), ""); |
| 531 | is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0); |
| 532 | debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0); |
| 533 | |
| 534 | // We must search by name because the multiArch flag hasn't been API |
| 535 | // frozen yet. |
| 536 | has_multi_arch = (GetAttributeIntegerDefault( |
| 537 | FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0); |
| 538 | |
| 539 | // Retrieve the app names for every locale the app supports |
| 540 | auto attr = FindAttribute(element, LABEL_ATTR); |
| 541 | for (auto& config : extractor()->locales()) { |
| 542 | if (auto label = GetAttributeString(attr, config.second)) { |
| 543 | if (label) { |
| 544 | locale_labels.insert(std::make_pair(config.first, *label)); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // Retrieve the icons for the densities the app supports |
| 550 | attr = FindAttribute(element, ICON_ATTR); |
| 551 | for (auto& config : extractor()->densities()) { |
| 552 | if (auto resource = GetAttributeString(attr, config.second)) { |
| 553 | if (resource) { |
| 554 | density_icons.insert(std::make_pair(config.first, *resource)); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 560 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 561 | // Print the labels for every locale |
| 562 | for (auto p : locale_labels) { |
| 563 | if (p.first.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 564 | printer->Print(StringPrintf("application-label:'%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 565 | android::ResTable::normalizeForOutput(p.second.data()) |
| 566 | .c_str())); |
| 567 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 568 | printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(), |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 569 | android::ResTable::normalizeForOutput(p.second.data()) |
| 570 | .c_str())); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // Print the icon paths for every density |
| 575 | for (auto p : density_icons) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 576 | 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] | 577 | } |
| 578 | |
| 579 | // Print the application info |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 580 | printer->Print(StringPrintf("application: label='%s' ", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 581 | android::ResTable::normalizeForOutput(label.data()).c_str())); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 582 | printer->Print(StringPrintf("icon='%s'", icon.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 583 | if (!banner.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 584 | printer->Print(StringPrintf(" banner='%s'", banner.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 585 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 586 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 587 | |
| 588 | if (test_only != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 589 | printer->Print(StringPrintf("testOnly='%d'\n", test_only)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 590 | } |
| 591 | if (is_game != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 592 | printer->Print("application-isGame\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 593 | } |
| 594 | if (debuggable != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 595 | printer->Print("application-debuggable\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | }; |
| 599 | |
| 600 | /** Represents <uses-sdk> elements. **/ |
| 601 | class UsesSdkBadging : public ManifestExtractor::Element { |
| 602 | public: |
| 603 | UsesSdkBadging() = default; |
| 604 | const int32_t* min_sdk = nullptr; |
| 605 | const std::string* min_sdk_name = nullptr; |
| 606 | const int32_t* max_sdk = nullptr; |
| 607 | const int32_t* target_sdk = nullptr; |
| 608 | const std::string* target_sdk_name = nullptr; |
| 609 | |
| 610 | void Extract(xml::Element* element) override { |
| 611 | min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR)); |
| 612 | min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR)); |
| 613 | max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR)); |
| 614 | target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR)); |
| 615 | target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR)); |
| 616 | |
| 617 | // Detect the target sdk of the element |
| 618 | if ((min_sdk_name && *min_sdk_name == "Donut") |
| 619 | || (target_sdk_name && *target_sdk_name == "Donut")) { |
| 620 | extractor()->RaiseTargetSdk(4); |
| 621 | } |
| 622 | if (min_sdk) { |
| 623 | extractor()->RaiseTargetSdk(*min_sdk); |
| 624 | } |
| 625 | if (target_sdk) { |
| 626 | extractor()->RaiseTargetSdk(*target_sdk); |
| 627 | } |
| 628 | } |
| 629 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 630 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 631 | if (min_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 632 | printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 633 | } else if (min_sdk_name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 634 | printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 635 | } |
| 636 | if (max_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 637 | printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 638 | } |
| 639 | if (target_sdk) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 640 | printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 641 | } else if (target_sdk_name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 642 | printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | }; |
| 646 | |
| 647 | /** Represents <uses-configuration> elements. **/ |
| 648 | class UsesConfiguarion : public ManifestExtractor::Element { |
| 649 | public: |
| 650 | UsesConfiguarion() = default; |
| 651 | int32_t req_touch_screen = 0; |
| 652 | int32_t req_keyboard_type = 0; |
| 653 | int32_t req_hard_keyboard = 0; |
| 654 | int32_t req_navigation = 0; |
| 655 | int32_t req_five_way_nav = 0; |
| 656 | |
| 657 | void Extract(xml::Element* element) override { |
| 658 | req_touch_screen = GetAttributeIntegerDefault( |
| 659 | FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0); |
| 660 | req_keyboard_type = GetAttributeIntegerDefault( |
| 661 | FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0); |
| 662 | req_hard_keyboard = GetAttributeIntegerDefault( |
| 663 | FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0); |
| 664 | req_navigation = GetAttributeIntegerDefault( |
| 665 | FindAttribute(element, REQ_NAVIGATION_ATTR), 0); |
| 666 | req_five_way_nav = GetAttributeIntegerDefault( |
| 667 | FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0); |
| 668 | } |
| 669 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 670 | void Print(text::Printer* printer) override { |
| 671 | printer->Print("uses-configuration:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 672 | if (req_touch_screen != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 673 | printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 674 | } |
| 675 | if (req_keyboard_type != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 676 | printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 677 | } |
| 678 | if (req_hard_keyboard != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 679 | printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 680 | } |
| 681 | if (req_navigation != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 682 | printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 683 | } |
| 684 | if (req_five_way_nav != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 685 | printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 686 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 687 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 688 | } |
| 689 | }; |
| 690 | |
| 691 | /** Represents <supports-screen> elements. **/ |
| 692 | class SupportsScreen : public ManifestExtractor::Element { |
| 693 | public: |
| 694 | SupportsScreen() = default; |
| 695 | int32_t small_screen = 1; |
| 696 | int32_t normal_screen = 1; |
| 697 | int32_t large_screen = 1; |
| 698 | int32_t xlarge_screen = 1; |
| 699 | int32_t any_density = 1; |
| 700 | int32_t requires_smallest_width_dp = 0; |
| 701 | int32_t compatible_width_limit_dp = 0; |
| 702 | int32_t largest_width_limit_dp = 0; |
| 703 | |
| 704 | void Extract(xml::Element* element) override { |
| 705 | small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1); |
| 706 | normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1); |
| 707 | large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1); |
| 708 | xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1); |
| 709 | any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1); |
| 710 | |
| 711 | requires_smallest_width_dp = GetAttributeIntegerDefault( |
| 712 | FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0); |
| 713 | compatible_width_limit_dp = GetAttributeIntegerDefault( |
| 714 | FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0); |
| 715 | largest_width_limit_dp = GetAttributeIntegerDefault( |
| 716 | FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0); |
| 717 | |
| 718 | // For modern apps, if screen size buckets haven't been specified |
| 719 | // but the new width ranges have, then infer the buckets from them. |
| 720 | if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0 |
| 721 | && requires_smallest_width_dp > 0) { |
| 722 | int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp |
| 723 | : requires_smallest_width_dp; |
| 724 | small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0; |
| 725 | normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0; |
| 726 | large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0; |
| 727 | xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0; |
| 728 | } |
| 729 | } |
| 730 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 731 | void PrintScreens(text::Printer* printer, int32_t target_sdk) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 732 | int32_t small_screen_temp = small_screen; |
| 733 | int32_t normal_screen_temp = normal_screen; |
| 734 | int32_t large_screen_temp = large_screen; |
| 735 | int32_t xlarge_screen_temp = xlarge_screen; |
| 736 | int32_t any_density_temp = any_density; |
| 737 | |
| 738 | // Determine default values for any unspecified screen sizes, |
| 739 | // based on the target SDK of the package. As of 4 (donut) |
| 740 | // the screen size support was introduced, so all default to |
| 741 | // enabled. |
| 742 | if (small_screen_temp > 0) { |
| 743 | small_screen_temp = target_sdk >= 4 ? -1 : 0; |
| 744 | } |
| 745 | if (normal_screen_temp > 0) { |
| 746 | normal_screen_temp = -1; |
| 747 | } |
| 748 | if (large_screen_temp > 0) { |
| 749 | large_screen_temp = target_sdk >= 4 ? -1 : 0; |
| 750 | } |
| 751 | if (xlarge_screen_temp > 0) { |
| 752 | // Introduced in Gingerbread. |
| 753 | xlarge_screen_temp = target_sdk >= 9 ? -1 : 0; |
| 754 | } |
| 755 | if (any_density_temp > 0) { |
| 756 | any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0 |
| 757 | || compatible_width_limit_dp > 0) ? -1 : 0; |
| 758 | } |
| 759 | |
| 760 | // Print the formatted screen info |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 761 | printer->Print("supports-screens:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 762 | if (small_screen_temp != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 763 | printer->Print(" 'small'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 764 | } |
| 765 | if (normal_screen_temp != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 766 | printer->Print(" 'normal'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 767 | } |
| 768 | if (large_screen_temp != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 769 | printer->Print(" 'large'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 770 | } |
| 771 | if (xlarge_screen_temp != 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 772 | printer->Print(" 'xlarge'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 773 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 774 | printer->Print("\n"); |
| 775 | printer->Print(StringPrintf("supports-any-density: '%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 776 | (any_density_temp ) ? "true" : "false")); |
| 777 | if (requires_smallest_width_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 778 | printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 779 | } |
| 780 | if (compatible_width_limit_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 781 | printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 782 | } |
| 783 | if (largest_width_limit_dp > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 784 | printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | }; |
| 788 | |
| 789 | /** Represents <feature-group> elements. **/ |
| 790 | class FeatureGroup : public ManifestExtractor::Element { |
| 791 | public: |
| 792 | FeatureGroup() = default; |
| 793 | std::string label; |
| 794 | int32_t open_gles_version = 0; |
| 795 | |
| 796 | void Extract(xml::Element* element) override { |
| 797 | label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); |
| 798 | } |
| 799 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 800 | virtual void PrintGroup(text::Printer* printer) { |
| 801 | printer->Print(StringPrintf("feature-group: label='%s'\n", label.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 802 | if (open_gles_version > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 803 | printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | for (auto feature : features_) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 807 | printer->Print(StringPrintf(" uses-feature%s: name='%s'", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 808 | (feature.second.required ? "" : "-not-required"), |
| 809 | feature.first.data())); |
| 810 | if (feature.second.version > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 811 | printer->Print(StringPrintf(" version='%d'", feature.second.version)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 812 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 813 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
| 817 | /** Adds a feature to the feature group. */ |
| 818 | void AddFeature(const std::string& name, bool required = true, int32_t version = -1) { |
| 819 | features_.insert(std::make_pair(name, Feature{ required, version })); |
| 820 | if (required) { |
| 821 | if (name == "android.hardware.camera.autofocus" || |
| 822 | name == "android.hardware.camera.flash") { |
| 823 | AddFeature("android.hardware.camera", true); |
| 824 | } else if (name == "android.hardware.location.gps" || |
| 825 | name == "android.hardware.location.network") { |
| 826 | AddFeature("android.hardware.location", true); |
| 827 | } else if (name == "android.hardware.faketouch.multitouch") { |
| 828 | AddFeature("android.hardware.faketouch", true); |
| 829 | } else if (name == "android.hardware.faketouch.multitouch.distinct" || |
| 830 | name == "android.hardware.faketouch.multitouch.jazzhands") { |
| 831 | AddFeature("android.hardware.faketouch.multitouch", true); |
| 832 | AddFeature("android.hardware.faketouch", true); |
| 833 | } else if (name == "android.hardware.touchscreen.multitouch") { |
| 834 | AddFeature("android.hardware.touchscreen", true); |
| 835 | } else if (name == "android.hardware.touchscreen.multitouch.distinct" || |
| 836 | name == "android.hardware.touchscreen.multitouch.jazzhands") { |
| 837 | AddFeature("android.hardware.touchscreen.multitouch", true); |
| 838 | AddFeature("android.hardware.touchscreen", true); |
| 839 | } else if (name == "android.hardware.opengles.aep") { |
| 840 | const int kOpenGLESVersion31 = 0x00030001; |
| 841 | if (kOpenGLESVersion31 > open_gles_version) { |
| 842 | open_gles_version = kOpenGLESVersion31; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** Returns true if the feature group has the given feature. */ |
| 849 | virtual bool HasFeature(const std::string& name) { |
| 850 | return features_.find(name) != features_.end(); |
| 851 | } |
| 852 | |
| 853 | /** Merges the features of another feature group into this group. */ |
| 854 | void Merge(FeatureGroup* group) { |
| 855 | open_gles_version = std::max(open_gles_version, group->open_gles_version); |
| 856 | for (auto& feature : group->features_) { |
| 857 | features_.insert(feature); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | protected: |
| 862 | struct Feature { |
| 863 | public: |
| 864 | bool required = false; |
| 865 | int32_t version = -1; |
| 866 | }; |
| 867 | |
| 868 | /* Mapping of feature names to their properties. */ |
| 869 | std::map<std::string, Feature> features_; |
| 870 | }; |
| 871 | |
| 872 | /** |
| 873 | * Represents the default feature group for the application if no <feature-group> elements are |
| 874 | * present in the manifest. |
| 875 | **/ |
| 876 | class CommonFeatureGroup : public FeatureGroup { |
| 877 | public: |
| 878 | CommonFeatureGroup() = default; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 879 | void PrintGroup(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 880 | FeatureGroup::PrintGroup(printer); |
| 881 | |
| 882 | // Also print the implied features |
| 883 | for (auto feature : implied_features_) { |
| 884 | if (features_.find(feature.first) == features_.end()) { |
| 885 | const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : ""; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 886 | printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data())); |
| 887 | printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 888 | feature.first.data())); |
| 889 | |
| 890 | // Print the reasons as a sentence |
| 891 | size_t count = 0; |
| 892 | for (auto reason : feature.second.reasons) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 893 | printer->Print(reason); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 894 | if (count + 2 < feature.second.reasons.size()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 895 | printer->Print(", "); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 896 | } else if (count + 1 < feature.second.reasons.size()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 897 | printer->Print(", and "); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 898 | } |
| 899 | count++; |
| 900 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 901 | printer->Print("'\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /** Returns true if the feature group has the given feature. */ |
| 907 | bool HasFeature(const std::string& name) override { |
| 908 | return FeatureGroup::HasFeature(name) |
| 909 | || implied_features_.find(name) != implied_features_.end(); |
| 910 | } |
| 911 | |
| 912 | /** Adds a feature to a set of implied features not explicitly requested in the manifest. */ |
| 913 | void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) { |
| 914 | auto entry = implied_features_.find(name); |
| 915 | if (entry == implied_features_.end()) { |
| 916 | implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23))); |
| 917 | entry = implied_features_.find(name); |
| 918 | } |
| 919 | |
| 920 | // A non-sdk 23 implied feature takes precedence. |
| 921 | if (entry->second.implied_from_sdk_k23 && !sdk23) { |
| 922 | entry->second.implied_from_sdk_k23 = false; |
| 923 | } |
| 924 | |
| 925 | entry->second.reasons.insert(reason); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Adds a feature to a set of implied features for all features that are implied by the presence |
| 930 | * of the permission. |
| 931 | **/ |
| 932 | void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) { |
| 933 | if (name == "android.permission.CAMERA") { |
| 934 | addImpliedFeature("android.hardware.camera", |
| 935 | StringPrintf("requested %s permission", name.data()), |
| 936 | sdk23); |
| 937 | |
| 938 | } else if (name == "android.permission.ACCESS_FINE_LOCATION") { |
| 939 | if (targetSdk < SDK_LOLLIPOP) { |
| 940 | addImpliedFeature("android.hardware.location.gps", |
| 941 | StringPrintf("requested %s permission", name.data()), |
| 942 | sdk23); |
| 943 | addImpliedFeature("android.hardware.location.gps", |
| 944 | StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP), |
| 945 | sdk23); |
| 946 | } |
| 947 | addImpliedFeature("android.hardware.location", |
| 948 | StringPrintf("requested %s permission", name.data()), |
| 949 | sdk23); |
| 950 | |
| 951 | } else if (name == "android.permission.ACCESS_COARSE_LOCATION") { |
| 952 | if (targetSdk < SDK_LOLLIPOP) { |
| 953 | addImpliedFeature("android.hardware.location.network", |
| 954 | StringPrintf("requested %s permission", name.data()), |
| 955 | sdk23); |
| 956 | addImpliedFeature("android.hardware.location.network", |
| 957 | StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP), |
| 958 | sdk23); |
| 959 | } |
| 960 | addImpliedFeature("android.hardware.location", |
| 961 | StringPrintf("requested %s permission", name.data()), |
| 962 | sdk23); |
| 963 | |
| 964 | } else if (name == "android.permission.ACCESS_MOCK_LOCATION" || |
| 965 | name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" || |
| 966 | name == "android.permission.INSTALL_LOCATION_PROVIDER") { |
| 967 | addImpliedFeature("android.hardware.location", |
| 968 | StringPrintf("requested %s permission", name.data()), |
| 969 | sdk23); |
| 970 | |
| 971 | } else if (name == "android.permission.BLUETOOTH" || |
| 972 | name == "android.permission.BLUETOOTH_ADMIN") { |
| 973 | if (targetSdk > SDK_DONUT) { |
| 974 | addImpliedFeature("android.hardware.bluetooth", |
| 975 | StringPrintf("requested %s permission", name.data()), |
| 976 | sdk23); |
| 977 | addImpliedFeature("android.hardware.bluetooth", |
| 978 | StringPrintf("targetSdkVersion > %d", SDK_DONUT), |
| 979 | sdk23); |
| 980 | } |
| 981 | |
| 982 | } else if (name == "android.permission.RECORD_AUDIO") { |
| 983 | addImpliedFeature("android.hardware.microphone", |
| 984 | StringPrintf("requested %s permission", name.data()), |
| 985 | sdk23); |
| 986 | |
| 987 | } else if (name == "android.permission.ACCESS_WIFI_STATE" || |
| 988 | name == "android.permission.CHANGE_WIFI_STATE" || |
| 989 | name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") { |
| 990 | addImpliedFeature("android.hardware.wifi", |
| 991 | StringPrintf("requested %s permission", name.data()), |
| 992 | sdk23); |
| 993 | |
| 994 | } else if (name == "android.permission.CALL_PHONE" || |
| 995 | name == "android.permission.CALL_PRIVILEGED" || |
| 996 | name == "android.permission.MODIFY_PHONE_STATE" || |
| 997 | name == "android.permission.PROCESS_OUTGOING_CALLS" || |
| 998 | name == "android.permission.READ_SMS" || |
| 999 | name == "android.permission.RECEIVE_SMS" || |
| 1000 | name == "android.permission.RECEIVE_MMS" || |
| 1001 | name == "android.permission.RECEIVE_WAP_PUSH" || |
| 1002 | name == "android.permission.SEND_SMS" || |
| 1003 | name == "android.permission.WRITE_APN_SETTINGS" || |
| 1004 | name == "android.permission.WRITE_SMS") { |
| 1005 | addImpliedFeature("android.hardware.telephony", |
| 1006 | "requested a telephony permission", |
| 1007 | sdk23); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | private: |
| 1012 | /** |
| 1013 | * Represents a feature that has been automatically added due to a pre-requisite or for some |
| 1014 | * other reason. |
| 1015 | */ |
| 1016 | struct ImpliedFeature { |
| 1017 | explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {} |
| 1018 | |
| 1019 | /** List of human-readable reasons for why this feature was implied. */ |
| 1020 | std::set<std::string> reasons; |
| 1021 | |
| 1022 | // Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />) |
| 1023 | bool implied_from_sdk_k23; |
| 1024 | }; |
| 1025 | |
| 1026 | /* Mapping of implied feature names to their properties. */ |
| 1027 | std::map<std::string, ImpliedFeature> implied_features_; |
| 1028 | }; |
| 1029 | |
| 1030 | /** Represents <uses-feature> elements. **/ |
| 1031 | class UsesFeature : public ManifestExtractor::Element { |
| 1032 | public: |
| 1033 | UsesFeature() = default; |
| 1034 | void Extract(xml::Element* element) override { |
| 1035 | const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1036 | int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR)); |
| 1037 | bool required = GetAttributeIntegerDefault( |
| 1038 | FindAttribute(element, REQUIRED_ATTR), true) != 0; |
| 1039 | int32_t version = GetAttributeIntegerDefault( |
| 1040 | FindAttribute(element, kAndroidNamespace, "version"), 0); |
| 1041 | |
| 1042 | // Add the feature to the parent feature group element if one exists; otherwise, add it to the |
| 1043 | // common feature group |
| 1044 | FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]); |
| 1045 | if (!feature_group) { |
| 1046 | feature_group = extractor()->GetCommonFeatureGroup(); |
| 1047 | } else { |
| 1048 | // All features in side of <feature-group> elements are required. |
| 1049 | required = true; |
| 1050 | } |
| 1051 | |
| 1052 | if (name) { |
| 1053 | feature_group->AddFeature(*name, required, version); |
| 1054 | } else if (gl) { |
| 1055 | feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl); |
| 1056 | } |
| 1057 | } |
| 1058 | }; |
| 1059 | |
| 1060 | /** Represents <uses-permission> elements. **/ |
| 1061 | class UsesPermission : public ManifestExtractor::Element { |
| 1062 | public: |
| 1063 | UsesPermission() = default; |
| 1064 | std::string name; |
| 1065 | std::string requiredFeature; |
| 1066 | std::string requiredNotFeature; |
| 1067 | int32_t required = true; |
| 1068 | int32_t maxSdkVersion = -1; |
| 1069 | |
| 1070 | void Extract(xml::Element* element) override { |
| 1071 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1072 | requiredFeature = GetAttributeStringDefault( |
| 1073 | FindAttribute(element, REQUIRED_FEATURE_ATTR), ""); |
| 1074 | requiredNotFeature = GetAttributeStringDefault( |
| 1075 | FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), ""); |
| 1076 | required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1); |
| 1077 | maxSdkVersion = GetAttributeIntegerDefault( |
| 1078 | FindAttribute(element, MAX_SDK_VERSION_ATTR), -1); |
| 1079 | |
| 1080 | if (!name.empty()) { |
| 1081 | CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup(); |
| 1082 | common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1086 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1087 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1088 | printer->Print(StringPrintf("uses-permission: name='%s'", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1089 | if (maxSdkVersion >= 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1090 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1091 | } |
| 1092 | if (!requiredFeature.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1093 | printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1094 | } |
| 1095 | if (!requiredNotFeature.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1096 | printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1097 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1098 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1099 | if (required == 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1100 | printer->Print(StringPrintf("optional-permission: name='%s'", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1101 | if (maxSdkVersion >= 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1102 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1103 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1104 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1109 | void PrintImplied(text::Printer* printer, const std::string& reason) { |
| 1110 | printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1111 | if (maxSdkVersion >= 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1112 | printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1113 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1114 | printer->Print(StringPrintf(" reason='%s'\n", reason.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1115 | } |
| 1116 | }; |
| 1117 | |
| 1118 | /** Represents <uses-permission-sdk-23> elements. **/ |
| 1119 | class UsesPermissionSdk23 : public ManifestExtractor::Element { |
| 1120 | public: |
| 1121 | UsesPermissionSdk23() = default; |
| 1122 | const std::string* name = nullptr; |
| 1123 | const int32_t* maxSdkVersion = nullptr; |
| 1124 | |
| 1125 | void Extract(xml::Element* element) override { |
| 1126 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1127 | maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR)); |
| 1128 | |
| 1129 | if (name) { |
| 1130 | CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup(); |
| 1131 | common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1135 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1136 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1137 | printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1138 | if (maxSdkVersion) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1139 | printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1140 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1141 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | }; |
| 1145 | |
| 1146 | /** Represents <permission> elements. These elements are only printing when dumping permissions. **/ |
| 1147 | class Permission : public ManifestExtractor::Element { |
| 1148 | public: |
| 1149 | Permission() = default; |
| 1150 | std::string name; |
| 1151 | |
| 1152 | void Extract(xml::Element* element) override { |
| 1153 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1154 | } |
| 1155 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1156 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1157 | if (extractor()->options_.only_permissions && !name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1158 | printer->Print(StringPrintf("permission: %s\n", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | }; |
| 1162 | |
| 1163 | /** Represents <activity> elements. **/ |
| 1164 | class Activity : public ManifestExtractor::Element { |
| 1165 | public: |
| 1166 | Activity() = default; |
| 1167 | std::string name; |
| 1168 | std::string icon; |
| 1169 | std::string label; |
| 1170 | std::string banner; |
| 1171 | |
| 1172 | bool has_component_ = false; |
| 1173 | bool has_launcher_category = false; |
| 1174 | bool has_leanback_launcher_category = false; |
| 1175 | bool has_main_action = false; |
| 1176 | |
| 1177 | void Extract(xml::Element* element) override { |
| 1178 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1179 | label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); |
| 1180 | icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), ""); |
| 1181 | banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), ""); |
| 1182 | |
| 1183 | // Retrieve the package name from the manifest |
| 1184 | std::string package; |
| 1185 | for (auto& parent : extractor()->parent_stack()) { |
| 1186 | if (auto manifest = ElementCast<Manifest>(parent)) { |
| 1187 | package = manifest->package; |
| 1188 | break; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Fully qualify the activity name |
| 1193 | ssize_t idx = name.find("."); |
| 1194 | if (idx == 0) { |
| 1195 | name = package + name; |
| 1196 | } else if (idx < 0) { |
| 1197 | name = package + "." + name; |
| 1198 | } |
| 1199 | |
| 1200 | auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR)); |
| 1201 | if (orientation) { |
| 1202 | CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup(); |
| 1203 | int orien = *orientation; |
| 1204 | if (orien == 0 || orien == 6 || orien == 8) { |
| 1205 | // Requests landscape, sensorLandscape, or reverseLandscape. |
| 1206 | common->addImpliedFeature("android.hardware.screen.landscape", |
| 1207 | "one or more activities have specified a landscape orientation", |
| 1208 | false); |
| 1209 | } else if (orien == 1 || orien == 7 || orien == 9) { |
| 1210 | // Requests portrait, sensorPortrait, or reversePortrait. |
| 1211 | common->addImpliedFeature("android.hardware.screen.portrait", |
| 1212 | "one or more activities have specified a portrait orientation", |
| 1213 | false); |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1218 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1219 | // Print whether the activity has the HOME category and a the MAIN action |
| 1220 | if (has_main_action && has_launcher_category) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1221 | printer->Print("launchable-activity:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1222 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1223 | printer->Print(StringPrintf(" name='%s' ", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1224 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1225 | printer->Print(StringPrintf(" label='%s' icon='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1226 | android::ResTable::normalizeForOutput(label.data()).c_str(), |
| 1227 | icon.data())); |
| 1228 | } |
| 1229 | |
| 1230 | // Print wether the activity has the HOME category and a the MAIN action |
| 1231 | if (has_leanback_launcher_category) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1232 | printer->Print("leanback-launchable-activity:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1233 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1234 | printer->Print(StringPrintf(" name='%s' ", name.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1235 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1236 | printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1237 | android::ResTable::normalizeForOutput(label.data()).c_str(), |
| 1238 | icon.data(), banner.data())); |
| 1239 | } |
| 1240 | } |
| 1241 | }; |
| 1242 | |
| 1243 | /** Represents <intent-filter> elements. */ |
| 1244 | class IntentFilter : public ManifestExtractor::Element { |
| 1245 | public: |
| 1246 | IntentFilter() = default; |
| 1247 | }; |
| 1248 | |
| 1249 | /** Represents <category> elements. */ |
| 1250 | class Category : public ManifestExtractor::Element { |
| 1251 | public: |
| 1252 | Category() = default; |
| 1253 | std::string component = ""; |
| 1254 | |
| 1255 | void Extract(xml::Element* element) override { |
| 1256 | const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1257 | |
| 1258 | auto parent_stack = extractor()->parent_stack(); |
| 1259 | if (category && ElementCast<IntentFilter>(parent_stack[0]) |
| 1260 | && ElementCast<Activity>(parent_stack[1])) { |
| 1261 | Activity* activity = ElementCast<Activity>(parent_stack[1]); |
| 1262 | |
| 1263 | if (*category == "android.intent.category.LAUNCHER") { |
| 1264 | activity->has_launcher_category = true; |
| 1265 | } else if (*category == "android.intent.category.LEANBACK_LAUNCHER") { |
| 1266 | activity->has_leanback_launcher_category = true; |
| 1267 | } else if (*category == "android.intent.category.HOME") { |
| 1268 | component = "launcher"; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | }; |
| 1273 | |
| 1274 | /** |
| 1275 | * Represents <provider> elements. The elements may have an <intent-filter> which may have <action> |
| 1276 | * elements nested within. |
| 1277 | **/ |
| 1278 | class Provider : public ManifestExtractor::Element { |
| 1279 | public: |
| 1280 | Provider() = default; |
| 1281 | bool has_required_saf_attributes = false; |
| 1282 | |
| 1283 | void Extract(xml::Element* element) override { |
| 1284 | const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR)); |
| 1285 | const int32_t* grant_uri_permissions = GetAttributeInteger( |
| 1286 | FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR)); |
| 1287 | const std::string* permission = GetAttributeString( |
| 1288 | FindAttribute(element, PERMISSION_ATTR)); |
| 1289 | |
| 1290 | has_required_saf_attributes = ((exported && *exported != 0) |
| 1291 | && (grant_uri_permissions && *grant_uri_permissions != 0) |
| 1292 | && (permission && *permission == "android.permission.MANAGE_DOCUMENTS")); |
| 1293 | } |
| 1294 | }; |
| 1295 | |
| 1296 | /** Represents <receiver> elements. **/ |
| 1297 | class Receiver : public ManifestExtractor::Element { |
| 1298 | public: |
| 1299 | Receiver() = default; |
| 1300 | const std::string* permission = nullptr; |
| 1301 | bool has_component = false; |
| 1302 | |
| 1303 | void Extract(xml::Element* element) override { |
| 1304 | permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR)); |
| 1305 | } |
| 1306 | }; |
| 1307 | |
| 1308 | /**Represents <service> elements. **/ |
| 1309 | class Service : public ManifestExtractor::Element { |
| 1310 | public: |
| 1311 | Service() = default; |
| 1312 | const std::string* permission = nullptr; |
| 1313 | bool has_component = false; |
| 1314 | |
| 1315 | void Extract(xml::Element* element) override { |
| 1316 | permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR)); |
| 1317 | } |
| 1318 | }; |
| 1319 | |
| 1320 | /** Represents <uses-library> elements. **/ |
| 1321 | class UsesLibrary : public ManifestExtractor::Element { |
| 1322 | public: |
| 1323 | UsesLibrary() = default; |
| 1324 | std::string name; |
| 1325 | int required; |
| 1326 | |
| 1327 | void Extract(xml::Element* element) override { |
| 1328 | auto parent_stack = extractor()->parent_stack(); |
| 1329 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1330 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1331 | required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1335 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1336 | if (!name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1337 | printer->Print(StringPrintf("uses-library%s:'%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1338 | (required == 0) ? "-not-required" : "", name.data())); |
| 1339 | } |
| 1340 | } |
| 1341 | }; |
| 1342 | |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1343 | /** Represents <static-library> elements. **/ |
| 1344 | class StaticLibrary : public ManifestExtractor::Element { |
| 1345 | public: |
| 1346 | StaticLibrary() = default; |
| 1347 | std::string name; |
| 1348 | int version; |
| 1349 | int versionMajor; |
| 1350 | |
| 1351 | void Extract(xml::Element* element) override { |
| 1352 | auto parent_stack = extractor()->parent_stack(); |
| 1353 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1354 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1355 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 1356 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1357 | } |
| 1358 | } |
| 1359 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1360 | void Print(text::Printer* printer) override { |
| 1361 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1362 | "static-library: name='%s' version='%d' versionMajor='%d'\n", |
| 1363 | name.data(), version, versionMajor)); |
| 1364 | } |
| 1365 | }; |
| 1366 | |
| 1367 | /** Represents <uses-static-library> elements. **/ |
| 1368 | class UsesStaticLibrary : public ManifestExtractor::Element { |
| 1369 | public: |
| 1370 | UsesStaticLibrary() = default; |
| 1371 | std::string name; |
| 1372 | int version; |
| 1373 | int versionMajor; |
| 1374 | std::vector<std::string> certDigests; |
| 1375 | |
| 1376 | void Extract(xml::Element* element) override { |
| 1377 | auto parent_stack = extractor()->parent_stack(); |
| 1378 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1379 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1380 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 1381 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1382 | AddCertDigest(element); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | void AddCertDigest(xml::Element* element) { |
| 1387 | std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), ""); |
| 1388 | // We allow ":" delimiters in the SHA declaration as this is the format |
| 1389 | // emitted by the certtool making it easy for developers to copy/paste. |
| 1390 | digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end()); |
| 1391 | if (!digest.empty()) { |
| 1392 | certDigests.push_back(digest); |
| 1393 | } |
| 1394 | } |
| 1395 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1396 | void Print(text::Printer* printer) override { |
| 1397 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1398 | "uses-static-library: name='%s' version='%d' versionMajor='%d'", |
| 1399 | name.data(), version, versionMajor)); |
| 1400 | for (size_t i = 0; i < certDigests.size(); i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1401 | printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1402 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1403 | printer->Print("\n"); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1404 | } |
| 1405 | }; |
| 1406 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1407 | /** |
| 1408 | * Represents <meta-data> elements. These tags are only printed when a flag is passed in to |
| 1409 | * explicitly enable meta data printing. |
| 1410 | **/ |
| 1411 | class MetaData : public ManifestExtractor::Element { |
| 1412 | public: |
| 1413 | MetaData() = default; |
| 1414 | std::string name; |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1415 | std::string value; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1416 | const int* value_int; |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1417 | std::string resource; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1418 | const int* resource_int; |
| 1419 | |
| 1420 | void Extract(xml::Element* element) override { |
| 1421 | name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1422 | value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), ""); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1423 | value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR)); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1424 | resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), ""); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1425 | resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR)); |
| 1426 | } |
| 1427 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1428 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1429 | if (extractor()->options_.include_meta_data && !name.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1430 | printer->Print(StringPrintf("meta-data: name='%s' ", name.data())); |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1431 | if (!value.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1432 | printer->Print(StringPrintf("value='%s' ", value.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1433 | } else if (value_int) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1434 | printer->Print(StringPrintf("value='%d' ", *value_int)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1435 | } else { |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 1436 | if (!resource.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1437 | printer->Print(StringPrintf("resource='%s' ", resource.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1438 | } else if (resource_int) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1439 | printer->Print(StringPrintf("resource='%d' ", *resource_int)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1442 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | }; |
| 1446 | |
| 1447 | /** |
| 1448 | * Represents <action> elements. Detects the presence of certain activity, provider, receiver, and |
| 1449 | * service components. |
| 1450 | **/ |
| 1451 | class Action : public ManifestExtractor::Element { |
| 1452 | public: |
| 1453 | Action() = default; |
| 1454 | std::string component = ""; |
| 1455 | |
| 1456 | void Extract(xml::Element* element) override { |
| 1457 | auto parent_stack = extractor()->parent_stack(); |
| 1458 | std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); |
| 1459 | |
| 1460 | if (ElementCast<IntentFilter>(parent_stack[0])) { |
| 1461 | if (ElementCast<Activity>(parent_stack[1])) { |
| 1462 | // Detects the presence of a particular type of activity. |
| 1463 | Activity* activity = ElementCast<Activity>(parent_stack[1]); |
| 1464 | auto map = std::map<std::string, std::string>({ |
| 1465 | { "android.intent.action.MAIN" , "main" }, |
| 1466 | { "android.intent.action.VIDEO_CAMERA" , "camera" }, |
| 1467 | { "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" }, |
| 1468 | }); |
| 1469 | |
| 1470 | auto entry = map.find(action); |
| 1471 | if (entry != map.end()) { |
| 1472 | component = entry->second; |
| 1473 | activity->has_component_ = true; |
| 1474 | } |
| 1475 | |
| 1476 | if (action == "android.intent.action.MAIN") { |
| 1477 | activity->has_main_action = true; |
| 1478 | } |
| 1479 | |
| 1480 | } else if (ElementCast<Receiver>(parent_stack[1])) { |
| 1481 | // Detects the presence of a particular type of receiver. If the action requires a |
| 1482 | // permission, then the receiver element is checked for the permission. |
| 1483 | Receiver* receiver = ElementCast<Receiver>(parent_stack[1]); |
| 1484 | auto map = std::map<std::string, std::string>({ |
| 1485 | { "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" }, |
| 1486 | { "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" }, |
| 1487 | }); |
| 1488 | |
| 1489 | auto permissions = std::map<std::string, std::string>({ |
| 1490 | { "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" }, |
| 1491 | }); |
| 1492 | |
| 1493 | auto entry = map.find(action); |
| 1494 | auto permission = permissions.find(action); |
| 1495 | if (entry != map.end() && (permission == permissions.end() |
| 1496 | || (receiver->permission && permission->second == *receiver->permission))) { |
| 1497 | receiver->has_component = true; |
| 1498 | component = entry->second; |
| 1499 | } |
| 1500 | |
| 1501 | } else if (ElementCast<Service>(parent_stack[1])) { |
| 1502 | // Detects the presence of a particular type of service. If the action requires a |
| 1503 | // permission, then the service element is checked for the permission. |
| 1504 | Service* service = ElementCast<Service>(parent_stack[1]); |
| 1505 | auto map = std::map<std::string, std::string>({ |
| 1506 | { "android.view.InputMethod" , "ime" }, |
| 1507 | { "android.service.wallpaper.WallpaperService" , "wallpaper" }, |
| 1508 | { "android.accessibilityservice.AccessibilityService" , "accessibility" }, |
| 1509 | { "android.printservice.PrintService" , "print-service" }, |
| 1510 | { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" }, |
| 1511 | { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" }, |
| 1512 | { "android.service.notification.NotificationListenerService" ,"notification-listener" }, |
| 1513 | { "android.service.dreams.DreamService" , "dream" }, |
| 1514 | }); |
| 1515 | |
| 1516 | auto permissions = std::map<std::string, std::string>({ |
| 1517 | { "android.accessibilityservice.AccessibilityService" , |
| 1518 | "android.permission.BIND_ACCESSIBILITY_SERVICE" }, |
| 1519 | { "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" }, |
| 1520 | { "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , |
| 1521 | "android.permission.BIND_NFC_SERVICE" }, |
| 1522 | { "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , |
| 1523 | "android.permission.BIND_NFC_SERVICE" }, |
| 1524 | { "android.service.notification.NotificationListenerService" , |
| 1525 | "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" }, |
| 1526 | { "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" }, |
| 1527 | }); |
| 1528 | |
| 1529 | auto entry = map.find(action); |
| 1530 | auto permission = permissions.find(action); |
| 1531 | if (entry != map.end() && (permission == permissions.end() |
| 1532 | || (service->permission && permission->second == *service->permission))) { |
| 1533 | service->has_component= true; |
| 1534 | component = entry->second; |
| 1535 | } |
| 1536 | |
| 1537 | } else if (ElementCast<Provider>(parent_stack[1])) { |
| 1538 | // Detects the presence of a particular type of receiver. If the provider requires a |
| 1539 | // permission, then the provider element is checked for the permission. |
| 1540 | // Detect whether this action |
| 1541 | Provider* provider = ElementCast<Provider>(parent_stack[1]); |
| 1542 | if (action == "android.content.action.DOCUMENTS_PROVIDER" |
| 1543 | && provider->has_required_saf_attributes) { |
| 1544 | component = "document-provider"; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | // Represents a searchable interface |
| 1550 | if (action == "android.intent.action.SEARCH") { |
| 1551 | component = "search"; |
| 1552 | } |
| 1553 | } |
| 1554 | }; |
| 1555 | |
| 1556 | /** |
| 1557 | * Represents <supports-input> elements. The element may have <input-type> elements nested within. |
| 1558 | **/ |
| 1559 | class SupportsInput : public ManifestExtractor::Element { |
| 1560 | public: |
| 1561 | SupportsInput() = default; |
| 1562 | std::vector<std::string> inputs; |
| 1563 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1564 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1565 | const size_t size = inputs.size(); |
| 1566 | if (size > 0) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1567 | printer->Print("supports-input: '"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1568 | for (size_t i = 0; i < size; i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1569 | printer->Print(StringPrintf("value='%s' ", inputs[i].data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1570 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1571 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | }; |
| 1575 | |
| 1576 | /** Represents <input-type> elements. **/ |
| 1577 | class InputType : public ManifestExtractor::Element { |
| 1578 | public: |
| 1579 | InputType() = default; |
| 1580 | void Extract(xml::Element* element) override { |
| 1581 | auto name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1582 | auto parent_stack = extractor()->parent_stack(); |
| 1583 | |
| 1584 | // Add the input to the set of supported inputs |
| 1585 | if (name && ElementCast<SupportsInput>(parent_stack[0])) { |
| 1586 | SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]); |
| 1587 | supports->inputs.push_back(*name); |
| 1588 | } |
| 1589 | } |
| 1590 | }; |
| 1591 | |
| 1592 | /** Represents <original-package> elements. **/ |
| 1593 | class OriginalPackage : public ManifestExtractor::Element { |
| 1594 | public: |
| 1595 | OriginalPackage() = default; |
| 1596 | const std::string* name = nullptr; |
| 1597 | |
| 1598 | void Extract(xml::Element* element) override { |
| 1599 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1600 | } |
| 1601 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1602 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1603 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1604 | printer->Print(StringPrintf("original-package:'%s'\n", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | }; |
| 1608 | |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 1609 | |
| 1610 | /** Represents <overlay> elements. **/ |
| 1611 | class Overlay : public ManifestExtractor::Element { |
| 1612 | public: |
| 1613 | Overlay() = default; |
| 1614 | const std::string* target_package = nullptr; |
| 1615 | int priority; |
| 1616 | bool is_static; |
| 1617 | const std::string* required_property_name = nullptr; |
| 1618 | const std::string* required_property_value = nullptr; |
| 1619 | |
| 1620 | void Extract(xml::Element* element) override { |
| 1621 | target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR)); |
| 1622 | priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0); |
| 1623 | is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0; |
| 1624 | required_property_name = GetAttributeString( |
| 1625 | FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR)); |
| 1626 | required_property_value = GetAttributeString( |
| 1627 | FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR)); |
| 1628 | } |
| 1629 | |
| 1630 | void Print(text::Printer* printer) override { |
| 1631 | printer->Print(StringPrintf("overlay:")); |
| 1632 | if (target_package) { |
| 1633 | printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str())); |
| 1634 | } |
| 1635 | printer->Print(StringPrintf(" priority='%d'", priority)); |
| 1636 | printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false")); |
| 1637 | if (required_property_name) { |
| 1638 | printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str())); |
| 1639 | } |
| 1640 | if (required_property_value) { |
| 1641 | printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str())); |
| 1642 | } |
| 1643 | printer->Print("\n"); |
| 1644 | } |
| 1645 | }; |
| 1646 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1647 | /** * Represents <package-verifier> elements. **/ |
| 1648 | class PackageVerifier : public ManifestExtractor::Element { |
| 1649 | public: |
| 1650 | PackageVerifier() = default; |
| 1651 | const std::string* name = nullptr; |
| 1652 | const std::string* public_key = nullptr; |
| 1653 | |
| 1654 | void Extract(xml::Element* element) override { |
| 1655 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1656 | public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR)); |
| 1657 | } |
| 1658 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1659 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1660 | if (name && public_key) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1661 | printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n", |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1662 | name->data(), public_key->data())); |
| 1663 | } |
| 1664 | } |
| 1665 | }; |
| 1666 | |
| 1667 | /** Represents <uses-package> elements. **/ |
| 1668 | class UsesPackage : public ManifestExtractor::Element { |
| 1669 | public: |
| 1670 | UsesPackage() = default; |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1671 | const std::string* packageType = nullptr; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1672 | const std::string* name = nullptr; |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1673 | int version; |
| 1674 | int versionMajor; |
| 1675 | std::vector<std::string> certDigests; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1676 | |
| 1677 | void Extract(xml::Element* element) override { |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1678 | auto parent_stack = extractor()->parent_stack(); |
| 1679 | if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) { |
| 1680 | packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR)); |
| 1681 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1682 | version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0); |
| 1683 | versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0); |
| 1684 | AddCertDigest(element); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | void AddCertDigest(xml::Element* element) { |
| 1689 | std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), ""); |
| 1690 | // We allow ":" delimiters in the SHA declaration as this is the format |
| 1691 | // emitted by the certtool making it easy for developers to copy/paste. |
| 1692 | digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end()); |
| 1693 | if (!digest.empty()) { |
| 1694 | certDigests.push_back(digest); |
| 1695 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1698 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1699 | if (name) { |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1700 | if (packageType) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1701 | printer->Print(StringPrintf( |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1702 | "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'", |
| 1703 | packageType->data(), name->data(), version, versionMajor)); |
| 1704 | for (size_t i = 0; i < certDigests.size(); i++) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1705 | printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1706 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1707 | printer->Print("\n"); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1708 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1709 | printer->Print(StringPrintf("uses-package:'%s'\n", name->data())); |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | }; |
| 1714 | |
| 1715 | /** Represents <additional-certificate> elements. **/ |
| 1716 | class AdditionalCertificate : public ManifestExtractor::Element { |
| 1717 | public: |
| 1718 | AdditionalCertificate() = default; |
| 1719 | |
| 1720 | void Extract(xml::Element* element) override { |
| 1721 | auto parent_stack = extractor()->parent_stack(); |
| 1722 | if (parent_stack.size() > 0) { |
| 1723 | if (ElementCast<UsesPackage>(parent_stack[0])) { |
| 1724 | UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]); |
| 1725 | uses->AddCertDigest(element); |
| 1726 | } else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) { |
| 1727 | UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]); |
| 1728 | uses->AddCertDigest(element); |
| 1729 | } |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | }; |
| 1733 | |
| 1734 | /** Represents <screen> elements found in <compatible-screens> elements. */ |
| 1735 | class Screen : public ManifestExtractor::Element { |
| 1736 | public: |
| 1737 | Screen() = default; |
| 1738 | const int32_t* size = nullptr; |
| 1739 | const int32_t* density = nullptr; |
| 1740 | |
| 1741 | void Extract(xml::Element* element) override { |
| 1742 | size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR)); |
| 1743 | density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR)); |
| 1744 | } |
| 1745 | }; |
| 1746 | |
| 1747 | /** |
| 1748 | * Represents <compatible-screens> elements. These elements have <screen> elements nested within |
| 1749 | * that each denote a supported screen size and screen density. |
| 1750 | **/ |
| 1751 | class CompatibleScreens : public ManifestExtractor::Element { |
| 1752 | public: |
| 1753 | CompatibleScreens() = default; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1754 | void Print(text::Printer* printer) override { |
| 1755 | printer->Print("compatible-screens:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1756 | |
| 1757 | bool first = true; |
| 1758 | ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){ |
| 1759 | if (auto screen = ElementCast<Screen>(el)) { |
| 1760 | if (first) { |
| 1761 | first = false; |
| 1762 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1763 | printer->Print(","); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | if (screen->size && screen->density) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1767 | printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | }); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1771 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1772 | } |
| 1773 | }; |
| 1774 | |
| 1775 | /** Represents <supports-gl-texture> elements. **/ |
| 1776 | class SupportsGlTexture : public ManifestExtractor::Element { |
| 1777 | public: |
| 1778 | SupportsGlTexture() = default; |
| 1779 | const std::string* name = nullptr; |
| 1780 | |
| 1781 | void Extract(xml::Element* element) override { |
| 1782 | name = GetAttributeString(FindAttribute(element, NAME_ATTR)); |
| 1783 | } |
| 1784 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1785 | void Print(text::Printer* printer) override { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1786 | if (name) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1787 | printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | }; |
| 1791 | |
| 1792 | /** Recursively prints the extracted badging element. */ |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1793 | static void Print(ManifestExtractor::Element* el, text::Printer* printer) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1794 | el->Print(printer); |
| 1795 | for (auto &child : el->children()) { |
| 1796 | Print(child.get(), printer); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1800 | bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1801 | // Load the manifest |
| 1802 | std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag); |
| 1803 | if (doc == nullptr) { |
| 1804 | diag->Error(DiagMessage() << "failed to find AndroidManifest.xml"); |
| 1805 | return false; |
| 1806 | } |
| 1807 | |
| 1808 | xml::Element* element = doc->root.get(); |
| 1809 | if (element->name != "manifest") { |
| 1810 | diag->Error(DiagMessage() << "manifest does not start with <manifest> tag"); |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | // Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if |
| 1815 | // printing only permission elements is requested |
| 1816 | if (options_.only_permissions) { |
| 1817 | std::unique_ptr<ManifestExtractor::Element> manifest_element = |
| 1818 | ManifestExtractor::Element::Inflate(this, element); |
| 1819 | |
| 1820 | if (auto manifest = ElementCast<Manifest>(manifest_element.get())) { |
| 1821 | for (xml::Element* child : element->GetChildElements()) { |
| 1822 | if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23" |
| 1823 | || child->name == "permission") { |
| 1824 | auto permission_element = ManifestExtractor::Element::Inflate(this, child); |
| 1825 | manifest->AddChild(permission_element); |
| 1826 | } |
| 1827 | } |
| 1828 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 1829 | printer->Print(StringPrintf("package: %s\n", manifest->package.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 1830 | ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void { |
| 1831 | el->Print(printer); |
| 1832 | }); |
| 1833 | |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
| 1840 | // Collect information about the resource configurations |
| 1841 | if (apk_->GetResourceTable()) { |
| 1842 | for (auto &package : apk_->GetResourceTable()->packages) { |
| 1843 | for (auto &type : package->types) { |
| 1844 | for (auto &entry : type->entries) { |
| 1845 | for (auto &value : entry->values) { |
| 1846 | std::string locale_str = value->config.GetBcp47LanguageTag(); |
| 1847 | |
| 1848 | // Collect all the unique locales of the apk |
| 1849 | if (locales_.find(locale_str) == locales_.end()) { |
| 1850 | ConfigDescription config = ManifestExtractor::DummyConfig(); |
| 1851 | config.setBcp47Locale(locale_str.data()); |
| 1852 | locales_.insert(std::make_pair(locale_str, config)); |
| 1853 | } |
| 1854 | |
| 1855 | // Collect all the unique density of the apk |
| 1856 | uint16_t density = (value->config.density == 0) ? (uint16_t) 160 |
| 1857 | : value->config.density; |
| 1858 | if (densities_.find(density) == densities_.end()) { |
| 1859 | ConfigDescription config = ManifestExtractor::DummyConfig(); |
| 1860 | config.density = density; |
| 1861 | densities_.insert(std::make_pair(density, config)); |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | // Extract badging information |
| 1870 | auto root = Visit(element); |
| 1871 | |
| 1872 | // Print the elements in order seen |
| 1873 | Print(root.get(), printer); |
| 1874 | |
| 1875 | /** Recursively checks the extracted elements for the specified permission. **/ |
| 1876 | auto FindPermission = [&](ManifestExtractor::Element* root, |
| 1877 | const std::string& name) -> ManifestExtractor::Element* { |
| 1878 | return FindElement(root, [&](ManifestExtractor::Element* el) -> bool { |
| 1879 | if (UsesPermission* permission = ElementCast<UsesPermission>(el)) { |
| 1880 | return permission->name == name; |
| 1881 | } |
| 1882 | return false; |
| 1883 | }); |
| 1884 | }; |
| 1885 | |
| 1886 | auto PrintPermission = [&printer](const std::string& name, const std::string& reason, |
| 1887 | int32_t max_sdk_version) -> void { |
| 1888 | auto permission = util::make_unique<UsesPermission>(); |
| 1889 | permission->name = name; |
| 1890 | permission->maxSdkVersion = max_sdk_version; |
| 1891 | permission->Print(printer); |
| 1892 | permission->PrintImplied(printer, reason); |
| 1893 | }; |
| 1894 | |
| 1895 | // Implied permissions |
| 1896 | // Pre-1.6 implicitly granted permission compatibility logic |
| 1897 | CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup(); |
| 1898 | bool insert_write_external = false; |
| 1899 | auto write_external_permission = ElementCast<UsesPermission>( |
| 1900 | FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE")); |
| 1901 | |
| 1902 | if (target_sdk() < 4) { |
| 1903 | if (!write_external_permission) { |
| 1904 | PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1); |
| 1905 | insert_write_external = true; |
| 1906 | } |
| 1907 | |
| 1908 | if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) { |
| 1909 | PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1); |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | // If the application has requested WRITE_EXTERNAL_STORAGE, we will |
| 1914 | // force them to always take READ_EXTERNAL_STORAGE as well. We always |
| 1915 | // do this (regardless of target API version) because we can't have |
| 1916 | // an app with write permission but not read permission. |
| 1917 | auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE"); |
| 1918 | if (!read_external && (insert_write_external || write_external_permission)) { |
| 1919 | PrintPermission("android.permission.READ_EXTERNAL_STORAGE", |
| 1920 | "requested WRITE_EXTERNAL_STORAGE", |
| 1921 | (write_external_permission) ? write_external_permission->maxSdkVersion : -1); |
| 1922 | } |
| 1923 | |
| 1924 | // Pre-JellyBean call log permission compatibility. |
| 1925 | if (target_sdk() < 16) { |
| 1926 | if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG") |
| 1927 | && FindPermission(root.get(), "android.permission.READ_CONTACTS")) { |
| 1928 | PrintPermission("android.permission.READ_CALL_LOG", |
| 1929 | "targetSdkVersion < 16 and requested READ_CONTACTS", -1); |
| 1930 | } |
| 1931 | |
| 1932 | if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG") |
| 1933 | && FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) { |
| 1934 | PrintPermission("android.permission.WRITE_CALL_LOG", |
| 1935 | "targetSdkVersion < 16 and requested WRITE_CONTACTS", -1); |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | // If the app hasn't declared the touchscreen as a feature requirement (either |
| 1940 | // directly or implied, required or not), then the faketouch feature is implied. |
| 1941 | if (!common_feature_group->HasFeature("android.hardware.touchscreen")) { |
| 1942 | common_feature_group->addImpliedFeature("android.hardware.faketouch", |
| 1943 | "default feature for all apps", false); |
| 1944 | } |
| 1945 | |
| 1946 | // Only print the common feature group if no feature group is defined |
| 1947 | std::vector<FeatureGroup*> feature_groups; |
| 1948 | ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void { |
| 1949 | if (auto feature_group = ElementCast<FeatureGroup>(el)) { |
| 1950 | feature_groups.push_back(feature_group); |
| 1951 | } |
| 1952 | }); |
| 1953 | |
| 1954 | if (feature_groups.empty()) { |
| 1955 | common_feature_group->PrintGroup(printer); |
| 1956 | } else { |
| 1957 | // Merge the common feature group into the feature group |
| 1958 | for (auto& feature_group : feature_groups) { |
| 1959 | feature_group->open_gles_version = std::max(feature_group->open_gles_version, |
| 1960 | common_feature_group->open_gles_version); |
| 1961 | feature_group->Merge(common_feature_group); |
| 1962 | feature_group->PrintGroup(printer); |
| 1963 | } |
| 1964 | }; |
| 1965 | |
| 1966 | // Collect the component types of the application |
| 1967 | std::set<std::string> components; |
| 1968 | ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void { |
| 1969 | if (ElementCast<Action>(el)) { |
| 1970 | auto action = ElementCast<Action>(el); |
| 1971 | if (!action->component.empty()) { |
| 1972 | components.insert(action->component); |
| 1973 | return; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | if (ElementCast<Category>(el)) { |
| 1978 | auto category = ElementCast<Category>(el); |
| 1979 | if (!category->component.empty()) { |
| 1980 | components.insert(category->component); |
| 1981 | return; |
| 1982 | } |
| 1983 | } |
| 1984 | }); |
| 1985 | |
| 1986 | // Check for the payment component |
| 1987 | auto apk = apk_; |
| 1988 | ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void { |
| 1989 | if (auto service = ElementCast<Service>(el)) { |
| 1990 | auto host_apdu_action = ElementCast<Action>(FindElement(service, |
| 1991 | [&](ManifestExtractor::Element* el) -> bool { |
| 1992 | if (auto action = ElementCast<Action>(el)) { |
| 1993 | return (action->component == "host-apdu"); |
| 1994 | } |
| 1995 | return false; |
| 1996 | })); |
| 1997 | |
| 1998 | auto offhost_apdu_action = ElementCast<Action>(FindElement(service, |
| 1999 | [&](ManifestExtractor::Element* el) -> bool { |
| 2000 | if (auto action = ElementCast<Action>(el)) { |
| 2001 | return (action->component == "offhost-apdu"); |
| 2002 | } |
| 2003 | return false; |
| 2004 | })); |
| 2005 | |
| 2006 | ForEachChild(service, [&apk, &components, &diag, &host_apdu_action, |
| 2007 | &offhost_apdu_action](ManifestExtractor::Element* el) -> void { |
| 2008 | if (auto meta_data = ElementCast<MetaData>(el)) { |
| 2009 | if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action) |
| 2010 | || (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service" |
| 2011 | && offhost_apdu_action)) { |
| 2012 | |
| 2013 | // Attempt to load the resource file |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 2014 | if (!meta_data->resource.empty()) { |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Ryan Mitchell | 2250c93 | 2018-10-04 11:07:40 -0700 | [diff] [blame] | 2017 | auto resource = apk->LoadXml(meta_data->resource, diag); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2018 | if (!resource) { |
| 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | // Look for the payment category on an <aid-group> element |
| 2023 | auto& root = resource.get()->root; |
| 2024 | if ((host_apdu_action && root->name == "host-apdu-service") |
| 2025 | || (offhost_apdu_action && root->name == "offhost-apdu-service")) { |
| 2026 | |
| 2027 | for (auto& child : root->GetChildElements()) { |
| 2028 | if (child->name == "aid-group") { |
| 2029 | auto category = FindAttribute(child, CATEGORY_ATTR); |
| 2030 | if (category && category->value == "payment") { |
| 2031 | components.insert("payment"); |
| 2032 | return; |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | }); |
| 2040 | } |
| 2041 | }); |
| 2042 | |
| 2043 | // Print the components types if they are present |
| 2044 | auto PrintComponent = [&components, &printer](const std::string& component) -> void { |
| 2045 | if (components.find(component) != components.end()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2046 | printer->Print(StringPrintf("provides-component:'%s'\n", component.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2047 | } |
| 2048 | }; |
| 2049 | |
| 2050 | PrintComponent("app-widget"); |
| 2051 | PrintComponent("device-admin"); |
| 2052 | PrintComponent("ime"); |
| 2053 | PrintComponent("wallpaper"); |
| 2054 | PrintComponent("accessibility"); |
| 2055 | PrintComponent("print-service"); |
| 2056 | PrintComponent("payment"); |
| 2057 | PrintComponent("search"); |
| 2058 | PrintComponent("document-provider"); |
| 2059 | PrintComponent("launcher"); |
| 2060 | PrintComponent("notification-listener"); |
| 2061 | PrintComponent("dream"); |
| 2062 | PrintComponent("camera"); |
| 2063 | PrintComponent("camera-secure"); |
| 2064 | |
| 2065 | // Print presence of main activity |
| 2066 | if (components.find("main") != components.end()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2067 | printer->Print("main\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | // Print presence of activities, recivers, and services with no special components |
| 2071 | FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { |
| 2072 | if (auto activity = ElementCast<Activity>(el)) { |
| 2073 | if (!activity->has_component_) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2074 | printer->Print("other-activities\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2075 | return true; |
| 2076 | } |
| 2077 | } |
| 2078 | return false; |
| 2079 | }); |
| 2080 | |
| 2081 | FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { |
| 2082 | if (auto receiver = ElementCast<Receiver>(el)) { |
| 2083 | if (!receiver->has_component) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2084 | printer->Print("other-receivers\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2085 | return true; |
| 2086 | } |
| 2087 | } |
| 2088 | return false; |
| 2089 | }); |
| 2090 | |
| 2091 | FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { |
| 2092 | if (auto service = ElementCast<Service>(el)) { |
| 2093 | if (!service->has_component) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2094 | printer->Print("other-services\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2095 | return true; |
| 2096 | } |
| 2097 | } |
| 2098 | return false; |
| 2099 | }); |
| 2100 | |
| 2101 | // Print the supported screens |
| 2102 | SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(), |
| 2103 | [&](ManifestExtractor::Element* el) -> bool { |
| 2104 | return ElementCast<SupportsScreen>(el) != nullptr; |
| 2105 | })); |
| 2106 | |
| 2107 | if (screen) { |
| 2108 | screen->PrintScreens(printer, target_sdk_); |
| 2109 | } else { |
| 2110 | // Print the default supported screens |
| 2111 | SupportsScreen default_screens; |
| 2112 | default_screens.PrintScreens(printer, target_sdk_); |
| 2113 | } |
| 2114 | |
| 2115 | // Print all the unique locales of the apk |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2116 | printer->Print("locales:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2117 | for (auto& config : locales_) { |
| 2118 | if (config.first.empty()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2119 | printer->Print(" '--_--'"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2120 | } else { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2121 | printer->Print(StringPrintf(" '%s'", config.first.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2122 | } |
| 2123 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2124 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2125 | |
| 2126 | // Print all the densities locales of the apk |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2127 | printer->Print("densities:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2128 | for (auto& config : densities_) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2129 | printer->Print(StringPrintf(" '%d'", config.first)); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2130 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2131 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2132 | |
| 2133 | // Print the supported architectures of the app |
| 2134 | std::set<std::string> architectures; |
| 2135 | auto it = apk_->GetFileCollection()->Iterator(); |
| 2136 | while (it->HasNext()) { |
| 2137 | auto file_path = it->Next()->GetSource().path; |
| 2138 | |
| 2139 | |
| 2140 | size_t pos = file_path.find("lib/"); |
| 2141 | if (pos != std::string::npos) { |
| 2142 | file_path = file_path.substr(pos + 4); |
| 2143 | pos = file_path.find("/"); |
| 2144 | if (pos != std::string::npos) { |
| 2145 | file_path = file_path.substr(0, pos); |
| 2146 | } |
| 2147 | |
| 2148 | architectures.insert(file_path); |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | // Determine if the application has multiArch supports |
| 2153 | auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool { |
| 2154 | if (auto application = ElementCast<Application>(el)) { |
| 2155 | return application->has_multi_arch; |
| 2156 | } |
| 2157 | return false; |
| 2158 | }); |
| 2159 | |
| 2160 | bool output_alt_native_code = false; |
| 2161 | // A multiArch package is one that contains 64-bit and |
| 2162 | // 32-bit versions of native code and expects 3rd-party |
| 2163 | // apps to load these native code libraries. Since most |
| 2164 | // 64-bit systems also support 32-bit apps, the apps |
| 2165 | // loading this multiArch package's code may be either |
| 2166 | if (has_multi_arch) { |
| 2167 | // If this is a multiArch package, report the 64-bit |
| 2168 | // version only. Then as a separate entry, report the |
| 2169 | // rest. |
| 2170 | // |
| 2171 | // If we report the 32-bit architecture, this APK will |
| 2172 | // be installed on a 32-bit device, causing a large waste |
| 2173 | // of bandwidth and disk space. This assumes that |
| 2174 | // the developer of the multiArch package has also |
| 2175 | // made a version that is 32-bit only. |
| 2176 | const std::string kIntel64 = "x86_64"; |
| 2177 | const std::string kArm64 = "arm64-v8a"; |
| 2178 | |
| 2179 | auto arch = architectures.find(kIntel64); |
| 2180 | if (arch == architectures.end()) { |
| 2181 | arch = architectures.find(kArm64); |
| 2182 | } |
| 2183 | |
| 2184 | if (arch != architectures.end()) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2185 | printer->Print(StringPrintf("native-code: '%s'\n", arch->data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2186 | architectures.erase(arch); |
| 2187 | output_alt_native_code = true; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | if (architectures.size() > 0) { |
| 2192 | if (output_alt_native_code) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2193 | printer->Print("alt-"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2194 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2195 | printer->Print("native-code:"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2196 | for (auto& arch : architectures) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2197 | printer->Print(StringPrintf(" '%s'", arch.data())); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2198 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2199 | printer->Print("\n"); |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | return true; |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * Returns the element casted to the type if the element is of that type. Otherwise, returns a null |
| 2207 | * pointer. |
| 2208 | **/ |
| 2209 | template<typename T> |
| 2210 | T* ElementCast(ManifestExtractor::Element* element) { |
| 2211 | if (element == nullptr) { |
| 2212 | return nullptr; |
| 2213 | } |
| 2214 | |
| 2215 | const std::unordered_map<std::string, bool> kTagCheck = { |
| 2216 | {"action", std::is_base_of<Action, T>::value}, |
| 2217 | {"activity", std::is_base_of<Activity, T>::value}, |
| 2218 | {"application", std::is_base_of<Application, T>::value}, |
| 2219 | {"category", std::is_base_of<Category, T>::value}, |
| 2220 | {"compatible-screens", std::is_base_of<CompatibleScreens, T>::value}, |
| 2221 | {"feature-group", std::is_base_of<FeatureGroup, T>::value}, |
| 2222 | {"input-type", std::is_base_of<InputType, T>::value}, |
| 2223 | {"intent-filter", std::is_base_of<IntentFilter, T>::value}, |
| 2224 | {"meta-data", std::is_base_of<MetaData, T>::value}, |
| 2225 | {"manifest", std::is_base_of<Manifest, T>::value}, |
| 2226 | {"original-package", std::is_base_of<OriginalPackage, T>::value}, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 2227 | {"overlay", std::is_base_of<Overlay, T>::value}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2228 | {"package-verifier", std::is_base_of<PackageVerifier, T>::value}, |
| 2229 | {"permission", std::is_base_of<Permission, T>::value}, |
| 2230 | {"provider", std::is_base_of<Provider, T>::value}, |
| 2231 | {"receiver", std::is_base_of<Receiver, T>::value}, |
| 2232 | {"screen", std::is_base_of<Screen, T>::value}, |
| 2233 | {"service", std::is_base_of<Service, T>::value}, |
| 2234 | {"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value}, |
| 2235 | {"supports-input", std::is_base_of<SupportsInput, T>::value}, |
| 2236 | {"supports-screens", std::is_base_of<SupportsScreen, T>::value}, |
| 2237 | {"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value}, |
| 2238 | {"uses-feature", std::is_base_of<UsesFeature, T>::value}, |
| 2239 | {"uses-permission", std::is_base_of<UsesPermission, T>::value}, |
| 2240 | {"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value}, |
| 2241 | {"uses-library", std::is_base_of<UsesLibrary, T>::value}, |
| 2242 | {"uses-package", std::is_base_of<UsesPackage, T>::value}, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2243 | {"static-library", std::is_base_of<StaticLibrary, T>::value}, |
| 2244 | {"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value}, |
| 2245 | {"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2246 | {"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value}, |
| 2247 | }; |
| 2248 | |
| 2249 | auto check = kTagCheck.find(element->tag()); |
| 2250 | if (check != kTagCheck.end() && check->second) { |
| 2251 | return static_cast<T*>(element); |
| 2252 | } |
| 2253 | return nullptr; |
| 2254 | } |
| 2255 | |
| 2256 | template<typename T> |
| 2257 | std::unique_ptr<T> CreateType() { |
| 2258 | return std::move(util::make_unique<T>()); |
| 2259 | } |
| 2260 | |
| 2261 | std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate( |
| 2262 | ManifestExtractor* extractor, xml::Element* el) { |
| 2263 | const std::unordered_map<std::string, |
| 2264 | std::function<std::unique_ptr<ManifestExtractor::Element>()>> |
| 2265 | kTagCheck = { |
| 2266 | {"action", &CreateType<Action>}, |
| 2267 | {"activity", &CreateType<Activity>}, |
| 2268 | {"application", &CreateType<Application>}, |
| 2269 | {"category", &CreateType<Category>}, |
| 2270 | {"compatible-screens", &CreateType<CompatibleScreens>}, |
| 2271 | {"feature-group", &CreateType<FeatureGroup>}, |
| 2272 | {"input-type", &CreateType<InputType>}, |
| 2273 | {"intent-filter",&CreateType<IntentFilter>}, |
| 2274 | {"manifest", &CreateType<Manifest>}, |
| 2275 | {"meta-data", &CreateType<MetaData>}, |
| 2276 | {"original-package", &CreateType<OriginalPackage>}, |
Anton Hansson | cd2d8e2 | 2018-12-11 13:52:17 +0000 | [diff] [blame] | 2277 | {"overlay", &CreateType<Overlay>}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2278 | {"package-verifier", &CreateType<PackageVerifier>}, |
| 2279 | {"permission", &CreateType<Permission>}, |
| 2280 | {"provider", &CreateType<Provider>}, |
| 2281 | {"receiver", &CreateType<Receiver>}, |
| 2282 | {"screen", &CreateType<Screen>}, |
| 2283 | {"service", &CreateType<Service>}, |
| 2284 | {"supports-gl-texture", &CreateType<SupportsGlTexture>}, |
| 2285 | {"supports-input", &CreateType<SupportsInput>}, |
| 2286 | {"supports-screens", &CreateType<SupportsScreen>}, |
| 2287 | {"uses-configuration", &CreateType<UsesConfiguarion>}, |
| 2288 | {"uses-feature", &CreateType<UsesFeature>}, |
| 2289 | {"uses-permission", &CreateType<UsesPermission>}, |
| 2290 | {"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>}, |
| 2291 | {"uses-library", &CreateType<UsesLibrary>}, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2292 | {"static-library", &CreateType<StaticLibrary>}, |
| 2293 | {"uses-static-library", &CreateType<UsesStaticLibrary>}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2294 | {"uses-package", &CreateType<UsesPackage>}, |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 2295 | {"additional-certificate", &CreateType<AdditionalCertificate>}, |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2296 | {"uses-sdk", &CreateType<UsesSdkBadging>}, |
| 2297 | }; |
| 2298 | |
| 2299 | // Attempt to map the xml tag to a element inflater |
| 2300 | std::unique_ptr<ManifestExtractor::Element> element; |
| 2301 | auto check = kTagCheck.find(el->name); |
| 2302 | if (check != kTagCheck.end()) { |
| 2303 | element = check->second(); |
| 2304 | } else { |
| 2305 | element = util::make_unique<ManifestExtractor::Element>(); |
| 2306 | } |
| 2307 | |
| 2308 | element->extractor_ = extractor; |
| 2309 | element->tag_ = el->name; |
| 2310 | element->Extract(el); |
| 2311 | return element; |
| 2312 | } |
| 2313 | |
| 2314 | std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) { |
| 2315 | auto element = ManifestExtractor::Element::Inflate(this, el); |
| 2316 | parent_stack_.insert(parent_stack_.begin(), element.get()); |
| 2317 | |
| 2318 | // Process the element and recursively visit the children |
| 2319 | for (xml::Element* child : el->GetChildElements()) { |
| 2320 | auto v = Visit(child); |
| 2321 | element->AddChild(v); |
| 2322 | } |
| 2323 | |
| 2324 | parent_stack_.erase(parent_stack_.begin()); |
| 2325 | return element; |
| 2326 | } |
| 2327 | |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2328 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 2329 | int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer, |
| 2330 | IDiagnostics* diag) { |
| 2331 | ManifestExtractor extractor(apk, options); |
Ryan Mitchell | 28c8880 | 2019-03-28 11:28:54 -0700 | [diff] [blame] | 2332 | return extractor.Dump(printer, diag) ? 0 : 1; |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 2335 | } // namespace aapt |