blob: f459584a1af999b7fa6a529c947ad88914e72ce9 [file] [log] [blame]
Dianne Hackborne6b68032011-10-13 16:26:02 -07001//
2// Copyright 2011 The Android Open Source Project
3//
4// Build resource files from raw assets.
5//
6
7#ifndef RESOURCE_FILTER_H
8#define RESOURCE_FILTER_H
9
Adam Lesinskifab50872014-04-16 14:40:42 -070010#include <androidfw/ResourceTypes.h>
11#include <set>
12#include <utility>
13#include <utils/Errors.h>
14#include <utils/String8.h>
15#include <utils/StrongPointer.h>
16#include <utils/Vector.h>
17
Dianne Hackborne6b68032011-10-13 16:26:02 -070018#include "AaptAssets.h"
Adam Lesinskifab50872014-04-16 14:40:42 -070019#include "ConfigDescription.h"
20
21class ResourceFilter : public virtual android::RefBase {
22public:
23 virtual bool match(const android::ResTable_config& config) const = 0;
24};
Dianne Hackborne6b68032011-10-13 16:26:02 -070025
26/**
27 * Implements logic for parsing and handling "-c" and "--preferred-configurations"
28 * options.
29 */
Adam Lesinskifab50872014-04-16 14:40:42 -070030class WeakResourceFilter : public ResourceFilter {
Dianne Hackborne6b68032011-10-13 16:26:02 -070031public:
Adam Lesinskifab50872014-04-16 14:40:42 -070032 WeakResourceFilter()
33 : mContainsPseudoAccented(false)
34 , mContainsPseudoBidi(false) {}
35
36 android::status_t parse(const android::String8& str);
37
38 bool match(const android::ResTable_config& config) const;
39
40 inline bool isEmpty() const {
41 return mConfigMask == 0;
42 }
43
44 inline bool containsPseudo() const {
45 return mContainsPseudoAccented;
46 }
47
48 inline bool containsPseudoBidi() const {
49 return mContainsPseudoBidi;
50 }
Dianne Hackborne6b68032011-10-13 16:26:02 -070051
52private:
Adam Lesinskifab50872014-04-16 14:40:42 -070053 ConfigDescription mDefault;
54 uint32_t mConfigMask;
55 android::Vector<std::pair<ConfigDescription, uint32_t> > mConfigs;
Narayan Kamath788fa412014-01-21 15:32:36 +000056
Anton Krumina2ef5c02014-03-12 14:46:44 -070057 bool mContainsPseudoAccented;
58 bool mContainsPseudoBidi;
Dianne Hackborne6b68032011-10-13 16:26:02 -070059};
60
Adam Lesinskifab50872014-04-16 14:40:42 -070061/**
62 * Matches resources that have at least one of the configurations
63 * that this filter is looking for. In order to match a configuration,
64 * the resource must have the exact same configuration.
65 *
66 * This filter acts as a logical OR when matching resources.
67 *
68 * For example, if the filter is looking for resources with
69 * fr-land, de-land, or sw600dp:
70 *
71 * (PASS) fr-land
72 * (FAIL) fr
73 * (PASS) de-land
74 * (FAIL) de
75 * (FAIL) de-sw600dp
76 * (PASS) sw600dp
77 * (FAIL) sw600dp-land
78 */
79class StrongResourceFilter : public ResourceFilter {
80public:
81 StrongResourceFilter() {}
82 StrongResourceFilter(const std::set<ConfigDescription>& configs)
83 : mConfigs(configs) {}
84
85 android::status_t parse(const android::String8& str);
86
87 bool match(const android::ResTable_config& config) const {
88 std::set<ConfigDescription>::const_iterator iter = mConfigs.begin();
89 for (; iter != mConfigs.end(); iter++) {
90 if (iter->compare(config) == 0) {
91 return true;
92 }
93 }
94 return false;
95 }
96
97 inline const std::set<ConfigDescription>& getConfigs() const {
98 return mConfigs;
99 }
100
101private:
102 std::set<ConfigDescription> mConfigs;
103};
104
105/**
106 * Negates the response of the target filter.
107 */
108class InverseResourceFilter : public ResourceFilter {
109public:
110 InverseResourceFilter(const android::sp<ResourceFilter>& filter)
111 : mFilter(filter) {}
112
113 bool match(const android::ResTable_config& config) const {
114 return !mFilter->match(config);
115 }
116
117private:
118 const android::sp<ResourceFilter> mFilter;
119};
120
121/**
122 * A logical AND of all the added filters.
123 */
124class AndResourceFilter : public ResourceFilter {
125public:
126 void addFilter(const android::sp<ResourceFilter>& filter) {
127 mFilters.add(filter);
128 }
129
130 bool match(const android::ResTable_config& config) const {
131 const size_t N = mFilters.size();
132 for (size_t i = 0; i < N; i++) {
133 if (!mFilters[i]->match(config)) {
134 return false;
135 }
136 }
137 return true;
138 }
139
140private:
141 android::Vector<android::sp<ResourceFilter> > mFilters;
142};
Dianne Hackborne6b68032011-10-13 16:26:02 -0700143#endif