blob: 347b4ec8346cd081a8c6e39512b11e20f8df8a07 [file] [log] [blame]
Adam Lesinski4452e132016-10-12 07:47:28 -07001/*
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 Lesinski4452e132016-10-12 07:47:28 -070017#include "androidfw/AttributeResolution.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080018
19#include <cstdint>
Adam Lesinski4452e132016-10-12 07:47:28 -070020
Mark Salyzyn6f773a02016-09-28 16:15:30 -070021#include <log/log.h>
Adam Lesinski4c67a472016-11-10 16:43:59 -080022
Adam Lesinskibebfcc42018-02-12 14:27:46 -080023#include "androidfw/AssetManager2.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080024#include "androidfw/AttributeFinder.h"
Adam Lesinski4452e132016-10-12 07:47:28 -070025
26constexpr bool kDebugStyles = false;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070027#define DEBUG_LOG(...) do { if (kDebugStyles) { ALOGI(__VA_ARGS__); } } while(0)
Adam Lesinski4452e132016-10-12 07:47:28 -070028
29namespace android {
30
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070031namespace {
32
Adam Lesinskibebfcc42018-02-12 14:27:46 -080033// Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0.
34static uint32_t ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) {
35 return cookie != kInvalidCookie ? static_cast<uint32_t>(cookie + 1) : static_cast<uint32_t>(-1);
36}
37
Adam Lesinski4c67a472016-11-10 16:43:59 -080038class XmlAttributeFinder
39 : public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> {
Adam Lesinski7a37b742016-10-12 14:05:55 -070040 public:
41 explicit XmlAttributeFinder(const ResXMLParser* parser)
Ryan Mitchell87e89542020-10-05 14:24:35 -070042 : BackTrackingAttributeFinder(0, parser != nullptr ? parser->getAttributeCount() : 0),
Adam Lesinski7a37b742016-10-12 14:05:55 -070043 parser_(parser) {}
Adam Lesinski4452e132016-10-12 07:47:28 -070044
Adam Lesinski4c67a472016-11-10 16:43:59 -080045 inline uint32_t GetAttribute(size_t index) const {
46 return parser_->getAttributeNameResID(index);
47 }
Adam Lesinski4452e132016-10-12 07:47:28 -070048
Adam Lesinski7a37b742016-10-12 14:05:55 -070049 private:
50 const ResXMLParser* parser_;
Adam Lesinski4452e132016-10-12 07:47:28 -070051};
52
Adam Lesinski7a37b742016-10-12 14:05:55 -070053class BagAttributeFinder
Adam Lesinskibebfcc42018-02-12 14:27:46 -080054 : public BackTrackingAttributeFinder<BagAttributeFinder, const ResolvedBag::Entry*> {
Adam Lesinski7a37b742016-10-12 14:05:55 -070055 public:
Chih-Hung Hsiehc2ace0c2018-12-20 13:46:53 -080056 explicit BagAttributeFinder(const ResolvedBag* bag)
Adam Lesinskibebfcc42018-02-12 14:27:46 -080057 : BackTrackingAttributeFinder(bag != nullptr ? bag->entries : nullptr,
58 bag != nullptr ? bag->entries + bag->entry_count : nullptr) {
59 }
Adam Lesinski4452e132016-10-12 07:47:28 -070060
Adam Lesinskibebfcc42018-02-12 14:27:46 -080061 inline uint32_t GetAttribute(const ResolvedBag::Entry* entry) const {
62 return entry->key;
Adam Lesinski7a37b742016-10-12 14:05:55 -070063 }
Adam Lesinski4452e132016-10-12 07:47:28 -070064};
65
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070066base::expected<const ResolvedBag*, NullOrIOError> GetStyleBag(Theme* theme,
67 uint32_t theme_attribute_resid,
68 uint32_t fallback_resid,
69 uint32_t* out_theme_flags) {
70 // Load the style from the attribute if specified.
71 if (theme_attribute_resid != 0U) {
72 std::optional<AssetManager2::SelectedValue> value = theme->GetAttribute(theme_attribute_resid);
73 if (value.has_value()) {
74 *out_theme_flags |= value->flags;
75 auto result = theme->GetAssetManager()->ResolveBag(*value);
76 if (result.has_value() || IsIOError(result)) {
77 return result;
Adam Lesinski7a37b742016-10-12 14:05:55 -070078 }
79 }
80 }
81
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070082 // Fallback to loading the style from the resource id if specified.
83 if (fallback_resid != 0U) {
84 return theme->GetAssetManager()->GetBag(fallback_resid);
85 }
86
87 return base::unexpected(std::nullopt);
88}
89
90base::expected<const ResolvedBag*, NullOrIOError> GetXmlStyleBag(Theme* theme,
91 ResXMLParser* xml_parser,
92 uint32_t* out_theme_flags) {
93 if (xml_parser == nullptr) {
94 return base::unexpected(std::nullopt);
95 }
96
97 // Retrieve the style resource ID associated with the current XML tag's style attribute.
98 Res_value value;
99 const ssize_t idx = xml_parser->indexOfStyle();
100 if (idx < 0 || xml_parser->getAttributeValue(idx, &value) < 0) {
101 return base::unexpected(std::nullopt);
102 }
103
104 if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
105 // Resolve the attribute with out theme.
106 if (std::optional<AssetManager2::SelectedValue> result = theme->GetAttribute(value.data)) {
107 *out_theme_flags |= result->flags;
108 return theme->GetAssetManager()->ResolveBag(*result);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800109 }
110 }
111
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700112 if (value.dataType == Res_value::TYPE_REFERENCE) {
113 return theme->GetAssetManager()->GetBag(value.data);
114 }
115
116 return base::unexpected(std::nullopt);
117}
118
119} // namespace
120
121base::expected<std::monostate, IOError> ResolveAttrs(Theme* theme, uint32_t def_style_attr,
122 uint32_t def_style_res, uint32_t* src_values,
123 size_t src_values_length, uint32_t* attrs,
124 size_t attrs_length, uint32_t* out_values,
125 uint32_t* out_indices) {
126 DEBUG_LOG("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x", theme, def_style_attr,
127 def_style_res);
128
129 int indices_idx = 0;
130 const AssetManager2* assetmanager = theme->GetAssetManager();
131
132 // Load default style from attribute or resource id, if specified...
133 uint32_t def_style_theme_flags = 0U;
134 const auto default_style_bag = GetStyleBag(theme, def_style_attr, def_style_res,
135 &def_style_theme_flags);
136 if (UNLIKELY(IsIOError(default_style_bag))) {
137 return base::unexpected(GetIOError(default_style_bag.error()));
138 }
139
140 BagAttributeFinder def_style_attr_finder(default_style_bag.value_or(nullptr));
Adam Lesinski7a37b742016-10-12 14:05:55 -0700141
142 // Now iterate through all of the attributes that the client has requested,
143 // filling in each with whatever data we can find.
144 for (size_t ii = 0; ii < attrs_length; ii++) {
145 const uint32_t cur_ident = attrs[ii];
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700146 DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident);
Adam Lesinski4452e132016-10-12 07:47:28 -0700147
Adam Lesinski7a37b742016-10-12 14:05:55 -0700148 // Try to find a value for this attribute... we prioritize values
149 // coming from, first XML attributes, then XML style, then default
150 // style, and finally the theme.
151
152 // Retrieve the current input value if available.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700153 AssetManager2::SelectedValue value{};
Adam Lesinski7a37b742016-10-12 14:05:55 -0700154 if (src_values_length > 0 && src_values[ii] != 0) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700155 value.type = Res_value::TYPE_ATTRIBUTE;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700156 value.data = src_values[ii];
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700157 DEBUG_LOG("-> From values: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski32e75012017-05-09 15:25:37 -0700158 } else {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800159 const ResolvedBag::Entry* const entry = def_style_attr_finder.Find(cur_ident);
160 if (entry != def_style_attr_finder.end()) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700161 value = AssetManager2::SelectedValue(*default_style_bag, *entry);
162 value.flags |= def_style_theme_flags;
163 DEBUG_LOG("-> From def style: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700164 }
165 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700166
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700167 if (value.type != Res_value::TYPE_NULL) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700168 // Take care of resolving the found resource to its final value.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700169 const auto result = theme->ResolveAttributeReference(value);
170 if (UNLIKELY(IsIOError(result))) {
171 return base::unexpected(GetIOError(result.error()));
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800172 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700173 DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski32e75012017-05-09 15:25:37 -0700174 } else if (value.data != Res_value::DATA_NULL_EMPTY) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800175 // If we still don't have a value for this attribute, try to find it in the theme!
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700176 if (auto attr_value = theme->GetAttribute(cur_ident)) {
177 value = *attr_value;
178 DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
179
Ryan Mitchell87e89542020-10-05 14:24:35 -0700180 const auto result = assetmanager->ResolveReference(value, true /* cache_value */);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700181 if (UNLIKELY(IsIOError(result))) {
182 return base::unexpected(GetIOError(result.error()));
Adam Lesinski4452e132016-10-12 07:47:28 -0700183 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700184 DEBUG_LOG("-> Resolved theme: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700185 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700186 }
187
Adam Lesinski7a37b742016-10-12 14:05:55 -0700188 // Deal with the special @null value -- it turns back to TYPE_NULL.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700189 if (value.type == Res_value::TYPE_REFERENCE && value.data == 0) {
190 DEBUG_LOG("-> Setting to @null!");
191 value.type = Res_value::TYPE_NULL;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700192 value.data = Res_value::DATA_NULL_UNDEFINED;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700193 value.cookie = kInvalidCookie;
Adam Lesinski4452e132016-10-12 07:47:28 -0700194 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700195
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700196 DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data);
Adam Lesinski4452e132016-10-12 07:47:28 -0700197
Adam Lesinski7a37b742016-10-12 14:05:55 -0700198 // Write the final value back to Java.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700199 out_values[STYLE_TYPE] = value.type;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700200 out_values[STYLE_DATA] = value.data;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700201 out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
202 out_values[STYLE_RESOURCE_ID] = value.resid;
203 out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
204 out_values[STYLE_DENSITY] = value.config.density;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700205
Adam Lesinski32e75012017-05-09 15:25:37 -0700206 if (out_indices != nullptr &&
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700207 (value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY)) {
208 out_indices[++indices_idx] = ii;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700209 }
210
211 out_values += STYLE_NUM_ENTRIES;
212 }
213
Adam Lesinski4c67a472016-11-10 16:43:59 -0800214 if (out_indices != nullptr) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700215 out_indices[0] = indices_idx;
216 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700217 return {};
Adam Lesinski7a37b742016-10-12 14:05:55 -0700218}
219
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700220base::expected<std::monostate, IOError> ApplyStyle(Theme* theme, ResXMLParser* xml_parser,
221 uint32_t def_style_attr,
222 uint32_t def_style_resid,
223 const uint32_t* attrs, size_t attrs_length,
224 uint32_t* out_values, uint32_t* out_indices) {
225 DEBUG_LOG("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x xml=0x%p", theme,
226 def_style_attr, def_style_resid, xml_parser);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700227
228 int indices_idx = 0;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700229 const AssetManager2* assetmanager = theme->GetAssetManager();
Adam Lesinski7a37b742016-10-12 14:05:55 -0700230
231 // Load default style from attribute, if specified...
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700232 uint32_t def_style_theme_flags = 0U;
233 const auto default_style_bag = GetStyleBag(theme, def_style_attr, def_style_resid,
234 &def_style_theme_flags);
235 if (IsIOError(default_style_bag)) {
236 return base::unexpected(GetIOError(default_style_bag.error()));
Adam Lesinski7a37b742016-10-12 14:05:55 -0700237 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700238
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800239 // Retrieve the style resource ID associated with the current XML tag's style attribute.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700240 uint32_t xml_style_theme_flags = 0U;
241 const auto xml_style_bag = GetXmlStyleBag(theme, xml_parser, &def_style_theme_flags);
242 if (IsIOError(xml_style_bag)) {
243 return base::unexpected(GetIOError(xml_style_bag.error()));
Adam Lesinski7a37b742016-10-12 14:05:55 -0700244 }
245
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700246 BagAttributeFinder def_style_attr_finder(default_style_bag.value_or(nullptr));
247 BagAttributeFinder xml_style_attr_finder(xml_style_bag.value_or(nullptr));
Adam Lesinski7a37b742016-10-12 14:05:55 -0700248 XmlAttributeFinder xml_attr_finder(xml_parser);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700249
250 // Now iterate through all of the attributes that the client has requested,
251 // filling in each with whatever data we can find.
252 for (size_t ii = 0; ii < attrs_length; ii++) {
253 const uint32_t cur_ident = attrs[ii];
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700254 DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700255
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700256 AssetManager2::SelectedValue value{};
Aurimas Liutikas949b05d2019-01-30 17:20:41 -0800257 uint32_t value_source_resid = 0;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700258
259 // Try to find a value for this attribute... we prioritize values
260 // coming from, first XML attributes, then XML style, then default
261 // style, and finally the theme.
262
263 // Walk through the xml attributes looking for the requested attribute.
264 const size_t xml_attr_idx = xml_attr_finder.Find(cur_ident);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800265 if (xml_attr_idx != xml_attr_finder.end()) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700266 // We found the attribute we were looking for.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700267 Res_value attribute_value;
268 xml_parser->getAttributeValue(xml_attr_idx, &attribute_value);
269 value.type = attribute_value.dataType;
270 value.data = attribute_value.data;
Aurimas Liutikas949b05d2019-01-30 17:20:41 -0800271 value_source_resid = xml_parser->getSourceResourceId();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700272 DEBUG_LOG("-> From XML: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski4452e132016-10-12 07:47:28 -0700273 }
274
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700275 if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) {
Adam Lesinski32e75012017-05-09 15:25:37 -0700276 // Walk through the style class values looking for the requested attribute.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800277 const ResolvedBag::Entry* entry = xml_style_attr_finder.Find(cur_ident);
278 if (entry != xml_style_attr_finder.end()) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700279 value = AssetManager2::SelectedValue(*xml_style_bag, *entry);
280 value.flags |= xml_style_theme_flags;
Aurimas Liutikas949b05d2019-01-30 17:20:41 -0800281 value_source_resid = entry->style;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700282 DEBUG_LOG("-> From style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data,
283 value_source_resid);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700284 }
285 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700286
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700287 if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) {
Adam Lesinski32e75012017-05-09 15:25:37 -0700288 // Walk through the default style values looking for the requested attribute.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800289 const ResolvedBag::Entry* entry = def_style_attr_finder.Find(cur_ident);
290 if (entry != def_style_attr_finder.end()) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700291 value = AssetManager2::SelectedValue(*default_style_bag, *entry);
292 value.flags |= def_style_theme_flags;
Aurimas Liutikas949b05d2019-01-30 17:20:41 -0800293 value_source_resid = entry->style;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700294 DEBUG_LOG("-> From def style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data,
295 entry->style);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700296 }
297 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700298
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700299 if (value.type != Res_value::TYPE_NULL) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700300 // Take care of resolving the found resource to its final value.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700301 auto result = theme->ResolveAttributeReference(value);
302 if (UNLIKELY(IsIOError(result))) {
303 return base::unexpected(GetIOError(result.error()));
Adam Lesinski7a37b742016-10-12 14:05:55 -0700304 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700305 DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski32e75012017-05-09 15:25:37 -0700306 } else if (value.data != Res_value::DATA_NULL_EMPTY) {
307 // If we still don't have a value for this attribute, try to find it in the theme!
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700308 if (auto attr_value = theme->GetAttribute(cur_ident)) {
309 value = *attr_value;
310 DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
Adam Lesinski4452e132016-10-12 07:47:28 -0700311
Ryan Mitchell87e89542020-10-05 14:24:35 -0700312 auto result = assetmanager->ResolveReference(value, true /* cache_value */);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700313 if (UNLIKELY(IsIOError(result))) {
314 return base::unexpected(GetIOError(result.error()));
Adam Lesinski4452e132016-10-12 07:47:28 -0700315 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700316 DEBUG_LOG("-> Resolved theme: type=0x%x, data=0x%08x", value.type, value.data);
317 // TODO: set value_source_resid for the style in the theme that was used.
Adam Lesinski7a37b742016-10-12 14:05:55 -0700318 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700319 }
320
Adam Lesinski7a37b742016-10-12 14:05:55 -0700321 // Deal with the special @null value -- it turns back to TYPE_NULL.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700322 if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) {
323 DEBUG_LOG("-> Setting to @null!");
324 value.type = Res_value::TYPE_NULL;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700325 value.data = Res_value::DATA_NULL_UNDEFINED;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700326 value.cookie = kInvalidCookie;
Adam Lesinski4452e132016-10-12 07:47:28 -0700327 }
Adam Lesinski7a37b742016-10-12 14:05:55 -0700328
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700329 DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data);
Adam Lesinski7a37b742016-10-12 14:05:55 -0700330
331 // Write the final value back to Java.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700332 out_values[STYLE_TYPE] = value.type;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700333 out_values[STYLE_DATA] = value.data;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700334 out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
335 out_values[STYLE_RESOURCE_ID] = value.resid;
336 out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
337 out_values[STYLE_DENSITY] = value.config.density;
Aurimas Liutikasf9dbd5f2019-03-07 11:21:51 -0800338 out_values[STYLE_SOURCE_RESOURCE_ID] = value_source_resid;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700339
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700340 if (value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY) {
Adam Lesinski06d3e8f2017-01-05 17:03:55 -0800341 // out_indices must NOT be nullptr.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700342 out_indices[++indices_idx] = ii;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700343 }
Adam Lesinski7a37b742016-10-12 14:05:55 -0700344 out_values += STYLE_NUM_ENTRIES;
345 }
346
Adam Lesinski06d3e8f2017-01-05 17:03:55 -0800347 // out_indices must NOT be nullptr.
348 out_indices[0] = indices_idx;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700349 return {};
Adam Lesinski4452e132016-10-12 07:47:28 -0700350}
351
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700352base::expected<std::monostate, IOError> RetrieveAttributes(AssetManager2* assetmanager,
353 ResXMLParser* xml_parser,
354 uint32_t* attrs,
355 size_t attrs_length,
356 uint32_t* out_values,
357 uint32_t* out_indices) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700358 int indices_idx = 0;
Adam Lesinski4452e132016-10-12 07:47:28 -0700359
Adam Lesinski7a37b742016-10-12 14:05:55 -0700360 // Retrieve the XML attributes, if requested.
Adam Lesinski7a37b742016-10-12 14:05:55 -0700361 size_t ix = 0;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700362 const size_t xml_attr_count = xml_parser->getAttributeCount();
Adam Lesinski7a37b742016-10-12 14:05:55 -0700363 uint32_t cur_xml_attr = xml_parser->getAttributeNameResID(ix);
Adam Lesinski4452e132016-10-12 07:47:28 -0700364
Adam Lesinski7a37b742016-10-12 14:05:55 -0700365 // Now iterate through all of the attributes that the client has requested,
366 // filling in each with whatever data we can find.
367 for (size_t ii = 0; ii < attrs_length; ii++) {
368 const uint32_t cur_ident = attrs[ii];
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700369 AssetManager2::SelectedValue value{};
Adam Lesinski4452e132016-10-12 07:47:28 -0700370
Adam Lesinski7a37b742016-10-12 14:05:55 -0700371 // Try to find a value for this attribute...
372 // Skip through XML attributes until the end or the next possible match.
373 while (ix < xml_attr_count && cur_ident > cur_xml_attr) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700374 cur_xml_attr = xml_parser->getAttributeNameResID(++ix);
Adam Lesinski4452e132016-10-12 07:47:28 -0700375 }
376
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700377 // Retrieve the current XML attribute if it matches, and step to next.
378 if (ix < xml_attr_count && cur_ident == cur_xml_attr) {
379 Res_value attribute_value;
380 xml_parser->getAttributeValue(ix, &attribute_value);
381 value.type = attribute_value.dataType;
382 value.data = attribute_value.data;
383 cur_xml_attr = xml_parser->getAttributeNameResID(++ix);
384 }
385
386 if (value.type != Res_value::TYPE_NULL) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700387 // Take care of resolving the found resource to its final value.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700388 auto result = assetmanager->ResolveReference(value);
389 if (UNLIKELY(IsIOError(result))) {
390 return base::unexpected(GetIOError(result.error()));
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800391 }
Adam Lesinski4452e132016-10-12 07:47:28 -0700392 }
Adam Lesinski7a37b742016-10-12 14:05:55 -0700393
394 // Deal with the special @null value -- it turns back to TYPE_NULL.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700395 if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) {
396 value.type = Res_value::TYPE_NULL;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700397 value.data = Res_value::DATA_NULL_UNDEFINED;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700398 value.cookie = kInvalidCookie;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700399 }
400
401 // Write the final value back to Java.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700402 out_values[STYLE_TYPE] = value.type;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700403 out_values[STYLE_DATA] = value.data;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700404 out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
405 out_values[STYLE_RESOURCE_ID] = value.resid;
406 out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
407 out_values[STYLE_DENSITY] = value.config.density;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700408
Adam Lesinski32e75012017-05-09 15:25:37 -0700409 if (out_indices != nullptr &&
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700410 (value.type != Res_value::TYPE_NULL ||
411 value.data == Res_value::DATA_NULL_EMPTY)) {
412 out_indices[++indices_idx] = ii;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700413 }
414
415 out_values += STYLE_NUM_ENTRIES;
416 }
417
Adam Lesinski4c67a472016-11-10 16:43:59 -0800418 if (out_indices != nullptr) {
Adam Lesinski7a37b742016-10-12 14:05:55 -0700419 out_indices[0] = indices_idx;
420 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700421 return {};
Adam Lesinski4452e132016-10-12 07:47:28 -0700422}
423
Adam Lesinski7a37b742016-10-12 14:05:55 -0700424} // namespace android