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