xref: /aosp_15_r20/frameworks/base/tools/aapt2/link/FlagDisabledResourceRemover.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
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 
23 using android::ConfigDescription;
24 
25 namespace aapt {
26 
KeepResourceEntry(const std::unique_ptr<ResourceEntry> & entry)27 static 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->value->GetFlagStatus() != FlagStatus::Disabled;
36                             });
37 
38   bool keep = remove_iter != entry->values.begin();
39 
40   entry->values.erase(remove_iter, end_iter);
41 
42   for (auto& value : entry->values) {
43     value->value->RemoveFlagDisabledElements();
44   }
45 
46   return keep;
47 }
48 
Consume(IAaptContext * context,ResourceTable * table)49 bool 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