[IStats AIDL] Added annotations for vendor atoms

- added new APIs to pass annotations

Test: m
Bug: 256982022
Bug: 259104358
Change-Id: Ife7d5250e4c37db311b98f70dd1c0d3a2e28735f
diff --git a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/Annotation.aidl b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/Annotation.aidl
new file mode 100644
index 0000000..825db0d
--- /dev/null
+++ b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/Annotation.aidl
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2023 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.frameworks.stats;
+@VintfStability
+parcelable Annotation {
+  android.frameworks.stats.AnnotationId annotationId = android.frameworks.stats.AnnotationId.UNKNOWN;
+  android.frameworks.stats.AnnotationValue value;
+}
diff --git a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationId.aidl b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationId.aidl
new file mode 100644
index 0000000..848b5e9
--- /dev/null
+++ b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationId.aidl
@@ -0,0 +1,46 @@
+//
+// Copyright (C) 2023 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.frameworks.stats;
+@Backing(type="int") @VintfStability
+enum AnnotationId {
+  UNKNOWN = 0,
+  IS_UID = 1,
+  TRUNCATE_TIMESTAMP = 2,
+  PRIMARY_FIELD = 3,
+  EXCLUSIVE_STATE = 4,
+  PRIMARY_FIELD_FIRST_UID = 5,
+  DEFAULT_STATE = 6,
+  TRIGGER_STATE_RESET = 7,
+  STATE_NESTED = 8,
+}
diff --git a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationSet.aidl b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationSet.aidl
new file mode 100644
index 0000000..4d78469
--- /dev/null
+++ b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationSet.aidl
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2023 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.frameworks.stats;
+@VintfStability
+parcelable AnnotationSet {
+  int valueIndex;
+  android.frameworks.stats.Annotation[] annotations;
+}
diff --git a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationValue.aidl b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationValue.aidl
new file mode 100644
index 0000000..316ee19
--- /dev/null
+++ b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/AnnotationValue.aidl
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2023 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.frameworks.stats;
+@VintfStability
+union AnnotationValue {
+  int intValue;
+  boolean boolValue;
+}
diff --git a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/VendorAtom.aidl b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/VendorAtom.aidl
index c91e4e0..3174d5f 100644
--- a/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/VendorAtom.aidl
+++ b/stats/aidl/aidl_api/android.frameworks.stats/current/android/frameworks/stats/VendorAtom.aidl
@@ -37,4 +37,6 @@
   String reverseDomainName;
   int atomId;
   android.frameworks.stats.VendorAtomValue[] values;
+  @nullable android.frameworks.stats.AnnotationSet[] valuesAnnotations;
+  @nullable android.frameworks.stats.Annotation[] atomAnnotations;
 }
