Make state_option top-level boolean annotations

- Add AnnotationId enum stats-log-api-gen
- Rename RESET_STATE annotation to TRIGGER_STATE_RESET. Also rename the proto annotation.

Bug: 152412767
Test: stats-log-api-gen-test
Test: m statslog-framework-java-gen
Change-Id: I2c12ea0b9222ef28b8cc11ea36b272e518a96259
diff --git a/cmds/statsd/src/annotations.h b/cmds/statsd/src/annotations.h
index 1e9390e..cf7f543 100644
--- a/cmds/statsd/src/annotations.h
+++ b/cmds/statsd/src/annotations.h
@@ -22,13 +22,11 @@
 
 const uint8_t ANNOTATION_ID_IS_UID = 1;
 const uint8_t ANNOTATION_ID_TRUNCATE_TIMESTAMP = 2;
-const uint8_t ANNOTATION_ID_STATE_OPTION = 3;
-const uint8_t ANNOTATION_ID_RESET_STATE = 5;
-const uint8_t ANNOTATION_ID_STATE_NESTED = 6;
-
-const int32_t STATE_OPTION_PRIMARY_FIELD = 1;
-const int32_t STATE_OPTION_EXCLUSIVE_STATE = 2;
-const int32_t STATE_OPTION_PRIMARY_FIELD_FIRST_UID = 3;
+const uint8_t ANNOTATION_ID_PRIMARY_FIELD = 3;
+const uint8_t ANNOTATION_ID_EXCLUSIVE_STATE = 4;
+const uint8_t ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID = 5;
+const uint8_t ANNOTATION_ID_TRIGGER_STATE_RESET = 7;
+const uint8_t ANNOTATION_ID_STATE_NESTED = 8;
 
 } // namespace statsd
 } // namespace os
diff --git a/cmds/statsd/src/atom_field_options.proto b/cmds/statsd/src/atom_field_options.proto
index afee79d..8527185 100644
--- a/cmds/statsd/src/atom_field_options.proto
+++ b/cmds/statsd/src/atom_field_options.proto
@@ -23,25 +23,12 @@
 
 import "google/protobuf/descriptor.proto";
 
-enum StateField {
-    // Default value for fields that are not a primary or exclusive state field.
-    STATE_FIELD_UNSET = 0;
-    // Fields that represent the key that the state belongs to.
-    // Used on simple proto fields. Do not use on attribution chains.
-    PRIMARY_FIELD = 1;
-    // The field that represents the state. It's an exclusive state.
-    EXCLUSIVE_STATE = 2;
-    // Used on an attribution chain field to indicate that the first uid is the
-    // primary field.
-    PRIMARY_FIELD_FIRST_UID = 3;
-}
-
 // Used to annotate an atom that represents a state change. A state change atom must have exactly
 // ONE exclusive state field, and any number of primary key fields. For example, message
 // UidProcessStateChanged {
-//    optional int32 uid = 1 [(state_field_option).option = PRIMARY_FIELD];
-//    optional android.app.ProcessStateEnum state = 2 [(state_field_option).option =
-//    EXCLUSIVE_STATE];
+//    optional int32 uid = 1 [(state_field_option).primary_field = true];
+//    optional android.app.ProcessStateEnum state =
+//            2 [(state_field_option).exclusive_state = true];
 //  }
 // Each UidProcessStateChanged atom event represents a state change for a specific uid.
 // A new state automatically overrides the previous state.
@@ -50,23 +37,23 @@
 // primary fields are the primary key.
 // For example:
 // message ThreadStateChanged {
-//    optional int32 pid = 1  [(state_field_option).option = PRIMARY_FIELD];
-//    optional int32 tid = 2  [(state_field_option).option = PRIMARY_FIELD];
-//    optional int32 state = 3 [(state_field_option).option = EXCLUSIVE_STATE];
+//    optional int32 pid = 1  [(state_field_option).primary_field = true];
+//    optional int32 tid = 2  [(state_field_option).primary_field = true];
+//    optional int32 state = 3 [(state_field_option).exclusive_state = true];
 // }
 //
 // Sometimes, there is no primary key field, when the state is GLOBAL.
 // For example,
 // message ScreenStateChanged {
