blob: 3ac17625023ea7976a252ddaeaa21c5839490266 [file] [log] [blame]
Jeremy Meyer423c0f52024-08-02 12:06:54 -07001/*
2 * Copyright (C) 2024 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/FlagDisabledResourceRemover.h"
18
19#include <algorithm>
20
21#include "ResourceTable.h"
22
23using android::ConfigDescription;
24
25namespace aapt {
26
27static bool KeepResourceEntry(const std::unique_ptr<ResourceEntry>& entry) {
28 if (entry->values.empty()) {
29 return true;
30 }
31 const auto end_iter = entry->values.end();
32 const auto remove_iter =
33 std::stable_partition(entry->values.begin(), end_iter,
34 [](const std::unique_ptr<ResourceConfigValue>& value) -> bool {
Jeremy Meyerd52bd682024-08-14 11:16:58 -070035 return value->value->GetFlagStatus() != FlagStatus::Disabled;
Jeremy Meyer423c0f52024-08-02 12:06:54 -070036 });
37
38 bool keep = remove_iter != entry->values.begin();
39
40 entry->values.erase(remove_iter, end_iter);
Jeremy Meyerd52bd682024-08-14 11:16:58 -070041
42 for (auto& value : entry->values) {
43 value->value->RemoveFlagDisabledElements();
44 }
45
Jeremy Meyer423c0f52024-08-02 12:06:54 -070046 return keep;
47}
48
49bool FlagDisabledResourceRemover::Consume(IAaptContext* context, ResourceTable* table) {
50 for (auto& pkg : table->packages) {
51 for (auto& type : pkg->types) {
52 const auto end_iter = type->entries.end();
53 const auto remove_iter = std::stable_partition(
54 type->entries.begin(), end_iter, [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
55 return KeepResourceEntry(entry);
56 });
57
58 type->entries.erase(remove_iter, end_iter);
59 }
60 }
61 return true;
62}
63
64} // namespace aapt