blob: ed06f605eaeb49c8af4ea195239e8434830b3ef3 [file] [log] [blame]
Dianne Hackborne6b68032011-10-13 16:26:02 -07001//
Adam Lesinskifab50872014-04-16 14:40:42 -07002// Copyright 2014 The Android Open Source Project
Dianne Hackborne6b68032011-10-13 16:26:02 -07003//
4// Build resource files from raw assets.
5//
6
7#include "ResourceFilter.h"
Adam Lesinskifab50872014-04-16 14:40:42 -07008#include "AaptUtil.h"
9#include "AaptConfig.h"
Dianne Hackborne6b68032011-10-13 16:26:02 -070010
11status_t
Adam Lesinskifab50872014-04-16 14:40:42 -070012WeakResourceFilter::parse(const String8& str)
Dianne Hackborne6b68032011-10-13 16:26:02 -070013{
Adam Lesinskifab50872014-04-16 14:40:42 -070014 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 Krumina2ef5c02014-03-12 14:46:44 -070021 if (part == "en_XA") {
22 mContainsPseudoAccented = true;
23 } else if (part == "ar_XB") {
24 mContainsPseudoBidi = true;
Dianne Hackborne6b68032011-10-13 16:26:02 -070025 }
Adam Lesinskifab50872014-04-16 14:40:42 -070026
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 Hackborne6b68032011-10-13 16:26:02 -070037 }
38
Adam Lesinskifab50872014-04-16 14:40:42 -070039 entry.second = mDefault.diff(entry.first);
Narayan Kamath788fa412014-01-21 15:32:36 +000040
Adam Lesinskifab50872014-04-16 14:40:42 -070041 // Ignore the version
42 entry.second &= ~ResTable_config::CONFIG_VERSION;
43
Adam Lesinskia2bb5652014-11-10 17:56:11 -080044 // 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 Lesinskifab50872014-04-16 14:40:42 -070051 mConfigMask |= entry.second;
Dianne Hackborne6b68032011-10-13 16:26:02 -070052 }
53
54 return NO_ERROR;
55}
56
Roozbeh Pournader3f32c272016-06-21 14:16:40 -070057// 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.
70inline bool
71scriptsMatch(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 Hackborne6b68032011-10-13 16:26:02 -070097bool
Adam Lesinskifab50872014-04-16 14:40:42 -070098WeakResourceFilter::match(const ResTable_config& config) const
Dianne Hackborne6b68032011-10-13 16:26:02 -070099{
Adam Lesinskifab50872014-04-16 14:40:42 -0700100 uint32_t mask = mDefault.diff(config);
101 if ((mConfigMask & mask) == 0) {
102 // The two configurations don't have any common axis.
Dianne Hackborne6b68032011-10-13 16:26:02 -0700103 return true;
104 }
Dianne Hackborne6b68032011-10-13 16:26:02 -0700105
Adam Lesinskia5cc0022014-08-22 14:10:31 -0700106 uint32_t matchedAxis = 0x0;
Adam Lesinskifab50872014-04-16 14:40:42 -0700107 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 Lesinskia5cc0022014-08-22 14:10:31 -0700112 // Mark the axis that was matched.
113 matchedAxis |= entry.second;
Adam Lesinskifab50872014-04-16 14:40:42 -0700114 } 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 Pournader3f32c272016-06-21 14:16:40 -0700118 //
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 Lesinskifab50872014-04-16 14:40:42 -0700131 }
Adam Lesinskia5cc0022014-08-22 14:10:31 -0700132 } 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 Hackborne6b68032011-10-13 16:26:02 -0700139 }
140 }
Adam Lesinskia5cc0022014-08-22 14:10:31 -0700141 return matchedAxis == (mConfigMask & mask);
Dianne Hackborne6b68032011-10-13 16:26:02 -0700142}
143
Adam Lesinskifab50872014-04-16 14:40:42 -0700144status_t
145StrongResourceFilter::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 Hackborne6b68032011-10-13 16:26:02 -0700155 }
Adam Lesinskifab50872014-04-16 14:40:42 -0700156 return NO_ERROR;
Dianne Hackborne6b68032011-10-13 16:26:02 -0700157}