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 | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 22 | #include "androidfw/ResourceTypes.h" |
| 23 | |
| 24 | #include "util/StringPiece.h" |
| 25 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | /* |
| 29 | * Subclass of ResTable_config that adds convenient |
| 30 | * initialization and comparison methods. |
| 31 | */ |
| 32 | struct ConfigDescription : public android::ResTable_config { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Returns an immutable default config. |
| 35 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 36 | static const ConfigDescription& DefaultConfig(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 45 | static bool Parse(const StringPiece& str, ConfigDescription* out = nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 52 | static void ApplyVersionForCompatibility(ConfigDescription* config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | ConfigDescription(); |
| 55 | ConfigDescription(const android::ResTable_config& o); // NOLINT(implicit) |
| 56 | ConfigDescription(const ConfigDescription& o); |
| 57 | ConfigDescription(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | ConfigDescription& operator=(const android::ResTable_config& o); |
| 60 | ConfigDescription& operator=(const ConfigDescription& o); |
| 61 | ConfigDescription& operator=(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 63 | ConfigDescription CopyWithoutSdkVersion() const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 74 | bool Dominates(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 81 | bool HasHigherPrecedenceThan(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 89 | bool ConflictsWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 97 | bool IsCompatibleWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 99 | bool MatchWithDensity(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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; |
| 106 | bool operator>(const ConfigDescription& o) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | inline ConfigDescription::ConfigDescription() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | memset(this, 0, sizeof(*this)); |
| 111 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | *static_cast<android::ResTable_config*>(this) = o; |
| 116 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | inline ConfigDescription::ConfigDescription(const ConfigDescription& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | *static_cast<android::ResTable_config*>(this) = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | inline ConfigDescription::ConfigDescription(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | *this = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | inline 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | inline ConfigDescription& ConfigDescription::operator=( |
| 135 | const ConfigDescription& o) { |
| 136 | *static_cast<android::ResTable_config*>(this) = o; |
| 137 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | *this = o; |
| 142 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 145 | inline bool ConfigDescription::MatchWithDensity( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | const ConfigDescription& o) const { |
| 147 | return match(o) && (density == 0 || density == o.density); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | inline bool ConfigDescription::operator<(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | return compare(o) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | inline bool ConfigDescription::operator<=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | return compare(o) <= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | inline bool ConfigDescription::operator==(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | return compare(o) == 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | inline bool ConfigDescription::operator!=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | return compare(o) != 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | inline bool ConfigDescription::operator>=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | return compare(o) >= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | inline bool ConfigDescription::operator>(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | return compare(o) > 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 175 | const ConfigDescription& o) { |
| 176 | return out << o.toString().string(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 180 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | #endif // AAPT_CONFIG_DESCRIPTION_H |