1 /*
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 ANDROIDFW_CONFIG_DESCRIPTION_H
18 #define ANDROIDFW_CONFIG_DESCRIPTION_H
19
20 #include <ostream>
21
22 #include "androidfw/ResourceTypes.h"
23 #include "androidfw/StringPiece.h"
24
25 namespace android {
26
27 using ApiVersion = int;
28
29 enum : ApiVersion {
30 SDK_CUPCAKE = 3,
31 SDK_DONUT = 4,
32 SDK_ECLAIR = 5,
33 SDK_ECLAIR_0_1 = 6,
34 SDK_ECLAIR_MR1 = 7,
35 SDK_FROYO = 8,
36 SDK_GINGERBREAD = 9,
37 SDK_GINGERBREAD_MR1 = 10,
38 SDK_HONEYCOMB = 11,
39 SDK_HONEYCOMB_MR1 = 12,
40 SDK_HONEYCOMB_MR2 = 13,
41 SDK_ICE_CREAM_SANDWICH = 14,
42 SDK_ICE_CREAM_SANDWICH_MR1 = 15,
43 SDK_JELLY_BEAN = 16,
44 SDK_JELLY_BEAN_MR1 = 17,
45 SDK_JELLY_BEAN_MR2 = 18,
46 SDK_KITKAT = 19,
47 SDK_KITKAT_WATCH = 20,
48 SDK_LOLLIPOP = 21,
49 SDK_LOLLIPOP_MR1 = 22,
50 SDK_MARSHMALLOW = 23,
51 SDK_NOUGAT = 24,
52 SDK_NOUGAT_MR1 = 25,
53 SDK_O = 26,
54 SDK_O_MR1 = 27,
55 SDK_P = 28,
56 SDK_Q = 29,
57 SDK_R = 30,
58 SDK_S = 31,
59 SDK_S_V2 = 32,
60 SDK_TIRAMISU = 33,
61 SDK_U = 34,
62 };
63
64 /*
65 * Subclass of ResTable_config that adds convenient
66 * initialization and comparison methods.
67 */
68 struct ConfigDescription : public ResTable_config {
69 /**
70 * Returns an immutable default config.
71 */
72 static const ConfigDescription& DefaultConfig();
73
74 /*
75 * Parse a string of the form 'fr-sw600dp-land' and fill in the
76 * given ResTable_config with resulting configuration parameters.
77 *
78 * The resulting configuration has the appropriate sdkVersion defined
79 * for backwards compatibility.
80 */
81 static bool Parse(android::StringPiece str, ConfigDescription* out = nullptr);
82
83 /**
84 * If the configuration uses an axis that was added after
85 * the original Android release, make sure the SDK version
86 * is set accordingly.
87 */
88 static void ApplyVersionForCompatibility(ConfigDescription* config);
89
90 ConfigDescription();
91 ConfigDescription(const android::ResTable_config& o); // NOLINT(google-explicit-constructor)
92 ConfigDescription(const ConfigDescription& o);
93 ConfigDescription(ConfigDescription&& o) noexcept;
94
95 ConfigDescription& operator=(const android::ResTable_config& o);
96 ConfigDescription& operator=(const ConfigDescription& o);
97 ConfigDescription& operator=(ConfigDescription&& o) noexcept;
98
99 ConfigDescription CopyWithoutSdkVersion() const;
100
101 // Returns the BCP-47 language tag of this configuration's locale.
102 std::string GetBcp47LanguageTag(bool canonicalize = false) const;
103
104 std::string to_string() const;
105
106 /**
107 * A configuration X dominates another configuration Y, if X has at least the
108 * precedence of Y and X is strictly more general than Y: for any type defined
109 * by X, the same type is defined by Y with a value equal to or, in the case
110 * of ranges, more specific than that of X.
111 *
112 * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It
113 * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'.
114 */
115 bool Dominates(const ConfigDescription& o) const;
116
117 /**
118 * Returns true if this configuration defines a more important configuration
119 * parameter than o. For example, "en" has higher precedence than "v23",
120 * whereas "en" has the same precedence as "en-v23".
121 */
122 bool HasHigherPrecedenceThan(const ConfigDescription& o) const;
123
124 /**
125 * A configuration conflicts with another configuration if both
126 * configurations define an incompatible configuration parameter. An
127 * incompatible configuration parameter is a non-range, non-density parameter
128 * that is defined in both configurations as a different, non-default value.
129 */
130 bool ConflictsWith(const ConfigDescription& o) const;
131
132 /**
133 * A configuration is compatible with another configuration if both
134 * configurations can match a common concrete device configuration and are
135 * unrelated by domination. For example, land-v11 conflicts with port-v21
136 * but is compatible with v21 (both land-v11 and v21 would match en-land-v23).
137 */
138 bool IsCompatibleWith(const ConfigDescription& o) const;
139
140 bool MatchWithDensity(const ConfigDescription& o) const;
141
142 bool operator<(const ConfigDescription& o) const;
143 bool operator<=(const ConfigDescription& o) const;
144 bool operator==(const ConfigDescription& o) const;
145 bool operator!=(const ConfigDescription& o) const;
146 bool operator>=(const ConfigDescription& o) const;
147 bool operator>(const ConfigDescription& o) const;
148 };
149
ConfigDescription()150 inline ConfigDescription::ConfigDescription() {
151 memset(this, 0, sizeof(*this));
152 size = sizeof(android::ResTable_config);
153 }
154
ConfigDescription(const android::ResTable_config & o)155 inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
156 *static_cast<android::ResTable_config*>(this) = o;
157 size = sizeof(android::ResTable_config);
158 }
159
ConfigDescription(const ConfigDescription & o)160 inline ConfigDescription::ConfigDescription(const ConfigDescription& o)
161 : android::ResTable_config(o) {
162 }
163
ConfigDescription(ConfigDescription && o)164 inline ConfigDescription::ConfigDescription(ConfigDescription&& o) noexcept {
165 *this = o;
166 }
167
168 inline ConfigDescription& ConfigDescription::operator=(
169 const android::ResTable_config& o) {
170 *static_cast<android::ResTable_config*>(this) = o;
171 size = sizeof(android::ResTable_config);
172 return *this;
173 }
174
175 inline ConfigDescription& ConfigDescription::operator=(
176 const ConfigDescription& o) {
177 *static_cast<android::ResTable_config*>(this) = o;
178 return *this;
179 }
180
181 inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) noexcept {
182 *this = o;
183 return *this;
184 }
185
MatchWithDensity(const ConfigDescription & o)186 inline bool ConfigDescription::MatchWithDensity(const ConfigDescription& o) const {
187 return match(o) && (density == 0 || o.density != 0);
188 }
189
190 inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
191 return compare(o) < 0;
192 }
193
194 inline bool ConfigDescription::operator<=(const ConfigDescription& o) const {
195 return compare(o) <= 0;
196 }
197
198 inline bool ConfigDescription::operator==(const ConfigDescription& o) const {
199 return compare(o) == 0;
200 }
201
202 inline bool ConfigDescription::operator!=(const ConfigDescription& o) const {
203 return compare(o) != 0;
204 }
205
206 inline bool ConfigDescription::operator>=(const ConfigDescription& o) const {
207 return compare(o) >= 0;
208 }
209
210 inline bool ConfigDescription::operator>(const ConfigDescription& o) const {
211 return compare(o) > 0;
212 }
213
214 inline ::std::ostream& operator<<(::std::ostream& out,
215 const ConfigDescription& o) {
216 return out << o.toString().c_str();
217 }
218
219 } // namespace android
220
221 #endif // ANDROIDFW_CONFIG_DESCRIPTION_H
222