Home | History | Annotate | Download | only in aapt
      1 //
      2 // Copyright 2014 The Android Open Source Project
      3 //
      4 // Build resource files from raw assets.
      5 //
      6 
      7 #include "ResourceFilter.h"
      8 #include "AaptUtil.h"
      9 #include "AaptConfig.h"
     10 
     11 status_t
     12 WeakResourceFilter::parse(const String8& str)
     13 {
     14     Vector<String8> configStrs = AaptUtil::split(str, ',');
     15     const size_t N = configStrs.size();
     16     mConfigs.clear();
     17     mConfigMask = 0;
     18     mConfigs.resize(N);
     19     for (size_t i = 0; i < N; i++) {
     20         const String8& part = configStrs[i];
     21         if (part == "en_XA") {
     22             mContainsPseudoAccented = true;
     23         } else if (part == "ar_XB") {
     24             mContainsPseudoBidi = true;
     25         }
     26 
     27         std::pair<ConfigDescription, uint32_t>& entry = mConfigs.editItemAt(i);
     28 
     29         AaptLocaleValue val;
     30         if (val.initFromFilterString(part)) {
     31             // For backwards compatibility, we accept configurations that
     32             // only specify locale in the standard 'en_US' format.
     33             val.writeTo(&entry.first);
     34         } else if (!AaptConfig::parse(part, &entry.first)) {
     35             fprintf(stderr, "Invalid configuration: %s\n", part.string());
     36             return UNKNOWN_ERROR;
     37         }
     38 
     39         entry.second = mDefault.diff(entry.first);
     40 
     41         // Ignore the version
     42         entry.second &= ~ResTable_config::CONFIG_VERSION;
     43 
     44         // Ignore any densities. Those are best handled in --preferred-density
     45         if ((entry.second & ResTable_config::CONFIG_DENSITY) != 0) {
     46             fprintf(stderr, "warning: ignoring flag -c %s. Use --preferred-density instead.\n", entry.first.toString().string());
     47             entry.first.density = 0;
     48             entry.second &= ~ResTable_config::CONFIG_DENSITY;
     49         }
     50 
     51         mConfigMask |= entry.second;
     52     }
     53 
     54     return NO_ERROR;
     55 }
     56 
     57 bool
     58 WeakResourceFilter::match(const ResTable_config& config) const
     59 {
     60     uint32_t mask = mDefault.diff(config);
     61     if ((mConfigMask & mask) == 0) {
     62         // The two configurations don't have any common axis.
     63         return true;
     64     }
     65 
     66     uint32_t matchedAxis = 0x0;
     67     const size_t N = mConfigs.size();
     68     for (size_t i = 0; i < N; i++) {
     69         const std::pair<ConfigDescription, uint32_t>& entry = mConfigs[i];
     70         uint32_t diff = entry.first.diff(config);
     71         if ((diff & entry.second) == 0) {
     72             // Mark the axis that was matched.
     73             matchedAxis |= entry.second;
     74         } else if ((diff & entry.second) == ResTable_config::CONFIG_LOCALE) {
     75             // If the locales differ, but the languages are the same and
     76             // the locale we are matching only has a language specified,
     77             // we match.
     78             if (config.language[0] &&
     79                     memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) {
     80                 if (config.country[0] == 0) {
     81                     matchedAxis |= ResTable_config::CONFIG_LOCALE;
     82                 }
     83             }
     84         } else if ((diff & entry.second) == ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) {
     85             // Special case if the smallest screen width doesn't match. We check that the
     86             // config being matched has a smaller screen width than the filter specified.
     87             if (config.smallestScreenWidthDp != 0 &&
     88                     config.smallestScreenWidthDp < entry.first.smallestScreenWidthDp) {
     89                 matchedAxis |= ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE;
     90             }
     91         }
     92     }
     93     return matchedAxis == (mConfigMask & mask);
     94 }
     95 
     96 status_t
     97 StrongResourceFilter::parse(const String8& str) {
     98     Vector<String8> configStrs = AaptUtil::split(str, ',');
     99     ConfigDescription config;
    100     mConfigs.clear();
    101     for (size_t i = 0; i < configStrs.size(); i++) {
    102         if (!AaptConfig::parse(configStrs[i], &config)) {
    103             fprintf(stderr, "Invalid configuration: %s\n", configStrs[i].string());
    104             return UNKNOWN_ERROR;
    105         }
    106         mConfigs.insert(config);
    107     }
    108     return NO_ERROR;
    109 }
    110