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 "ConfigDescription.h" 24#include "ResourceTable.h" 25#include "SdkConstants.h" 26#include "ValueVisitor.h" 27 28namespace aapt { 29 30bool 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. 33 CHECK(sdk_version_to_generate > config.sdkVersion); 34 return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config); 35} 36 37ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, 38 const ConfigDescription& config) { 39 const auto end_iter = entry->values.end(); 40 auto iter = entry->values.begin(); 41 for (; iter != end_iter; ++iter) { 42 if ((*iter)->config == config) { 43 break; 44 } 45 } 46 47 // The source config came from this list, so it should be here. 48 CHECK(iter != entry->values.end()); 49 ++iter; 50 51 // The next configuration either only varies in sdkVersion, or it is completely different 52 // and therefore incompatible. If it is incompatible, we must generate the versioned resource. 53 54 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other 55 // qualifiers, so we need to iterate through the entire list to be sure there 56 // are no higher sdk level versions of this resource. 57 ConfigDescription temp_config(config); 58 for (; iter != end_iter; ++iter) { 59 temp_config.sdkVersion = (*iter)->config.sdkVersion; 60 if (temp_config == (*iter)->config) { 61 // The two configs are the same, return the sdkVersion. 62 return (*iter)->config.sdkVersion; 63 } 64 } 65 66 // Didn't find another config with a different sdk version, so return the highest possible value. 67 return std::numeric_limits<ApiVersion>::max(); 68} 69 70bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) { 71 for (auto& package : table->packages) { 72 for (auto& type : package->types) { 73 if (type->type != ResourceType::kStyle) { 74 continue; 75 } 76 77 for (auto& entry : type->entries) { 78 for (size_t i = 0; i < entry->values.size(); i++) { 79 ResourceConfigValue* config_value = entry->values[i].get(); 80 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) { 81 // If this configuration is only used on L-MR1 then we don't need 82 // to do anything since we use private attributes since that 83 // version. 84 continue; 85 } 86 87 if (Style* style = ValueCast<Style>(config_value->value.get())) { 88 Maybe<ApiVersion> min_sdk_stripped; 89 std::vector<Style::Entry> stripped; 90 91 auto iter = style->entries.begin(); 92 while (iter != style->entries.end()) { 93 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked"; 94 95 // Find the SDK level that is higher than the configuration 96 // allows. 97 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value()); 98 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) { 99 // Record that we are about to strip this. 100 stripped.emplace_back(std::move(*iter)); 101 102 // We use the smallest SDK level to generate the new style. 103 if (min_sdk_stripped) { 104 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level); 105 } else { 106 min_sdk_stripped = sdk_level; 107 } 108 109 // Erase this from this style. 110 iter = style->entries.erase(iter); 111 continue; 112 } 113 ++iter; 114 } 115 116 if (min_sdk_stripped && !stripped.empty()) { 117 // We found attributes from a higher SDK level. Check that 118 // there is no other defined resource for the version we want to 119 // generate. 120 if (ShouldGenerateVersionedResource(entry.get(), 121 config_value->config, 122 min_sdk_stripped.value())) { 123 // Let's create a new Style for this versioned resource. 124 ConfigDescription new_config(config_value->config); 125 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value()); 126 127 std::unique_ptr<Style> new_style(style->Clone(&table->string_pool)); 128 new_style->SetComment(style->GetComment()); 129 new_style->SetSource(style->GetSource()); 130 131 // Move the previously stripped attributes into this style. 132 new_style->entries.insert( 133 new_style->entries.end(), 134 std::make_move_iterator(stripped.begin()), 135 std::make_move_iterator(stripped.end())); 136 137 // Insert the new Resource into the correct place. 138 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style); 139 } 140 } 141 } 142 } 143 } 144 } 145 } 146 return true; 147} 148 149} // namespace aapt 150