diff --git a/stats/aidl/android/frameworks/stats/Annotation.aidl b/stats/aidl/android/frameworks/stats/Annotation.aidl
new file mode 100644
index 0000000..4c90ac9
--- /dev/null
+++ b/stats/aidl/android/frameworks/stats/Annotation.aidl
@@ -0,0 +1,28 @@
+//
+// Copyright (C) 2023 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.
+//
+package android.frameworks.stats;
+
+import android.frameworks.stats.AnnotationId;
+import android.frameworks.stats.AnnotationValue;
+
+/*
+ * Atom/Field annotation single entry
+ */
+@VintfStability
+parcelable Annotation {
+    AnnotationId annotationId = AnnotationId.UNKNOWN;
+    AnnotationValue value;
+}
diff --git a/stats/aidl/android/frameworks/stats/AnnotationId.aidl b/stats/aidl/android/frameworks/stats/AnnotationId.aidl
new file mode 100644
index 0000000..31aa8ac
--- /dev/null
+++ b/stats/aidl/android/frameworks/stats/AnnotationId.aidl
@@ -0,0 +1,67 @@
+//
+// Copyright (C) 2023 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.
+//
+package android.frameworks.stats;
+
+/*
+ * Mimics packages/modules/StatsD/lib/libstatssocket/include/stats_annotations.h
+ * These ids must stay consistent with those in stats_annotations.h
+ */
+@VintfStability
+@Backing(type="int")
+enum AnnotationId {
+    UNKNOWN = 0,
+    /**
+     * Annotation ID constant for logging UID field.
+     */
+    IS_UID = 1,
+
+    /**
+     * Annotation ID constant to indicate logged atom event's timestamp should be truncated.
+     */
+    TRUNCATE_TIMESTAMP = 2,
+
+    /**
+     * Annotation ID constant for a state atom's primary field.
+     */
+    PRIMARY_FIELD = 3,
+
+    /**
+     * Annotation ID constant for state atom's state field.
+     */
+    EXCLUSIVE_STATE = 4,
+
+    /**
+     * Annotation ID constant to indicate the first UID in the attribution chain
+     * is a primary field.
+     */
+    PRIMARY_FIELD_FIRST_UID = 5,
+
+    /**
+     * Annotation ID constant to indicate which state is default for the state atom.
+     */
+    DEFAULT_STATE = 6,
+
+    /**
+     * Annotation ID constant to signal that all states should be reset to the default state.
+     */
+    TRIGGER_STATE_RESET = 7,
+
+    /**
+     * Annotation ID constant to indicate state changes need to account for nesting.
+     * This should only be used with binary state atoms.
+     */
+    STATE_NESTED = 8,
+}
diff --git a/stats/aidl/android/frameworks/stats/AnnotationSet.aidl b/stats/aidl/android/frameworks/stats/AnnotationSet.aidl
new file mode 100644
index 0000000..4d3fb9a
--- /dev/null
+++ b/stats/aidl/android/frameworks/stats/AnnotationSet.aidl
@@ -0,0 +1,34 @@
+//
+// Copyright (C) 2023 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.
+//
+package android.frameworks.stats;
+
+import android.frameworks.stats.Annotation;
+
+/*
+ * Single VendorAtomValue could have multiple annotations associated with it
+ */
+@VintfStability
+parcelable AnnotationSet {
+    /*
+     * Index to map VendorAtomValueAnnotation array with an individual VendorAtomValue
+     * from VendorAtom.values
+     */
+    int valueIndex;
+    /*
+     * Vector of annotations associated with VendorAtom.values[valueIndex]
+     */
+    Annotation[] annotations;
+}
diff --git a/stats/aidl/android/frameworks/stats/AnnotationValue.aidl b/stats/aidl/android/frameworks/stats/AnnotationValue.aidl
new file mode 100644
index 0000000..7beb196
--- /dev/null
+++ b/stats/aidl/android/frameworks/stats/AnnotationValue.aidl
@@ -0,0 +1,25 @@
+//
+// Copyright (C) 2023 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.
+//
+package android.frameworks.stats;
+
+/*
+ * Supported field types for this struct.
+ */
+@VintfStability
+union AnnotationValue {
+    int intValue;
+    boolean boolValue;
+}
diff --git a/stats/aidl/android/frameworks/stats/VendorAtom.aidl b/stats/aidl/android/frameworks/stats/VendorAtom.aidl
index 9f34395..58038ad 100644
--- a/stats/aidl/android/frameworks/stats/VendorAtom.aidl
+++ b/stats/aidl/android/frameworks/stats/VendorAtom.aidl
@@ -15,6 +15,8 @@
 //
 package android.frameworks.stats;
 
+import android.frameworks.stats.Annotation;
+import android.frameworks.stats.AnnotationSet;
 import android.frameworks.stats.VendorAtomValue;
 
 /*
@@ -50,5 +52,42 @@
      * Vector of fields in the order that the LogEvent should be filled.
      */
     VendorAtomValue[] values;
-}
 
