blob: f71955247d784a9096ab684c23d9744a7821c8ec [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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_CONFIG_DESCRIPTION_H
18#define AAPT_CONFIG_DESCRIPTION_H
19
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <ostream>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080023#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025namespace aapt {
26
Adam Lesinskibb94f322017-07-12 07:41:55 -070027/*
28 * Subclass of ResTable_config that adds convenient
29 * initialization and comparison methods.
30 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031struct ConfigDescription : public android::ResTable_config {
Adam Lesinskibb94f322017-07-12 07:41:55 -070032 /**
33 * Returns an immutable default config.
34 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 static const ConfigDescription& DefaultConfig();
Adam Lesinski52364f72016-01-11 13:10:24 -080036
Adam Lesinskibb94f322017-07-12 07:41:55 -070037 /*
38 * Parse a string of the form 'fr-sw600dp-land' and fill in the
39 * given ResTable_config with resulting configuration parameters.
40 *
41 * The resulting configuration has the appropriate sdkVersion defined
42 * for backwards compatibility.
43 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080044 static bool Parse(const android::StringPiece& str, ConfigDescription* out = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinskibb94f322017-07-12 07:41:55 -070046 /**
47 * If the configuration uses an axis that was added after
48 * the original Android release, make sure the SDK version
49 * is set accordingly.
50 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 static void ApplyVersionForCompatibility(ConfigDescription* config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 ConfigDescription();
54 ConfigDescription(const android::ResTable_config& o); // NOLINT(implicit)
55 ConfigDescription(const ConfigDescription& o);
56 ConfigDescription(ConfigDescription&& o);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 ConfigDescription& operator=(const android::ResTable_config& o);
59 ConfigDescription& operator=(const ConfigDescription& o);
60 ConfigDescription& operator=(ConfigDescription&& o);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 ConfigDescription CopyWithoutSdkVersion() const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070063
Adam Lesinskib58c3ef2017-09-12 17:39:52 -070064 // Returns the BCP-47 language tag of this configuration's locale.
65 std::string GetBcp47LanguageTag(bool canonicalize = false) const;
66
Adam Lesinski93190b72017-11-03 15:20:17 -070067 std::string to_string() const;
68
Adam Lesinskibb94f322017-07-12 07:41:55 -070069 /**
70 * A configuration X dominates another configuration Y, if X has at least the
71 * precedence of Y and X is strictly more general than Y: for any type defined
72 * by X, the same type is defined by Y with a value equal to or, in the case
73 * of ranges, more specific than that of X.
74 *
75 * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It
76 * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'.
77 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 bool Dominates(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070079
Adam Lesinskibb94f322017-07-12 07:41:55 -070080 /**
81 * Returns true if this configuration defines a more important configuration
82 * parameter than o. For example, "en" has higher precedence than "v23",
83 * whereas "en" has the same precedence as "en-v23".
84 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 bool HasHigherPrecedenceThan(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070086
Adam Lesinskibb94f322017-07-12 07:41:55 -070087 /**
88 * A configuration conflicts with another configuration if both
89 * configurations define an incompatible configuration parameter. An
90 * incompatible configuration parameter is a non-range, non-density parameter
91 * that is defined in both configurations as a different, non-default value.
92 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 bool ConflictsWith(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070094
Adam Lesinskibb94f322017-07-12 07:41:55 -070095 /**
96 * A configuration is compatible with another configuration if both
97 * configurations can match a common concrete device configuration and are
98 * unrelated by domination. For example, land-v11 conflicts with port-v21
99 * but is compatible with v21 (both land-v11 and v21 would match en-land-v23).
100 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 bool IsCompatibleWith(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 bool MatchWithDensity(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 bool operator<(const ConfigDescription& o) const;
106 bool operator<=(const ConfigDescription& o) const;
107 bool operator==(const ConfigDescription& o) const;
108 bool operator!=(const ConfigDescription& o) const;
109 bool operator>=(const ConfigDescription& o) const;
110 bool operator>(const ConfigDescription& o) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111};
112
113inline ConfigDescription::ConfigDescription() {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 memset(this, 0, sizeof(*this));
115 size = sizeof(android::ResTable_config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116}
117
118inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 *static_cast<android::ResTable_config*>(this) = o;
120 size = sizeof(android::ResTable_config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121}
122
123inline ConfigDescription::ConfigDescription(const ConfigDescription& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 *static_cast<android::ResTable_config*>(this) = o;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125}
126
127inline ConfigDescription::ConfigDescription(ConfigDescription&& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 *this = o;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129}
130
Adam Lesinskibb94f322017-07-12 07:41:55 -0700131inline ConfigDescription& ConfigDescription::operator=(
132 const android::ResTable_config& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 *static_cast<android::ResTable_config*>(this) = o;
134 size = sizeof(android::ResTable_config);
135 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136}
137
Adam Lesinskibb94f322017-07-12 07:41:55 -0700138inline ConfigDescription& ConfigDescription::operator=(
139 const ConfigDescription& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 *static_cast<android::ResTable_config*>(this) = o;
141 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142}
143
144inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 *this = o;
146 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147}
148
Adam Lesinskibb94f322017-07-12 07:41:55 -0700149inline bool ConfigDescription::MatchWithDensity(
150 const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 return match(o) && (density == 0 || density == o.density);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700152}
153
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 return compare(o) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156}
157
158inline bool ConfigDescription::operator<=(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 return compare(o) <= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160}
161
162inline bool ConfigDescription::operator==(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 return compare(o) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164}
165
166inline bool ConfigDescription::operator!=(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 return compare(o) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168}
169
170inline bool ConfigDescription::operator>=(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 return compare(o) >= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172}
173
174inline bool ConfigDescription::operator>(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 return compare(o) > 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176}
177
Adam Lesinskibb94f322017-07-12 07:41:55 -0700178inline ::std::ostream& operator<<(::std::ostream& out,
179 const ConfigDescription& o) {
180 return out << o.toString().string();
181}
182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185#endif // AAPT_CONFIG_DESCRIPTION_H