OSDN Git Service

DO NOT MERGE Do not call RecoverySystem with DPMS lock held am: 4ffe72dcc8 am: ab8d3f...
[android-x86/frameworks-base.git] / tools / aapt / ResourceFilter.cpp
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
11 status_t
12 WeakResourceFilter::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         // 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
51         mConfigMask |= entry.second;
52     }
53
54     return NO_ERROR;
55 }
56
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
97 bool
98 WeakResourceFilter::match(const ResTable_config& config) const
99 {
100     uint32_t mask = mDefault.diff(config);
101     if ((mConfigMask & mask) == 0) {
102         // The two configurations don't have any common axis.
103         return true;
104     }
105
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,
117             // we match.
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;
131             }
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             }
139         }
140     }
141     return matchedAxis == (mConfigMask & mask);
142 }
143
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);
155     }
156     return NO_ERROR;
157 }