Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 1 | // |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 2 | // Copyright 2014 The Android Open Source Project |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 3 | // |
| 4 | // Build resource files from raw assets. |
| 5 | // |
| 6 | |
| 7 | #include "ResourceFilter.h" |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 8 | #include "AaptUtil.h" |
| 9 | #include "AaptConfig.h" |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 10 | |
| 11 | status_t |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 12 | WeakResourceFilter::parse(const String8& str) |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 13 | { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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]; |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 21 | if (part == "en_XA") { |
| 22 | mContainsPseudoAccented = true; |
| 23 | } else if (part == "ar_XB") { |
| 24 | mContainsPseudoBidi = true; |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 25 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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; |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 39 | entry.second = mDefault.diff(entry.first); |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 40 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 41 | // Ignore the version |
| 42 | entry.second &= ~ResTable_config::CONFIG_VERSION; |
| 43 | |
Adam Lesinski | a2bb565 | 2014-11-10 17:56:11 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 51 | mConfigMask |= entry.second; |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return NO_ERROR; |
| 55 | } |
| 56 | |
| 57 | bool |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 58 | WeakResourceFilter::match(const ResTable_config& config) const |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 59 | { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 60 | uint32_t mask = mDefault.diff(config); |
| 61 | if ((mConfigMask & mask) == 0) { |
| 62 | // The two configurations don't have any common axis. |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 63 | return true; |
| 64 | } |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 66 | uint32_t matchedAxis = 0x0; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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) { |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 72 | // Mark the axis that was matched. |
| 73 | matchedAxis |= entry.second; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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. |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 78 | if (config.language[0] && |
| 79 | memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 80 | if (config.country[0] == 0) { |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 81 | matchedAxis |= ResTable_config::CONFIG_LOCALE; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 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 | } |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 93 | return matchedAxis == (mConfigMask & mask); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 107 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 108 | return NO_ERROR; |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 109 | } |