1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.compatibility.common.util;
17 
18 import android.util.JsonWriter;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Arrays;
26 import java.util.List;
27 
28 public class DeviceInfoStore extends InfoStore {
29 
30     protected File mJsonFile;
31     protected JsonWriter mJsonWriter = null;
32 
DeviceInfoStore()33     public DeviceInfoStore() {
34         mJsonFile = null;
35     }
36 
DeviceInfoStore(File file)37     public DeviceInfoStore(File file) throws Exception {
38         mJsonFile = file;
39     }
40 
41     /**
42      * Opens the file for storage and creates the writer.
43      */
44     @Override
open()45     public void open() throws IOException {
46         FileOutputStream out = new FileOutputStream(mJsonFile);
47         mJsonWriter = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
48         // TODO(agathaman): remove to make json output less pretty
49         mJsonWriter.setIndent("  ");
50         mJsonWriter.beginObject();
51     }
52 
53     /**
54      * Closes the writer.
55      */
56     @Override
close()57     public void close() throws Exception {
58         mJsonWriter.endObject();
59         mJsonWriter.flush();
60         mJsonWriter.close();
61     }
62 
63     /**
64      * Start a new group of result.
65      */
66     @Override
startGroup()67     public void startGroup() throws IOException {
68         mJsonWriter.beginObject();
69     }
70 
71     /**
72      * Start a new group of result with specified name.
73      */
74     @Override
startGroup(String name)75     public void startGroup(String name) throws IOException {
76         mJsonWriter.name(name);
77         mJsonWriter.beginObject();
78     }
79 
80     /**
81      * Complete adding result to the last started group.
82      */
83     @Override
endGroup()84     public void endGroup() throws IOException {
85         mJsonWriter.endObject();
86     }
87 
88     /**
89      * Start a new array of result.
90      */
91     @Override
startArray()92     public void startArray() throws IOException {
93         mJsonWriter.beginArray();
94     }
95 
96     /**
97      * Start a new array of result with specified name.
98      */
99     @Override
startArray(String name)100     public void startArray(String name) throws IOException {
101         checkName(name);
102         mJsonWriter.name(name);
103         mJsonWriter.beginArray();
104     }
105 
106     /**
107      * Complete adding result to the last started array.
108      */
109     @Override
endArray()110     public void endArray() throws IOException {
111         mJsonWriter.endArray();
112     }
113 
114     /**
115      * Adds a int value to the InfoStore
116      */
117     @Override
addResult(String name, int value)118     public void addResult(String name, int value) throws IOException {
119         checkName(name);
120         mJsonWriter.name(name);
121         mJsonWriter.value(value);
122     }
123 
124     /**
125      * Adds a long value to the InfoStore
126      */
127     @Override
addResult(String name, long value)128     public void addResult(String name, long value) throws IOException {
129         checkName(name);
130         mJsonWriter.name(name);
131         mJsonWriter.value(value);
132     }
133 
134     /**
135      * Adds a float value to the InfoStore
136      */
137     @Override
addResult(String name, float value)138     public void addResult(String name, float value) throws IOException {
139         addResult(name, (double) value);
140     }
141 
142     /**
143      * Adds a double value to the InfoStore
144      */
145     @Override
addResult(String name, double value)146     public void addResult(String name, double value) throws IOException {
147         checkName(name);
148         if (isDoubleNaNOrInfinite(value)) {
149             return;
150         } else {
151             mJsonWriter.name(name);
152             mJsonWriter.value(value);
153         }
154     }
155 
156     /**
157      * Adds a boolean value to the InfoStore
158      */
159     @Override
addResult(String name, boolean value)160     public void addResult(String name, boolean value) throws IOException {
161         checkName(name);
162         mJsonWriter.name(name);
163         mJsonWriter.value(value);
164     }
165 
166     /**
167      * Adds a String value to the InfoStore
168      */
169     @Override
addResult(String name, String value)170     public void addResult(String name, String value) throws IOException {
171         checkName(name);
172         mJsonWriter.name(name);
173         mJsonWriter.value(checkString(value));
174     }
175 
176     /**
177      * Adds a int array to the InfoStore
178      */
179     @Override
addArrayResult(String name, int[] array)180     public void addArrayResult(String name, int[] array) throws IOException {
181         checkName(name);
182         mJsonWriter.name(name);
183         mJsonWriter.beginArray();
184         for (int value : checkArray(array)) {
185             mJsonWriter.value(value);
186         }
187         mJsonWriter.endArray();
188     }
189 
190     /**
191      * Adds a long array to the InfoStore
192      */
193     @Override
addArrayResult(String name, long[] array)194     public void addArrayResult(String name, long[] array) throws IOException {
195         checkName(name);
196         mJsonWriter.name(name);
197         mJsonWriter.beginArray();
198         for (long value : checkArray(array)) {
199             mJsonWriter.value(value);
200         }
201         mJsonWriter.endArray();
202     }
203 
204     /**
205      * Adds a float array to the InfoStore
206      */
207     @Override
addArrayResult(String name, float[] array)208     public void addArrayResult(String name, float[] array) throws IOException {
209         double[] doubleArray = new double[array.length];
210         for (int i = 0; i < array.length; i++) {
211             doubleArray[i] = array[i];
212         }
213         addArrayResult(name, doubleArray);
214     }
215 
216     /**
217      * Adds a double array to the InfoStore
218      */
219     @Override
addArrayResult(String name, double[] array)220     public void addArrayResult(String name, double[] array) throws IOException {
221         checkName(name);
222         mJsonWriter.name(name);
223         mJsonWriter.beginArray();
224         for (double value : checkArray(array)) {
225             if (isDoubleNaNOrInfinite(value)) {
226                 continue;
227             }
228             mJsonWriter.value(value);
229         }
230         mJsonWriter.endArray();
231     }
232 
233     /**
234      * Adds a boolean array to the InfoStore
235      */
236     @Override
addArrayResult(String name, boolean[] array)237     public void addArrayResult(String name, boolean[] array) throws IOException {
238         checkName(name);
239         mJsonWriter.name(name);
240         mJsonWriter.beginArray();
241         for (boolean value : checkArray(array)) {
242             mJsonWriter.value(value);
243         }
244         mJsonWriter.endArray();
245     }
246 
247     /**
248      * Adds a List of String to the InfoStore
249      */
250     @Override
addListResult(String name, List<String> list)251     public void addListResult(String name, List<String> list) throws IOException {
252         checkName(name);
253         mJsonWriter.name(name);
254         mJsonWriter.beginArray();
255         for (String value : checkStringList(list)) {
256             mJsonWriter.value(checkString(value));
257         }
258         mJsonWriter.endArray();
259     }
260 }
261