-//    optional android.view.DisplayStateEnum state = 1 [(state_field_option).option =
-//    EXCLUSIVE_STATE];
+//    optional android.view.DisplayStateEnum state =
+//          1 [(state_field_option).exclusive_state = true];
 // }
 //
 // For state atoms with attribution chain, sometimes the primary key is the first uid in the chain.
 // For example:
 // message AudioStateChanged {
 //   repeated AttributionNode attribution_node = 1
-//       [(stateFieldOption).option = PRIMARY_KEY_FIRST_UID];
+//       [(stateFieldOption).primary_field_first_uid = true];
 //
 //    enum State {
 //      OFF = 0;
@@ -74,10 +61,19 @@
 //      // RESET indicates all audio stopped. Used when it (re)starts (e.g. after it crashes).
 //      RESET = 2;
 //    }
-//    optional State state = 2 [(stateFieldOption).option = EXCLUSIVE_STATE];
+//    optional State state = 2 [(stateFieldOption).exclusive_state = true];
 // }
 message StateAtomFieldOption {
-    optional StateField option = 1 [default = STATE_FIELD_UNSET];
+    // Fields that represent the key that the state belongs to.
+    // Used on simple proto fields. Do not use on attribution chains.
+    optional bool primary_field = 1 [default = false];
+
+    // The field that represents the state. It's an exclusive state.
+    optional bool exclusive_state = 2 [default = false];
+
+    // Used on an attribution chain field to indicate that the first uid is the
+    // primary field.
+    optional bool primary_field_first_uid = 3 [default = false];
 
     // Note: We cannot annotate directly on the enums because many enums are imported from other
     // proto files in the platform. proto-lite cc library does not support annotations unfortunately
@@ -85,14 +81,14 @@
     // Knowing the default state value allows state trackers to remove entries that become the
     // default state. If there is no default value specified, the default value is unknown, and all
     // states will be tracked in memory.
-    optional int32 default_state_value = 2;
+    optional int32 default_state_value = 4;
 
     // A reset state signals all states go to default value. For example, BLE reset means all active
     // BLE scans are to be turned off.
-    optional int32 reset_state_value = 3;
+    optional int32 trigger_state_reset_value = 5;
 
     // If the state change needs to count nesting.
-    optional bool nested = 4 [default = true];
+    optional bool nested = 6 [default = true];
 }
 
 // Used to generate StatsLog.write APIs.
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto
index 5cd00c3..c7dbf77 100644
--- a/cmds/statsd/src/atoms.proto
+++ b/cmds/statsd/src/atoms.proto
@@ -610,7 +610,7 @@
 message ScreenStateChanged {
     // New screen state, from frameworks/base/core/proto/android/view/enums.proto.
     optional android.view.DisplayStateEnum state = 1
-            [(state_field_option).option = EXCLUSIVE_STATE, (state_field_option).nested = false];
+            [(state_field_option).exclusive_state = true, (state_field_option).nested = false];
 }
 
 /**
@@ -621,11 +621,11 @@
  *   frameworks/base/services/core/java/com/android/server/am/BatteryStatsService.java
  */
 message UidProcessStateChanged {
-    optional int32 uid = 1 [(state_field_option).option = PRIMARY_FIELD, (is_uid) = true];
+    optional int32 uid = 1 [(state_field_option).primary_field = true, (is_uid) = true];
 
     // The state, from frameworks/base/core/proto/android/app/enums.proto.
     optional android.app.ProcessStateEnum state = 2
-            [(state_field_option).option = EXCLUSIVE_STATE, (state_field_option).nested = false];
+            [(state_field_option).exclusive_state = true, (state_field_option).nested = false];
 }
 
 /**
@@ -657,7 +657,7 @@
         ASLEEP = 1;
         AWAKE = 2;
     }
-    optional State state = 1 [(state_field_option).option = EXCLUSIVE_STATE];
+    optional State state = 1 [(state_field_option).exclusive_state = true];
 }
 
 /**
@@ -676,7 +676,7 @@
         CRITICAL = 4;   // critical memory.
 
     }
-    optional State factor = 1 [(state_field_option).option = EXCLUSIVE_STATE];
+    optional State factor = 1 [(state_field_option).exclusive_state = true];
 }
 
 /**
@@ -854,7 +854,7 @@
  */
 message BleScanStateChanged {
     repeated AttributionNode attribution_node = 1
-            [(state_field_option).option = PRIMARY_FIELD_FIRST_UID];
+            [(state_field_option).primary_field_first_uid = true];
 
     enum State {
         OFF = 0;
@@ -863,18 +863,18 @@
         RESET = 2;
     }
     optional State state = 2 [
-        (state_field_option).option = EXCLUSIVE_STATE,
+        (state_field_option).exclusive_state = true,
         (state_field_option).default_state_value = 0 /* State.OFF */,
-        (state_field_option).reset_state_value = 2 /* State.RESET */,
+        (state_field_option).trigger_state_reset_value = 2 /* State.RESET */,
         (state_field_option).nested = true
     ];
 
     // Does the scan have a filter.
-    optional bool is_filtered = 3 [(state_field_option).option = PRIMARY_FIELD];
+    optional bool is_filtered = 3 [(state_field_option).primary_field = true];
     // Whether the scan is a CALLBACK_TYPE_FIRST_MATCH scan. Called 'background' scan internally.
-    optional bool is_first_match = 4 [(state_field_option).option = PRIMARY_FIELD];
+    optional bool is_first_match = 4 [(state_field_option).primary_field = true];
     // Whether the scan set to piggy-back off the results of other scans (SCAN_MODE_OPPORTUNISTIC).
-    optional bool is_opportunistic = 5 [(state_field_option).option = PRIMARY_FIELD];
+    optional bool is_opportunistic = 5 [(state_field_option).primary_field = true];
 }
 
 /**
@@ -1105,15 +1105,14 @@
  */
 message WakelockStateChanged {
     repeated AttributionNode attribution_node = 1
-            [(state_field_option).option = PRIMARY_FIELD_FIRST_UID];
+            [(state_field_option).primary_field_first_uid = true];
 
     // The type (level) of the wakelock; e.g. a partial wakelock or a full wakelock.
     // From frameworks/base/core/proto/android/os/enums.proto.
-    optional android.os.WakeLockLevelEnum type = 2 [(state_field_option).option = PRIMARY_FIELD];
-    ;
+    optional android.os.WakeLockLevelEnum type = 2 [(state_field_option).primary_field = true];
 
     // The wakelock tag (Called tag in the Java API, sometimes name elsewhere).
-    optional string tag = 3 [(state_field_option).option = PRIMARY_FIELD];
+    optional string tag = 3 [(state_field_option).primary_field = true];
 
     enum State {
         RELEASE = 0;
@@ -1122,7 +1121,7 @@
         CHANGE_ACQUIRE = 3;
     }
     optional State state = 4 [
-        (state_field_option).option = EXCLUSIVE_STATE,
+        (state_field_option).exclusive_state = true,
         (state_field_option).default_state_value = 0,
         (state_field_option).nested = true
     ];
@@ -3494,9 +3493,9 @@
  *     services/core/java/com/android/server/wm/Session.java
  */
 message OverlayStateChanged {
-    optional int32 uid = 1 [(state_field_option).option = PRIMARY_FIELD, (is_uid) = true];
+    optional int32 uid = 1 [(state_field_option).primary_field = true, (is_uid) = true];
 
-    optional string package_name = 2 [(state_field_option).option = PRIMARY_FIELD];
+    optional string package_name = 2 [(state_field_option).primary_field = true];
 
     optional bool using_alert_window = 3;
 
@@ -3505,7 +3504,7 @@
         EXITED = 2;
     }
     optional State state = 4 [
-        (state_field_option).option = EXCLUSIVE_STATE,
+        (state_field_option).exclusive_state = true,
         (state_field_option).nested = false,
         (state_field_option).default_state_value = 2
     ];
@@ -3713,7 +3712,7 @@
  */
 message AppDied {
     // timestamp(elapsedRealtime) of record creation
-    optional uint64 timestamp_millis = 1 [(state_field_option).option = EXCLUSIVE_STATE];
+    optional uint64 timestamp_millis = 1 [(state_field_option).exclusive_state = true];
 }
 
 /**
@@ -4235,7 +4234,7 @@
         DIALOG_LINE_ITEM = 5;
     }
 
-    optional Type type = 1 [(state_field_option).option = EXCLUSIVE_STATE];
+    optional Type type = 1 [(state_field_option).exclusive_state = true];
 
     // Used if the type is LINE_ITEM
     optional string package_name = 2;
diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp
index 0ec11f9..96bf04f 100644
--- a/cmds/statsd/src/logd/LogEvent.cpp
+++ b/cmds/statsd/src/logd/LogEvent.cpp
@@ -17,14 +17,14 @@
 #define DEBUG false  // STOPSHIP if true
 #include "logd/LogEvent.h"
 
+#include <android-base/stringprintf.h>
+#include <android/binder_ibinder.h>
+#include <private/android_filesystem_config.h>
+
 #include "annotations.h"
 #include "stats_log_util.h"
 #include "statslog_statsd.h"
 
-#include <android/binder_ibinder.h>
-#include <android-base/stringprintf.h>
-#include <private/android_filesystem_config.h>
-
 namespace android {
 namespace os {
 namespace statsd {
@@ -76,9 +76,7 @@
 }
 
 LogEvent::LogEvent(int32_t uid, int32_t pid)
-    : mLogdTimestampNs(time(nullptr)),
-      mLogUid(uid),
-      mLogPid(pid) {
+    : mLogdTimestampNs(time(nullptr)), mLogUid(uid), mLogPid(pid) {
 }
 
 LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
@@ -193,7 +191,7 @@
         uint8_t typeInfo = readNextValue<uint8_t>();
         switch (getTypeId(typeInfo)) {
             case INT32_TYPE:
-                pos[2] = 2; // pos[2] determined by index of type in KeyValuePair in atoms.proto
+                pos[2] = 2;  // pos[2] determined by index of type in KeyValuePair in atoms.proto
                 parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0);
                 break;
             case INT64_TYPE:
@@ -261,33 +259,38 @@
     mTruncateTimestamp = readNextValue<uint8_t>();
 }
 
-void LogEvent::parseStateOptionAnnotation(uint8_t annotationType, int firstUidInChainIndex) {
-    if (mValues.empty() || annotationType != INT32_TYPE) {
+void LogEvent::parsePrimaryFieldAnnotation(uint8_t annotationType) {
+    if (mValues.empty() || annotationType != BOOL_TYPE) {
         mValid = false;
         return;
     }
 
-    int32_t stateOption = readNextValue<int32_t>();
-    switch (stateOption) {
-        case STATE_OPTION_EXCLUSIVE_STATE:
-            mValues[mValues.size() - 1].mAnnotations.setExclusiveState(true);
-            break;
-        case STATE_OPTION_PRIMARY_FIELD:
-            mValues[mValues.size() - 1].mAnnotations.setPrimaryField(true);
-            break;
-        case STATE_OPTION_PRIMARY_FIELD_FIRST_UID:
-            if (firstUidInChainIndex == -1) {
-                mValid = false;
-            } else {
-                mValues[firstUidInChainIndex].mAnnotations.setPrimaryField(true);
-            }
-            break;
-        default:
-            mValid = false;
-    }
+    const bool primaryField = readNextValue<uint8_t>();
+    mValues[mValues.size() - 1].mAnnotations.setPrimaryField(primaryField);
 }
 
-void LogEvent::parseResetStateAnnotation(uint8_t annotationType) {
+void LogEvent::parsePrimaryFieldFirstUidAnnotation(uint8_t annotationType,
+                                                   int firstUidInChainIndex) {
+    if (mValues.empty() || annotationType != BOOL_TYPE || -1 == firstUidInChainIndex) {
+        mValid = false;
+        return;
+    }
+
+    const bool primaryField = readNextValue<uint8_t>();
+    mValues[firstUidInChainIndex].mAnnotations.setPrimaryField(primaryField);
+}
+
+void LogEvent::parseExclusiveStateAnnotation(uint8_t annotationType) {
+    if (mValues.empty() || annotationType != BOOL_TYPE) {
+        mValid = false;
+        return;
+    }
+
+    const bool exclusiveState = readNextValue<uint8_t>();
+    mValues[mValues.size() - 1].mAnnotations.setExclusiveState(exclusiveState);
+}
+
+void LogEvent::parseTriggerStateResetAnnotation(uint8_t annotationType) {
     if (mValues.empty() || annotationType != INT32_TYPE) {
         mValid = false;
         return;
@@ -321,11 +324,17 @@
             case ANNOTATION_ID_TRUNCATE_TIMESTAMP:
                 parseTruncateTimestampAnnotation(annotationType);
                 break;
-            case ANNOTATION_ID_STATE_OPTION:
-                parseStateOptionAnnotation(annotationType, firstUidInChainIndex);
+            case ANNOTATION_ID_PRIMARY_FIELD:
+                parsePrimaryFieldAnnotation(annotationType);
                 break;
-            case ANNOTATION_ID_RESET_STATE:
-                parseResetStateAnnotation(annotationType);
+            case ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID:
+                parsePrimaryFieldFirstUidAnnotation(annotationType, firstUidInChainIndex);
+                break;
+            case ANNOTATION_ID_EXCLUSIVE_STATE:
+                parseExclusiveStateAnnotation(annotationType);
+                break;
+            case ANNOTATION_ID_TRIGGER_STATE_RESET:
+                parseTriggerStateResetAnnotation(annotationType);
                 break;
             case ANNOTATION_ID_STATE_NESTED:
                 parseStateNestedAnnotation(annotationType);
@@ -362,8 +371,7 @@
     if (getTypeId(typeInfo) != INT32_TYPE) mValid = false;
     mTagId = readNextValue<int32_t>();
     numElements--;
-    parseAnnotations(getNumAnnotations(typeInfo)); // atom-level annotations
-
+    parseAnnotations(getNumAnnotations(typeInfo));  // atom-level annotations
 
     for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) {
         last[0] = (pos[0] == numElements);
@@ -372,7 +380,7 @@
         uint8_t typeId = getTypeId(typeInfo);
 
         // TODO(b/144373276): handle errors passed to the socket
-        switch(typeId) {
+        switch (typeId) {
             case BOOL_TYPE:
                 parseBool(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
                 break;
@@ -409,11 +417,11 @@
 }
 
 uint8_t LogEvent::getTypeId(uint8_t typeInfo) {
-    return typeInfo & 0x0F; // type id in lower 4 bytes
+    return typeInfo & 0x0F;  // type id in lower 4 bytes
 }
 
 uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) {
-    return (typeInfo >> 4) & 0x0F; // num annotations in upper 4 bytes
+    return (typeInfo >> 4) & 0x0F;  // num annotations in upper 4 bytes
 }
 
 int64_t LogEvent::GetLong(size_t key, status_t* err) const {
@@ -524,17 +532,17 @@
 std::vector<uint8_t> LogEvent::GetStorage(size_t key, status_t* err) const {
     int field = getSimpleField(key);
     for (const auto& value : mValues) {
-      if (value.mField.getField() == field) {
-        if (value.mValue.getType() == STORAGE) {
-          return value.mValue.storage_value;
-        } else {
-          *err = BAD_TYPE;
-          return vector<uint8_t>();
+        if (value.mField.getField() == field) {
+            if (value.mValue.getType() == STORAGE) {
+                return value.mValue.storage_value;
+            } else {
+                *err = BAD_TYPE;
+                return vector<uint8_t>();
+            }
         }
-      }
-      if ((size_t)value.mField.getPosAtDepth(0) > key) {
-        break;
-      }
+        if ((size_t)value.mField.getPosAtDepth(0) > key) {
+            break;
+        }
     }
 
     *err = BAD_INDEX;
diff --git a/cmds/statsd/src/logd/LogEvent.h b/cmds/statsd/src/logd/LogEvent.h
index 6c6aab1..850e2b0 100644
--- a/cmds/statsd/src/logd/LogEvent.h
+++ b/cmds/statsd/src/logd/LogEvent.h
@@ -208,8 +208,10 @@
     void parseAnnotations(uint8_t numAnnotations, int firstUidInChainIndex = -1);
     void parseIsUidAnnotation(uint8_t annotationType);
     void parseTruncateTimestampAnnotation(uint8_t annotationType);
-    void parseStateOptionAnnotation(uint8_t annotationType, int firstUidInChainIndex);
-    void parseResetStateAnnotation(uint8_t annotationType);
+    void parsePrimaryFieldAnnotation(uint8_t annotationType);
+    void parsePrimaryFieldFirstUidAnnotation(uint8_t annotationType, int firstUidInChainIndex);
+    void parseExclusiveStateAnnotation(uint8_t annotationType);
+    void parseTriggerStateResetAnnotation(uint8_t annotationType);
     void parseStateNestedAnnotation(uint8_t annotationType);
 
     /**