ResourceFilter.cpp revision fab50875b98e8274ac8ee44b38ba42521bbbf1f9
1//
2// Copyright 2014 The Android Open Source Project
3//
4// Build resource files from raw assets.
5//
6
7#include "ResourceFilter.h"
8#include "AaptUtil.h"
9#include "AaptConfig.h"
10
11status_t
12WeakResourceFilter::parse(const String8& str)
13{
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];
21        if (part == "en_XA") {
22            mContainsPseudoAccented = true;
23        } else if (part == "ar_XB") {
24            mContainsPseudoBidi = true;
25        }
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;
37        }
38
39        entry.second = mDefault.diff(entry.first);
40
41        // Ignore the version
42        entry.second &= ~ResTable_config::CONFIG_VERSION;
43
44        mConfigMask |= entry.second;
45    }
46
47    return NO_ERROR;
48}
49
50bool
51WeakResourceFilter::match(const ResTable_config& config) const
52{
53    uint32_t mask = mDefault.diff(config);
54    if ((mConfigMask & mask) == 0) {
55        // The two configurations don't have any common axis.
56        return true;
57    }
58
59    const size_t N = mConfigs.size();
60    for (size_t i = 0; i < N; i++) {
61        const std::pair<ConfigDescription, uint32_t>& entry = mConfigs[i];
62        uint32_t diff = entry.first.diff(config);
63        if ((diff & entry.second) == 0) {
64            return true;
65        } else if ((diff & entry.second) == ResTable_config::CONFIG_LOCALE) {
66            // If the locales differ, but the languages are the same and
67            // the locale we are matching only has a language specified,
68            // we match.
69            if (config.language[0] && memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) {
70                if (config.country[0] == 0) {
71                    return true;
72                }
73            }
74        }
75    }
76    return false;
77}
78
79status_t
80StrongResourceFilter::parse(const String8& str) {
81    Vector<String8> configStrs = AaptUtil::split(str, ',');
82    ConfigDescription config;
83    mConfigs.clear();
84    for (size_t i = 0; i < configStrs.size(); i++) {
85        if (!AaptConfig::parse(configStrs[i], &config)) {
86            fprintf(stderr, "Invalid configuration: %s\n", configStrs[i].string());
87            return UNKNOWN_ERROR;
88        }
89        mConfigs.insert(config);
90    }
91    return NO_ERROR;
92}
93