• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   CloningValueTransformer cloner(&table->string_pool);
76   for (auto& package : table->packages) {
77     for (auto& type : package->types) {
78       if (type->type != ResourceType::kStyle) {
79         continue;
80       }
81 
82       for (auto& entry : type->entries) {
83         for (size_t i = 0; i < entry->values.size(); i++) {
84           ResourceConfigValue* config_value = entry->values[i].get();
85           if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
86             // If this configuration is only used on L-MR1 then we don't need
87             // to do anything since we use private attributes since that
88             // version.
89             continue;
90           }
91 
92           if (Style* style = ValueCast<Style>(config_value->value.get())) {
93             Maybe<ApiVersion> min_sdk_stripped;
94             std::vector<Style::Entry> stripped;
95 
96             auto iter = style->entries.begin();
97             while (iter != style->entries.end()) {
98               CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
99 
100               // Find the SDK level that is higher than the configuration
101               // allows.
102               const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
103               if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
104                 // Record that we are about to strip this.
105                 stripped.emplace_back(std::move(*iter));
106 
107                 // We use the smallest SDK level to generate the new style.
108                 if (min_sdk_stripped) {
109                   min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
110                 } else {
111                   min_sdk_stripped = sdk_level;
112                 }
113 
114                 // Erase this from this style.
115                 iter = style->entries.erase(iter);
116                 continue;
117               }
118               ++iter;
119             }
120 
121             if (min_sdk_stripped && !stripped.empty()) {
122               // We found attributes from a higher SDK level. Check that
123               // there is no other defined resource for the version we want to
124               // generate.
125               if (ShouldGenerateVersionedResource(entry.get(),
126                                                   config_value->config,
127                                                   min_sdk_stripped.value())) {
128                 // Let's create a new Style for this versioned resource.
129                 ConfigDescription new_config(config_value->config);
130                 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
131 
132                 std::unique_ptr<Style> new_style(style->Transform(cloner));
133                 new_style->SetComment(style->GetComment());
134                 new_style->SetSource(style->GetSource());
135 
136                 // Move the previously stripped attributes into this style.
137                 new_style->entries.insert(
138                     new_style->entries.end(),
139                     std::make_move_iterator(stripped.begin()),
140                     std::make_move_iterator(stripped.end()));
141 
142                 // Insert the new Resource into the correct place.
143                 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
144               }
145             }
146           }
147         }
148       }
149     }
150   }
151   return true;
152 }
153 
154 }  // namespace aapt
155