blob: 5fbe77e98144c8e9818319571e2189b440c537c4 [file] [log] [blame]
Adam Lesinski6a008172016-02-02 17:02:58 -08001/*
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
Adam Lesinski6a008172016-02-02 17:02:58 -080017#include "filter/ConfigFilter.h"
18
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "androidfw/ResourceTypes.h"
20
21#include "ConfigDescription.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080022
23namespace aapt {
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025void AxisConfigFilter::AddConfig(ConfigDescription config) {
26 uint32_t diff_mask = ConfigDescription::DefaultConfig().diff(config);
Adam Lesinski6a008172016-02-02 17:02:58 -080027
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 // Ignore the version
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 diff_mask &= ~android::ResTable_config::CONFIG_VERSION;
Adam Lesinski6a008172016-02-02 17:02:58 -080030
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 // Ignore any densities. Those are best handled in --preferred-density
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 if ((diff_mask & android::ResTable_config::CONFIG_DENSITY) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 config.density = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 diff_mask &= ~android::ResTable_config::CONFIG_DENSITY;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 }
Adam Lesinski6a008172016-02-02 17:02:58 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 configs_.insert(std::make_pair(config, diff_mask));
38 config_mask_ |= diff_mask;
Adam Lesinski6a008172016-02-02 17:02:58 -080039}
40
Adam Lesinski13308bc2017-05-16 15:26:54 -070041// 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.
54static 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078bool AxisConfigFilter::Match(const ConfigDescription& config) const {
79 const uint32_t mask = ConfigDescription::DefaultConfig().diff(config);
80 if ((config_mask_ & mask) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 // The two configurations don't have any common axis.
82 return true;
83 }
Adam Lesinski6a008172016-02-02 17:02:58 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 uint32_t matched_axis = 0;
86 for (const auto& entry : configs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 const ConfigDescription& target = entry.first;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 const uint32_t diff_mask = entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 uint32_t diff = target.diff(config);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 if ((diff & diff_mask) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 // Mark the axis that was matched.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 matched_axis |= diff_mask;
93 } else if ((diff & diff_mask) == android::ResTable_config::CONFIG_LOCALE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 // 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.
Adam Lesinski13308bc2017-05-16 15:26:54 -070097 //
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;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 } else if ((diff & diff_mask) ==
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 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) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 matched_axis |= android::ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
Adam Lesinski6a008172016-02-02 17:02:58 -0800118 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 return matched_axis == (config_mask_ & mask);
Adam Lesinski6a008172016-02-02 17:02:58 -0800121}
122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123} // namespace aapt