blob: 8ed27c3b95f6db58afdd15f4d92527e4870070df [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070021#include "link/Linkers.h"
22
23#include <algorithm>
24#include <cassert>
25
26namespace aapt {
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
29 const int sdkVersionToGenerate) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -070030 // We assume the caller is trying to generate a version greater than the current configuration.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031 assert(sdkVersionToGenerate > config.sdkVersion);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033 const auto endIter = entry->values.end();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080034 auto iter = entry->values.begin();
35 for (; iter != endIter; ++iter) {
36 if ((*iter)->config == config) {
37 break;
38 }
39 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
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 Lesinskie4bb9eb2016-02-12 22:18:51 -080053 tempConfig.sdkVersion = (*iter)->config.sdkVersion;
54 if (tempConfig == (*iter)->config) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 // The two configs are the same, check the sdk version.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080056 return sdkVersionToGenerate < (*iter)->config.sdkVersion;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 }
58 }
59
60 // No match was found, so we should generate the versioned resource.
61 return true;
62}
63
64bool 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 Lesinskie4bb9eb2016-02-12 22:18:51 -080073 ResourceConfigValue* configValue = entry->values[i].get();
74 if (configValue->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 // 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 Lesinskie4bb9eb2016-02-12 22:18:51 -080080 if (Style* style = valueCast<Style>(configValue->value.get())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 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 Lesinskie4bb9eb2016-02-12 22:18:51 -080090 if (sdkLevel > std::max<size_t>(configValue->config.sdkVersion, 1)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 // 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 Lesinskie78fd612015-10-22 12:48:43 -0700112 if (shouldGenerateVersionedResource(entry.get(),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800113 configValue->config,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 minSdkStripped.value())) {
115 // Let's create a new Style for this versioned resource.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800116 ConfigDescription newConfig(configValue->config);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 newConfig.sdkVersion = minSdkStripped.value();
118
Adam Lesinskie78fd612015-10-22 12:48:43 -0700119 std::unique_ptr<Style> newStyle(style->clone(&table->stringPool));
120 newStyle->setComment(style->getComment());
121 newStyle->setSource(style->getSource());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122
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 Lesinskie4bb9eb2016-02-12 22:18:51 -0800129 entry->findOrCreateValue(newConfig, {})->value =
130 std::move(newStyle);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 return true;
139}
140
141} // namespace aapt