blob: 459c330cfbdcc629489fecf05eaf51f61d58d5c0 [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) {
30 assert(sdkVersionToGenerate > config.sdkVersion);
31 const auto endIter = entry->values.end();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080032 auto iter = entry->values.begin();
33 for (; iter != endIter; ++iter) {
34 if ((*iter)->config == config) {
35 break;
36 }
37 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038
39 // The source config came from this list, so it should be here.
40 assert(iter != entry->values.end());
41 ++iter;
42
43 // The next configuration either only varies in sdkVersion, or it is completely different
44 // and therefore incompatible. If it is incompatible, we must generate the versioned resource.
45
46 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other
47 // qualifiers, so we need to iterate through the entire list to be sure there
48 // are no higher sdk level versions of this resource.
49 ConfigDescription tempConfig(config);
50 for (; iter != endIter; ++iter) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080051 tempConfig.sdkVersion = (*iter)->config.sdkVersion;
52 if (tempConfig == (*iter)->config) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053 // The two configs are the same, check the sdk version.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080054 return sdkVersionToGenerate < (*iter)->config.sdkVersion;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 }
56 }
57
58 // No match was found, so we should generate the versioned resource.
59 return true;
60}
61
62bool AutoVersioner::consume(IAaptContext* context, ResourceTable* table) {
63 for (auto& package : table->packages) {
64 for (auto& type : package->types) {
65 if (type->type != ResourceType::kStyle) {
66 continue;
67 }
68
69 for (auto& entry : type->entries) {
70 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080071 ResourceConfigValue* configValue = entry->values[i].get();
72 if (configValue->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 // If this configuration is only used on L-MR1 then we don't need
74 // to do anything since we use private attributes since that version.
75 continue;
76 }
77
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080078 if (Style* style = valueCast<Style>(configValue->value.get())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079 Maybe<size_t> minSdkStripped;
80 std::vector<Style::Entry> stripped;
81
82 auto iter = style->entries.begin();
83 while (iter != style->entries.end()) {
84 assert(iter->key.id && "IDs must be assigned and linked");
85
86 // Find the SDK level that is higher than the configuration allows.
87 const size_t sdkLevel = findAttributeSdkLevel(iter->key.id.value());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080088 if (sdkLevel > std::max<size_t>(configValue->config.sdkVersion, 1)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 // Record that we are about to strip this.
90 stripped.emplace_back(std::move(*iter));
91
92 // We use the smallest SDK level to generate the new style.
93 if (minSdkStripped) {
94 minSdkStripped = std::min(minSdkStripped.value(), sdkLevel);
95 } else {
96 minSdkStripped = sdkLevel;
97 }
98
99 // Erase this from this style.
100 iter = style->entries.erase(iter);
101 continue;
102 }
103 ++iter;
104 }
105
106 if (minSdkStripped && !stripped.empty()) {
107 // We found attributes from a higher SDK level. Check that
108 // there is no other defined resource for the version we want to
109 // generate.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700110 if (shouldGenerateVersionedResource(entry.get(),
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800111 configValue->config,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 minSdkStripped.value())) {
113 // Let's create a new Style for this versioned resource.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800114 ConfigDescription newConfig(configValue->config);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115 newConfig.sdkVersion = minSdkStripped.value();
116
Adam Lesinskie78fd612015-10-22 12:48:43 -0700117 std::unique_ptr<Style> newStyle(style->clone(&table->stringPool));
118 newStyle->setComment(style->getComment());
119 newStyle->setSource(style->getSource());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120
121 // Move the previously stripped attributes into this style.
122 newStyle->entries.insert(newStyle->entries.end(),
123 std::make_move_iterator(stripped.begin()),
124 std::make_move_iterator(stripped.end()));
125
126 // Insert the new Resource into the correct place.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800127 entry->findOrCreateValue(newConfig, {})->value =
128 std::move(newStyle);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 return true;
137}
138
139} // namespace aapt