blob: 9d10d595882a6c893fff9afc477cf7233f7957b8 [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
Mårten Kongstad5c541f62018-06-20 08:46:41 +020019#include "androidfw/ConfigDescription.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "androidfw/ResourceTypes.h"
21
Mårten Kongstad5c541f62018-06-20 08:46:41 +020022using ::android::ConfigDescription;
Adam Lesinski6a008172016-02-02 17:02:58 -080023
24namespace aapt {
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026void AxisConfigFilter::AddConfig(ConfigDescription config) {
27 uint32_t diff_mask = ConfigDescription::DefaultConfig().diff(config);
Adam Lesinski6a008172016-02-02 17:02:58 -080028
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 // Ignore the version
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 diff_mask &= ~android::ResTable_config::CONFIG_VERSION;
Adam Lesinski6a008172016-02-02 17:02:58 -080031
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 // Ignore any densities. Those are best handled in --preferred-density
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 if ((diff_mask & android::ResTable_config::CONFIG_DENSITY) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 config.density = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 diff_mask &= ~android::ResTable_config::CONFIG_DENSITY;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 }
Adam Lesinski6a008172016-02-02 17:02:58 -080037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 configs_.insert(std::make_pair(config, diff_mask));
39 config_mask_ |= diff_mask;
Adam Lesinski6a008172016-02-02 17:02:58 -080040}
41
Adam Lesinski13308bc2017-05-16 15:26:54 -070042// Returns true if the locale script of the config should be considered matching
43// the locale script of entry.
44//
45// If both the scripts are empty, the scripts are considered matching for
46// backward compatibility reasons.
47//
48// If only one script is empty, we try to compute it based on the provided
49// language and country. If we could not compute it, we assume it's either a
50// new language we don't know about, or a private use language. We return true
51// since we don't know any better and they might as well be a match.
52//
53// Finally, when we have two scripts (one of which could be computed), we return
54// true if and only if they are an exact match.
55static bool ScriptsMatch(const ConfigDescription& config, const ConfigDescription& entry) {
56 const char* config_script = config.localeScript;
57 const char* entry_script = entry.localeScript;
58 if (config_script[0] == '\0' && entry_script[0] == '\0') {
59 return true; // both scripts are empty. We match for backward compatibility reasons.
60 }
61
62 char script_buffer[sizeof(config.localeScript)];
63 if (config_script[0] == '\0') {
64 android::localeDataComputeScript(script_buffer, config.language, config.country);
65 if (script_buffer[0] == '\0') { // We can't compute the script, so we match.
66 return true;
67 }
68 config_script = script_buffer;
69 } else if (entry_script[0] == '\0') {
70 android::localeDataComputeScript(script_buffer, entry.language, entry.country);
71 if (script_buffer[0] == '\0') { // We can't compute the script, so we match.
72 return true;
73 }
74 entry_script = script_buffer;
75 }
76 return memcmp(config_script, entry_script, sizeof(config.localeScript)) == 0;
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079bool AxisConfigFilter::Match(const ConfigDescription& config) const {
80 const uint32_t mask = ConfigDescription::DefaultConfig().diff(config);
81 if ((config_mask_ & mask) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 // The two configurations don't have any common axis.
83 return true;
84 }
Adam Lesinski6a008172016-02-02 17:02:58 -080085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 uint32_t matched_axis = 0;
87 for (const auto& entry : configs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 const ConfigDescription& target = entry.first;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 const uint32_t diff_mask = entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 uint32_t diff = target.diff(config);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 if ((diff & diff_mask) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 // Mark the axis that was matched.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 matched_axis |= diff_mask;
94 } else if ((diff & diff_mask) == android::ResTable_config::CONFIG_LOCALE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 // If the locales differ, but the languages are the same and
96 // the locale we are matching only has a language specified,
97 // we match.
Adam Lesinski13308bc2017-05-16 15:26:54 -070098 //
99 // Exception: we won't match if a script is specified for at least
100 // one of the locales and it's different from the other locale's
101 // script. (We will compute the other script if at least one of the
102 // scripts were explicitly set. In cases we can't compute an script,
103 // we match.)
104 if (config.language[0] != '\0' && config.country[0] == '\0' &&
105 config.localeVariant[0] == '\0' && config.language[0] == entry.first.language[0] &&
106 config.language[1] == entry.first.language[1] && ScriptsMatch(config, entry.first)) {
107 matched_axis |= android::ResTable_config::CONFIG_LOCALE;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 } else if ((diff & diff_mask) ==
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 android::ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) {
111 // Special case if the smallest screen width doesn't match. We check that
112 // the
113 // config being matched has a smaller screen width than the filter
114 // specified.
115 if (config.smallestScreenWidthDp != 0 &&
116 config.smallestScreenWidthDp < target.smallestScreenWidthDp) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 matched_axis |= android::ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 }
Adam Lesinski6a008172016-02-02 17:02:58 -0800119 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 return matched_axis == (config_mask_ & mask);
Adam Lesinski6a008172016-02-02 17:02:58 -0800122}
123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124} // namespace aapt