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 "link/Linkers.h"
18
19 #include <algorithm>
20
21 #include "android-base/logging.h"
22
23 #include "ResourceTable.h"
24 #include "SdkConstants.h"
25 #include "ValueVisitor.h"
26 #include "trace/TraceBuffer.h"
27
28 using android::ConfigDescription;
29
30 namespace aapt {
31
ShouldGenerateVersionedResource(const ResourceEntry * entry,const ConfigDescription & config,const ApiVersion sdk_version_to_generate)32 bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
33 const ConfigDescription& config,
34 const ApiVersion sdk_version_to_generate) {
35 // We assume the caller is trying to generate a version greater than the current configuration.
36 CHECK(sdk_version_to_generate > config.sdkVersion);
37 return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config);
38 }
39
FindNextApiVersionForConfig(const ResourceEntry * entry,const ConfigDescription & config)40 ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry,
41 const ConfigDescription& config) {
42 const auto end_iter = entry->values.end();
43 auto iter = entry->values.begin();
44 for (; iter != end_iter; ++iter) {
45 if ((*iter)->config == config) {
46 break;
47 }
48 }
49
50 // The source config came from this list, so it should be here.
51 CHECK(iter != entry->values.end());
52 ++iter;
53
54 // The next configuration either only varies in sdkVersion, or it is completely different
55 // and therefore incompatible. If it is incompatible, we must generate the versioned resource.
56
57 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other
58 // qualifiers, so we need to iterate through the entire list to be sure there
59 // are no higher sdk level versions of this resource.
60 ConfigDescription temp_config(config);
61 for (; iter != end_iter; ++iter) {
62 temp_config.sdkVersion = (*iter)->config.sdkVersion;
63 if (temp_config == (*iter)->config) {
64 // The two configs are the same, return the sdkVersion.
65 return (*iter)->config.sdkVersion;
66 }
67 }
68
69 // Didn't find another config with a different sdk version, so return the highest possible value.
70 return std::numeric_limits<ApiVersion>::max();
71 }
72
Consume(IAaptContext * context,ResourceTable * table)73 bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) {
74 TRACE_NAME("AutoVersioner::Consume");
75 for (auto& package : table->packages) {
76 for (auto& type : package->types) {
77 if (type->type != ResourceType::kStyle) {
78 continue;
79 }
80
81 for (auto& entry : type->entries) {
82 for (size_t i = 0; i < entry->values.size(); i++) {
83 ResourceConfigValue* config_value = entry->values[i].get();
84 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
85 // If this configuration is only used on L-MR1 then we don't need
86 // to do anything since we use private attributes since that
87 // version.
88 continue;
89 }
90
91 if (Style* style = ValueCast<Style>(config_value->value.get())) {
92 Maybe<ApiVersion> min_sdk_stripped;
93 std::vector<Style::Entry> stripped;
94
95 auto iter = style->entries.begin();
96 while (iter != style->entries.end()) {
97 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
98
99 // Find the SDK level that is higher than the configuration
100 // allows.
101 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
102 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
103 // Record that we are about to strip this.
104 stripped.emplace_back(std::move(*iter));
105
106 // We use the smallest SDK level to generate the new style.
107 if (min_sdk_stripped) {
108 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
109 } else {
110 min_sdk_stripped = sdk_level;
111 }
112
113 // Erase this from this style.
114 iter = style->entries.erase(iter);
115 continue;
116 }
117 ++iter;
118 }
119
120 if (min_sdk_stripped && !stripped.empty()) {
121 // We found attributes from a higher SDK level. Check that
122 // there is no other defined resource for the version we want to
123 // generate.
124 if (ShouldGenerateVersionedResource(entry.get(),
125 config_value->config,
126 min_sdk_stripped.value())) {
127 // Let's create a new Style for this versioned resource.
128 ConfigDescription new_config(config_value->config);
129 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
130
131 std::unique_ptr<Style> new_style(style->Clone(&table->string_pool));
132 new_style->SetComment(style->GetComment());
133 new_style->SetSource(style->GetSource());
134
135 // Move the previously stripped attributes into this style.
136 new_style->entries.insert(
137 new_style->entries.end(),
138 std::make_move_iterator(stripped.begin()),
139 std::make_move_iterator(stripped.end()));
140
141 // Insert the new Resource into the correct place.
142 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
143 }
144 }
145 }
146 }
147 }
148 }
149 }
150 return true;
151 }
152
153 } // namespace aapt
154