Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include "ConfigDescription.h" |
| 18 | #include "ResourceTable.h" |
| 19 | #include "SdkConstants.h" |
| 20 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "link/Linkers.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <cassert> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config, |
| 29 | const int sdkVersionToGenerate) { |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 30 | // We assume the caller is trying to generate a version greater than the current configuration. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | assert(sdkVersionToGenerate > config.sdkVersion); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | const auto endIter = entry->values.end(); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 34 | auto iter = entry->values.begin(); |
| 35 | for (; iter != endIter; ++iter) { |
| 36 | if ((*iter)->config == config) { |
| 37 | break; |
| 38 | } |
| 39 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | |
| 41 | // The source config came from this list, so it should be here. |
| 42 | assert(iter != entry->values.end()); |
| 43 | ++iter; |
| 44 | |
| 45 | // The next configuration either only varies in sdkVersion, or it is completely different |
| 46 | // and therefore incompatible. If it is incompatible, we must generate the versioned resource. |
| 47 | |
| 48 | // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other |
| 49 | // qualifiers, so we need to iterate through the entire list to be sure there |
| 50 | // are no higher sdk level versions of this resource. |
| 51 | ConfigDescription tempConfig(config); |
| 52 | for (; iter != endIter; ++iter) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 53 | tempConfig.sdkVersion = (*iter)->config.sdkVersion; |
| 54 | if (tempConfig == (*iter)->config) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | // The two configs are the same, check the sdk version. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 56 | return sdkVersionToGenerate < (*iter)->config.sdkVersion; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | // No match was found, so we should generate the versioned resource. |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool AutoVersioner::consume(IAaptContext* context, ResourceTable* table) { |
| 65 | for (auto& package : table->packages) { |
| 66 | for (auto& type : package->types) { |
| 67 | if (type->type != ResourceType::kStyle) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | for (auto& entry : type->entries) { |
| 72 | for (size_t i = 0; i < entry->values.size(); i++) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 73 | ResourceConfigValue* configValue = entry->values[i].get(); |
| 74 | if (configValue->config.sdkVersion >= SDK_LOLLIPOP_MR1) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | // If this configuration is only used on L-MR1 then we don't need |
| 76 | // to do anything since we use private attributes since that version. |
| 77 | continue; |
| 78 | } |
| 79 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 80 | if (Style* style = valueCast<Style>(configValue->value.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | Maybe<size_t> minSdkStripped; |
| 82 | std::vector<Style::Entry> stripped; |
| 83 | |
| 84 | auto iter = style->entries.begin(); |
| 85 | while (iter != style->entries.end()) { |
| 86 | assert(iter->key.id && "IDs must be assigned and linked"); |
| 87 | |
| 88 | // Find the SDK level that is higher than the configuration allows. |
| 89 | const size_t sdkLevel = findAttributeSdkLevel(iter->key.id.value()); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 90 | if (sdkLevel > std::max<size_t>(configValue->config.sdkVersion, 1)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 91 | // Record that we are about to strip this. |
| 92 | stripped.emplace_back(std::move(*iter)); |
| 93 | |
| 94 | // We use the smallest SDK level to generate the new style. |
| 95 | if (minSdkStripped) { |
| 96 | minSdkStripped = std::min(minSdkStripped.value(), sdkLevel); |
| 97 | } else { |
| 98 | minSdkStripped = sdkLevel; |
| 99 | } |
| 100 | |
| 101 | // Erase this from this style. |
| 102 | iter = style->entries.erase(iter); |
| 103 | continue; |
| 104 | } |
| 105 | ++iter; |
| 106 | } |
| 107 | |
| 108 | if (minSdkStripped && !stripped.empty()) { |
| 109 | // We found attributes from a higher SDK level. Check that |
| 110 | // there is no other defined resource for the version we want to |
| 111 | // generate. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 112 | if (shouldGenerateVersionedResource(entry.get(), |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 113 | configValue->config, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | minSdkStripped.value())) { |
| 115 | // Let's create a new Style for this versioned resource. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 116 | ConfigDescription newConfig(configValue->config); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | newConfig.sdkVersion = minSdkStripped.value(); |
| 118 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 119 | std::unique_ptr<Style> newStyle(style->clone(&table->stringPool)); |
| 120 | newStyle->setComment(style->getComment()); |
| 121 | newStyle->setSource(style->getSource()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | |
| 123 | // Move the previously stripped attributes into this style. |
| 124 | newStyle->entries.insert(newStyle->entries.end(), |
| 125 | std::make_move_iterator(stripped.begin()), |
| 126 | std::make_move_iterator(stripped.end())); |
| 127 | |
| 128 | // Insert the new Resource into the correct place. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 129 | entry->findOrCreateValue(newConfig, {})->value = |
| 130 | std::move(newStyle); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | } // namespace aapt |