Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f688df3f authored by Muhammad Qureshi's avatar Muhammad Qureshi Committed by Android (Google) Code Review
Browse files

Merge "Add KeyValuePairs support for modules."

parents 93f815f8 b6b3995f
Loading
Loading
Loading
Loading
+224 −8
Original line number Diff line number Diff line
@@ -75,9 +75,7 @@ int write_java_methods_q_schema(
                        java_type_name(chainField.javaType), chainField.name.c_str());
                }
            } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
                // Module logging does not yet support key value pair.
                fprintf(stderr, "Module logging does not yet support key value pair.\n");
                continue;
                fprintf(out, ", android.util.SparseArray<Object> valueMap");
            } else {
                fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
            }
@@ -161,9 +159,114 @@ int write_java_methods_q_schema(
                fprintf(out, "%s    needed += attrSize;\n", indent.c_str());
                break;
            }
            case JAVA_TYPE_KEY_VALUE_PAIR:
            {
                fprintf(out,
                        "%s    // Calculate bytes needed by Key Value Pairs.\n",
                        indent.c_str());
                fprintf(out,
                        "%s    final int count = valueMap.size();\n", indent.c_str());
                fprintf(out,
                        "%s    android.util.SparseIntArray intMap = null;\n", indent.c_str());
                fprintf(out,
                        "%s    android.util.SparseLongArray longMap = null;\n", indent.c_str());
                fprintf(out,
                        "%s    android.util.SparseArray<String> stringMap = null;\n",
                        indent.c_str());
                fprintf(out,
                        "%s    android.util.SparseArray<Float> floatMap = null;\n", indent.c_str());
                fprintf(out,
                        "%s    int keyValuePairSize = LIST_TYPE_OVERHEAD * 5;\n",
                        indent.c_str());
                fprintf(out,
                        "%s    for (int i = 0; i < count; i++) {\n", indent.c_str());
                fprintf(out,
                        "%s        final int key = valueMap.keyAt(i);\n", indent.c_str());
                fprintf(out,
                        "%s        final Object value = valueMap.valueAt(i);\n",
                        indent.c_str());
                fprintf(out,
                        "%s        if (value instanceof Integer) {\n", indent.c_str());
                fprintf(out,
                        "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
                        indent.c_str());
                fprintf(out,
                        "%s                    + INT_TYPE_SIZE + INT_TYPE_SIZE;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            if (null == intMap) {\n", indent.c_str());
                fprintf(out,
                        "%s                intMap = new android.util.SparseIntArray();\n", indent.c_str());
                fprintf(out,
                        "%s            }\n", indent.c_str());
                fprintf(out,
                        "%s            intMap.put(key, (Integer) value);\n", indent.c_str());
                fprintf(out,
                        "%s        } else if (value instanceof Long) {\n", indent.c_str());
                fprintf(out,
                        "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
                        indent.c_str());
                fprintf(out,
                        "%s                    + INT_TYPE_SIZE + LONG_TYPE_SIZE;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            if (null == longMap) {\n", indent.c_str());
                fprintf(out,
                        "%s                longMap = new android.util.SparseLongArray();\n", indent.c_str());
                fprintf(out,
                        "%s            }\n", indent.c_str());
                fprintf(out,
                        "%s            longMap.put(key, (Long) value);\n", indent.c_str());
                fprintf(out,
                        "%s        } else if (value instanceof String) {\n", indent.c_str());
                fprintf(out,
                        "%s            final String str = (value == null) ? \"\" : "
                        "(String) value;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            final int len = "
                        "str.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            keyValuePairSize += LIST_TYPE_OVERHEAD + INT_TYPE_SIZE\n",
                        indent.c_str());
                fprintf(out,
                        "%s                    + STRING_TYPE_OVERHEAD + len;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            if (null == stringMap) {\n", indent.c_str());
                fprintf(out,
                        "%s                stringMap = new android.util.SparseArray<>();\n", indent.c_str());
                fprintf(out,
                        "%s            }\n", indent.c_str());
                fprintf(out,
                        "%s            stringMap.put(key, str);\n", indent.c_str());
                fprintf(out,
                        "%s        } else if (value instanceof Float) {\n", indent.c_str());
                fprintf(out,
                        "%s            keyValuePairSize += LIST_TYPE_OVERHEAD\n",
                        indent.c_str());
                fprintf(out,
                        "%s                    + INT_TYPE_SIZE + FLOAT_TYPE_SIZE;\n",
                        indent.c_str());
                fprintf(out,
                        "%s            if (null == floatMap) {\n", indent.c_str());
                fprintf(out,
                        "%s                floatMap = new android.util.SparseArray<>();\n", indent.c_str());
                fprintf(out,
                        "%s            }\n", indent.c_str());
                fprintf(out,
                        "%s            floatMap.put(key, (Float) value);\n", indent.c_str());
                fprintf(out,
                        "%s        }\n", indent.c_str());
                fprintf(out,
                        "%s    }\n", indent.c_str());
                fprintf(out, "%s    needed += keyValuePairSize;\n", indent.c_str());
                break;
            }
            default:
                // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
                fprintf(stderr, "Module logging does not yet support key value pair.\n");
                // Unsupported types: OBJECT, DOUBLE.
                fprintf(stderr, "Module logging does not yet support Object and Double.\n");
                return 1;
            }
            argIndex++;
