blob: 40d5b752000ca6a7c47c73eb33571ec22e96abae [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/**
Adam Lesinskia5cc0022014-08-22 14:10:31 -070027 * Implements logic for parsing and handling "-c" options.
Dianne Hackborne6b68032011-10-13 16:26:02 -070028 */
Adam Lesinskifab50872014-04-16 14:40:42 -070029class WeakResourceFilter : public ResourceFilter {
Dianne Hackborne6b68032011-10-13 16:26:02 -070030public:
Adam Lesinskifab50872014-04-16 14:40:42 -070031 WeakResourceFilter()
32 : mContainsPseudoAccented(false)
33 , mContainsPseudoBidi(false) {}
34
35 android::status_t parse(const android::String8& str);
36
37 bool match(const android::ResTable_config& config) const;
38
39 inline bool isEmpty() const {
40 return mConfigMask == 0;
41 }
42
43 inline bool containsPseudo() const {
44 return mContainsPseudoAccented;
45 }
46
47 inline bool containsPseudoBidi() const {
48 return mContainsPseudoBidi;
49 }
Dianne Hackborne6b68032011-10-13 16:26:02 -070050
51private:
Adam Lesinskifab50872014-04-16 14:40:42 -070052 ConfigDescription mDefault;
53 uint32_t mConfigMask;
54 android::Vector<std::pair<ConfigDescription, uint32_t> > mConfigs;
Narayan Kamath788fa412014-01-21 15:32:36 +000055
Anton Krumina2ef5c02014-03-12 14:46:44 -070056 bool mContainsPseudoAccented;
57 bool mContainsPseudoBidi;
Dianne Hackborne6b68032011-10-13 16:26:02 -070058};
59
Adam Lesinskifab50872014-04-16 14:40:42 -070060/**
61 * Matches resources that have at least one of the configurations
62 * that this filter is looking for. In order to match a configuration,
63 * the resource must have the exact same configuration.
64 *
65 * This filter acts as a logical OR when matching resources.
66 *
67 * For example, if the filter is looking for resources with
68 * fr-land, de-land, or sw600dp:
69 *
70 * (PASS) fr-land
71 * (FAIL) fr
72 * (PASS) de-land
73 * (FAIL) de
74 * (FAIL) de-sw600dp
75 * (PASS) sw600dp
76 * (FAIL) sw600dp-land
77 */
78class StrongResourceFilter : public ResourceFilter {
79public:
80 StrongResourceFilter() {}
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -070081 explicit StrongResourceFilter(const std::set<ConfigDescription>& configs)
Adam Lesinskifab50872014-04-16 14:40:42 -070082 : mConfigs(configs) {}
83
84 android::status_t parse(const android::String8& str);
85
86 bool match(const android::ResTable_config& config) const {
87 std::set<ConfigDescription>::const_iterator iter = mConfigs.begin();
88 for (; iter != mConfigs.end(); iter++) {
89 if (iter->compare(config) == 0) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 inline const std::set<ConfigDescription>& getConfigs() const {
97 return mConfigs;
98 }
99
100private:
101 std::set<ConfigDescription> mConfigs;
102};
103
104/**
105 * Negates the response of the target filter.
106 */
107class InverseResourceFilter : public ResourceFilter {
108public:
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700109 explicit InverseResourceFilter(const android::sp<ResourceFilter>& filter)
Adam Lesinskifab50872014-04-16 14:40:42 -0700110 : mFilter(filter) {}
111
112 bool match(const android::ResTable_config& config) const {
113 return !mFilter->match(config);
114 }
115
116private:
117 const android::sp<ResourceFilter> mFilter;
118};
119
120/**
121 * A logical AND of all the added filters.
122 */
123class AndResourceFilter : public ResourceFilter {
124public:
125 void addFilter(const android::sp<ResourceFilter>& filter) {
126 mFilters.add(filter);
127 }
128
129 bool match(const android::ResTable_config& config) const {
130 const size_t N = mFilters.size();
131 for (size_t i = 0; i < N; i++) {
132 if (!mFilters[i]->match(config)) {
133 return false;
134 }
135 }
136 return true;
137 }
138
139private:
140 android::Vector<android::sp<ResourceFilter> > mFilters;
141};
Dianne Hackborne6b68032011-10-13 16:26:02 -0700142#endif