1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "filter/ConfigFilter.h"
18
19 #include "androidfw/ResourceTypes.h"
20
21 #include "ConfigDescription.h"
22
23 namespace aapt {
24
AddConfig(ConfigDescription config)25 void AxisConfigFilter::AddConfig(ConfigDescription config) {
26 uint32_t diff_mask = ConfigDescription::DefaultConfig().diff(config);
27
28 // Ignore the version
29 diff_mask &= ~android::ResTable_config::CONFIG_VERSION;
30
31 // Ignore any densities. Those are best handled in --preferred-density
32 if ((diff_mask & android::ResTable_config::CONFIG_DENSITY) != 0) {
33 config.density = 0;
34 diff_mask &= ~android::ResTable_config::CONFIG_DENSITY;
35 }
36
37 configs_.insert(std::make_pair(config, diff_mask));
38 config_mask_ |= diff_mask;
39 }
40
41 // Returns true if the locale script of the config should be considered matching
42 // the locale script of entry.
43 //
44 // If both the scripts are empty, the scripts are considered matching for
45 // backward compatibility reasons.
46 //
47 // If only one script is empty, we try to compute it based on the provided
48 // language and country. If we could not compute it, we assume it's either a
49 // new language we don't know about, or a private use language. We return true
50 // since we don't know any better and they might as well be a match.
51 //
52 // Finally, when we have two scripts (one of which could be computed), we return
53 // true if and only if they are an exact match.
ScriptsMatch(const ConfigDescription & config,const ConfigDescription & entry)54 static bool ScriptsMatch(const ConfigDescription& config, const ConfigDescription& entry) {
55 const char* config_script = config.localeScript;
56 const char* entry_script = entry.localeScript;
57 if (config_script[0] == '\0' && entry_script[0] == '\0') {
58 return true; // both scripts are empty. We match for backward compatibility reasons.
59 }
60
61 char script_buffer[sizeof(config.localeScript)];
62 if (config_script[0] == '\0') {
63 android::localeDataComputeScript(script_buffer, config.language, config.country);
64 if (script_buffer[0] == '\0') { // We can't compute the script, so we match.
65 return true;
66 }
67 config_script = script_buffer;
68 } else if (entry_script[0] == '\0') {
69 android::localeDataComputeScript(script_buffer, entry.language, entry.country);
70 if (script_buffer[0] == '\0') { // We can't compute the script, so we match.
71 return true;
72 }
73 entry_script = script_buffer;
74 }
75 return memcmp(config_script, entry_script, sizeof(config.localeScript)) == 0;
76 }
77
Match(const ConfigDescription & config) const78 bool AxisConfigFilter::Match(const ConfigDescription& config) const {
79 const uint32_t mask = ConfigDescription::DefaultConfig().diff(config);
80 if ((config_mask_ & mask) == 0) {
81 // The two configurations don't have any common axis.
82 return true;
83 }
84
85 uint32_t matched_axis = 0;
86 for (const auto& entry : configs_) {
87 const ConfigDescription& target = entry.first;
88 const uint32_t diff_mask = entry.second;
89 uint32_t diff = target.diff(config);
90 if ((diff & diff_mask) == 0) {
91 // Mark the axis that was matched.
92 matched_axis |= diff_mask;
93 } else if ((diff & diff_mask) == android::ResTable_config::CONFIG_LOCALE) {
94 // If the locales differ, but the languages are the same and
95 // the locale we are matching only has a language specified,
96 // we match.
97 //
98 // Exception: we won't match if a script is specified for at least
99 // one of the locales and it's different from the other locale's
100 // script. (We will compute the other script if at least one of the
101 // scripts were explicitly set. In cases we can't compute an script,
102 // we match.)
103 if (config.language[0] != '\0' && config.country[0] == '\0' &&
104 config.localeVariant[0] == '\0' && config.language[0] == entry.first.language[0] &&
105 config.language[1] == entry.first.language[1] && ScriptsMatch(config, entry.first)) {
106 matched_axis |= android::ResTable_config::CONFIG_LOCALE;
107 }
108 } else if ((diff & diff_mask) ==
109 android::ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) {
110 // Special case if the smallest screen width doesn't match. We check that
111 // the
112 // config being matched has a smaller screen width than the filter
113 // specified.
114 if (config.smallestScreenWidthDp != 0 &&
115 config.smallestScreenWidthDp < target.smallestScreenWidthDp) {
116 matched_axis |= android::ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE;
117 }
118 }
119 }
120 return matched_axis == (config_mask_ & mask);
121 }
122
123 } // namespace aapt
124