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 };
57
58 /*
59 * Subclass of ResTable_config that adds convenient
60 * initialization and comparison methods.
61 */
62 struct ConfigDescription : public ResTable_config {
63 /**
64 * Returns an immutable default config.
65 */
66 static const ConfigDescription& DefaultConfig();
67
68 /*
69 * Parse a string of the form 'fr-sw600dp-land' and fill in the
70 * given ResTable_config with resulting configuration parameters.
71 *
72 * The resulting configuration has the appropriate sdkVersion defined
73 * for backwards compatibility.
74 */
75 static bool Parse(const android::StringPiece& str, ConfigDescription* out = nullptr);
76
77 /**
78 * If the configuration uses an axis that was added after
79 * the original Android release, make sure the SDK version
80 * is set accordingly.
81 */
82 static void ApplyVersionForCompatibility(ConfigDescription* config);
83
84 ConfigDescription();
85 ConfigDescription(const android::ResTable_config& o); // NOLINT(google-explicit-constructor)
86 ConfigDescription(const ConfigDescription& o);
87 ConfigDescription(ConfigDescription&& o) noexcept;
88
89 ConfigDescription& operator=(const android::ResTable_config& o);
90 ConfigDescription& operator=(const ConfigDescription& o);
91 ConfigDescription& operator=(ConfigDescription&& o) noexcept;
92
93 ConfigDescription CopyWithoutSdkVersion() const;
94
95 // Returns the BCP-47 language tag of this configuration's locale.
96 std::string GetBcp47LanguageTag(bool canonicalize = false) const;
97
98 std::string to_string() const;
99
100 /**
101 * A configuration X dominates another configuration Y, if X has at least the
102 * precedence of Y and X is strictly more general than Y: for any type defined
103 * by X, the same type is defined by Y with a value equal to or, in the case
104 * of ranges, more specific than that of X.
105 *
106 * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It
107 * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'.
108 */
109 bool Dominates(const ConfigDescription& o) const;
110
111 /**
112 * Returns true if this configuration defines a more important configuration
113 * parameter than o. For example, "en" has higher precedence than "v23",
114 * whereas "en" has the same precedence as "en-v23".
115 */
116 bool HasHigherPrecedenceThan(const ConfigDescription& o) const;
117
118 /**
119 * A configuration conflicts with another configuration if both
120 * configurations define an incompatible configuration parameter. An
121 * incompatible configuration parameter is a non-range, non-density parameter
122 * that is defined in both configurations as a different, non-default value.
123 */
124 bool ConflictsWith(const ConfigDescription& o) const;
125
126 /**
127 * A configuration is compatible with another configuration if both
128 * configurations can match a common concrete device configuration and are
129 * unrelated by domination. For example, land-v11 conflicts with port-v21
130 * but is compatible with v21 (both land-v11 and v21 would match en-land-v23).
131 */
132 bool IsCompatibleWith(const ConfigDescription& o) const;
133
134 bool MatchWithDensity(const ConfigDescription& o) const;
135
136 bool operator<(const ConfigDescription& o) const;
137 bool operator<=(const ConfigDescription& o) const;
138 bool operator==(const ConfigDescription& o) const;
139 bool operator!=(const ConfigDescription& o) const;
140 bool operator>=(const ConfigDescription& o) const;
141 bool operator>(const ConfigDescription& o) const;
142 };
143
ConfigDescription()144 inline ConfigDescription::ConfigDescription() {
145 memset(this, 0, sizeof(*this));
146 size = sizeof(android::ResTable_config);
147 }
148
ConfigDescription(const android::ResTable_config & o)149 inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
150 *static_cast<android::ResTable_config*>(this) = o;
151 size = sizeof(android::ResTable_config);
152 }
153
ConfigDescription(const ConfigDescription & o)154 inline ConfigDescription::ConfigDescription(const ConfigDescription& o)
155 : android::ResTable_config(o) {
156 }
157
ConfigDescription(ConfigDescription && o)158 inline ConfigDescription::ConfigDescription(ConfigDescription&& o) noexcept {
159 *this = o;
160 }
161
162 inline ConfigDescription& ConfigDescription::operator=(
163 const android::ResTable_config& o) {
164 *static_cast<android::ResTable_config*>(this) = o;
165 size = sizeof(android::ResTable_config);
166 return *this;
167 }
168
169 inline ConfigDescription& ConfigDescription::operator=(
170 const ConfigDescription& o) {
171 *static_cast<android::ResTable_config*>(this) = o;
172 return *this;
173 }
174
175 inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) noexcept {
176 *this = o;
177 return *this;
178 }
179
MatchWithDensity(const ConfigDescription & o)180 inline bool ConfigDescription::MatchWithDensity(
181 const ConfigDescription& o) const {
182 return match(o) && (density == 0 || density == o.density);
183 }
184
185 inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
186 return compare(o) < 0;
187 }
188
189 inline bool ConfigDescription::operator<=(const ConfigDescription& o) const {
190 return compare(o) <= 0;
191 }
192
193 inline bool ConfigDescription::operator==(const ConfigDescription& o) const {
194 return compare(o) == 0;
195 }
196
197 inline bool ConfigDescription::operator!=(const ConfigDescription& o) const {
198 return compare(o) != 0;
199 }
200
201 inline bool ConfigDescription::operator>=(const ConfigDescription& o) const {
202 return compare(o) >= 0;
203 }
204
205 inline bool ConfigDescription::operator>(const ConfigDescription& o) const {
206 return compare(o) > 0;
207 }
208
209 inline ::std::ostream& operator<<(::std::ostream& out,
210 const ConfigDescription& o) {
211 return out << o.toString().string();
212 }
213
214 } // namespace android
215
216 #endif // ANDROIDFW_CONFIG_DESCRIPTION_H
217