1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "androidfw/AttributeResolution.h"
#include <cstdint>
#include <log/log.h>
#include "androidfw/AssetManager2.h"
#include "androidfw/AttributeFinder.h"
constexpr bool kDebugStyles = false;
#define DEBUG_LOG(...) do { if (kDebugStyles) { ALOGI(__VA_ARGS__); } } while(0)
namespace android {
namespace {
// Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0.
static uint32_t ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) {
return cookie != kInvalidCookie ? static_cast<uint32_t>(cookie + 1) : static_cast<uint32_t>(-1);
}
class XmlAttributeFinder
: public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> {
public:
explicit XmlAttributeFinder(const ResXMLParser* parser)
: BackTrackingAttributeFinder(0, parser != nullptr ? parser->getAttributeCount() : 0),
parser_(parser) {}
inline uint32_t GetAttribute(size_t index) const {
return parser_->getAttributeNameResID(index);
}
private:
const ResXMLParser* parser_;
};
class BagAttributeFinder
: public BackTrackingAttributeFinder<BagAttributeFinder, const ResolvedBag::Entry*> {
public:
explicit BagAttributeFinder(const ResolvedBag* bag)
: BackTrackingAttributeFinder(bag != nullptr ? bag->entries : nullptr,
bag != nullptr ? bag->entries + bag->entry_count : nullptr) {
}
inline uint32_t GetAttribute(const ResolvedBag::Entry* entry) const {
return entry->key;
}
};
base::expected<const ResolvedBag*, NullOrIOError> GetStyleBag(Theme* theme,
uint32_t theme_attribute_resid,
uint32_t fallback_resid,
uint32_t* out_theme_flags) {
// Load the style from the attribute if specified.
if (theme_attribute_resid != 0U) {
std::optional<AssetManager2::SelectedValue> value = theme->GetAttribute(theme_attribute_resid);
if (value.has_value()) {
*out_theme_flags |= value->flags;
auto result = theme->GetAssetManager()->ResolveBag(*value);
if (result.has_value() || IsIOError(result)) {
return result;
}
}
}
// Fallback to loading the style from the resource id if specified.
if (fallback_resid != 0U) {
return theme->GetAssetManager()->GetBag(fallback_resid);
}
return base::unexpected(std::nullopt);
}
base::expected<const ResolvedBag*, NullOrIOError> GetXmlStyleBag(Theme* theme,
ResXMLParser* xml_parser,
uint32_t* out_theme_flags) {
if (xml_parser == nullptr) {
return base::unexpected(std::nullopt);
}
// Retrieve the style resource ID associated with the current XML tag's style attribute.
Res_value value;
const ssize_t idx = xml_parser->indexOfStyle();
if (idx < 0 || xml_parser->getAttributeValue(idx, &value) < 0) {
return base::unexpected(std::nullopt);
}
if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
// Resolve the attribute with out theme.
if (std::optional<AssetManager2::SelectedValue> result = theme->GetAttribute(value.data)) {
*out_theme_flags |= result->flags;
return theme->GetAssetManager()->ResolveBag(*result);
}
}
if (value.dataType == Res_value::TYPE_REFERENCE) {
return theme->GetAssetManager()->GetBag(value.data);
}
return base::unexpected(std::nullopt);
}
} // namespace
base::expected<std::monostate, IOError> ResolveAttrs(Theme* theme, uint32_t def_style_attr,
uint32_t def_style_res, uint32_t* src_values,
size_t src_values_length, uint32_t* attrs,
size_t attrs_length, uint32_t* out_values,
uint32_t* out_indices) {
DEBUG_LOG("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x", theme, def_style_attr,
def_style_res);
int indices_idx = 0;
const AssetManager2* assetmanager = theme->GetAssetManager();
// Load default style from attribute or resource id, if specified...
uint32_t def_style_theme_flags = 0U;
const auto default_style_bag = GetStyleBag(theme, def_style_attr, def_style_res,
&def_style_theme_flags);
if (UNLIKELY(IsIOError(default_style_bag))) {
return base::unexpected(GetIOError(default_style_bag.error()));
}
BagAttributeFinder def_style_attr_finder(default_style_bag.value_or(nullptr));
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
for (size_t ii = 0; ii < attrs_length; ii++) {
const uint32_t cur_ident = attrs[ii];
DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident);
// Try to find a value for this attribute... we prioritize values
// coming from, first XML attributes, then XML style, then default
// style, and finally the theme.
// Retrieve the current input value if available.
AssetManager2::SelectedValue value{};
if (src_values_length > 0 && src_values[ii] != 0) {
value.type = Res_value::TYPE_ATTRIBUTE;
value.data = src_values[ii];
DEBUG_LOG("-> From values: type=0x%x, data=0x%08x", value.type, value.data);
} else {
const ResolvedBag::Entry* const entry = def_style_attr_finder.Find(cur_ident);
if (entry != def_style_attr_finder.end()) {
value = AssetManager2::SelectedValue(*default_style_bag, *entry);
value.flags |= def_style_theme_flags;
DEBUG_LOG("-> From def style: type=0x%x, data=0x%08x", value.type, value.data);
}
}
if (value.type != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
const auto result = theme->ResolveAttributeReference(value);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data);
} else if (value.data != Res_value::DATA_NULL_EMPTY) {
// If we still don't have a value for this attribute, try to find it in the theme!
if (auto attr_value = theme->GetAttribute(cur_ident)) {
value = *attr_value;
DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
const auto result = assetmanager->ResolveReference(value, true /* cache_value */);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
DEBUG_LOG("-> Resolved theme: type=0x%x, data=0x%08x", value.type, value.data);
}
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.type == Res_value::TYPE_REFERENCE && value.data == 0) {
DEBUG_LOG("-> Setting to @null!");
value.type = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
value.cookie = kInvalidCookie;
}
DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data);
// Write the final value back to Java.
out_values[STYLE_TYPE] = value.type;
out_values[STYLE_DATA] = value.data;
out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
out_values[STYLE_RESOURCE_ID] = value.resid;
out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
out_values[STYLE_DENSITY] = value.config.density;
if (out_indices != nullptr &&
(value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY)) {
out_indices[++indices_idx] = ii;
}
out_values += STYLE_NUM_ENTRIES;
}
if (out_indices != nullptr) {
out_indices[0] = indices_idx;
}
return {};
}
base::expected<std::monostate, IOError> ApplyStyle(Theme* theme, ResXMLParser* xml_parser,
uint32_t def_style_attr,
uint32_t def_style_resid,
const uint32_t* attrs, size_t attrs_length,
uint32_t* out_values, uint32_t* out_indices) {
DEBUG_LOG("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x xml=0x%p", theme,
def_style_attr, def_style_resid, xml_parser);
int indices_idx = 0;
const AssetManager2* assetmanager = theme->GetAssetManager();
// Load default style from attribute, if specified...
uint32_t def_style_theme_flags = 0U;
const auto default_style_bag = GetStyleBag(theme, def_style_attr, def_style_resid,
&def_style_theme_flags);
if (IsIOError(default_style_bag)) {
return base::unexpected(GetIOError(default_style_bag.error()));
}
// Retrieve the style resource ID associated with the current XML tag's style attribute.
uint32_t xml_style_theme_flags = 0U;
const auto xml_style_bag = GetXmlStyleBag(theme, xml_parser, &def_style_theme_flags);
if (IsIOError(xml_style_bag)) {
return base::unexpected(GetIOError(xml_style_bag.error()));
}
BagAttributeFinder def_style_attr_finder(default_style_bag.value_or(nullptr));
BagAttributeFinder xml_style_attr_finder(xml_style_bag.value_or(nullptr));
XmlAttributeFinder xml_attr_finder(xml_parser);
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
for (size_t ii = 0; ii < attrs_length; ii++) {
const uint32_t cur_ident = attrs[ii];
DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident);
AssetManager2::SelectedValue value{};
uint32_t value_source_resid = 0;
// Try to find a value for this attribute... we prioritize values
// coming from, first XML attributes, then XML style, then default
// style, and finally the theme.
// Walk through the xml attributes looking for the requested attribute.
const size_t xml_attr_idx = xml_attr_finder.Find(cur_ident);
if (xml_attr_idx != xml_attr_finder.end()) {
// We found the attribute we were looking for.
Res_value attribute_value{};
xml_parser->getAttributeValue(xml_attr_idx, &attribute_value);
value.type = attribute_value.dataType;
value.data = attribute_value.data;
value_source_resid = xml_parser->getSourceResourceId();
DEBUG_LOG("-> From XML: type=0x%x, data=0x%08x", value.type, value.data);
}
if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) {
// Walk through the style class values looking for the requested attribute.
const ResolvedBag::Entry* entry = xml_style_attr_finder.Find(cur_ident);
if (entry != xml_style_attr_finder.end()) {
value = AssetManager2::SelectedValue(*xml_style_bag, *entry);
value.flags |= xml_style_theme_flags;
value_source_resid = entry->style;
DEBUG_LOG("-> From style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data,
value_source_resid);
}
}
if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) {
// Walk through the default style values looking for the requested attribute.
const ResolvedBag::Entry* entry = def_style_attr_finder.Find(cur_ident);
if (entry != def_style_attr_finder.end()) {
value = AssetManager2::SelectedValue(*default_style_bag, *entry);
value.flags |= def_style_theme_flags;
value_source_resid = entry->style;
DEBUG_LOG("-> From def style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data,
entry->style);
}
}
if (value.type != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
auto result = theme->ResolveAttributeReference(value);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data);
} else if (value.data != Res_value::DATA_NULL_EMPTY) {
// If we still don't have a value for this attribute, try to find it in the theme!
if (auto attr_value = theme->GetAttribute(cur_ident)) {
value = *attr_value;
DEBUG_LOG("-> From theme: type=0x%x, data=0x%08x", value.type, value.data);
auto result = assetmanager->ResolveReference(value, true /* cache_value */);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
DEBUG_LOG("-> Resolved theme: type=0x%x, data=0x%08x", value.type, value.data);
// TODO: set value_source_resid for the style in the theme that was used.
}
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) {
DEBUG_LOG("-> Setting to @null!");
value.type = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
value.cookie = kInvalidCookie;
}
DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data);
// Write the final value back to Java.
out_values[STYLE_TYPE] = value.type;
out_values[STYLE_DATA] = value.data;
out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
out_values[STYLE_RESOURCE_ID] = value.resid;
out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
out_values[STYLE_DENSITY] = value.config.density;
out_values[STYLE_SOURCE_RESOURCE_ID] = value_source_resid;
if (value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY) {
// out_indices must NOT be nullptr.
out_indices[++indices_idx] = ii;
}
out_values += STYLE_NUM_ENTRIES;
}
// out_indices must NOT be nullptr.
out_indices[0] = indices_idx;
return {};
}
base::expected<std::monostate, IOError> RetrieveAttributes(AssetManager2* assetmanager,
ResXMLParser* xml_parser,
uint32_t* attrs,
size_t attrs_length,
uint32_t* out_values,
uint32_t* out_indices) {
int indices_idx = 0;
// Retrieve the XML attributes, if requested.
size_t ix = 0;
const size_t xml_attr_count = xml_parser->getAttributeCount();
uint32_t cur_xml_attr = xml_parser->getAttributeNameResID(ix);
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
for (size_t ii = 0; ii < attrs_length; ii++) {
const uint32_t cur_ident = attrs[ii];
AssetManager2::SelectedValue value{};
// Try to find a value for this attribute...
// Skip through XML attributes until the end or the next possible match.
while (ix < xml_attr_count && cur_ident > cur_xml_attr) {
cur_xml_attr = xml_parser->getAttributeNameResID(++ix);
}
// Retrieve the current XML attribute if it matches, and step to next.
if (ix < xml_attr_count && cur_ident == cur_xml_attr) {
Res_value attribute_value{};
xml_parser->getAttributeValue(ix, &attribute_value);
value.type = attribute_value.dataType;
value.data = attribute_value.data;
cur_xml_attr = xml_parser->getAttributeNameResID(++ix);
}
if (value.type != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
auto result = assetmanager->ResolveReference(value);
if (UNLIKELY(IsIOError(result))) {
return base::unexpected(GetIOError(result.error()));
}
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) {
value.type = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
value.cookie = kInvalidCookie;
}
// Write the final value back to Java.
out_values[STYLE_TYPE] = value.type;
out_values[STYLE_DATA] = value.data;
out_values[STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(value.cookie);
out_values[STYLE_RESOURCE_ID] = value.resid;
out_values[STYLE_CHANGING_CONFIGURATIONS] = value.flags;
out_values[STYLE_DENSITY] = value.config.density;
if (out_indices != nullptr &&
(value.type != Res_value::TYPE_NULL ||
value.data == Res_value::DATA_NULL_EMPTY)) {
out_indices[++indices_idx] = ii;
}
out_values += STYLE_NUM_ENTRIES;
}
if (out_indices != nullptr) {
out_indices[0] = indices_idx;
}
return {};
}
} // namespace android
|