blob: 4ac70d91bcc9d59c30c382ec424da2daf3d69484 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "link/Linkers.h"
18
19#include <algorithm>
20
21#include "android-base/logging.h"
22
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ConfigDescription.h"
24#include "ResourceTable.h"
25#include "SdkConstants.h"
26#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027
28namespace aapt {
29
Adam Lesinskic744ae82017-05-17 19:28:38 -070030bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
31 const ApiVersion sdk_version_to_generate) {
32 // We assume the caller is trying to generate a version greater than the current configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 CHECK(sdk_version_to_generate > config.sdkVersion);
Adam Lesinskic744ae82017-05-17 19:28:38 -070034 return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config);
35}
Adam Lesinskifb6312f2016-06-28 14:40:32 -070036
Adam Lesinski5b7337f2017-06-26 14:57:22 -070037ApiVersion FindNextApiVersionForConfigInSortedVector(
38 std::vector<std::unique_ptr<ResourceConfigValue>>::const_iterator start,
39 std::vector<std::unique_ptr<ResourceConfigValue>>::const_iterator end) {
40 const ConfigDescription start_config = (*start)->config.CopyWithoutSdkVersion();
41 ++start;
42 if (start != end) {
43 if ((*start)->config.CopyWithoutSdkVersion() == start_config) {
44 return static_cast<ApiVersion>((*start)->config.sdkVersion);
45 }
46 }
47 return std::numeric_limits<ApiVersion>::max();
48}
49
Adam Lesinskic744ae82017-05-17 19:28:38 -070050ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry,
51 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 const auto end_iter = entry->values.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 auto iter = entry->values.begin();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 for (; iter != end_iter; ++iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 if ((*iter)->config == config) {
56 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080057 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // The source config came from this list, so it should be here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 CHECK(iter != entry->values.end());
Adam Lesinski5b7337f2017-06-26 14:57:22 -070062 return FindNextApiVersionForConfigInSortedVector(iter, end_iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 for (auto& package : table->packages) {
67 for (auto& type : package->types) {
68 if (type->type != ResourceType::kStyle) {
69 continue;
70 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 for (auto& entry : type->entries) {
73 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 ResourceConfigValue* config_value = entry->values[i].get();
75 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 // If this configuration is only used on L-MR1 then we don't need
77 // to do anything since we use private attributes since that
78 // version.
79 continue;
80 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 if (Style* style = ValueCast<Style>(config_value->value.get())) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070083 Maybe<ApiVersion> min_sdk_stripped;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 std::vector<Style::Entry> stripped;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 auto iter = style->entries.begin();
87 while (iter != style->entries.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 // Find the SDK level that is higher than the configuration
91 // allows.
Adam Lesinskic744ae82017-05-17 19:28:38 -070092 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
93 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 // Record that we are about to strip this.
95 stripped.emplace_back(std::move(*iter));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 // We use the smallest SDK level to generate the new style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 if (min_sdk_stripped) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070099 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 min_sdk_stripped = sdk_level;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
104 // Erase this from this style.
105 iter = style->entries.erase(iter);
106 continue;
107 }
108 ++iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 if (min_sdk_stripped && !stripped.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // We found attributes from a higher SDK level. Check that
113 // there is no other defined resource for the version we want to
114 // generate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 if (ShouldGenerateVersionedResource(entry.get(),
116 config_value->config,
117 min_sdk_stripped.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 // Let's create a new Style for this versioned resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 ConfigDescription new_config(config_value->config);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700120 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
Adam Lesinskic744ae82017-05-17 19:28:38 -0700122 std::unique_ptr<Style> new_style(style->Clone(&table->string_pool));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 new_style->SetComment(style->GetComment());
124 new_style->SetSource(style->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125
126 // Move the previously stripped attributes into this style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 new_style->entries.insert(
128 new_style->entries.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 std::make_move_iterator(stripped.begin()),
130 std::make_move_iterator(stripped.end()));
131
132 // Insert the new Resource into the correct place.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700133 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
135 }
136 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
141 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142}
143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144} // namespace aapt