Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 17 | #include "androidfw/Locale.h" |
| 18 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 19 | #include "link/NoDefaultResourceRemover.h" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | |
| 23 | #include "ResourceTable.h" |
| 24 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | using android::ConfigDescription; |
| 26 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 29 | static bool KeepResource(const std::unique_ptr<ResourceEntry>& entry, int minSdk) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 30 | if (entry->visibility.level == Visibility::Level::kPublic) { |
| 31 | // Removing a public API without the developer knowing is bad, so just leave this here for now. |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | if (entry->HasDefaultValue()) { |
| 36 | // There is a default value, no removal needed. |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | // There is no default value defined, check if removal is required. |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 41 | bool defaultRequired = false; |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 42 | for (const auto& config_value : entry->values) { |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 43 | const int config = ConfigDescription::DefaultConfig().diff(config_value->config); |
| 44 | // If a resource defines a value for a locale-only configuration, the default configuration is |
| 45 | // required. |
| 46 | if (config == ConfigDescription::CONFIG_LOCALE) { |
| 47 | defaultRequired = true; |
| 48 | } |
| 49 | // If a resource defines a version-only config, the config value can be used as a default if |
| 50 | // the version is at most the minimum sdk version |
| 51 | else if (config == ConfigDescription::CONFIG_VERSION |
| 52 | && config_value->config.sdkVersion <= minSdk) { |
| 53 | return true; |
| 54 | } |
| 55 | // If a resource defines a value for a density only configuration, then that value could be used |
| 56 | // as a default and the entry should not be removed |
| 57 | else if (config == ConfigDescription::CONFIG_DENSITY |
| 58 | || (config == (ConfigDescription::CONFIG_DENSITY | ConfigDescription::CONFIG_VERSION) |
| 59 | && config_value->config.sdkVersion <= minSdk)) { |
| 60 | return true; |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 63 | |
| 64 | return !defaultRequired; |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool NoDefaultResourceRemover::Consume(IAaptContext* context, ResourceTable* table) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 68 | for (auto& pkg : table->packages) { |
| 69 | for (auto& type : pkg->types) { |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 70 | // Gather the entries without defaults that must be removed |
| 71 | const int minSdk = context->GetMinSdkVersion(); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 72 | const auto end_iter = type->entries.end(); |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 73 | const auto remove_iter = std::stable_partition(type->entries.begin(), end_iter, |
| 74 | [&minSdk](const std::unique_ptr<ResourceEntry>& entry) -> bool { |
| 75 | return KeepResource(entry, minSdk); |
| 76 | }); |
| 77 | |
| 78 | for (auto iter = remove_iter; iter != end_iter; ++iter) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 79 | const ResourceName name(pkg->name, type->named_type, (*iter)->name); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 80 | android::IDiagnostics* diag = context->GetDiagnostics(); |
| 81 | diag->Warn(android::DiagMessage() |
| 82 | << "removing resource " << name << " without required default value"); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 83 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 84 | diag->Note(android::DiagMessage() << " did you forget to remove all definitions?"); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 85 | for (const auto& config_value : (*iter)->values) { |
| 86 | if (config_value->value != nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 87 | diag->Note(android::DiagMessage(config_value->value->GetSource()) << "defined here"); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 93 | type->entries.erase(remove_iter, end_iter); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | } // namespace aapt |