• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019, 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 
17 #include "java_writer_q.h"
18 
19 #include "utils.h"
20 
21 namespace android {
22 namespace stats_log_api_gen {
23 
write_java_q_logging_constants(FILE * out,const string & indent)24 void write_java_q_logging_constants(FILE* out, const string& indent) {
25     fprintf(out, "%s// Payload limits.\n", indent.c_str());
26     fprintf(out, "%sprivate static final int LOGGER_ENTRY_MAX_PAYLOAD = 4068;\n", indent.c_str());
27     fprintf(out,
28             "%sprivate static final int MAX_EVENT_PAYLOAD = "
29             "LOGGER_ENTRY_MAX_PAYLOAD - 4;\n",
30             indent.c_str());
31 
32     // Value types. Must match with EventLog.java and log.h.
33     fprintf(out, "\n");
34     fprintf(out, "%s// Value types.\n", indent.c_str());
35     fprintf(out, "%sprivate static final byte INT_TYPE = 0;\n", indent.c_str());
36     fprintf(out, "%sprivate static final byte LONG_TYPE = 1;\n", indent.c_str());
37     fprintf(out, "%sprivate static final byte STRING_TYPE = 2;\n", indent.c_str());
38     fprintf(out, "%sprivate static final byte LIST_TYPE = 3;\n", indent.c_str());
39     fprintf(out, "%sprivate static final byte FLOAT_TYPE = 4;\n", indent.c_str());
40 
41     // Size of each value type.
42     // Booleans, ints, floats, and enums take 5 bytes, 1 for the type and 4 for
43     // the value.
44     fprintf(out, "\n");
45     fprintf(out, "%s// Size of each value type.\n", indent.c_str());
46     fprintf(out, "%sprivate static final int INT_TYPE_SIZE = 5;\n", indent.c_str());
47     fprintf(out, "%sprivate static final int FLOAT_TYPE_SIZE = 5;\n", indent.c_str());
48     // Longs take 9 bytes, 1 for the type and 8 for the value.
49     fprintf(out, "%sprivate static final int LONG_TYPE_SIZE = 9;\n", indent.c_str());
50     // Strings take 5 metadata bytes: 1 byte is for the type, 4 are for the
51     // length.
52     fprintf(out, "%sprivate static final int STRING_TYPE_OVERHEAD = 5;\n", indent.c_str());
53     fprintf(out, "%sprivate static final int LIST_TYPE_OVERHEAD = 2;\n", indent.c_str());
54 }
55 
write_java_methods_q_schema(FILE * out,const SignatureInfoMap & signatureInfoMap,const AtomDecl & attributionDecl,const string & indent)56 int write_java_methods_q_schema(FILE* out, const SignatureInfoMap& signatureInfoMap,
57                                 const AtomDecl& attributionDecl, const string& indent) {
58     int requiredHelpers = 0;
59     for (auto signatureInfoMapIt = signatureInfoMap.begin();
60          signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
61         // Print method signature.
62         vector<java_type_t> signature = signatureInfoMapIt->first;
63         fprintf(out, "%spublic static void write(int code", indent.c_str());
64         int argIndex = 1;
65         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
66              arg++) {
67             if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
68                 for (const auto& chainField : attributionDecl.fields) {
69                     fprintf(out, ", %s[] %s", java_type_name(chainField.javaType),
70                             chainField.name.c_str());
71                 }
72             } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
73                 fprintf(out, ", android.util.SparseArray<Object> valueMap");
74             } else {
75                 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
76             }
77             argIndex++;
78         }
79         fprintf(out, ") {\n");
80 
81         // Calculate the size of the buffer.
82         fprintf(out, "%s    // Initial overhead of the list, timestamp, and atom tag.\n",
83                 indent.c_str());
84         fprintf(out,
85                 "%s    int needed = LIST_TYPE_OVERHEAD + LONG_TYPE_SIZE + "
86                 "INT_TYPE_SIZE;\n",
87                 indent.c_str());
88         argIndex = 1;
89         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
90              arg++) {
91             switch (*arg) {
92                 case JAVA_TYPE_BOOLEAN:
93                 case JAVA_TYPE_INT:
94                 case JAVA_TYPE_FLOAT:
95                 case JAVA_TYPE_ENUM:
96                     fprintf(out, "%s    needed += INT_TYPE_SIZE;\n", indent.c_str());
97                     break;
98                 case JAVA_TYPE_LONG:
99                     // Longs take 9 bytes, 1 for the type and 8 for the value.
100                     fprintf(out, "%s    needed += LONG_TYPE_SIZE;\n", indent.c_str());
101                     break;
102                 case JAVA_TYPE_STRING:
103                     // Strings take 5 metadata bytes + length of byte encoded string.
104                     fprintf(out, "%s    if (arg%d == null) {\n", indent.c_str(), argIndex);
105                     fprintf(out, "%s        arg%d = \"\";\n", indent.c_str(), argIndex);
106                     fprintf(out, "%s    }\n", indent.c_str());
107                     fprintf(out,
108                             "%s    byte[] arg%dBytes = "
109                             "arg%d.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
110                             indent.c_str(), argIndex, argIndex);
111                     fprintf(out, "%s    needed += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
112                             indent.c_str(), argIndex);
113                     break;
114                 case JAVA_TYPE_BYTE_ARRAY:
115                     // Byte arrays take 5 metadata bytes + length of byte array.
116                     fprintf(out, "%s    if (arg%d == null) {\n", indent.c_str(), argIndex);
117                     fprintf(out, "%s        arg%d = new byte[0];\n", indent.c_str(), argIndex);
118                     fprintf(out, "%s    }\n", indent.c_str());
119                     fprintf(out, "%s    needed += STRING_TYPE_OVERHEAD + arg%d.length;\n",
120                             indent.c_str(), argIndex);
121                     break;
122                 case JAVA_TYPE_ATTRIBUTION_CHAIN: {
123                     const char* uidName = attributionDecl.fields.front().name.c_str();
124                     const char* tagName = attributionDecl.fields.back().name.c_str();
125                     // Null checks on the params.
126                     fprintf(out, "%s    if (%s == null) {\n", indent.c_str(), uidName);
127                     fprintf(out, "%s        %s = new %s[0];\n", indent.c_str(), uidName,
128                             java_type_name(attributionDecl.fields.front().javaType));
129                     fprintf(out, "%s    }\n", indent.c_str());
130                     fprintf(out, "%s    if (%s == null) {\n", indent.c_str(), tagName);
131                     fprintf(out, "%s        %s = new %s[0];\n", indent.c_str(), tagName,
132                             java_type_name(attributionDecl.fields.back().javaType));
133                     fprintf(out, "%s    }\n", indent.c_str());
134 
135                     // First check that the lengths of the uid and tag arrays are the
136                     // same.
137                     fprintf(out, "%s    if (%s.length != %s.length) {\n", indent.c_str(), uidName,
138                             tagName);
139                     fprintf(out, "%s        return;\n", indent.c_str());
140                     fprintf(out, "%s    }\n", indent.c_str());
141                     fprintf(out, "%s    int attrSize = LIST_TYPE_OVERHEAD;\n", indent.c_str());
142                     fprintf(out, "%s    for (int i = 0; i < %s.length; i++) {\n", indent.c_str(),
143                             tagName);
144                     fprintf(out, "%s        String str%d = (%s[i] == null) ? \"\" : %s[i];\n",
145                             indent.c_str(), argIndex, tagName, tagName);
146                     fprintf(out,
147                             "%s        int str%dlen = "
148                             "str%d.getBytes(java.nio.charset.StandardCharsets.UTF_8)."
149                             "length;\n",
150                             indent.c_str(), argIndex, argIndex);
151                     fprintf(out,
152                             "%s        attrSize += "
153                             "LIST_TYPE_OVERHEAD + INT_TYPE_SIZE + STRING_TYPE_OVERHEAD + "
154                             "str%dlen;\n",
155                             indent.c_str(), argIndex);
156                     fprintf(out, "%s    }\n", indent.c_str());
157                     fprintf(out, "%s    needed += attrSize;\n", indent.c_str());
158                     break;
159                 }
160                 case JAVA_TYPE_KEY_VALUE_PAIR: {
161                     fprintf(out, "%s    // Calculate bytes needed by Key Value Pairs.\n",
162                             indent.c_str());
163                     fprintf(out, "%s    final int count = valueMap.size();\n", indent.c_str());
164                     fprintf(out, "%s    android.util.SparseIntArray intMap = null;\n",
165                             indent.c_str());
166                     fprintf(out, "%s    android.util.SparseLongArray longMap = null;\n",
167                             indent.c_str());
168                     fprintf(out, "%s    android.util.SparseArray<String> stringMap = null;\n",
169                             indent.c_str());
170                     fprintf(out, "%s    android.util.SparseArray<Float> floatMap = null;\n",
171                             indent.c_str());
172                     fprintf(out, "%s    int keyValuePairSize = LIST_TYPE_OVERHEAD;\n",
173                             indent.c_str());
174                     fprintf(out, "%s    for (int i = 0; i < count; i++) {\n", indent.c_str());
175                     fprintf(out, "%s        final int key = valueMap.keyAt(i);\n", indent.c_str());
176                     fprintf(out, "%s        final Object value = valueMap.valueAt(i);\n",
177                             indent.c_str());
178                     fprintf(out, "%s        if (value instanceof Integer) {\n", indent.c_str());
179                     fprintf(out, "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
180                             indent.c_str());
181                     fprintf(out, "%s                    + INT_TYPE_SIZE + INT_TYPE_SIZE;\n",
182                             indent.c_str());
183                     fprintf(out, "%s            if (null == intMap) {\n", indent.c_str());
184                     fprintf(out, "%s                intMap = new android.util.SparseIntArray();\n",
185                             indent.c_str());
186                     fprintf(out, "%s            }\n", indent.c_str());
187                     fprintf(out, "%s            intMap.put(key, (Integer) value);\n",
188                             indent.c_str());
189                     fprintf(out, "%s        } else if (value instanceof Long) {\n", indent.c_str());
190                     fprintf(out, "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
191                             indent.c_str());
192                     fprintf(out, "%s                    + INT_TYPE_SIZE + LONG_TYPE_SIZE;\n",
193                             indent.c_str());
194                     fprintf(out, "%s            if (null == longMap) {\n", indent.c_str());
195                     fprintf(out,
196                             "%s                longMap = new "
197                             "android.util.SparseLongArray();\n",
198                             indent.c_str());
199                     fprintf(out, "%s            }\n", indent.c_str());
200                     fprintf(out, "%s            longMap.put(key, (Long) value);\n", indent.c_str());
201                     fprintf(out, "%s        } else if (value instanceof String) {\n",
202                             indent.c_str());
203                     fprintf(out,
204                             "%s            final String str = (value == null) ? \"\" : "
205                             "(String) value;\n",
206                             indent.c_str());
207                     fprintf(out,
208                             "%s            final int len = "
209                             "str.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n",
210                             indent.c_str());
211                     fprintf(out,
212                             "%s            keyValuePairSize += LIST_TYPE_OVERHEAD + "
213                             "INT_TYPE_SIZE\n",
214                             indent.c_str());
215                     fprintf(out, "%s                    + STRING_TYPE_OVERHEAD + len;\n",
216                             indent.c_str());
217                     fprintf(out, "%s            if (null == stringMap) {\n", indent.c_str());
218                     fprintf(out,
219                             "%s                stringMap = new "
220                             "android.util.SparseArray<>();\n",
221                             indent.c_str());
222                     fprintf(out, "%s            }\n", indent.c_str());
223                     fprintf(out, "%s            stringMap.put(key, str);\n", indent.c_str());
224                     fprintf(out, "%s        } else if (value instanceof Float) {\n",
225                             indent.c_str());
226                     fprintf(out, "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
227                             indent.c_str());
228                     fprintf(out, "%s                    + INT_TYPE_SIZE + FLOAT_TYPE_SIZE;\n",
229                             indent.c_str());
230                     fprintf(out, "%s            if (null == floatMap) {\n", indent.c_str());
231                     fprintf(out,
232                             "%s                floatMap = new "
233                             "android.util.SparseArray<>();\n",
234                             indent.c_str());
235                     fprintf(out, "%s            }\n", indent.c_str());
236                     fprintf(out, "%s            floatMap.put(key, (Float) value);\n",
237                             indent.c_str());
238                     fprintf(out, "%s        }\n", indent.c_str());
239                     fprintf(out, "%s    }\n", indent.c_str());
240                     fprintf(out, "%s    needed += keyValuePairSize;\n", indent.c_str());
241                     break;
242                 }
243                 default:
244                     // Unsupported types: OBJECT, DOUBLE.
245                     fprintf(stderr, "Module logging does not yet support Object and Double.\n");
246                     return 1;
247             }
248             argIndex++;
249         }
250 
251         // Now we have the size that is needed. Check for overflow and return if
252         // needed.
253         fprintf(out, "%s    if (needed > MAX_EVENT_PAYLOAD) {\n", indent.c_str());
254         fprintf(out, "%s        return;\n", indent.c_str());
255         fprintf(out, "%s    }\n", indent.c_str());
256 
257         // Create new buffer, and associated data types.
258         fprintf(out, "%s    byte[] buff = new byte[needed];\n", indent.c_str());
259         fprintf(out, "%s    int pos = 0;\n", indent.c_str());
260 
261         // Initialize the buffer with list data type.
262         fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
263         fprintf(out, "%s    buff[pos + 1] = %zu;\n", indent.c_str(), signature.size() + 2);
264         fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
265 
266         // Write timestamp.
267         fprintf(out, "%s    long elapsedRealtime = SystemClock.elapsedRealtimeNanos();\n",
268                 indent.c_str());
269         fprintf(out, "%s    buff[pos] = LONG_TYPE;\n", indent.c_str());
270         fprintf(out, "%s    copyLong(buff, pos + 1, elapsedRealtime);\n", indent.c_str());
271         fprintf(out, "%s    pos += LONG_TYPE_SIZE;\n", indent.c_str());
272 
273         // Write atom code.
274         fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
275         fprintf(out, "%s    copyInt(buff, pos + 1, code);\n", indent.c_str());
276         fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
277 
278         // Write the args.
279         argIndex = 1;
280         for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
281              arg++) {
282             switch (*arg) {
283                 case JAVA_TYPE_BOOLEAN:
284                     fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
285                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d? 1 : 0);\n", indent.c_str(),
286                             argIndex);
287                     fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
288                     break;
289                 case JAVA_TYPE_INT:
290                 case JAVA_TYPE_ENUM:
291                     fprintf(out, "%s    buff[pos] = INT_TYPE;\n", indent.c_str());
292                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d);\n", indent.c_str(),
293                             argIndex);
294                     fprintf(out, "%s    pos += INT_TYPE_SIZE;\n", indent.c_str());
295                     break;
296                 case JAVA_TYPE_FLOAT:
297                     requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
298                     fprintf(out, "%s    buff[pos] = FLOAT_TYPE;\n", indent.c_str());
299                     fprintf(out, "%s    copyFloat(buff, pos + 1, arg%d);\n", indent.c_str(),
300                             argIndex);
301                     fprintf(out, "%s    pos += FLOAT_TYPE_SIZE;\n", indent.c_str());
302                     break;
303                 case JAVA_TYPE_LONG:
304                     fprintf(out, "%s    buff[pos] = LONG_TYPE;\n", indent.c_str());
305                     fprintf(out, "%s    copyLong(buff, pos + 1, arg%d);\n", indent.c_str(),
306                             argIndex);
307                     fprintf(out, "%s    pos += LONG_TYPE_SIZE;\n", indent.c_str());
308                     break;
309                 case JAVA_TYPE_STRING:
310                     fprintf(out, "%s    buff[pos] = STRING_TYPE;\n", indent.c_str());
311                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%dBytes.length);\n",
312                             indent.c_str(), argIndex);
313                     fprintf(out,
314                             "%s    System.arraycopy("
315                             "arg%dBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, "
316                             "arg%dBytes.length);\n",
317                             indent.c_str(), argIndex, argIndex);
318                     fprintf(out, "%s    pos += STRING_TYPE_OVERHEAD + arg%dBytes.length;\n",
319                             indent.c_str(), argIndex);
320                     break;
321                 case JAVA_TYPE_BYTE_ARRAY:
322                     fprintf(out, "%s    buff[pos] = STRING_TYPE;\n", indent.c_str());
323                     fprintf(out, "%s    copyInt(buff, pos + 1, arg%d.length);\n", indent.c_str(),
324                             argIndex);
325                     fprintf(out,
326                             "%s    System.arraycopy("
327                             "arg%d, 0, buff, pos + STRING_TYPE_OVERHEAD, arg%d.length);\n",
328                             indent.c_str(), argIndex, argIndex);
329                     fprintf(out, "%s    pos += STRING_TYPE_OVERHEAD + arg%d.length;\n",
330                             indent.c_str(), argIndex);
331                     break;
332                 case JAVA_TYPE_ATTRIBUTION_CHAIN: {
333                     requiredHelpers |= JAVA_MODULE_REQUIRES_ATTRIBUTION;
334                     const char* uidName = attributionDecl.fields.front().name.c_str();
335                     const char* tagName = attributionDecl.fields.back().name.c_str();
336 
337                     fprintf(out, "%s    writeAttributionChain(buff, pos, %s, %s);\n",
338                             indent.c_str(), uidName, tagName);
339                     fprintf(out, "%s    pos += attrSize;\n", indent.c_str());
340                     break;
341                 }
342                 case JAVA_TYPE_KEY_VALUE_PAIR:
343                     requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
344                     requiredHelpers |= JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS;
345                     fprintf(out,
346                             "%s    writeKeyValuePairs(buff, pos, (byte) count, intMap, "
347                             "longMap, "
348                             "stringMap, floatMap);\n",
349                             indent.c_str());
350                     fprintf(out, "%s    pos += keyValuePairSize;\n", indent.c_str());
351                     break;
352                 default:
353                     // Unsupported types: OBJECT, DOUBLE.
354                     fprintf(stderr, "Object and Double are not supported in module logging");
355                     return 1;
356             }
357             argIndex++;
358         }
359 
360         fprintf(out, "%s    StatsLog.writeRaw(buff, pos);\n", indent.c_str());
361         fprintf(out, "%s}\n", indent.c_str());
362         fprintf(out, "\n");
363     }
364 
365     write_java_helpers_for_q_schema_methods(out, attributionDecl, requiredHelpers, indent);
366 
367     return 0;
368 }
369 
write_java_helpers_for_q_schema_methods(FILE * out,const AtomDecl & attributionDecl,const int requiredHelpers,const string & indent)370 void write_java_helpers_for_q_schema_methods(FILE* out, const AtomDecl& attributionDecl,
371                                              const int requiredHelpers, const string& indent) {
372     fprintf(out, "\n");
373     fprintf(out, "%s// Helper methods for copying primitives\n", indent.c_str());
374     fprintf(out, "%sprivate static void copyInt(byte[] buff, int pos, int val) {\n",
375             indent.c_str());
376     fprintf(out, "%s    buff[pos] = (byte) (val);\n", indent.c_str());
377     fprintf(out, "%s    buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str());
378     fprintf(out, "%s    buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str());
379     fprintf(out, "%s    buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str());
380     fprintf(out, "%s    return;\n", indent.c_str());
381     fprintf(out, "%s}\n", indent.c_str());
382     fprintf(out, "\n");
383 
384     fprintf(out, "%sprivate static void copyLong(byte[] buff, int pos, long val) {\n",
385             indent.c_str());
386     fprintf(out, "%s    buff[pos] = (byte) (val);\n", indent.c_str());
387     fprintf(out, "%s    buff[pos + 1] = (byte) (val >> 8);\n", indent.c_str());
388     fprintf(out, "%s    buff[pos + 2] = (byte) (val >> 16);\n", indent.c_str());
389     fprintf(out, "%s    buff[pos + 3] = (byte) (val >> 24);\n", indent.c_str());
390     fprintf(out, "%s    buff[pos + 4] = (byte) (val >> 32);\n", indent.c_str());
391     fprintf(out, "%s    buff[pos + 5] = (byte) (val >> 40);\n", indent.c_str());
392     fprintf(out, "%s    buff[pos + 6] = (byte) (val >> 48);\n", indent.c_str());
393     fprintf(out, "%s    buff[pos + 7] = (byte) (val >> 56);\n", indent.c_str());
394     fprintf(out, "%s    return;\n", indent.c_str());
395     fprintf(out, "%s}\n", indent.c_str());
396     fprintf(out, "\n");
397 
398     if (requiredHelpers & JAVA_MODULE_REQUIRES_FLOAT) {
399         fprintf(out, "%sprivate static void copyFloat(byte[] buff, int pos, float val) {\n",
400                 indent.c_str());
401         fprintf(out, "%s    copyInt(buff, pos, Float.floatToIntBits(val));\n", indent.c_str());
402         fprintf(out, "%s    return;\n", indent.c_str());
403         fprintf(out, "%s}\n", indent.c_str());
404         fprintf(out, "\n");
405     }
406 
407     if (requiredHelpers & JAVA_MODULE_REQUIRES_ATTRIBUTION) {
408         fprintf(out, "%sprivate static void writeAttributionChain(byte[] buff, int pos",
409                 indent.c_str());
410         for (const auto& chainField : attributionDecl.fields) {
411             fprintf(out, ", %s[] %s", java_type_name(chainField.javaType), chainField.name.c_str());
412         }
413         fprintf(out, ") {\n");
414 
415         const char* uidName = attributionDecl.fields.front().name.c_str();
416         const char* tagName = attributionDecl.fields.back().name.c_str();
417 
418         // Write the first list begin.
419         fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
420         fprintf(out, "%s    buff[pos + 1] = (byte) (%s.length);\n", indent.c_str(), tagName);
421         fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
422 
423         // Iterate through the attribution chain and write the nodes.
424         fprintf(out, "%s    for (int i = 0; i < %s.length; i++) {\n", indent.c_str(), tagName);
425         // Write the list begin.
426         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
427         fprintf(out, "%s        buff[pos + 1] = %lu;\n", indent.c_str(),
428                 attributionDecl.fields.size());
429         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
430 
431         // Write the uid.
432         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
433         fprintf(out, "%s        copyInt(buff, pos + 1, %s[i]);\n", indent.c_str(), uidName);
434         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
435 
436         // Write the tag.
437         fprintf(out, "%s        String %sStr = (%s[i] == null) ? \"\" : %s[i];\n", indent.c_str(),
438                 tagName, tagName, tagName);
439         fprintf(out,
440                 "%s        byte[] %sByte = "
441                 "%sStr.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
442                 indent.c_str(), tagName, tagName);
443         fprintf(out, "%s        buff[pos] = STRING_TYPE;\n", indent.c_str());
444         fprintf(out, "%s        copyInt(buff, pos + 1, %sByte.length);\n", indent.c_str(), tagName);
445         fprintf(out,
446                 "%s        System.arraycopy("
447                 "%sByte, 0, buff, pos + STRING_TYPE_OVERHEAD, %sByte.length);\n",
448                 indent.c_str(), tagName, tagName);
449         fprintf(out, "%s        pos += STRING_TYPE_OVERHEAD + %sByte.length;\n", indent.c_str(),
450                 tagName);
451         fprintf(out, "%s    }\n", indent.c_str());
452         fprintf(out, "%s}\n", indent.c_str());
453         fprintf(out, "\n");
454     }
455 
456     if (requiredHelpers & JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS) {
457         fprintf(out,
458                 "%sprivate static void writeKeyValuePairs(byte[] buff, int pos, "
459                 "byte numPairs,\n",
460                 indent.c_str());
461         fprintf(out, "%s        final android.util.SparseIntArray intMap,\n", indent.c_str());
462         fprintf(out, "%s        final android.util.SparseLongArray longMap,\n", indent.c_str());
463         fprintf(out, "%s        final android.util.SparseArray<String> stringMap,\n",
464                 indent.c_str());
465         fprintf(out, "%s        final android.util.SparseArray<Float> floatMap) {\n",
466                 indent.c_str());
467 
468         // Start list of lists.
469         fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
470         fprintf(out, "%s    buff[pos + 1] = (byte) numPairs;\n", indent.c_str());
471         fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
472 
473         // Write integers.
474         fprintf(out, "%s    final int intMapSize = null == intMap ? 0 : intMap.size();\n",
475                 indent.c_str());
476         fprintf(out, "%s    for (int i = 0; i < intMapSize; i++) {\n", indent.c_str());
477         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
478         fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
479         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
480         fprintf(out, "%s        final int key = intMap.keyAt(i);\n", indent.c_str());
481         fprintf(out, "%s        final int value = intMap.valueAt(i);\n", indent.c_str());
482         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
483         fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
484         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
485         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
486         fprintf(out, "%s        copyInt(buff, pos + 1, value);\n", indent.c_str());
487         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
488         fprintf(out, "%s    }\n", indent.c_str());
489 
490         // Write longs.
491         fprintf(out, "%s    final int longMapSize = null == longMap ? 0 : longMap.size();\n",
492                 indent.c_str());
493         fprintf(out, "%s    for (int i = 0; i < longMapSize; i++) {\n", indent.c_str());
494         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
495         fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
496         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
497         fprintf(out, "%s        final int key = longMap.keyAt(i);\n", indent.c_str());
498         fprintf(out, "%s        final long value = longMap.valueAt(i);\n", indent.c_str());
499         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
500         fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
501         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
502         fprintf(out, "%s        buff[pos] = LONG_TYPE;\n", indent.c_str());
503         fprintf(out, "%s        copyLong(buff, pos + 1, value);\n", indent.c_str());
504         fprintf(out, "%s        pos += LONG_TYPE_SIZE;\n", indent.c_str());
505         fprintf(out, "%s    }\n", indent.c_str());
506 
507         // Write Strings.
508         fprintf(out,
509                 "%s    final int stringMapSize = null == stringMap ? 0 : "
510                 "stringMap.size();\n",
511                 indent.c_str());
512         fprintf(out, "%s    for (int i = 0; i < stringMapSize; i++) {\n", indent.c_str());
513         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
514         fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
515         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
516         fprintf(out, "%s        final int key = stringMap.keyAt(i);\n", indent.c_str());
517         fprintf(out, "%s        final String value = stringMap.valueAt(i);\n", indent.c_str());
518         fprintf(out,
519                 "%s        final byte[] valueBytes = "
520                 "value.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
521                 indent.c_str());
522         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
523         fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
524         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
525         fprintf(out, "%s        buff[pos] = STRING_TYPE;\n", indent.c_str());
526         fprintf(out, "%s        copyInt(buff, pos + 1, valueBytes.length);\n", indent.c_str());
527         fprintf(out,
528                 "%s        System.arraycopy("
529                 "valueBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, "
530                 "valueBytes.length);\n",
531                 indent.c_str());
532         fprintf(out, "%s        pos += STRING_TYPE_OVERHEAD + valueBytes.length;\n",
533                 indent.c_str());
534         fprintf(out, "%s    }\n", indent.c_str());
535 
536         // Write floats.
537         fprintf(out,
538                 "%s    final int floatMapSize = null == floatMap ? 0 : "
539                 "floatMap.size();\n",
540                 indent.c_str());
541         fprintf(out, "%s    for (int i = 0; i < floatMapSize; i++) {\n", indent.c_str());
542         fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
543         fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
544         fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
545         fprintf(out, "%s        final int key = floatMap.keyAt(i);\n", indent.c_str());
546         fprintf(out, "%s        final float value = floatMap.valueAt(i);\n", indent.c_str());
547         fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
548         fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
549         fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
550         fprintf(out, "%s        buff[pos] = FLOAT_TYPE;\n", indent.c_str());
551         fprintf(out, "%s        copyFloat(buff, pos + 1, value);\n", indent.c_str());
552         fprintf(out, "%s        pos += FLOAT_TYPE_SIZE;\n", indent.c_str());
553         fprintf(out, "%s    }\n", indent.c_str());
554         fprintf(out, "%s}\n", indent.c_str());
555         fprintf(out, "\n");
556     }
557 }
558 
559 }  // namespace stats_log_api_gen
560 }  // namespace android
561