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 <regex>
21 
22 #include "androidfw/StringPiece.h"
23 
24 #include "AppInfo.h"
25 #include "Diagnostics.h"
26 #include "SdkConstants.h"
27 #include "filter/ConfigFilter.h"
28 #include "split/TableSplitter.h"
29 #include "util/Maybe.h"
30 #include "xml/XmlDom.h"
31 
32 namespace aapt {
33 
34 // Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc).
35 // Returns Nothing and logs a human friendly error message if the string was not legal.
36 Maybe<uint16_t> ParseTargetDensityParameter(const android::StringPiece& arg, IDiagnostics* diag);
37 
38 // Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in
39 // `out_path` with the path and `out_split` with the set of ConfigDescriptions.
40 // Returns false and logs a human friendly error message if the string was not legal.
41 bool ParseSplitParameter(const android::StringPiece& arg, IDiagnostics* diag, std::string* out_path,
42                          SplitConstraints* out_split);
43 
44 // Parses a set of config filter strings of the form 'en,fr-rFR' and returns an IConfigFilter.
45 // Returns nullptr and logs a human friendly error message if the string was not legal.
46 std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
47                                                            IDiagnostics* diag);
48 
49 // Adjust the SplitConstraints so that their SDK version is stripped if it
50 // is less than or equal to the min_sdk. Otherwise the resources that have had
51 // their SDK version stripped due to min_sdk won't ever match.
52 std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
53     int min_sdk, const std::vector<SplitConstraints>& split_constraints);
54 
55 // Generates a split AndroidManifest.xml given the split constraints and app info. The resulting
56 // XmlResource does not need to be linked via XmlReferenceLinker.
57 // This will never fail/return nullptr.
58 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
59                                                         const SplitConstraints& constraints);
60 
61 // Extracts relevant info from the AndroidManifest.xml.
62 Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
63                                                 IDiagnostics* diag);
64 
65 // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
66 // replacing nonconforming characters with underscores.
67 //
68 // See frameworks/base/core/java/android/content/pm/PackageParser.java which
69 // checks this at runtime.
70 std::string MakePackageSafeName(const std::string &name);
71 
72 // Sets the versionCode and versionCodeMajor attributes to the version code. Attempts to encode the
73 // version code using the versionCode attribute only, and encodes using both versionCode and
74 // versionCodeMajor if the version code requires more than 32 bits.
75 void SetLongVersionCode(xml::Element* manifest, uint64_t version_code);
76 
77 // Returns a case insensitive regular expression based on the input.
78 std::regex GetRegularExpression(const std::string &input);
79 
80 }  // namespace aapt
81 
82 #endif /* AAPT_SPLIT_UTIL_H */
83