Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 17 | #include "androidfw/AttributeResolution.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 18 | |
| 19 | #include <cstdint> |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 20 | |
Mark Salyzyn | 6f773a0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 22 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 23 | #include "androidfw/AssetManager2.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 24 | #include "androidfw/AttributeFinder.h" |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 25 | |
| 26 | constexpr bool kDebugStyles = false; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 27 | #define DEBUG_LOG(...) do { if (kDebugStyles) { ALOGI(__VA_ARGS__); } } while(0) |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 33 | // Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0. |
| 34 | static uint32_t ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) { |
| 35 | return cookie != kInvalidCookie ? static_cast<uint32_t>(cookie + 1) : static_cast<uint32_t>(-1); |
| 36 | } |
| 37 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 38 | class XmlAttributeFinder |
| 39 | : public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 40 | public: |
| 41 | explicit XmlAttributeFinder(const ResXMLParser* parser) |
Ryan Mitchell | 87e8954 | 2020-10-05 14:24:35 -0700 | [diff] [blame] | 42 | : BackTrackingAttributeFinder(0, parser != nullptr ? parser->getAttributeCount() : 0), |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 43 | parser_(parser) {} |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 45 | inline uint32_t GetAttribute(size_t index) const { |
| 46 | return parser_->getAttributeNameResID(index); |
| 47 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 49 | private: |
| 50 | const ResXMLParser* parser_; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 53 | class BagAttributeFinder |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 54 | : public BackTrackingAttributeFinder<BagAttributeFinder, const ResolvedBag::Entry*> { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 55 | public: |
Chih-Hung Hsieh | c2ace0c | 2018-12-20 13:46:53 -0800 | [diff] [blame] | 56 | explicit BagAttributeFinder(const ResolvedBag* bag) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 57 | : BackTrackingAttributeFinder(bag != nullptr ? bag->entries : nullptr, |
| 58 | bag != nullptr ? bag->entries + bag->entry_count : nullptr) { |
| 59 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 61 | inline uint32_t GetAttribute(const ResolvedBag::Entry* entry) const { |
| 62 | return entry->key; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 63 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 66 | base::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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 82 | // 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 | |
| 90 | base::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 Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 112 | 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 | |
| 121 | base::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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 141 | |
| 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 146 | DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 148 | // 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 153 | AssetManager2::SelectedValue value{}; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 154 | if (src_values_length > 0 && src_values[ii] != 0) { |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 155 | value.type = Res_value::TYPE_ATTRIBUTE; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 156 | value.data = src_values[ii]; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 157 | DEBUG_LOG("-> From values: type=0x%x, data=0x%08x", value.type, value.data); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 158 | } else { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 159 | const ResolvedBag::Entry* const entry = def_style_attr_finder.Find(cur_ident); |
| 160 | if (entry != def_style_attr_finder.end()) { |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 161 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 166 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 167 | if (value.type != Res_value::TYPE_NULL) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 168 | // Take care of resolving the found resource to its final value. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 169 | const auto result = theme->ResolveAttributeReference(value); |
| 170 | if (UNLIKELY(IsIOError(result))) { |
| 171 | return base::unexpected(GetIOError(result.error())); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 172 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 173 | DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 174 | } else if (value.data != Res_value::DATA_NULL_EMPTY) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 175 | // If we still don't have a value for this attribute, try to find it in the theme! |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 176 | 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 Mitchell | 87e8954 | 2020-10-05 14:24:35 -0700 | [diff] [blame] | 180 | const auto result = assetmanager->ResolveReference(value, true /* cache_value */); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 181 | if (UNLIKELY(IsIOError(result))) { |
| 182 | return base::unexpected(GetIOError(result.error())); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 183 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 184 | DEBUG_LOG("-> Resolved theme: type=0x%x, data=0x%08x", value.type, value.data); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 185 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 188 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 189 | if (value.type == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 190 | DEBUG_LOG("-> Setting to @null!"); |
| 191 | value.type = Res_value::TYPE_NULL; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 192 | value.data = Res_value::DATA_NULL_UNDEFINED; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 193 | value.cookie = kInvalidCookie; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 194 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 195 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 196 | DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 197 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 198 | // Write the final value back to Java. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 199 | out_values[STYLE_TYPE] = value.type; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 200 | out_values[STYLE_DATA] = value.data; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 201 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 206 | if (out_indices != nullptr && |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 207 | (value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY)) { |
| 208 | out_indices[++indices_idx] = ii; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | out_values += STYLE_NUM_ENTRIES; |
| 212 | } |
| 213 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 214 | if (out_indices != nullptr) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 215 | out_indices[0] = indices_idx; |
| 216 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 217 | return {}; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 220 | base::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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 227 | |
| 228 | int indices_idx = 0; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 229 | const AssetManager2* assetmanager = theme->GetAssetManager(); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 230 | |
| 231 | // Load default style from attribute, if specified... |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 232 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 237 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 239 | // Retrieve the style resource ID associated with the current XML tag's style attribute. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 240 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 246 | BagAttributeFinder def_style_attr_finder(default_style_bag.value_or(nullptr)); |
| 247 | BagAttributeFinder xml_style_attr_finder(xml_style_bag.value_or(nullptr)); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 248 | XmlAttributeFinder xml_attr_finder(xml_parser); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 249 | |
| 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 254 | DEBUG_LOG("RETRIEVING ATTR 0x%08x...", cur_ident); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 255 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 256 | AssetManager2::SelectedValue value{}; |
Aurimas Liutikas | 949b05d | 2019-01-30 17:20:41 -0800 | [diff] [blame] | 257 | uint32_t value_source_resid = 0; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 258 | |
| 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 Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 265 | if (xml_attr_idx != xml_attr_finder.end()) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 266 | // We found the attribute we were looking for. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 267 | 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 Liutikas | 949b05d | 2019-01-30 17:20:41 -0800 | [diff] [blame] | 271 | value_source_resid = xml_parser->getSourceResourceId(); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 272 | DEBUG_LOG("-> From XML: type=0x%x, data=0x%08x", value.type, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 275 | if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) { |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 276 | // Walk through the style class values looking for the requested attribute. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 277 | const ResolvedBag::Entry* entry = xml_style_attr_finder.Find(cur_ident); |
| 278 | if (entry != xml_style_attr_finder.end()) { |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 279 | value = AssetManager2::SelectedValue(*xml_style_bag, *entry); |
| 280 | value.flags |= xml_style_theme_flags; |
Aurimas Liutikas | 949b05d | 2019-01-30 17:20:41 -0800 | [diff] [blame] | 281 | value_source_resid = entry->style; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 282 | DEBUG_LOG("-> From style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data, |
| 283 | value_source_resid); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 286 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 287 | if (value.type == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY) { |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 288 | // Walk through the default style values looking for the requested attribute. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 289 | const ResolvedBag::Entry* entry = def_style_attr_finder.Find(cur_ident); |
| 290 | if (entry != def_style_attr_finder.end()) { |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 291 | value = AssetManager2::SelectedValue(*default_style_bag, *entry); |
| 292 | value.flags |= def_style_theme_flags; |
Aurimas Liutikas | 949b05d | 2019-01-30 17:20:41 -0800 | [diff] [blame] | 293 | value_source_resid = entry->style; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 294 | DEBUG_LOG("-> From def style: type=0x%x, data=0x%08x, style=0x%08x", value.type, value.data, |
| 295 | entry->style); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 298 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 299 | if (value.type != Res_value::TYPE_NULL) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 300 | // Take care of resolving the found resource to its final value. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 301 | auto result = theme->ResolveAttributeReference(value); |
| 302 | if (UNLIKELY(IsIOError(result))) { |
| 303 | return base::unexpected(GetIOError(result.error())); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 304 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 305 | DEBUG_LOG("-> Resolved attr: type=0x%x, data=0x%08x", value.type, value.data); |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 306 | } 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 308 | 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 Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 311 | |
Ryan Mitchell | 87e8954 | 2020-10-05 14:24:35 -0700 | [diff] [blame] | 312 | auto result = assetmanager->ResolveReference(value, true /* cache_value */); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 313 | if (UNLIKELY(IsIOError(result))) { |
| 314 | return base::unexpected(GetIOError(result.error())); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 315 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 316 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 318 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 321 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 322 | if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) { |
| 323 | DEBUG_LOG("-> Setting to @null!"); |
| 324 | value.type = Res_value::TYPE_NULL; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 325 | value.data = Res_value::DATA_NULL_UNDEFINED; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 326 | value.cookie = kInvalidCookie; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 327 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 328 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 329 | DEBUG_LOG("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.type, value.data); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 330 | |
| 331 | // Write the final value back to Java. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 332 | out_values[STYLE_TYPE] = value.type; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 333 | out_values[STYLE_DATA] = value.data; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 334 | 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 Liutikas | f9dbd5f | 2019-03-07 11:21:51 -0800 | [diff] [blame] | 338 | out_values[STYLE_SOURCE_RESOURCE_ID] = value_source_resid; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 339 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 340 | if (value.type != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY) { |
Adam Lesinski | 06d3e8f | 2017-01-05 17:03:55 -0800 | [diff] [blame] | 341 | // out_indices must NOT be nullptr. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 342 | out_indices[++indices_idx] = ii; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 343 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 344 | out_values += STYLE_NUM_ENTRIES; |
| 345 | } |
| 346 | |
Adam Lesinski | 06d3e8f | 2017-01-05 17:03:55 -0800 | [diff] [blame] | 347 | // out_indices must NOT be nullptr. |
| 348 | out_indices[0] = indices_idx; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 349 | return {}; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 352 | base::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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 358 | int indices_idx = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 359 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 360 | // Retrieve the XML attributes, if requested. |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 361 | size_t ix = 0; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 362 | const size_t xml_attr_count = xml_parser->getAttributeCount(); |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 363 | uint32_t cur_xml_attr = xml_parser->getAttributeNameResID(ix); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 364 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 365 | // 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 369 | AssetManager2::SelectedValue value{}; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 370 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 371 | // 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 Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 374 | cur_xml_attr = xml_parser->getAttributeNameResID(++ix); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 377 | // 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 387 | // Take care of resolving the found resource to its final value. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 388 | auto result = assetmanager->ResolveReference(value); |
| 389 | if (UNLIKELY(IsIOError(result))) { |
| 390 | return base::unexpected(GetIOError(result.error())); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 391 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 392 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 393 | |
| 394 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 395 | if (value.type == Res_value::TYPE_REFERENCE && value.data == 0U) { |
| 396 | value.type = Res_value::TYPE_NULL; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 397 | value.data = Res_value::DATA_NULL_UNDEFINED; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 398 | value.cookie = kInvalidCookie; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Write the final value back to Java. |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 402 | out_values[STYLE_TYPE] = value.type; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 403 | out_values[STYLE_DATA] = value.data; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 404 | 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 Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 408 | |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 409 | if (out_indices != nullptr && |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 410 | (value.type != Res_value::TYPE_NULL || |
| 411 | value.data == Res_value::DATA_NULL_EMPTY)) { |
| 412 | out_indices[++indices_idx] = ii; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | out_values += STYLE_NUM_ENTRIES; |
| 416 | } |
| 417 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 418 | if (out_indices != nullptr) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 419 | out_indices[0] = indices_idx; |
| 420 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 421 | return {}; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 424 | } // namespace android |