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 "ConfigDescription.h"
18 #include "ResourceTable.h"
19 #include "SdkConstants.h"
20 #include "ValueVisitor.h"
21 #include "link/Linkers.h"
22 
23 #include <algorithm>
24 #include <cassert>
25 
26 namespace aapt {
27 
shouldGenerateVersionedResource(const ResourceEntry * entry,const ConfigDescription & config,const int sdkVersionToGenerate)28 bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
29                                      const int sdkVersionToGenerate) {
30     assert(sdkVersionToGenerate > config.sdkVersion);
31     const auto endIter = entry->values.end();
32     auto iter = entry->values.begin();
33     for (; iter != endIter; ++iter) {
34         if ((*iter)->config == config) {
35             break;
36         }
37     }
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 
consume(IAaptContext * context,ResourceTable * table)62 bool 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].get();
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(),
111                                                                 configValue->config,
112                                                                 minSdkStripped.value())) {
113                                 // Let's create a new Style for this versioned resource.
114                                 ConfigDescription newConfig(configValue->config);
115                                 newConfig.sdkVersion = minSdkStripped.value();
116 
117                                 std::unique_ptr<Style> newStyle(style->clone(&table->stringPool));
118                                 newStyle->setComment(style->getComment());
119                                 newStyle->setSource(style->getSource());
120 
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.
127                                 entry->findOrCreateValue(newConfig, {})->value =
128                                         std::move(newStyle);
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135     }
136     return true;
137 }
138 
139 } // namespace aapt
140