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;
57 // Returns true if the locale script of the config should be considered matching
58 // the locale script of entry.
60 // If both the scripts are empty, the scripts are considered matching for
61 // backward compatibility reasons.
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.
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.
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.
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.
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.
91 entryScript = scriptBuffer;
93 return (memcmp(configScript, entryScript, sizeof(config.localeScript)) == 0);
98 WeakResourceFilter::match(const ResTable_config& config) const
100 uint32_t mask = mDefault.diff(config);
101 if ((mConfigMask & mask) == 0) {
102 // The two configurations don't have any common axis.
106 uint32_t matchedAxis = 0x0;
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) {
112 // Mark the axis that was matched.
113 matchedAxis |= entry.second;
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,
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,
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;
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;
141 return matchedAxis == (mConfigMask & mask);
145 StrongResourceFilter::parse(const String8& str) {
146 Vector<String8> configStrs = AaptUtil::split(str, ',');
147 ConfigDescription config;
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;
154 mConfigs.insert(config);