1 /*
2  * Copyright (C) 2017 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 #ifndef AAPT_SPLIT_UTIL_H
18 #define AAPT_SPLIT_UTIL_H
19 
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <optional>
24 #include <regex>
25 #include <set>
26 #include <string>
27 #include <unordered_set>
28 
29 #include "AppInfo.h"
30 #include "SdkConstants.h"
31 #include "androidfw/IDiagnostics.h"
32 #include "androidfw/StringPiece.h"
33 #include "filter/ConfigFilter.h"
34 #include "process/IResourceTableConsumer.h"
35 #include "split/TableSplitter.h"
36 #include "xml/XmlDom.h"
37 
38 namespace aapt {
39 
40 using FeatureFlagValues = std::map<std::string, std::optional<bool>, std::less<>>;
41 
42 // Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc).
43 // Returns Nothing and logs a human friendly error message if the string was not legal.
44 std::optional<uint16_t> ParseTargetDensityParameter(android::StringPiece arg,
45                                                     android::IDiagnostics* diag);
46 
47 // Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in
48 // `out_path` with the path and `out_split` with the set of ConfigDescriptions.
49 // Returns false and logs a human friendly error message if the string was not legal.
50 bool ParseSplitParameter(android::StringPiece arg, android::IDiagnostics* diag,
51                          std::string* out_path, SplitConstraints* out_split);
52 
53 // Parses a set of config filter strings of the form 'en,fr-rFR' and returns an IConfigFilter.
54 // Returns nullptr and logs a human friendly error message if the string was not legal.
55 std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
56                                                            android::IDiagnostics* diag);
57 
58 // Parses a feature flags parameter, which can contain one or more pairs of flag names and optional
59 // values, and fills in `out_feature_flag_values` with the parsed values. The pairs in the argument
60 // are separated by ',' and the name is separated from the value by '=' if there is a value given.
61 // Example arg: "flag1=true,flag2=false,flag3=,flag4" where flag3 and flag4 have no given value.
62 bool ParseFeatureFlagsParameter(android::StringPiece arg, android::IDiagnostics* diag,
63                                 FeatureFlagValues* out_feature_flag_values);
64 
65 // Adjust the SplitConstraints so that their SDK version is stripped if it
66 // is less than or equal to the min_sdk. Otherwise the resources that have had
67 // their SDK version stripped due to min_sdk won't ever match.
68 std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
69     int min_sdk, const std::vector<SplitConstraints>& split_constraints);
70 
71 // Generates a split AndroidManifest.xml given the split constraints and app info. The resulting
72 // XmlResource does not need to be linked via XmlReferenceLinker.
73 // This will never fail/return nullptr.
74 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
75                                                         const SplitConstraints& constraints);
76 
77 // Extracts relevant info from the AndroidManifest.xml.
78 std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
79                                                         android::IDiagnostics* diag);
80 
81 // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
82 // replacing nonconforming characters with underscores.
83 //
84 // See frameworks/base/core/java/android/content/pm/PackageParser.java which
85 // checks this at runtime.
86 std::string MakePackageSafeName(const std::string &name);
87 
88 // Sets the versionCode and versionCodeMajor attributes to the version code. Attempts to encode the
89 // version code using the versionCode attribute only, and encodes using both versionCode and
90 // versionCodeMajor if the version code requires more than 32 bits.
91 void SetLongVersionCode(xml::Element* manifest, uint64_t version_code);
92 
93 // Returns a case insensitive regular expression based on the input.
94 std::regex GetRegularExpression(const std::string &input);
95 
96 bool ParseResourceConfig(const std::string& content, IAaptContext* context,
97                          std::unordered_set<ResourceName>& out_resource_exclude_list,
98                          std::set<ResourceName>& out_name_collapse_exemptions,
99                          std::set<ResourceName>& out_path_shorten_exemptions);
100 
101 }  // namespace aapt
102 
103 #endif /* AAPT_SPLIT_UTIL_H */
104