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#include "cmd/Util.h"
18
19#include <vector>
20
21#include "android-base/logging.h"
22
23#include "ConfigDescription.h"
24#include "Locale.h"
25#include "ResourceUtils.h"
26#include "ValueVisitor.h"
27#include "split/TableSplitter.h"
28#include "util/Maybe.h"
29#include "util/Util.h"
30
31using android::StringPiece;
32
33namespace aapt {
34
35Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
36  ConfigDescription preferred_density_config;
37  if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
38    diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
39    return {};
40  }
41
42  // Clear the version that can be automatically added.
43  preferred_density_config.sdkVersion = 0;
44
45  if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
46      ConfigDescription::CONFIG_DENSITY) {
47    diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
48                              << "Preferred density must only be a density value");
49    return {};
50  }
51  return preferred_density_config.density;
52}
53
54bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
55                         SplitConstraints* out_split) {
56  CHECK(diag != nullptr);
57  CHECK(out_path != nullptr);
58  CHECK(out_split != nullptr);
59
60#ifdef _WIN32
61  const char sSeparator = ';';
62#else
63  const char sSeparator = ':';
64#endif
65
66  std::vector<std::string> parts = util::Split(arg, sSeparator);
67  if (parts.size() != 2) {
68    diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
69    diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
70                             << "<config>[,<config>...].");
71    return false;
72  }
73
74  *out_path = parts[0];
75  std::vector<ConfigDescription> configs;
76  for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
77    ConfigDescription config;
78    if (!ConfigDescription::Parse(config_str, &config)) {
79      diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
80                                << arg << "'");
81      return false;
82    }
83    out_split->configs.insert(config);
84  }
85  return true;
86}
87
88std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
89                                                           IDiagnostics* diag) {
90  std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
91  for (const std::string& config_arg : args) {
92    for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
93      ConfigDescription config;
94      LocaleValue lv;
95      if (lv.InitFromFilterString(config_str)) {
96        lv.WriteTo(&config);
97      } else if (!ConfigDescription::Parse(config_str, &config)) {
98        diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
99        return {};
100      }
101
102      if (config.density != 0) {
103        diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
104      } else {
105        filter->AddConfig(config);
106      }
107    }
108  }
109  return std::move(filter);
110}
111
112// Adjust the SplitConstraints so that their SDK version is stripped if it
113// is less than or equal to the minSdk. Otherwise the resources that have had
114// their SDK version stripped due to minSdk won't ever match.
115std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
116    int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
117  std::vector<SplitConstraints> adjusted_constraints;
118  adjusted_constraints.reserve(split_constraints.size());
119  for (const SplitConstraints& constraints : split_constraints) {
120    SplitConstraints constraint;
121    for (const ConfigDescription& config : constraints.configs) {
122      if (config.sdkVersion <= min_sdk) {
123        constraint.configs.insert(config.CopyWithoutSdkVersion());
124      } else {
125        constraint.configs.insert(config);
126      }
127    }
128    adjusted_constraints.push_back(std::move(constraint));
129  }
130  return adjusted_constraints;
131}
132
133static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
134  return xml::AaptAttribute(Attribute(), id);
135}
136
137std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
138                                                        const SplitConstraints& constraints) {
139  const ResourceId kVersionCode(0x0101021b);
140  const ResourceId kRevisionCode(0x010104d5);
141  const ResourceId kHasCode(0x0101000c);
142
143  std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
144
145  std::unique_ptr<xml::Namespace> namespace_android = util::make_unique<xml::Namespace>();
146  namespace_android->namespace_uri = xml::kSchemaAndroid;
147  namespace_android->namespace_prefix = "android";
148
149  std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
150  manifest_el->name = "manifest";
151  manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
152
153  if (app_info.version_code) {
154    const uint32_t version_code = app_info.version_code.value();
155    manifest_el->attributes.push_back(xml::Attribute{
156        xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
157        CreateAttributeWithId(kVersionCode),
158        util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
159  }
160
161  if (app_info.revision_code) {
162    const uint32_t revision_code = app_info.revision_code.value();
163    manifest_el->attributes.push_back(xml::Attribute{
164        xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
165        CreateAttributeWithId(kRevisionCode),
166        util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
167  }
168
169  std::stringstream split_name;
170  if (app_info.split_name) {
171    split_name << app_info.split_name.value() << ".";
172  }
173  split_name << "config." << util::Joiner(constraints.configs, "_");
174
175  manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
176
177  if (app_info.split_name) {
178    manifest_el->attributes.push_back(
179        xml::Attribute{"", "configForSplit", app_info.split_name.value()});
180  }
181
182  std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
183  application_el->name = "application";
184  application_el->attributes.push_back(
185      xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
186                     util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
187
188  manifest_el->AppendChild(std::move(application_el));
189  namespace_android->AppendChild(std::move(manifest_el));
190  doc->root = std::move(namespace_android);
191  return doc;
192}
193
194static Maybe<std::string> ExtractCompiledString(xml::Attribute* attr, std::string* out_error) {
195  if (attr->compiled_value != nullptr) {
196    String* compiled_str = ValueCast<String>(attr->compiled_value.get());
197    if (compiled_str != nullptr) {
198      if (!compiled_str->value->empty()) {
199        return *compiled_str->value;
200      } else {
201        *out_error = "compiled value is an empty string";
202        return {};
203      }
204    }
205    *out_error = "compiled value is not a string";
206    return {};
207  }
208
209  // Fallback to the plain text value if there is one.
210  if (!attr->value.empty()) {
211    return attr->value;
212  }
213  *out_error = "value is an empty string";
214  return {};
215}
216
217static Maybe<uint32_t> ExtractCompiledInt(xml::Attribute* attr, std::string* out_error) {
218  if (attr->compiled_value != nullptr) {
219    BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
220    if (compiled_prim != nullptr) {
221      if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
222          compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
223        return compiled_prim->value.data;
224      }
225    }
226    *out_error = "compiled value is not an integer";
227    return {};
228  }
229
230  // Fallback to the plain text value if there is one.
231  Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr->value);
232  if (integer) {
233    return integer;
234  }
235  std::stringstream error_msg;
236  error_msg << "'" << attr->value << "' is not a valid integer";
237  *out_error = error_msg.str();
238  return {};
239}
240
241static Maybe<int> ExtractSdkVersion(xml::Attribute* attr, std::string* out_error) {
242  if (attr->compiled_value != nullptr) {
243    BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
244    if (compiled_prim != nullptr) {
245      if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
246          compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
247        return compiled_prim->value.data;
248      }
249      *out_error = "compiled value is not an integer or string";
250      return {};
251    }
252
253    String* compiled_str = ValueCast<String>(attr->compiled_value.get());
254    if (compiled_str != nullptr) {
255      Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
256      if (sdk_version) {
257        return sdk_version;
258      }
259
260      *out_error = "compiled string value is not a valid SDK version";
261      return {};
262    }
263    *out_error = "compiled value is not an integer or string";
264    return {};
265  }
266
267  // Fallback to the plain text value if there is one.
268  Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr->value);
269  if (sdk_version) {
270    return sdk_version;
271  }
272  std::stringstream error_msg;
273  error_msg << "'" << attr->value << "' is not a valid SDK version";
274  *out_error = error_msg.str();
275  return {};
276}
277
278Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
279  // Make sure the first element is <manifest> with package attribute.
280  xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
281  if (manifest_el == nullptr) {
282    return {};
283  }
284
285  AppInfo app_info;
286
287  if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
288    diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
289    return {};
290  }
291
292  xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
293  if (!package_attr) {
294    diag->Error(DiagMessage(xml_res->file.source) << "<manifest> must have a 'package' attribute");
295    return {};
296  }
297
298  std::string error_msg;
299  Maybe<std::string> maybe_package = ExtractCompiledString(package_attr, &error_msg);
300  if (!maybe_package) {
301    diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
302                << "invalid package name: " << error_msg);
303    return {};
304  }
305  app_info.package = maybe_package.value();
306
307  if (xml::Attribute* version_code_attr =
308          manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
309    Maybe<uint32_t> maybe_code = ExtractCompiledInt(version_code_attr, &error_msg);
310    if (!maybe_code) {
311      diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
312                  << "invalid android:versionCode: " << error_msg);
313      return {};
314    }
315    app_info.version_code = maybe_code.value();
316  }
317
318  if (xml::Attribute* revision_code_attr =
319          manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
320    Maybe<uint32_t> maybe_code = ExtractCompiledInt(revision_code_attr, &error_msg);
321    if (!maybe_code) {
322      diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
323                  << "invalid android:revisionCode: " << error_msg);
324      return {};
325    }
326    app_info.revision_code = maybe_code.value();
327  }
328
329  if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
330    Maybe<std::string> maybe_split_name = ExtractCompiledString(split_name_attr, &error_msg);
331    if (!maybe_split_name) {
332      diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
333                  << "invalid split name: " << error_msg);
334      return {};
335    }
336    app_info.split_name = maybe_split_name.value();
337  }
338
339  if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
340    if (xml::Attribute* min_sdk =
341            uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
342      Maybe<int> maybe_sdk = ExtractSdkVersion(min_sdk, &error_msg);
343      if (!maybe_sdk) {
344        diag->Error(DiagMessage(xml_res->file.source.WithLine(uses_sdk_el->line_number))
345                    << "invalid android:minSdkVersion: " << error_msg);
346        return {};
347      }
348      app_info.min_sdk_version = maybe_sdk.value();
349    }
350  }
351  return app_info;
352}
353
354}  // namespace aapt
355