+    /*
+    * Vector of annotations associated with VendorAtom.values
+    *
+    * Having the atom with below definition
+    *
+    * message SimpleVendorAtom {
+    *   optional string reverse_domain_name = 1;
+    *   optional int field1 = 2 [annotation1 = 1, annotation2 = true];
+    *   optional float field2 = 3;
+    *   optional float field3 = 4 [annotation1 = 2, annotation2 = false];
+    * }
+    *
+    * The valuesAnnotations will contain 2 entries
+    *  - valuesAnnotations[0] for field1
+    *  - valuesAnnotations[1] for field3
+    *
+    * The VendorAtomAnnotationSet[i].valueIndex used for mapping each individual
+    * annotation set to specific atom value by VendorAtom.values array index:
+    *
+    * valuesAnnotations[0].valueIndex = 0 // index of field1 in VendorAtom.values[] array
+    * valuesAnnotations[0].annotations[0].type = annotation1
+    * valuesAnnotations[0].annotations[0].value = 1
+    * valuesAnnotations[0].annotations[1].type = annotation2
+    * valuesAnnotations[0].annotations[1].value = true
+    *
+    * valuesAnnotations[1].valueIndex = 2 // index of field1 in VendorAtom.values[] array
+    * valuesAnnotations[1].annotations[0].type = annotation1
+    * valuesAnnotations[1].annotations[0].value = 2
+    * valuesAnnotations[1].annotations[1].type = annotation2
+    * valuesAnnotations[1].annotations[1].value = false
+    */
+    @nullable AnnotationSet[] valuesAnnotations;
+
+    /*
+    * Vector of annotations associated with VendorAtom
+    */
+    @nullable Annotation[] atomAnnotations;
+}
diff --git a/stats/aidl/test_client/AidlStatsClient.cpp b/stats/aidl/test_client/AidlStatsClient.cpp
index afa130a..04c0ab5 100644
--- a/stats/aidl/test_client/AidlStatsClient.cpp
+++ b/stats/aidl/test_client/AidlStatsClient.cpp
@@ -14,17 +14,13 @@
  * limitations under the License.
  */
 #include <aidl/android/frameworks/stats/IStats.h>
-
 #include <android/binder_manager.h>
-
+#include <getopt.h>
 #include <statslog.h>
 
-#include <getopt.h>
 #include <iostream>
 
-using aidl::android::frameworks::stats::IStats;
-using aidl::android::frameworks::stats::VendorAtom;
-using aidl::android::frameworks::stats::VendorAtomValue;
+using namespace aidl::android::frameworks::stats;
 
 void expect_message(int32_t action) {
     std::cout << "expect the following log in logcat:\n";
@@ -105,7 +101,51 @@
                 std::vector<uint8_t> byteArrayValue = {21, 50, 3};
                 tmp.set<VendorAtomValue::byteArrayValue>(byteArrayValue);
                 values.push_back(tmp);
-                VendorAtom atom = {.reverseDomainName = "", .atomId = 104999, .values = values};
+
+                // example of atom level annotation
+                Annotation atomAnnotation{AnnotationId::TRUNCATE_TIMESTAMP, true};
+                std::vector<std::optional<Annotation>> atomAnnotations;
+                atomAnnotations.push_back(std::make_optional<Annotation>(atomAnnotation));
+
+                // values annotation
+                std::vector<std::optional<AnnotationSet>> valuesAnnotations;
+                {
+                    AnnotationSet valueAnnotationSet;
+                    valueAnnotationSet.valueIndex = 0;
+                    valueAnnotationSet.annotations.push_back(
+                        Annotation{AnnotationId::PRIMARY_FIELD, true});
+                    valuesAnnotations.push_back(
+                        std::make_optional<AnnotationSet>(valueAnnotationSet));
+                }
+                {
+                    AnnotationSet valueAnnotationSet;
+                    valueAnnotationSet.valueIndex = 1;
+                    valueAnnotationSet.annotations.push_back(
+                        Annotation{AnnotationId::IS_UID, true});
+                    valuesAnnotations.push_back(
+                        std::make_optional<AnnotationSet>(valueAnnotationSet));
+                }
+                {
+                    AnnotationSet valueAnnotationSet;
+                    valueAnnotationSet.valueIndex = 4;
+                    valueAnnotationSet.annotations.push_back(
+                        Annotation{AnnotationId::EXCLUSIVE_STATE, true});
+                    valueAnnotationSet.annotations.push_back(
+                        Annotation{AnnotationId::STATE_NESTED, true});
+                    valueAnnotationSet.annotations.push_back(
+                        Annotation{AnnotationId::TRIGGER_STATE_RESET, 0});
+                    valuesAnnotations.push_back(
+                        std::make_optional<AnnotationSet>(valueAnnotationSet));
+                }
+
+                VendorAtom atom = {
+                    .atomId = 104999,
+                    .values = values,
+                    .atomAnnotations =
+                        std::make_optional<std::vector<std::optional<Annotation>>>(atomAnnotations),
+                    .valuesAnnotations =
+                        std::make_optional<std::vector<std::optional<AnnotationSet>>>(
+                            valuesAnnotations)};
                 const ndk::ScopedAStatus ret = service->reportVendorAtom(atom);
                 if (!ret.isOk()) {
                     std::cout << "reportVendorAtom failed: " << ret.getServiceSpecificError()