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