[go: nahoru, domu]

Update HealthEvent proto parsing

Test: ./gradlew :health:health-services-client:test
Bug: 313766681
Change-Id: I095d41f829b7ddd3508a8cd3821b2bda5f6759e3
diff --git a/health/health-services-client/src/main/java/androidx/health/services/client/data/HealthEvent.kt b/health/health-services-client/src/main/java/androidx/health/services/client/data/HealthEvent.kt
index 5c2b868..7964629 100644
--- a/health/health-services-client/src/main/java/androidx/health/services/client/data/HealthEvent.kt
+++ b/health/health-services-client/src/main/java/androidx/health/services/client/data/HealthEvent.kt
@@ -55,11 +55,10 @@
 
         override fun toString(): String = name
 
-        internal fun toProto(): DataProto.HealthEvent.HealthEventType =
-            DataProto.HealthEvent.HealthEventType.forNumber(id)
-                ?: DataProto.HealthEvent.HealthEventType.HEALTH_EVENT_TYPE_UNKNOWN
+        internal fun toProto(): Int = id
 
         public companion object {
+            private const val CUSTOM_TYPE_NAME_PREFIX = "health_services.device_private."
             /**
              * The Health Event is unknown, or is represented by a value too new for this library
              * version to parse.
@@ -76,20 +75,30 @@
 
             internal fun fromProto(proto: DataProto.HealthEvent.HealthEventType): Type =
                 VALUES.firstOrNull { it.id == proto.number } ?: UNKNOWN
+
+            internal fun fromProto(typeId: Int): Type {
+                if (isInCustomHealthEventRange(typeId)) {
+                    return Type(typeId, CUSTOM_TYPE_NAME_PREFIX + typeId)
+                }
+
+                return VALUES.firstOrNull { it.id == typeId } ?: UNKNOWN
+            }
+
+            private fun isInCustomHealthEventRange(id: Int) = id in 0x40000..0x4ffff
         }
     }
 
     internal constructor(
         proto: DataProto.HealthEvent
     ) : this(
-        Type.fromProto(proto.type),
+        Type.fromProto(proto.healthEventTypeId),
         Instant.ofEpochMilli(proto.eventTimeEpochMs),
         fromHealthEventProto(proto)
     )
 
     internal val proto: DataProto.HealthEvent =
         DataProto.HealthEvent.newBuilder()
-            .setType(type.toProto())
+            .setHealthEventTypeId(type.toProto())
             .setEventTimeEpochMs(eventTime.toEpochMilli())
             .addAllMetrics(toEventProtoList(metrics))
             .build()
diff --git a/health/health-services-client/src/main/proto/data.proto b/health/health-services-client/src/main/proto/data.proto
index 2a2e5ee..a25ee7c5 100644
--- a/health/health-services-client/src/main/proto/data.proto
+++ b/health/health-services-client/src/main/proto/data.proto
@@ -499,7 +499,7 @@
     reserved 3 to max;  // Next ID
   }
 
-  optional HealthEventType type = 1;
+  optional int32 health_event_type_id = 1;
   optional int64 event_time_epoch_ms = 2;
   repeated MetricsEntry metrics = 3;
   reserved 4 to max;  // Next ID
@@ -561,7 +561,7 @@
   repeated DataType supported_data_types_passive_monitoring = 1;
   repeated DataType supported_data_types_passive_goals = 2;
   repeated int32 supported_hr_sampling_intervals_seconds = 3 [packed = true];
-  repeated HealthEvent.HealthEventType supported_health_event_types = 4
+  repeated int32 supported_health_event_types = 4
       [packed = true];
   repeated UserActivityState supported_user_activity_states = 5 [packed = true];
   reserved 6 to max;  // Next ID
@@ -571,7 +571,7 @@
   repeated DataType data_types = 1;
   optional bool include_user_activity_state = 2;
   repeated PassiveGoal passive_goals = 3;
-  repeated HealthEvent.HealthEventType health_event_types = 4;
+  repeated int32 health_event_types = 4;
   reserved 5 to max;  // Next ID
 }
 
diff --git a/health/health-services-client/src/main/proto/requests.proto b/health/health-services-client/src/main/proto/requests.proto
index 0a6d865..b548312 100644
--- a/health/health-services-client/src/main/proto/requests.proto
+++ b/health/health-services-client/src/main/proto/requests.proto
@@ -63,7 +63,7 @@
 message HealthEventsRegistrationRequest {
   optional string package_name = 1;
   optional string receiver_class_name = 2;
-  repeated HealthEvent.HealthEventType event_types = 3;
+  repeated int32 event_types = 3;
   reserved 4 to max;  // Next ID
 }
 
diff --git a/health/health-services-client/src/test/java/androidx/health/services/client/impl/ServiceBackedPassiveMonitoringClientTest.kt b/health/health-services-client/src/test/java/androidx/health/services/client/impl/ServiceBackedPassiveMonitoringClientTest.kt
index cd408a3..c56d440e 100644
--- a/health/health-services-client/src/test/java/androidx/health/services/client/impl/ServiceBackedPassiveMonitoringClientTest.kt
+++ b/health/health-services-client/src/test/java/androidx/health/services/client/impl/ServiceBackedPassiveMonitoringClientTest.kt
@@ -48,7 +48,6 @@
 import androidx.health.services.client.impl.response.PassiveMonitoringUpdateResponse
 import androidx.health.services.client.proto.DataProto
 import androidx.health.services.client.proto.DataProto.ComparisonType.COMPARISON_TYPE_GREATER_THAN
-import androidx.health.services.client.proto.DataProto.HealthEvent.HealthEventType.HEALTH_EVENT_TYPE_FALL_DETECTED
 import androidx.health.services.client.proto.DataProto.PassiveGoal.TriggerFrequency.TRIGGER_FREQUENCY_ONCE
 import androidx.health.services.client.proto.DataProto.UserActivityState.USER_ACTIVITY_STATE_PASSIVE
 import androidx.health.services.client.proto.ResponsesProto
@@ -290,7 +289,7 @@
             HealthEventResponse(
                 ResponsesProto.HealthEventResponse.newBuilder().setHealthEvent(
                     DataProto.HealthEvent.newBuilder()
-                        .setType(HEALTH_EVENT_TYPE_FALL_DETECTED)
+                        .setHealthEventTypeId(FALL_DETECTED.id)
                         .build()
                 ).build()
             )