Home | History | Annotate | Download | only in aapt2
      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 
     20 #include <ostream>
     21 
     22 #include "androidfw/ResourceTypes.h"
     23 #include "androidfw/StringPiece.h"
     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 {
     32   /**
     33    * Returns an immutable default config.
     34    */
     35   static const ConfigDescription& DefaultConfig();
     36 
     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 android::StringPiece& str, ConfigDescription* out = nullptr);
     45 
     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);
     52 
     53   ConfigDescription();
     54   ConfigDescription(const android::ResTable_config& o);  // NOLINT(implicit)
     55   ConfigDescription(const ConfigDescription& o);
     56   ConfigDescription(ConfigDescription&& o);
     57 
     58   ConfigDescription& operator=(const android::ResTable_config& o);
     59   ConfigDescription& operator=(const ConfigDescription& o);
     60   ConfigDescription& operator=(ConfigDescription&& o);
     61 
     62   ConfigDescription CopyWithoutSdkVersion() const;
     63 
     64   // Returns the BCP-47 language tag of this configuration's locale.
     65   std::string GetBcp47LanguageTag(bool canonicalize = false) const;
     66 
     67   std::string to_string() const;
     68 
     69   /**
     70    * A configuration X dominates another configuration Y, if X has at least the
     71    * precedence of Y and X is strictly more general than Y: for any type defined
     72    * by X, the same type is defined by Y with a value equal to or, in the case
     73    * of ranges, more specific than that of X.
     74    *
     75    * For example, the configuration 'en-w800dp' dominates 'en-rGB-w1024dp'. It
     76    * does not dominate 'fr', 'en-w720dp', or 'mcc001-en-w800dp'.
     77    */
     78   bool Dominates(const ConfigDescription& o) const;
     79 
     80   /**
     81    * Returns true if this configuration defines a more important configuration
     82    * parameter than o. For example, "en" has higher precedence than "v23",
     83    * whereas "en" has the same precedence as "en-v23".
     84    */
     85   bool HasHigherPrecedenceThan(const ConfigDescription& o) const;
     86 
     87   /**
     88    * A configuration conflicts with another configuration if both
     89    * configurations define an incompatible configuration parameter. An
     90    * incompatible configuration parameter is a non-range, non-density parameter
     91    * that is defined in both configurations as a different, non-default value.
     92    */
     93   bool ConflictsWith(const ConfigDescription& o) const;
     94 
     95   /**
     96    * A configuration is compatible with another configuration if both
     97    * configurations can match a common concrete device configuration and are
     98    * unrelated by domination. For example, land-v11 conflicts with port-v21
     99    * but is compatible with v21 (both land-v11 and v21 would match en-land-v23).
    100    */
    101   bool IsCompatibleWith(const ConfigDescription& o) const;
    102 
    103   bool MatchWithDensity(const ConfigDescription& o) const;
    104 
    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;
    109   bool operator>=(const ConfigDescription& o) const;
    110   bool operator>(const ConfigDescription& o) const;
    111 };
    112 
    113 inline ConfigDescription::ConfigDescription() {
    114   memset(this, 0, sizeof(*this));
    115   size = sizeof(android::ResTable_config);
    116 }
    117 
    118 inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
    119   *static_cast<android::ResTable_config*>(this) = o;
    120   size = sizeof(android::ResTable_config);
    121 }
    122 
    123 inline ConfigDescription::ConfigDescription(const ConfigDescription& o) {
    124   *static_cast<android::ResTable_config*>(this) = o;
    125 }
    126 
    127 inline ConfigDescription::ConfigDescription(ConfigDescription&& o) {
    128   *this = o;
    129 }
    130 
    131 inline ConfigDescription& ConfigDescription::operator=(
    132     const android::ResTable_config& o) {
    133   *static_cast<android::ResTable_config*>(this) = o;
    134   size = sizeof(android::ResTable_config);
    135   return *this;
    136 }
    137 
    138 inline ConfigDescription& ConfigDescription::operator=(
    139     const ConfigDescription& o) {
    140   *static_cast<android::ResTable_config*>(this) = o;
    141   return *this;
    142 }
    143 
    144 inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) {
    145   *this = o;
    146   return *this;
    147 }
    148 
    149 inline bool ConfigDescription::MatchWithDensity(
    150     const ConfigDescription& o) const {
    151   return match(o) && (density == 0 || density == o.density);
    152 }
    153 
    154 inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
    155   return compare(o) < 0;
    156 }
    157 
    158 inline bool ConfigDescription::operator<=(const ConfigDescription& o) const {
    159   return compare(o) <= 0;
    160 }
    161 
    162 inline bool ConfigDescription::operator==(const ConfigDescription& o) const {
    163   return compare(o) == 0;
    164 }
    165 
    166 inline bool ConfigDescription::operator!=(const ConfigDescription& o) const {
    167   return compare(o) != 0;
    168 }
    169 
    170 inline bool ConfigDescription::operator>=(const ConfigDescription& o) const {
    171   return compare(o) >= 0;
    172 }
    173 
    174 inline bool ConfigDescription::operator>(const ConfigDescription& o) const {
    175   return compare(o) > 0;
    176 }
    177 
    178 inline ::std::ostream& operator<<(::std::ostream& out,
    179                                   const ConfigDescription& o) {
    180   return out << o.toString().string();
    181 }
    182 
    183 }  // namespace aapt
    184 
    185 #endif  // AAPT_CONFIG_DESCRIPTION_H
    186