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