2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <utils/String8.h>
21 using namespace android;
25 inline static void indentStr(String8& str, int indent) {
32 Rule::Rule(const Rule& rhs)
37 , stringArgs(rhs.stringArgs)
38 , longArgs(rhs.longArgs)
39 , subrules(rhs.subrules) {
42 String8 Rule::toJson(int indent) const {
44 indentStr(str, indent);
47 indentStr(str, indent);
48 str.append("\"op\": \"");
51 str.append("ALWAYS_TRUE");
54 str.append("GREATER_THAN");
57 str.append("LESS_THAN");
63 str.append("AND_SUBRULES");
66 str.append("OR_SUBRULES");
69 str.append("CONTAINS_ANY");
72 str.appendFormat("%d", op);
79 indentStr(str, indent);
80 str.append("\"negate\": true");
83 bool includeKey = true;
95 indentStr(str, indent);
96 str.append("\"property\": \"");
102 str.append("SDK_VERSION");
105 str.append("SCREEN_DENSITY");
107 case NATIVE_PLATFORM:
108 str.append("NATIVE_PLATFORM");
111 str.append("LANGUAGE");
114 str.appendFormat("%d", key);
120 if (op == AND_SUBRULES || op == OR_SUBRULES) {
122 indentStr(str, indent);
123 str.append("\"subrules\": [\n");
124 const size_t subruleCount = subrules.size();
125 for (size_t i = 0; i < subruleCount; i++) {
126 str.append(subrules[i]->toJson(indent + 1));
127 if (i != subruleCount - 1) {
132 indentStr(str, indent);
137 case SCREEN_DENSITY: {
139 indentStr(str, indent);
140 str.append("\"args\": [");
141 const size_t argCount = longArgs.size();
142 for (size_t i = 0; i < argCount; i++) {
146 str.appendFormat("%d", longArgs[i]);
152 case NATIVE_PLATFORM: {
154 indentStr(str, indent);
155 str.append("\"args\": [");
156 const size_t argCount = stringArgs.size();
157 for (size_t i = 0; i < argCount; i++) {
161 str.append(stringArgs[i]);
172 indentStr(str, indent);
177 sp<Rule> Rule::simplify(sp<Rule> rule) {
178 if (rule->op != AND_SUBRULES && rule->op != OR_SUBRULES) {
182 Vector<sp<Rule> > newSubrules;
183 newSubrules.setCapacity(rule->subrules.size());
184 const size_t subruleCount = rule->subrules.size();
185 for (size_t i = 0; i < subruleCount; i++) {
186 sp<Rule> simplifiedRule = simplify(rule->subrules.editItemAt(i));
187 if (simplifiedRule != NULL) {
188 if (simplifiedRule->op == rule->op) {
189 newSubrules.appendVector(simplifiedRule->subrules);
191 newSubrules.add(simplifiedRule);
196 const size_t newSubruleCount = newSubrules.size();
197 if (newSubruleCount == 0) {
199 } else if (subruleCount == 1) {
200 return newSubrules.editTop();
202 rule->subrules = newSubrules;