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 | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 17 | #include "AttributeFinder.h" |
| 18 | |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 19 | #include "androidfw/AttributeResolution.h" |
| 20 | #include "androidfw/ResourceTypes.h" |
| 21 | |
| 22 | #include <android/log.h> |
| 23 | #include <cstdint> |
| 24 | |
| 25 | constexpr bool kDebugStyles = false; |
| 26 | |
| 27 | namespace android { |
| 28 | |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 29 | class XmlAttributeFinder : public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 30 | public: |
| 31 | explicit XmlAttributeFinder(const ResXMLParser* parser) |
| 32 | : BackTrackingAttributeFinder(0, parser != NULL ? parser->getAttributeCount() : 0), |
| 33 | parser_(parser) {} |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 35 | inline uint32_t GetAttribute(size_t index) const { return parser_->getAttributeNameResID(index); } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 37 | private: |
| 38 | const ResXMLParser* parser_; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 41 | class BagAttributeFinder |
| 42 | : public BackTrackingAttributeFinder<BagAttributeFinder, const ResTable::bag_entry*> { |
| 43 | public: |
| 44 | BagAttributeFinder(const ResTable::bag_entry* start, const ResTable::bag_entry* end) |
| 45 | : BackTrackingAttributeFinder(start, end) {} |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 47 | inline uint32_t GetAttribute(const ResTable::bag_entry* entry) const { |
| 48 | return entry->map.name.ident; |
| 49 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 52 | bool ResolveAttrs(ResTable::Theme* theme, uint32_t def_style_attr, uint32_t def_style_res, |
| 53 | uint32_t* src_values, size_t src_values_length, uint32_t* attrs, |
| 54 | size_t attrs_length, uint32_t* out_values, uint32_t* out_indices) { |
| 55 | if (kDebugStyles) { |
| 56 | ALOGI("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x", theme, def_style_attr, |
| 57 | def_style_res); |
| 58 | } |
| 59 | |
| 60 | const ResTable& res = theme->getResTable(); |
| 61 | ResTable_config config; |
| 62 | Res_value value; |
| 63 | |
| 64 | int indices_idx = 0; |
| 65 | |
| 66 | // Load default style from attribute, if specified... |
| 67 | uint32_t def_style_bag_type_set_flags = 0; |
| 68 | if (def_style_attr != 0) { |
| 69 | Res_value value; |
| 70 | if (theme->getAttribute(def_style_attr, &value, &def_style_bag_type_set_flags) >= 0) { |
| 71 | if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 72 | def_style_res = value.data; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Now lock down the resource object and start pulling stuff from it. |
| 78 | res.lock(); |
| 79 | |
| 80 | // Retrieve the default style bag, if requested. |
| 81 | const ResTable::bag_entry* def_style_start = NULL; |
| 82 | uint32_t def_style_type_set_flags = 0; |
| 83 | ssize_t bag_off = |
| 84 | def_style_res != 0 |
| 85 | ? res.getBagLocked(def_style_res, &def_style_start, &def_style_type_set_flags) |
| 86 | : -1; |
| 87 | def_style_type_set_flags |= def_style_bag_type_set_flags; |
| 88 | const ResTable::bag_entry* const def_style_end = def_style_start + (bag_off >= 0 ? bag_off : 0); |
| 89 | BagAttributeFinder def_style_attr_finder(def_style_start, def_style_end); |
| 90 | |
| 91 | // Now iterate through all of the attributes that the client has requested, |
| 92 | // filling in each with whatever data we can find. |
| 93 | for (size_t ii = 0; ii < attrs_length; ii++) { |
| 94 | const uint32_t cur_ident = attrs[ii]; |
| 95 | |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 96 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 97 | ALOGI("RETRIEVING ATTR 0x%08x...", cur_ident); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 100 | ssize_t block = -1; |
| 101 | uint32_t type_set_flags = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 103 | value.dataType = Res_value::TYPE_NULL; |
| 104 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 105 | config.density = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 107 | // Try to find a value for this attribute... we prioritize values |
| 108 | // coming from, first XML attributes, then XML style, then default |
| 109 | // style, and finally the theme. |
| 110 | |
| 111 | // Retrieve the current input value if available. |
| 112 | if (src_values_length > 0 && src_values[ii] != 0) { |
| 113 | value.dataType = Res_value::TYPE_ATTRIBUTE; |
| 114 | value.data = src_values[ii]; |
| 115 | if (kDebugStyles) { |
| 116 | ALOGI("-> From values: type=0x%x, data=0x%08x", value.dataType, value.data); |
| 117 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 120 | if (value.dataType == Res_value::TYPE_NULL) { |
| 121 | const ResTable::bag_entry* const def_style_entry = def_style_attr_finder.Find(cur_ident); |
| 122 | if (def_style_entry != def_style_end) { |
| 123 | block = def_style_entry->stringBlock; |
| 124 | type_set_flags = def_style_type_set_flags; |
| 125 | value = def_style_entry->map.value; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 126 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 127 | ALOGI("-> From def style: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 128 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 131 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 132 | uint32_t resid = 0; |
| 133 | if (value.dataType != Res_value::TYPE_NULL) { |
| 134 | // Take care of resolving the found resource to its final value. |
| 135 | ssize_t new_block = |
| 136 | theme->resolveAttributeReference(&value, block, &resid, &type_set_flags, &config); |
| 137 | if (new_block >= 0) block = new_block; |
| 138 | if (kDebugStyles) { |
| 139 | ALOGI("-> Resolved attr: type=0x%x, data=0x%08x", value.dataType, value.data); |
| 140 | } |
| 141 | } else { |
| 142 | // If we still don't have a value for this attribute, try to find |
| 143 | // it in the theme! |
| 144 | ssize_t new_block = theme->getAttribute(cur_ident, &value, &type_set_flags); |
| 145 | if (new_block >= 0) { |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 146 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 147 | ALOGI("-> From theme: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 148 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 149 | new_block = res.resolveReference(&value, new_block, &resid, &type_set_flags, &config); |
| 150 | if (new_block >= 0) block = new_block; |
| 151 | if (kDebugStyles) { |
| 152 | ALOGI("-> Resolved theme: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 153 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 157 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 158 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 159 | if (kDebugStyles) { |
| 160 | ALOGI("-> Setting to @null!"); |
| 161 | } |
| 162 | value.dataType = Res_value::TYPE_NULL; |
| 163 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 164 | block = -1; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 166 | |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 167 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 168 | ALOGI("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 171 | // Write the final value back to Java. |
| 172 | out_values[STYLE_TYPE] = value.dataType; |
| 173 | out_values[STYLE_DATA] = value.data; |
| 174 | out_values[STYLE_ASSET_COOKIE] = |
| 175 | block != -1 ? static_cast<uint32_t>(res.getTableCookie(block)) : static_cast<uint32_t>(-1); |
| 176 | out_values[STYLE_RESOURCE_ID] = resid; |
| 177 | out_values[STYLE_CHANGING_CONFIGURATIONS] = type_set_flags; |
| 178 | out_values[STYLE_DENSITY] = config.density; |
| 179 | |
| 180 | if (out_indices != NULL && value.dataType != Res_value::TYPE_NULL) { |
| 181 | indices_idx++; |
| 182 | out_indices[indices_idx] = ii; |
| 183 | } |
| 184 | |
| 185 | out_values += STYLE_NUM_ENTRIES; |
| 186 | } |
| 187 | |
| 188 | res.unlock(); |
| 189 | |
| 190 | if (out_indices != NULL) { |
| 191 | out_indices[0] = indices_idx; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 196 | void ApplyStyle(ResTable::Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr, |
| 197 | uint32_t def_style_res, const uint32_t* attrs, size_t attrs_length, |
| 198 | uint32_t* out_values, uint32_t* out_indices) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 199 | if (kDebugStyles) { |
| 200 | ALOGI("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x xml=0x%p", theme, |
| 201 | def_style_attr, def_style_res, xml_parser); |
| 202 | } |
| 203 | |
| 204 | const ResTable& res = theme->getResTable(); |
| 205 | ResTable_config config; |
| 206 | Res_value value; |
| 207 | |
| 208 | int indices_idx = 0; |
| 209 | |
| 210 | // Load default style from attribute, if specified... |
| 211 | uint32_t def_style_bag_type_set_flags = 0; |
| 212 | if (def_style_attr != 0) { |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 213 | Res_value value; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 214 | if (theme->getAttribute(def_style_attr, &value, &def_style_bag_type_set_flags) >= 0) { |
| 215 | if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 216 | def_style_res = value.data; |
| 217 | } |
| 218 | } |
| 219 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 220 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 221 | // Retrieve the style class associated with the current XML tag. |
| 222 | int style = 0; |
| 223 | uint32_t style_bag_type_set_flags = 0; |
| 224 | if (xml_parser != NULL) { |
| 225 | ssize_t idx = xml_parser->indexOfStyle(); |
| 226 | if (idx >= 0 && xml_parser->getAttributeValue(idx, &value) >= 0) { |
| 227 | if (value.dataType == value.TYPE_ATTRIBUTE) { |
| 228 | if (theme->getAttribute(value.data, &value, &style_bag_type_set_flags) < 0) { |
| 229 | value.dataType = Res_value::TYPE_NULL; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 230 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 231 | } |
| 232 | if (value.dataType == value.TYPE_REFERENCE) { |
| 233 | style = value.data; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Now lock down the resource object and start pulling stuff from it. |
| 239 | res.lock(); |
| 240 | |
| 241 | // Retrieve the default style bag, if requested. |
| 242 | const ResTable::bag_entry* def_style_attr_start = NULL; |
| 243 | uint32_t def_style_type_set_flags = 0; |
| 244 | ssize_t bag_off = |
| 245 | def_style_res != 0 |
| 246 | ? res.getBagLocked(def_style_res, &def_style_attr_start, &def_style_type_set_flags) |
| 247 | : -1; |
| 248 | def_style_type_set_flags |= def_style_bag_type_set_flags; |
| 249 | const ResTable::bag_entry* const def_style_attr_end = |
| 250 | def_style_attr_start + (bag_off >= 0 ? bag_off : 0); |
| 251 | BagAttributeFinder def_style_attr_finder(def_style_attr_start, def_style_attr_end); |
| 252 | |
| 253 | // Retrieve the style class bag, if requested. |
| 254 | const ResTable::bag_entry* style_attr_start = NULL; |
| 255 | uint32_t style_type_set_flags = 0; |
| 256 | bag_off = style != 0 ? res.getBagLocked(style, &style_attr_start, &style_type_set_flags) : -1; |
| 257 | style_type_set_flags |= style_bag_type_set_flags; |
| 258 | const ResTable::bag_entry* const style_attr_end = style_attr_start + (bag_off >= 0 ? bag_off : 0); |
| 259 | BagAttributeFinder style_attr_finder(style_attr_start, style_attr_end); |
| 260 | |
| 261 | // Retrieve the XML attributes, if requested. |
| 262 | static const ssize_t kXmlBlock = 0x10000000; |
| 263 | XmlAttributeFinder xml_attr_finder(xml_parser); |
| 264 | const size_t xml_attr_end = xml_parser != NULL ? xml_parser->getAttributeCount() : 0; |
| 265 | |
| 266 | // Now iterate through all of the attributes that the client has requested, |
| 267 | // filling in each with whatever data we can find. |
| 268 | for (size_t ii = 0; ii < attrs_length; ii++) { |
| 269 | const uint32_t cur_ident = attrs[ii]; |
| 270 | |
| 271 | if (kDebugStyles) { |
| 272 | ALOGI("RETRIEVING ATTR 0x%08x...", cur_ident); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 275 | ssize_t block = kXmlBlock; |
| 276 | uint32_t type_set_flags = 0; |
| 277 | |
| 278 | value.dataType = Res_value::TYPE_NULL; |
| 279 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 280 | config.density = 0; |
| 281 | |
| 282 | // Try to find a value for this attribute... we prioritize values |
| 283 | // coming from, first XML attributes, then XML style, then default |
| 284 | // style, and finally the theme. |
| 285 | |
| 286 | // Walk through the xml attributes looking for the requested attribute. |
| 287 | const size_t xml_attr_idx = xml_attr_finder.Find(cur_ident); |
| 288 | if (xml_attr_idx != xml_attr_end) { |
| 289 | // We found the attribute we were looking for. |
| 290 | xml_parser->getAttributeValue(xml_attr_idx, &value); |
| 291 | if (kDebugStyles) { |
| 292 | ALOGI("-> From XML: type=0x%x, data=0x%08x", value.dataType, value.data); |
| 293 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 296 | if (value.dataType == Res_value::TYPE_NULL) { |
| 297 | // Walk through the style class values looking for the requested attribute. |
| 298 | const ResTable::bag_entry* const style_attr_entry = style_attr_finder.Find(cur_ident); |
| 299 | if (style_attr_entry != style_attr_end) { |
| 300 | // We found the attribute we were looking for. |
| 301 | block = style_attr_entry->stringBlock; |
| 302 | type_set_flags = style_type_set_flags; |
| 303 | value = style_attr_entry->map.value; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 304 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 305 | ALOGI("-> From style: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 306 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 309 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 310 | if (value.dataType == Res_value::TYPE_NULL) { |
| 311 | // Walk through the default style values looking for the requested attribute. |
| 312 | const ResTable::bag_entry* const def_style_attr_entry = def_style_attr_finder.Find(cur_ident); |
| 313 | if (def_style_attr_entry != def_style_attr_end) { |
| 314 | // We found the attribute we were looking for. |
| 315 | block = def_style_attr_entry->stringBlock; |
| 316 | type_set_flags = style_type_set_flags; |
| 317 | value = def_style_attr_entry->map.value; |
| 318 | if (kDebugStyles) { |
| 319 | ALOGI("-> From def style: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 320 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 323 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 324 | uint32_t resid = 0; |
| 325 | if (value.dataType != Res_value::TYPE_NULL) { |
| 326 | // Take care of resolving the found resource to its final value. |
| 327 | ssize_t new_block = |
| 328 | theme->resolveAttributeReference(&value, block, &resid, &type_set_flags, &config); |
| 329 | if (new_block >= 0) { |
| 330 | block = new_block; |
| 331 | } |
| 332 | |
| 333 | if (kDebugStyles) { |
| 334 | ALOGI("-> Resolved attr: type=0x%x, data=0x%08x", value.dataType, value.data); |
| 335 | } |
| 336 | } else { |
| 337 | // If we still don't have a value for this attribute, try to find |
| 338 | // it in the theme! |
| 339 | ssize_t new_block = theme->getAttribute(cur_ident, &value, &type_set_flags); |
| 340 | if (new_block >= 0) { |
| 341 | if (kDebugStyles) { |
| 342 | ALOGI("-> From theme: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 343 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 344 | new_block = res.resolveReference(&value, new_block, &resid, &type_set_flags, &config); |
| 345 | if (new_block >= 0) { |
| 346 | block = new_block; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (kDebugStyles) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 350 | ALOGI("-> Resolved theme: type=0x%x, data=0x%08x", value.dataType, value.data); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 351 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 352 | } |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 355 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 356 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 357 | if (kDebugStyles) { |
| 358 | ALOGI("-> Setting to @null!"); |
| 359 | } |
| 360 | value.dataType = Res_value::TYPE_NULL; |
| 361 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 362 | block = kXmlBlock; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 363 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 364 | |
| 365 | if (kDebugStyles) { |
| 366 | ALOGI("Attribute 0x%08x: type=0x%x, data=0x%08x", cur_ident, value.dataType, value.data); |
| 367 | } |
| 368 | |
| 369 | // Write the final value back to Java. |
| 370 | out_values[STYLE_TYPE] = value.dataType; |
| 371 | out_values[STYLE_DATA] = value.data; |
| 372 | out_values[STYLE_ASSET_COOKIE] = block != kXmlBlock |
| 373 | ? static_cast<uint32_t>(res.getTableCookie(block)) |
| 374 | : static_cast<uint32_t>(-1); |
| 375 | out_values[STYLE_RESOURCE_ID] = resid; |
| 376 | out_values[STYLE_CHANGING_CONFIGURATIONS] = type_set_flags; |
| 377 | out_values[STYLE_DENSITY] = config.density; |
| 378 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 379 | if (value.dataType != Res_value::TYPE_NULL) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 380 | indices_idx++; |
| 381 | out_indices[indices_idx] = ii; |
| 382 | } |
| 383 | |
| 384 | out_values += STYLE_NUM_ENTRIES; |
| 385 | } |
| 386 | |
| 387 | res.unlock(); |
| 388 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 389 | out_indices[0] = indices_idx; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 392 | bool RetrieveAttributes(const ResTable* res, ResXMLParser* xml_parser, uint32_t* attrs, |
| 393 | size_t attrs_length, uint32_t* out_values, uint32_t* out_indices) { |
| 394 | ResTable_config config; |
| 395 | Res_value value; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 397 | int indices_idx = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 398 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 399 | // Now lock down the resource object and start pulling stuff from it. |
| 400 | res->lock(); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 401 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 402 | // Retrieve the XML attributes, if requested. |
| 403 | const size_t xml_attr_count = xml_parser->getAttributeCount(); |
| 404 | size_t ix = 0; |
| 405 | uint32_t cur_xml_attr = xml_parser->getAttributeNameResID(ix); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 406 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 407 | static const ssize_t kXmlBlock = 0x10000000; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 408 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 409 | // Now iterate through all of the attributes that the client has requested, |
| 410 | // filling in each with whatever data we can find. |
| 411 | for (size_t ii = 0; ii < attrs_length; ii++) { |
| 412 | const uint32_t cur_ident = attrs[ii]; |
| 413 | ssize_t block = kXmlBlock; |
| 414 | uint32_t type_set_flags = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 415 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 416 | value.dataType = Res_value::TYPE_NULL; |
| 417 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 418 | config.density = 0; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 419 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 420 | // Try to find a value for this attribute... |
| 421 | // Skip through XML attributes until the end or the next possible match. |
| 422 | while (ix < xml_attr_count && cur_ident > cur_xml_attr) { |
| 423 | ix++; |
| 424 | cur_xml_attr = xml_parser->getAttributeNameResID(ix); |
| 425 | } |
| 426 | // Retrieve the current XML attribute if it matches, and step to next. |
| 427 | if (ix < xml_attr_count && cur_ident == cur_xml_attr) { |
| 428 | xml_parser->getAttributeValue(ix, &value); |
| 429 | ix++; |
| 430 | cur_xml_attr = xml_parser->getAttributeNameResID(ix); |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 433 | uint32_t resid = 0; |
| 434 | if (value.dataType != Res_value::TYPE_NULL) { |
| 435 | // Take care of resolving the found resource to its final value. |
| 436 | // printf("Resolving attribute reference\n"); |
| 437 | ssize_t new_block = res->resolveReference(&value, block, &resid, &type_set_flags, &config); |
| 438 | if (new_block >= 0) block = new_block; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 439 | } |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 440 | |
| 441 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 442 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 443 | value.dataType = Res_value::TYPE_NULL; |
| 444 | value.data = Res_value::DATA_NULL_UNDEFINED; |
| 445 | block = kXmlBlock; |
| 446 | } |
| 447 | |
| 448 | // Write the final value back to Java. |
| 449 | out_values[STYLE_TYPE] = value.dataType; |
| 450 | out_values[STYLE_DATA] = value.data; |
| 451 | out_values[STYLE_ASSET_COOKIE] = block != kXmlBlock |
| 452 | ? static_cast<uint32_t>(res->getTableCookie(block)) |
| 453 | : static_cast<uint32_t>(-1); |
| 454 | out_values[STYLE_RESOURCE_ID] = resid; |
| 455 | out_values[STYLE_CHANGING_CONFIGURATIONS] = type_set_flags; |
| 456 | out_values[STYLE_DENSITY] = config.density; |
| 457 | |
| 458 | if (out_indices != NULL && value.dataType != Res_value::TYPE_NULL) { |
| 459 | indices_idx++; |
| 460 | out_indices[indices_idx] = ii; |
| 461 | } |
| 462 | |
| 463 | out_values += STYLE_NUM_ENTRIES; |
| 464 | } |
| 465 | |
| 466 | res->unlock(); |
| 467 | |
| 468 | if (out_indices != NULL) { |
| 469 | out_indices[0] = indices_idx; |
| 470 | } |
| 471 | return true; |
Adam Lesinski | 4452e13 | 2016-10-12 07:47:28 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 474 | } // namespace android |