ResourceFilter.cpp revision a5cc002bfe7ca8a6536549189b44e7143c8611dc
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 11status_t 12WeakResourceFilter::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 mConfigMask |= entry.second; 45 } 46 47 return NO_ERROR; 48} 49 50bool 51WeakResourceFilter::match(const ResTable_config& config) const 52{ 53 uint32_t mask = mDefault.diff(config); 54 if ((mConfigMask & mask) == 0) { 55 // The two configurations don't have any common axis. 56 return true; 57 } 58 59 uint32_t matchedAxis = 0x0; 60 const size_t N = mConfigs.size(); 61 for (size_t i = 0; i < N; i++) { 62 const std::pair<ConfigDescription, uint32_t>& entry = mConfigs[i]; 63 uint32_t diff = entry.first.diff(config); 64 if ((diff & entry.second) == 0) { 65 // Mark the axis that was matched. 66 matchedAxis |= entry.second; 67 } else if ((diff & entry.second) == ResTable_config::CONFIG_LOCALE) { 68 // If the locales differ, but the languages are the same and 69 // the locale we are matching only has a language specified, 70 // we match. 71 if (config.language[0] && 72 memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) { 73 if (config.country[0] == 0) { 74 matchedAxis |= ResTable_config::CONFIG_LOCALE; 75 } 76 } 77 } else if ((diff & entry.second) == ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) { 78 // Special case if the smallest screen width doesn't match. We check that the 79 // config being matched has a smaller screen width than the filter specified. 80 if (config.smallestScreenWidthDp != 0 && 81 config.smallestScreenWidthDp < entry.first.smallestScreenWidthDp) { 82 matchedAxis |= ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE; 83 } 84 } 85 } 86 return matchedAxis == (mConfigMask & mask); 87} 88 89status_t 90StrongResourceFilter::parse(const String8& str) { 91 Vector<String8> configStrs = AaptUtil::split(str, ','); 92 ConfigDescription config; 93 mConfigs.clear(); 94 for (size_t i = 0; i < configStrs.size(); i++) { 95 if (!AaptConfig::parse(configStrs[i], &config)) { 96 fprintf(stderr, "Invalid configuration: %s\n", configStrs[i].string()); 97 return UNKNOWN_ERROR; 98 } 99 mConfigs.insert(config); 100 } 101 return NO_ERROR; 102} 103