blob: 97d0f38a5af1e87c0bcb86128962aa6d6664a5d1 [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"
23
24#include "util/StringPiece.h"
25
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026namespace aapt {
27
28/*
29 * Subclass of ResTable_config that adds convenient
30 * initialization and comparison methods.
31 */
32struct ConfigDescription : public android::ResTable_config {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 /**
34 * Returns an immutable default config.
35 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 static const ConfigDescription& DefaultConfig();
Adam Lesinski52364f72016-01-11 13:10:24 -080037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 /*
39 * Parse a string of the form 'fr-sw600dp-land' and fill in the
40 * given ResTable_config with resulting configuration parameters.
41 *
42 * The resulting configuration has the appropriate sdkVersion defined
43 * for backwards compatibility.
44 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 static bool Parse(const StringPiece& str, ConfigDescription* out = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 /**
48 * If the configuration uses an axis that was added after
49 * the original Android release, make sure the SDK version
50 * is set accordingly.
51 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 static void ApplyVersionForCompatibility(ConfigDescription* config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 ConfigDescription();
55 ConfigDescription(const android::ResTable_config& o); // NOLINT(implicit)
56 ConfigDescription(const ConfigDescription& o);
57 ConfigDescription(ConfigDescription&& o);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 ConfigDescription& operator=(const android::ResTable_config& o);
60 ConfigDescription& operator=(const ConfigDescription& o);
61 ConfigDescription& operator=(ConfigDescription&& o);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ConfigDescription CopyWithoutSdkVersion() const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 /**
66 * A configuration X dominates another configuration Y, if X has at least the
67 * precedence of Y and X is strictly more general than Y: for any type defined
68 * by X, the same type is defined by Y with a value equal to or, in the case
69 * of ranges, more specific than that of X.
70 *
71 * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It
72 * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'.
73 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 bool Dominates(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 /**
77 * Returns true if this configuration defines a more important configuration
78 * parameter than o. For example, "en" has higher precedence than "v23",
79 * whereas "en" has the same precedence as "en-v23".
80 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 bool HasHigherPrecedenceThan(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 /**
84 * A configuration conflicts with another configuration if both
85 * configurations define an incompatible configuration parameter. An
86 * incompatible configuration parameter is a non-range, non-density parameter
87 * that is defined in both configurations as a different, non-default value.
88 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 bool ConflictsWith(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 /**
92 * A configuration is compatible with another configuration if both
93 * configurations can match a common concrete device configuration and are
94 * unrelated by domination. For example, land-v11 conflicts with port-v21
95 * but is compatible with v21 (both land-v11 and v21 would match en-land-v23).
96 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool IsCompatibleWith(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070098
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 bool MatchWithDensity(const ConfigDescription& o) const;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 bool operator<(const ConfigDescription& o) const;
102 bool operator<=(const ConfigDescription& o) const;
103 bool operator==(const ConfigDescription& o) const;
104 bool operator!=(const ConfigDescription& o) const;
105 bool operator>=(const ConfigDescription& o) const;
106 bool operator>(const ConfigDescription& o) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107};
108
109inline ConfigDescription::ConfigDescription() {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 memset(this, 0, sizeof(*this));
111 size = sizeof(android::ResTable_config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112}
113
114inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 *static_cast<android::ResTable_config*>(this) = o;
116 size = sizeof(android::ResTable_config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117}
118
119inline ConfigDescription::ConfigDescription(const ConfigDescription& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 *static_cast<android::ResTable_config*>(this) = o;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121}
122
123inline ConfigDescription::ConfigDescription(ConfigDescription&& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 *this = o;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125}
126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127inline ConfigDescription& ConfigDescription::operator=(
128 const android::ResTable_config& o) {
129 *static_cast<android::ResTable_config*>(this) = o;
130 size = sizeof(android::ResTable_config);
131 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132}
133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134inline ConfigDescription& ConfigDescription::operator=(
135 const ConfigDescription& o) {
136 *static_cast<android::ResTable_config*>(this) = o;
137 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138}
139
140inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 *this = o;
142 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143}
144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145inline bool ConfigDescription::MatchWithDensity(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 const ConfigDescription& o) const {
147 return match(o) && (density == 0 || density == o.density);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700148}
149
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 return compare(o) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152}
153
154inline 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
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174inline ::std::ostream& operator<<(::std::ostream& out,
175 const ConfigDescription& o) {
176 return out << o.toString().string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177}
178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181#endif // AAPT_CONFIG_DESCRIPTION_H