Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 AAPT_CONFIG_DESCRIPTION_H |
| 18 | #define AAPT_CONFIG_DESCRIPTION_H |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | |
| 22 | #include <androidfw/ResourceTypes.h> |
| 23 | #include <ostream> |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | /* |
| 28 | * Subclass of ResTable_config that adds convenient |
| 29 | * initialization and comparison methods. |
| 30 | */ |
| 31 | struct ConfigDescription : public android::ResTable_config { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 32 | /** |
| 33 | * Returns an immutable default config. |
| 34 | */ |
| 35 | static const ConfigDescription& defaultConfig(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 37 | /* |
| 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 | */ |
| 44 | static bool parse(const StringPiece& str, ConfigDescription* out = nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 46 | /** |
| 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 | */ |
| 51 | static void applyVersionForCompatibility(ConfigDescription* config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | ConfigDescription(); |
| 54 | ConfigDescription(const android::ResTable_config& o); // NOLINT(implicit) |
| 55 | ConfigDescription(const ConfigDescription& o); |
| 56 | ConfigDescription(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | ConfigDescription& operator=(const android::ResTable_config& o); |
| 59 | ConfigDescription& operator=(const ConfigDescription& o); |
| 60 | ConfigDescription& operator=(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 62 | ConfigDescription copyWithoutSdkVersion() const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 64 | /** |
| 65 | * A configuration X dominates another configuration Y, if X has at least the |
| 66 | * precedence of Y and X is strictly more general than Y: for any type defined |
| 67 | * by X, the same type is defined by Y with a value equal to or, in the case |
| 68 | * of ranges, more specific than that of X. |
| 69 | * |
| 70 | * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It |
| 71 | * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'. |
| 72 | */ |
| 73 | bool dominates(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 75 | /** |
| 76 | * Returns true if this configuration defines a more important configuration |
| 77 | * parameter than o. For example, "en" has higher precedence than "v23", |
| 78 | * whereas "en" has the same precedence as "en-v23". |
| 79 | */ |
| 80 | bool hasHigherPrecedenceThan(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 82 | /** |
| 83 | * A configuration conflicts with another configuration if both |
| 84 | * configurations define an incompatible configuration parameter. An |
| 85 | * incompatible configuration parameter is a non-range, non-density parameter |
| 86 | * that is defined in both configurations as a different, non-default value. |
| 87 | */ |
| 88 | bool conflictsWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | /** |
| 91 | * A configuration is compatible with another configuration if both |
| 92 | * configurations can match a common concrete device configuration and are |
| 93 | * unrelated by domination. For example, land-v11 conflicts with port-v21 |
| 94 | * but is compatible with v21 (both land-v11 and v21 would match en-land-v23). |
| 95 | */ |
| 96 | bool isCompatibleWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 98 | bool matchWithDensity(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 100 | bool operator<(const ConfigDescription& o) const; |
| 101 | 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; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | inline ConfigDescription::ConfigDescription() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 109 | memset(this, 0, sizeof(*this)); |
| 110 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 114 | *static_cast<android::ResTable_config*>(this) = o; |
| 115 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | inline ConfigDescription::ConfigDescription(const ConfigDescription& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 119 | *static_cast<android::ResTable_config*>(this) = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | inline ConfigDescription::ConfigDescription(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 123 | *this = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 126 | inline ConfigDescription& ConfigDescription::operator=( |
| 127 | const android::ResTable_config& o) { |
| 128 | *static_cast<android::ResTable_config*>(this) = o; |
| 129 | size = sizeof(android::ResTable_config); |
| 130 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 133 | inline ConfigDescription& ConfigDescription::operator=( |
| 134 | const ConfigDescription& o) { |
| 135 | *static_cast<android::ResTable_config*>(this) = o; |
| 136 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 140 | *this = o; |
| 141 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 144 | inline bool ConfigDescription::matchWithDensity( |
| 145 | const ConfigDescription& o) const { |
| 146 | return match(o) && (density == 0 || density == o.density); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 149 | inline bool ConfigDescription::operator<(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 150 | return compare(o) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inline bool ConfigDescription::operator<=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 154 | return compare(o) <= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | inline bool ConfigDescription::operator==(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 158 | return compare(o) == 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | inline bool ConfigDescription::operator!=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 162 | return compare(o) != 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | inline bool ConfigDescription::operator>=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 166 | return compare(o) >= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | inline bool ConfigDescription::operator>(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 170 | return compare(o) > 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 173 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 174 | const ConfigDescription& o) { |
| 175 | return out << o.toString().string(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 178 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 179 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 180 | #endif // AAPT_CONFIG_DESCRIPTION_H |