blob: e3289e2a173ab2d75c376e838ad4e4a74d206f03 [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 {
35 return value->flag_status != FlagStatus::Disabled;
36 });
37
38 bool keep = remove_iter != entry->values.begin();
39
40 entry->values.erase(remove_iter, end_iter);
41 return keep;
42}
43
44bool FlagDisabledResourceRemover::Consume(IAaptContext* context, ResourceTable* table) {
45 for (auto& pkg : table->packages) {
46 for (auto& type : pkg->types) {
47 const auto end_iter = type->entries.end();
48 const auto remove_iter = std::stable_partition(
49 type->entries.begin(), end_iter, [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
50 return KeepResourceEntry(entry);
51 });
52
53 type->entries.erase(remove_iter, end_iter);
54 }
55 }
56 return true;
57}
58
59} // namespace aapt