• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 "optimize/VersionCollapser.h"
18 
19 #include <algorithm>
20 #include <vector>
21 
22 #include "ResourceTable.h"
23 
24 namespace aapt {
25 
26 template <typename Iterator, typename Pred>
27 class FilterIterator {
28  public:
FilterIterator(Iterator begin,Iterator end,Pred pred=Pred ())29   FilterIterator(Iterator begin, Iterator end, Pred pred = Pred())
30       : current_(begin), end_(end), pred_(pred) {
31     Advance();
32   }
33 
HasNext()34   bool HasNext() { return current_ != end_; }
35 
NextIter()36   Iterator NextIter() {
37     Iterator iter = current_;
38     ++current_;
39     Advance();
40     return iter;
41   }
42 
Next()43   typename Iterator::reference Next() { return *NextIter(); }
44 
45  private:
Advance()46   void Advance() {
47     for (; current_ != end_; ++current_) {
48       if (pred_(*current_)) {
49         return;
50       }
51     }
52   }
53 
54   Iterator current_, end_;
55   Pred pred_;
56 };
57 
58 template <typename Iterator, typename Pred>
make_filter_iterator(Iterator begin,Iterator end=Iterator (),Pred pred=Pred ())59 FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
60                                                     Iterator end = Iterator(),
61                                                     Pred pred = Pred()) {
62   return FilterIterator<Iterator, Pred>(begin, end, pred);
63 }
64 
65 /**
66  * Every Configuration with an SDK version specified that is less than minSdk will be removed. The
67  * exception is when there is no exact matching resource for the minSdk. The next smallest one will
68  * be kept.
69  */
CollapseVersions(int min_sdk,ResourceEntry * entry)70 static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
71   // First look for all sdks less than minSdk.
72   for (auto iter = entry->values.rbegin(); iter != entry->values.rend();
73        ++iter) {
74     // Check if the item was already marked for removal.
75     if (!(*iter)) {
76       continue;
77     }
78 
79     const ConfigDescription& config = (*iter)->config;
80     if (config.sdkVersion <= min_sdk) {
81       // This is the first configuration we've found with a smaller or equal SDK level to the
82       // minimum. We MUST keep this one, but remove all others we find, which get overridden by this
83       // one.
84 
85       ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
86       auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
87         // Check that the value hasn't already been marked for removal.
88         if (!val) {
89           return false;
90         }
91 
92         // Only return Configs that differ in SDK version.
93         config_without_sdk.sdkVersion = val->config.sdkVersion;
94         return config_without_sdk == val->config &&
95                val->config.sdkVersion <= min_sdk;
96       };
97 
98       // Remove the rest that match.
99       auto filter_iter =
100           make_filter_iterator(iter + 1, entry->values.rend(), pred);
101       while (filter_iter.HasNext()) {
102         filter_iter.Next() = {};
103       }
104     }
105   }
106 
107   // Now erase the nullptr values.
108   entry->values.erase(
109       std::remove_if(entry->values.begin(), entry->values.end(),
110                      [](const std::unique_ptr<ResourceConfigValue>& val)
111                          -> bool { return val == nullptr; }),
112       entry->values.end());
113 
114   // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that
115   // the resource entries are all packed together in the same ResTable_type struct and take up less
116   // space in the resources.arsc table.
117   bool modified = false;
118   for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
119     if (config_value->config.sdkVersion != 0 &&
120         config_value->config.sdkVersion <= min_sdk) {
121       // Override the resource with a Configuration without an SDK.
122       std::unique_ptr<ResourceConfigValue> new_value =
123           util::make_unique<ResourceConfigValue>(
124               config_value->config.CopyWithoutSdkVersion(),
125               config_value->product);
126       new_value->value = std::move(config_value->value);
127       config_value = std::move(new_value);
128 
129       modified = true;
130     }
131   }
132 
133   if (modified) {
134     // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort
135     // to ensure ordering guarantees hold.
136     std::sort(entry->values.begin(), entry->values.end(),
137               [](const std::unique_ptr<ResourceConfigValue>& a,
138                  const std::unique_ptr<ResourceConfigValue>& b) -> bool {
139                 return a->config.compare(b->config) < 0;
140               });
141   }
142 }
143 
Consume(IAaptContext * context,ResourceTable * table)144 bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) {
145   const int min_sdk = context->GetMinSdkVersion();
146   for (auto& package : table->packages) {
147     for (auto& type : package->types) {
148       for (auto& entry : type->entries) {
149         CollapseVersions(min_sdk, entry.get());
150       }
151     }
152   }
153   return true;
154 }
155 
156 }  // namespace aapt
157