1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Rule.h"
#include <utils/String8.h>
using namespace android;
namespace split {
inline static void indentStr(String8& str, int indent) {
while (indent > 0) {
str.append(" ");
indent--;
}
}
Rule::Rule(const Rule& rhs)
: RefBase()
, op(rhs.op)
, key(rhs.key)
, negate(rhs.negate)
, stringArgs(rhs.stringArgs)
, longArgs(rhs.longArgs)
, subrules(rhs.subrules) {
}
String8 Rule::toJson(int indent) const {
String8 str;
indentStr(str, indent);
str.append("{\n");
indent++;
indentStr(str, indent);
str.append("\"op\": \"");
switch (op) {
case ALWAYS_TRUE:
str.append("ALWAYS_TRUE");
break;
case GREATER_THAN:
str.append("GREATER_THAN");
break;
case LESS_THAN:
str.append("LESS_THAN");
break;
case EQUALS:
str.append("EQUALS");
break;
case AND_SUBRULES:
str.append("AND_SUBRULES");
break;
case OR_SUBRULES:
str.append("OR_SUBRULES");
break;
case CONTAINS_ANY:
str.append("CONTAINS_ANY");
break;
default:
str.appendFormat("%d", op);
break;
}
str.append("\"");
if (negate) {
str.append(",\n");
indentStr(str, indent);
str.append("\"negate\": true");
}
bool includeKey = true;
switch (op) {
case AND_SUBRULES:
case OR_SUBRULES:
includeKey = false;
break;
default:
break;
}
if (includeKey) {
str.append(",\n");
indentStr(str, indent);
str.append("\"property\": \"");
switch (key) {
case NONE:
str.append("NONE");
break;
case SDK_VERSION:
str.append("SDK_VERSION");
break;
case SCREEN_DENSITY:
str.append("SCREEN_DENSITY");
break;
case NATIVE_PLATFORM:
str.append("NATIVE_PLATFORM");
break;
case LANGUAGE:
str.append("LANGUAGE");
break;
default:
str.appendFormat("%d", key);
break;
}
str.append("\"");
}
if (op == AND_SUBRULES || op == OR_SUBRULES) {
str.append(",\n");
indentStr(str, indent);
str.append("\"subrules\": [\n");
const size_t subruleCount = subrules.size();
for (size_t i = 0; i < subruleCount; i++) {
str.append(subrules[i]->toJson(indent + 1));
if (i != subruleCount - 1) {
str.append(",");
}
str.append("\n");
}
indentStr(str, indent);
str.append("]");
} else {
switch (key) {
case SDK_VERSION:
case SCREEN_DENSITY: {
str.append(",\n");
indentStr(str, indent);
str.append("\"args\": [");
const size_t argCount = longArgs.size();
for (size_t i = 0; i < argCount; i++) {
if (i != 0) {
str.append(", ");
}
str.appendFormat("%d", longArgs[i]);
}
str.append("]");
break;
}
case LANGUAGE:
case NATIVE_PLATFORM: {
str.append(",\n");
indentStr(str, indent);
str.append("\"args\": [");
const size_t argCount = stringArgs.size();
for (size_t i = 0; i < argCount; i++) {
if (i != 0) {
str.append(", ");
}
str.append(stringArgs[i]);
}
str.append("]");
break;
}
default:
break;
}
}
str.append("\n");
indent--;
indentStr(str, indent);
str.append("}");
return str;
}
sp<Rule> Rule::simplify(sp<Rule> rule) {
if (rule->op != AND_SUBRULES && rule->op != OR_SUBRULES) {
return rule;
}
Vector<sp<Rule> > newSubrules;
newSubrules.setCapacity(rule->subrules.size());
const size_t subruleCount = rule->subrules.size();
for (size_t i = 0; i < subruleCount; i++) {
sp<Rule> simplifiedRule = simplify(rule->subrules.editItemAt(i));
if (simplifiedRule != NULL) {
if (simplifiedRule->op == rule->op) {
newSubrules.appendVector(simplifiedRule->subrules);
} else {
newSubrules.add(simplifiedRule);
}
}
}
const size_t newSubruleCount = newSubrules.size();
if (newSubruleCount == 0) {
return NULL;
} else if (subruleCount == 1) {
return newSubrules.editTop();
}
rule->subrules = newSubrules;
return rule;
}
} // namespace split
|