blob: 0ccafc2107d012a2b3d011e4bdfffe2ddd98274e [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"
21
22#include "link/Linkers.h"
23
24#include <algorithm>
25#include <cassert>
26
27namespace aapt {
28
29static bool cmpConfigValue(const ResourceConfigValue& lhs, const ConfigDescription& config) {
30 return lhs.config < config;
31}
32
33bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
34 const int sdkVersionToGenerate) {
35 assert(sdkVersionToGenerate > config.sdkVersion);
36 const auto endIter = entry->values.end();
37 auto iter = std::lower_bound(entry->values.begin(), endIter, config, cmpConfigValue);
38
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) {
51 tempConfig.sdkVersion = iter->config.sdkVersion;
52 if (tempConfig == iter->config) {
53 // The two configs are the same, check the sdk version.
54 return sdkVersionToGenerate < iter->config.sdkVersion;
55 }
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++) {
71 ResourceConfigValue& configValue = entry->values[i];
72 if (configValue.config.sdkVersion >= SDK_LOLLIPOP_MR1) {
73 // 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
78 if (Style* style = valueCast<Style>(configValue.value.get())) {
79 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());
88 if (sdkLevel > std::max<size_t>(configValue.config.sdkVersion, 1)) {
89 // 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.
110 if (shouldGenerateVersionedResource(entry.get(), configValue.config,
111 minSdkStripped.value())) {
112 // Let's create a new Style for this versioned resource.
113 ConfigDescription newConfig(configValue.config);
114 newConfig.sdkVersion = minSdkStripped.value();
115
116 ResourceConfigValue newValue = {
117 newConfig,
118 configValue.source,
119 configValue.comment,
120 std::unique_ptr<Value>(configValue.value->clone(
121 &table->stringPool))
122 };
123
124 Style* newStyle = static_cast<Style*>(newValue.value.get());
125
126 // Move the previously stripped attributes into this style.
127 newStyle->entries.insert(newStyle->entries.end(),
128 std::make_move_iterator(stripped.begin()),
129 std::make_move_iterator(stripped.end()));
130
131 // Insert the new Resource into the correct place.
132 auto iter = std::lower_bound(entry->values.begin(),
133 entry->values.end(), newConfig,
134 cmpConfigValue);
135 entry->values.insert(iter, std::move(newValue));
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 return true;
144}
145
146} // namespace aapt