[go: nahoru, domu]

blob: 7964629e5dbaab8cc787282d04f35847e65ba643 [file] [log] [blame]
Sean Kelley125448c2022-04-26 17:26:10 -07001/*
2 * Copyright (C) 2022 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
17package androidx.health.services.client.data
18
Batuhan Ertasdde12cc2023-11-22 18:08:14 +000019import androidx.annotation.RestrictTo
20import androidx.annotation.RestrictTo.Scope
Sean Kelley125448c2022-04-26 17:26:10 -070021import androidx.health.services.client.proto.DataProto
Sean Kelleyb9f566f2022-04-25 10:24:17 -070022import androidx.health.services.client.proto.DataProto.HealthEvent.MetricsEntry
Sean Kelley125448c2022-04-26 17:26:10 -070023import java.time.Instant
24
25/** Represents a user's health event. */
26public class HealthEvent(
27 /** Gets the type of event. */
28 public val type: Type,
29
30 /** Returns the time of the health event. */
31 public val eventTime: Instant,
32
33 /** Gets metrics associated to the event. */
Sean Kelleyb9f566f2022-04-25 10:24:17 -070034 public val metrics: DataPointContainer,
Sean Kelley125448c2022-04-26 17:26:10 -070035) {
36
37 /** Health event types. */
Batuhan Ertasdde12cc2023-11-22 18:08:14 +000038 public class Type @RestrictTo(Scope.LIBRARY) constructor(
39 /** Returns a unique identifier for the [Type], as an `int`. */
40 public val id: Int,
41
42 /** Returns a human readable name to represent this [Type]. */
43 public val name: String
44 ) {
Sean Kelley125448c2022-04-26 17:26:10 -070045
46 override fun equals(other: Any?): Boolean {
47 if (this === other) return true
48 if (other !is Type) return false
49 if (id != other.id) return false
50
51 return true
52 }
53
54 override fun hashCode(): Int = id
55
56 override fun toString(): String = name
57
Batuhan Ertasbaea0e32023-11-30 22:00:55 +000058 internal fun toProto(): Int = id
Sean Kelley125448c2022-04-26 17:26:10 -070059
60 public companion object {
Batuhan Ertasbaea0e32023-11-30 22:00:55 +000061 private const val CUSTOM_TYPE_NAME_PREFIX = "health_services.device_private."
Sean Kelley125448c2022-04-26 17:26:10 -070062 /**
63 * The Health Event is unknown, or is represented by a value too new for this library
64 * version to parse.
65 */
66 @JvmField
67 public val UNKNOWN: Type = Type(0, "UNKNOWN")
68
69 /** Health Event signifying the device detected that the user fell. */
70 @JvmField
71 public val FALL_DETECTED: Type = Type(3, "FALL_DETECTED")
72
73 @JvmField
74 internal val VALUES: List<Type> = listOf(UNKNOWN, FALL_DETECTED)
75
76 internal fun fromProto(proto: DataProto.HealthEvent.HealthEventType): Type =
77 VALUES.firstOrNull { it.id == proto.number } ?: UNKNOWN
Batuhan Ertasbaea0e32023-11-30 22:00:55 +000078
79 internal fun fromProto(typeId: Int): Type {
80 if (isInCustomHealthEventRange(typeId)) {
81 return Type(typeId, CUSTOM_TYPE_NAME_PREFIX + typeId)
82 }
83
84 return VALUES.firstOrNull { it.id == typeId } ?: UNKNOWN
85 }
86
87 private fun isInCustomHealthEventRange(id: Int) = id in 0x40000..0x4ffff
Sean Kelley125448c2022-04-26 17:26:10 -070088 }
89 }
90
91 internal constructor(
92 proto: DataProto.HealthEvent
93 ) : this(
Batuhan Ertasbaea0e32023-11-30 22:00:55 +000094 Type.fromProto(proto.healthEventTypeId),
Sean Kelley125448c2022-04-26 17:26:10 -070095 Instant.ofEpochMilli(proto.eventTimeEpochMs),
Sean Kelleyb9f566f2022-04-25 10:24:17 -070096 fromHealthEventProto(proto)
Sean Kelley125448c2022-04-26 17:26:10 -070097 )
98
Hayley Ferr06230da2022-08-08 20:35:16 +000099 internal val proto: DataProto.HealthEvent =
Sean Kelley125448c2022-04-26 17:26:10 -0700100 DataProto.HealthEvent.newBuilder()
Batuhan Ertasbaea0e32023-11-30 22:00:55 +0000101 .setHealthEventTypeId(type.toProto())
Sean Kelley125448c2022-04-26 17:26:10 -0700102 .setEventTimeEpochMs(eventTime.toEpochMilli())
Sean Kelleyb9f566f2022-04-25 10:24:17 -0700103 .addAllMetrics(toEventProtoList(metrics))
Sean Kelley125448c2022-04-26 17:26:10 -0700104 .build()
Sean Kelleyb9f566f2022-04-25 10:24:17 -0700105
106 override fun equals(other: Any?): Boolean {
107 if (this === other) return true
108 if (other !is HealthEvent) return false
109 if (type != other.type) return false
110 if (eventTime != other.eventTime) return false
111 if (metrics != other.metrics) return false
112
113 return true
114 }
115
116 override fun hashCode(): Int {
117 var result = type.hashCode()
118 result = 31 * result + eventTime.hashCode()
119 result = 31 * result + metrics.hashCode()
120 return result
121 }
122
123 internal companion object {
124 internal fun toEventProtoList(container: DataPointContainer): List<MetricsEntry> {
125 val list = mutableListOf<MetricsEntry>()
126
127 for (entry in container.dataPoints) {
128 if (entry.value.isEmpty()) {
129 continue
130 }
131
132 when (entry.key.timeType) {
133 DataType.TimeType.SAMPLE -> {
134 list.add(
135 MetricsEntry.newBuilder()
136 .setDataType(entry.key.proto)
137 .addAllDataPoints(entry.value.map { (it as SampleDataPoint).proto })
138 .build()
139 )
140 }
141 DataType.TimeType.INTERVAL -> {
142 list.add(
143 MetricsEntry.newBuilder()
144 .setDataType(entry.key.proto)
145 .addAllDataPoints(entry.value.map {
146 (it as IntervalDataPoint).proto
147 })
148 .build()
149 )
150 }
151 }
152 }
153 return list.sortedBy { it.dataType.name } // Required to ensure equals() works
154 }
155
156 internal fun fromHealthEventProto(
157 proto: DataProto.HealthEvent
158 ): DataPointContainer {
159 val dataTypeToDataPoints: Map<DataType<*, *>, List<DataPoint<*>>> =
160 proto.metricsList.associate { entry ->
161 DataType.deltaFromProto(entry.dataType) to entry.dataPointsList.map {
162 DataPoint.fromProto(it)
163 }
164 }
165 return DataPointContainer(dataTypeToDataPoints)
166 }
167 }
Aurimas Liutikas4d534002023-07-06 15:51:33 -0700168}