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