xref: /aosp_15_r20/frameworks/base/tools/aapt2/process/ProductFilter.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 #pragma once
2 
3 #include <memory>
4 #include <optional>
5 #include <string>
6 #include <unordered_set>
7 #include <utility>
8 #include <vector>
9 
10 #include "Resource.h"
11 #include "android-base/macros.h"
12 #include "androidfw/ConfigDescription.h"
13 #include "androidfw/IDiagnostics.h"
14 #include "process/IResourceTableConsumer.h"
15 
16 namespace aapt {
17 
18 class ResourceConfigValue;
19 
20 class ProductFilter : public IResourceTableConsumer {
21  public:
22   using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
23 
24   // Setting remove_default_config_values will remove all values other than
25   // specified product, including default. For example, if the following table
26   //
27   //     <string name="foo" product="default">foo_default</string>
28   //     <string name="foo" product="tablet">foo_tablet</string>
29   //     <string name="bar">bar</string>
30   //
31   // is consumed with tablet, it will result in
32   //
33   //     <string name="foo">foo_tablet</string>
34   //
35   // removing foo_default and bar. This option is to generate an RRO package
36   // with given product.
ProductFilter(std::unordered_set<std::string> products,bool remove_default_config_values)37   explicit ProductFilter(std::unordered_set<std::string> products,
38                          bool remove_default_config_values)
39       : products_(std::move(products)),
40         remove_default_config_values_(remove_default_config_values) {
41   }
42 
43   bool Consume(IAaptContext* context, ResourceTable* table) override;
44 
45  private:
46   DISALLOW_COPY_AND_ASSIGN(ProductFilter);
47 
48   // SelectProductToKeep returns an iterator for the selected value.
49   //
50   // Returns std::nullopt in case of failure (e.g. ambiguous values, missing or duplicated default
51   // values).
52   // Returns `end` if keep_as_default_product is set and no value for the specified product was
53   // found.
54   std::optional<ResourceConfigValueIter> SelectProductToKeep(const ResourceNameRef& name,
55                                                              ResourceConfigValueIter begin,
56                                                              ResourceConfigValueIter end,
57                                                              android::IDiagnostics* diag);
58 
59   void ClearEmptyValues(ResourceTable* table);
60 
61   std::unordered_set<std::string> products_;
62   bool remove_default_config_values_;
63 };
64 
65 }  // namespace aapt
66