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 | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 27 | // Subclass of ResTable_config that adds convenient |
| 28 | // initialization and comparison methods. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | struct ConfigDescription : public android::ResTable_config { |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 30 | // Returns an immutable default config. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | static const ConfigDescription& DefaultConfig(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 32 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 33 | // Parse a string of the form 'fr-sw600dp-land' and fill in the |
| 34 | // given ResTable_config with resulting configuration parameters. |
| 35 | // |
| 36 | // The resulting configuration has the appropriate sdkVersion defined |
| 37 | // for backwards compatibility. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 38 | static bool Parse(const android::StringPiece& str, ConfigDescription* out = nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 40 | // If the configuration uses an axis that was added after |
| 41 | // the original Android release, make sure the SDK version |
| 42 | // is set accordingly. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | static void ApplyVersionForCompatibility(ConfigDescription* config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | ConfigDescription(); |
| 46 | ConfigDescription(const android::ResTable_config& o); // NOLINT(implicit) |
| 47 | ConfigDescription(const ConfigDescription& o); |
| 48 | ConfigDescription(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | ConfigDescription& operator=(const android::ResTable_config& o); |
| 51 | ConfigDescription& operator=(const ConfigDescription& o); |
| 52 | ConfigDescription& operator=(ConfigDescription&& o); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | ConfigDescription CopyWithoutSdkVersion() const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 56 | // A configuration X dominates another configuration Y, if X has at least the |
| 57 | // precedence of Y and X is strictly more general than Y: for any type defined |
| 58 | // by X, the same type is defined by Y with a value equal to or, in the case |
| 59 | // of ranges, more specific than that of X. |
| 60 | // |
| 61 | // For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It |
| 62 | // does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | bool Dominates(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 65 | // Returns true if this configuration defines a more important configuration |
| 66 | // parameter than o. For example, "en" has higher precedence than "v23", |
| 67 | // whereas "en" has the same precedence as "en-v23". |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | bool HasHigherPrecedenceThan(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 70 | // A configuration conflicts with another configuration if both |
| 71 | // configurations define an incompatible configuration parameter. An |
| 72 | // incompatible configuration parameter is a non-range, non-density parameter |
| 73 | // that is defined in both configurations as a different, non-default value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | bool ConflictsWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 76 | // A configuration is compatible with another configuration if both |
| 77 | // configurations can match a common concrete device configuration and are |
| 78 | // unrelated by domination. For example, land-v11 conflicts with port-v21 |
| 79 | // but is compatible with v21 (both land-v11 and v21 would match en-land-v23). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | bool IsCompatibleWith(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | bool MatchWithDensity(const ConfigDescription& o) const; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | bool operator<(const ConfigDescription& o) const; |
| 85 | bool operator<=(const ConfigDescription& o) const; |
| 86 | bool operator==(const ConfigDescription& o) const; |
| 87 | bool operator!=(const ConfigDescription& o) const; |
| 88 | bool operator>=(const ConfigDescription& o) const; |
| 89 | bool operator>(const ConfigDescription& o) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 92 | ::std::ostream& operator<<(::std::ostream& out, const ConfigDescription& o); |
| 93 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | inline ConfigDescription::ConfigDescription() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | memset(this, 0, sizeof(*this)); |
| 96 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | *static_cast<android::ResTable_config*>(this) = o; |
| 101 | size = sizeof(android::ResTable_config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | inline ConfigDescription::ConfigDescription(const ConfigDescription& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | *static_cast<android::ResTable_config*>(this) = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | inline ConfigDescription::ConfigDescription(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | *this = o; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 112 | inline ConfigDescription& ConfigDescription::operator=(const android::ResTable_config& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | *static_cast<android::ResTable_config*>(this) = o; |
| 114 | size = sizeof(android::ResTable_config); |
| 115 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 118 | inline ConfigDescription& ConfigDescription::operator=(const ConfigDescription& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | *static_cast<android::ResTable_config*>(this) = o; |
| 120 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | *this = o; |
| 125 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Adam Lesinski | 5b7337f | 2017-06-26 14:57:22 -0700 | [diff] [blame] | 128 | inline bool ConfigDescription::MatchWithDensity(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | return match(o) && (density == 0 || density == o.density); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 132 | inline bool ConfigDescription::operator<(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | return compare(o) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | inline bool ConfigDescription::operator<=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | return compare(o) <= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | inline bool ConfigDescription::operator==(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | return compare(o) == 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | inline bool ConfigDescription::operator!=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | return compare(o) != 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | inline bool ConfigDescription::operator>=(const ConfigDescription& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | return compare(o) >= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | #endif // AAPT_CONFIG_DESCRIPTION_H |