blob: 8693999c7cc4ed93dc8b3edc79bc478a6a1c1b77 [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
57bool
Adam Lesinskifab50872014-04-16 14:40:42 -070058WeakResourceFilter::match(const ResTable_config& config) const
Dianne Hackborne6b68032011-10-13 16:26:02 -070059{
Adam Lesinskifab50872014-04-16 14:40:42 -070060 uint32_t mask = mDefault.diff(config);
61 if ((mConfigMask & mask) == 0) {
62 // The two configurations don't have any common axis.
Dianne Hackborne6b68032011-10-13 16:26:02 -070063 return true;
64 }
Dianne Hackborne6b68032011-10-13 16:26:02 -070065
Adam Lesinskia5cc0022014-08-22 14:10:31 -070066 uint32_t matchedAxis = 0x0;
Adam Lesinskifab50872014-04-16 14:40:42 -070067 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 Lesinskia5cc0022014-08-22 14:10:31 -070072 // Mark the axis that was matched.
73 matchedAxis |= entry.second;
Adam Lesinskifab50872014-04-16 14:40:42 -070074 } 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 Lesinskia5cc0022014-08-22 14:10:31 -070078 if (config.language[0] &&
79 memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) {
Adam Lesinskifab50872014-04-16 14:40:42 -070080 if (config.country[0] == 0) {
Adam Lesinskia5cc0022014-08-22 14:10:31 -070081 matchedAxis |= ResTable_config::CONFIG_LOCALE;
Adam Lesinskifab50872014-04-16 14:40:42 -070082 }
83 }
Adam Lesinskia5cc0022014-08-22 14:10:31 -070084 } 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 Hackborne6b68032011-10-13 16:26:02 -070091 }
92 }
Adam Lesinskia5cc0022014-08-22 14:10:31 -070093 return matchedAxis == (mConfigMask & mask);
Dianne Hackborne6b68032011-10-13 16:26:02 -070094}
95
Adam Lesinskifab50872014-04-16 14:40:42 -070096status_t
97StrongResourceFilter::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 Hackborne6b68032011-10-13 16:26:02 -0700107 }
Adam Lesinskifab50872014-04-16 14:40:42 -0700108 return NO_ERROR;
Dianne Hackborne6b68032011-10-13 16:26:02 -0700109}