@@ -253,10 +356,18 @@ int write_java_methods_q_schema(
                fprintf(out, "%s    pos += attrSize;\n", indent.c_str());
                break;
            }
            case JAVA_TYPE_KEY_VALUE_PAIR:
                requiredHelpers |= JAVA_MODULE_REQUIRES_FLOAT;
                requiredHelpers |= JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS;
                fprintf(out,
                        "%s    writeKeyValuePairs(buff, pos, intMap, longMap, stringMap, "
                        "floatMap);\n", indent.c_str());
                fprintf(out, "%s    pos += keyValuePairSize;\n", indent.c_str());
                break;
            default:
                // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIR.
                // Unsupported types: OBJECT, DOUBLE.
                fprintf(stderr,
                        "Object, Double, and KeyValuePairs are not supported in module logging");
                        "Object and Double are not supported in module logging");
                return 1;
            }
            argIndex++;
@@ -359,6 +470,111 @@ void write_java_helpers_for_q_schema_methods(
        fprintf(out, "%s}\n", indent.c_str());
        fprintf(out, "\n");
    }

    if (requiredHelpers & JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS) {
        fprintf(out, "%sprivate static void writeKeyValuePairs(byte[] buff, int pos,\n",
                indent.c_str());
        fprintf(out, "%s        final android.util.SparseIntArray intMap,\n", indent.c_str());
        fprintf(out, "%s        final android.util.SparseLongArray longMap,\n", indent.c_str());
        fprintf(out, "%s        final android.util.SparseArray<String> stringMap,\n",
                indent.c_str());
        fprintf(out, "%s        final android.util.SparseArray<Float> floatMap) {\n",
                indent.c_str());

        // Start list of lists.
        fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s    buff[pos + 1] = (byte) 4;\n", indent.c_str());
        fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());

        // Write integers.
        fprintf(out, "%s    final int intMapSize = null == intMap ? 0 : intMap.size();\n",
                indent.c_str());
        fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s    buff[pos + 1] = (byte) intMapSize;\n", indent.c_str());
        fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s    for (int i = 0; i < intMapSize; i++) {\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
        fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s        final int key = intMap.keyAt(i);\n", indent.c_str());
        fprintf(out, "%s        final int value = intMap.valueAt(i);\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
        fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, value);\n", indent.c_str());
        fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s    }\n", indent.c_str());

        // Write longs.
        fprintf(out, "%s    final int longMapSize = null == longMap ? 0 : longMap.size();\n",
                indent.c_str());
        fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s    buff[pos + 1] = (byte) longMapSize;\n", indent.c_str());
        fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s    for (int i = 0; i < longMapSize; i++) {\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
        fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s        final int key = longMap.keyAt(i);\n", indent.c_str());
        fprintf(out, "%s        final long value = longMap.valueAt(i);\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
        fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = LONG_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyLong(buff, pos + 1, value);\n", indent.c_str());
        fprintf(out, "%s        pos += LONG_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s    }\n", indent.c_str());

        // Write Strings.
        fprintf(out, "%s    final int stringMapSize = null == stringMap ? 0 : stringMap.size();\n",
                indent.c_str());
        fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s    buff[pos + 1] = (byte) stringMapSize;\n", indent.c_str());
        fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s    for (int i = 0; i < stringMapSize; i++) {\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
        fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s        final int key = stringMap.keyAt(i);\n", indent.c_str());
        fprintf(out, "%s        final String value = stringMap.valueAt(i);\n", indent.c_str());
        fprintf(out, "%s        final byte[] valueBytes = "
                "value.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n",
                indent.c_str());
        fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
        fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = STRING_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, valueBytes.length);\n", indent.c_str());
        fprintf(out, "%s        System.arraycopy("
                "valueBytes, 0, buff, pos + STRING_TYPE_OVERHEAD, valueBytes.length);\n",
                indent.c_str());
        fprintf(out, "%s        pos += STRING_TYPE_OVERHEAD + valueBytes.length;\n",
                indent.c_str());
        fprintf(out, "%s    }\n", indent.c_str());

        // Write floats.
        fprintf(out, "%s    final int floatMapSize = null == floatMap ? 0 : floatMap.size();\n",
                indent.c_str());
        fprintf(out, "%s    buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s    buff[pos + 1] = (byte) floatMapSize;\n", indent.c_str());
        fprintf(out, "%s    pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s    for (int i = 0; i < floatMapSize; i++) {\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = LIST_TYPE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos + 1] = (byte) 2;\n", indent.c_str());
        fprintf(out, "%s        pos += LIST_TYPE_OVERHEAD;\n", indent.c_str());
        fprintf(out, "%s        final int key = floatMap.keyAt(i);\n", indent.c_str());
        fprintf(out, "%s        final float value = floatMap.valueAt(i);\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = INT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyInt(buff, pos + 1, key);\n", indent.c_str());
        fprintf(out, "%s        pos += INT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s        buff[pos] = FLOAT_TYPE;\n", indent.c_str());
        fprintf(out, "%s        copyFloat(buff, pos + 1, value);\n", indent.c_str());
        fprintf(out, "%s        pos += FLOAT_TYPE_SIZE;\n", indent.c_str());
        fprintf(out, "%s    }\n", indent.c_str());
        fprintf(out, "%s}\n", indent.c_str());
        fprintf(out, "\n");
    }
}

#if defined(STATS_SCHEMA_LEGACY)
@@ -382,7 +598,7 @@ static void write_java_method(
                        java_type_name(chainField.javaType), chainField.name.c_str());
                }
            } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
                fprintf(out, ", android.util.SparseArray<Object> value_map");
                fprintf(out, ", android.util.SparseArray<Object> valueMap");
            } else {
                fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
            }
+1 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ const string DEFAULT_JAVA_CLASS = "StatsLogInternal";

const int JAVA_MODULE_REQUIRES_FLOAT = 0x01;
const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02;
const int JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS = 0x04;

string make_constant_name(const string& str);