2 // Copyright 2014 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "ResourceFilter.h"
9 #include "AaptConfig.h"
12 WeakResourceFilter::parse(const String8& str)
14 Vector<String8> configStrs = AaptUtil::split(str, ',');
15 const size_t N = configStrs.size();
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;
27 std::pair<ConfigDescription, uint32_t>& entry = mConfigs.editItemAt(i);
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());
39 entry.second = mDefault.diff(entry.first);
42 entry.second &= ~ResTable_config::CONFIG_VERSION;
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;
51 mConfigMask |= entry.second;
58 WeakResourceFilter::match(const ResTable_config& config) const
60 uint32_t mask = mDefault.diff(config);
61 if ((mConfigMask & mask) == 0) {
62 // The two configurations don't have any common axis.
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,
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;
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;
93 return matchedAxis == (mConfigMask & mask);
97 StrongResourceFilter::parse(const String8& str) {
98 Vector<String8> configStrs = AaptUtil::split(str, ',');
99 ConfigDescription config;
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;
106 mConfigs.insert(config);