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 | |
Roozbeh Pournader | 3f32c27 | 2016-06-21 14:16:40 -0700 | [diff] [blame] | 57 | // Returns true if the locale script of the config should be considered matching |
| 58 | // the locale script of entry. |
| 59 | // |
| 60 | // If both the scripts are empty, the scripts are considered matching for |
| 61 | // backward compatibility reasons. |
| 62 | // |
| 63 | // If only one script is empty, we try to compute it based on the provided |
| 64 | // language and country. If we could not compute it, we assume it's either a |
| 65 | // new language we don't know about, or a private use language. We return true |
| 66 | // since we don't know any better and they might as well be a match. |
| 67 | // |
| 68 | // Finally, when we have two scripts (one of which could be computed), we return |
| 69 | // true if and only if they are an exact match. |
| 70 | inline bool |
| 71 | scriptsMatch(const ResTable_config& config, const ResTable_config& entry) { |
| 72 | const char* configScript = config.localeScript; |
| 73 | const char* entryScript = entry.localeScript; |
| 74 | if (configScript[0] == '\0' && entryScript[0] == '\0') { |
| 75 | return true; // both scripts are empty. We match for backward compatibility reasons. |
| 76 | } |
| 77 | |
| 78 | char scriptBuffer[sizeof(config.localeScript)]; |
| 79 | if (configScript[0] == '\0') { |
| 80 | localeDataComputeScript(scriptBuffer, config.language, config.country); |
| 81 | if (scriptBuffer[0] == '\0') { // We can't compute the script, so we match. |
| 82 | return true; |
| 83 | } |
| 84 | configScript = scriptBuffer; |
| 85 | } else if (entryScript[0] == '\0') { |
| 86 | localeDataComputeScript( |
| 87 | scriptBuffer, entry.language, entry.country); |
| 88 | if (scriptBuffer[0] == '\0') { // We can't compute the script, so we match. |
| 89 | return true; |
| 90 | } |
| 91 | entryScript = scriptBuffer; |
| 92 | } |
| 93 | return (memcmp(configScript, entryScript, sizeof(config.localeScript)) == 0); |
| 94 | } |
| 95 | |
| 96 | |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 97 | bool |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 98 | WeakResourceFilter::match(const ResTable_config& config) const |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 99 | { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 100 | uint32_t mask = mDefault.diff(config); |
| 101 | if ((mConfigMask & mask) == 0) { |
| 102 | // The two configurations don't have any common axis. |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 105 | |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 106 | uint32_t matchedAxis = 0x0; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 107 | const size_t N = mConfigs.size(); |
| 108 | for (size_t i = 0; i < N; i++) { |
| 109 | const std::pair<ConfigDescription, uint32_t>& entry = mConfigs[i]; |
| 110 | uint32_t diff = entry.first.diff(config); |
| 111 | if ((diff & entry.second) == 0) { |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 112 | // Mark the axis that was matched. |
| 113 | matchedAxis |= entry.second; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 114 | } else if ((diff & entry.second) == ResTable_config::CONFIG_LOCALE) { |
| 115 | // If the locales differ, but the languages are the same and |
| 116 | // the locale we are matching only has a language specified, |
| 117 | // we match. |
Roozbeh Pournader | 3f32c27 | 2016-06-21 14:16:40 -0700 | [diff] [blame] | 118 | // |
| 119 | // Exception: we won't match if a script is specified for at least |
| 120 | // one of the locales and it's different from the other locale's |
| 121 | // script. (We will compute the other script if at least one of the |
| 122 | // scripts were explicitly set. In cases we can't compute an script, |
| 123 | // we match.) |
| 124 | if (config.language[0] != '\0' && |
| 125 | config.country[0] == '\0' && |
| 126 | config.localeVariant[0] == '\0' && |
| 127 | config.language[0] == entry.first.language[0] && |
| 128 | config.language[1] == entry.first.language[1] && |
| 129 | scriptsMatch(config, entry.first)) { |
| 130 | matchedAxis |= ResTable_config::CONFIG_LOCALE; |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 132 | } else if ((diff & entry.second) == ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) { |
| 133 | // Special case if the smallest screen width doesn't match. We check that the |
| 134 | // config being matched has a smaller screen width than the filter specified. |
| 135 | if (config.smallestScreenWidthDp != 0 && |
| 136 | config.smallestScreenWidthDp < entry.first.smallestScreenWidthDp) { |
| 137 | matchedAxis |= ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE; |
| 138 | } |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
Adam Lesinski | a5cc002 | 2014-08-22 14:10:31 -0700 | [diff] [blame] | 141 | return matchedAxis == (mConfigMask & mask); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 144 | status_t |
| 145 | StrongResourceFilter::parse(const String8& str) { |
| 146 | Vector<String8> configStrs = AaptUtil::split(str, ','); |
| 147 | ConfigDescription config; |
| 148 | mConfigs.clear(); |
| 149 | for (size_t i = 0; i < configStrs.size(); i++) { |
| 150 | if (!AaptConfig::parse(configStrs[i], &config)) { |
| 151 | fprintf(stderr, "Invalid configuration: %s\n", configStrs[i].string()); |
| 152 | return UNKNOWN_ERROR; |
| 153 | } |
| 154 | mConfigs.insert(config); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 156 | return NO_ERROR; |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 157 | } |