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