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