[go: nahoru, domu]

blob: 5c2b86855452501358db03b02b5c7f1251010abd [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
58 internal fun toProto(): DataProto.HealthEvent.HealthEventType =
59 DataProto.HealthEvent.HealthEventType.forNumber(id)
60 ?: DataProto.HealthEvent.HealthEventType.HEALTH_EVENT_TYPE_UNKNOWN
61
62 public companion object {
63 /**
64 * The Health Event is unknown, or is represented by a value too new for this library
65 * version to parse.
66 */
67 @JvmField
68 public val UNKNOWN: Type = Type(0, "UNKNOWN")
69
70 /** Health Event signifying the device detected that the user fell. */
71 @JvmField
72 public val FALL_DETECTED: Type = Type(3, "FALL_DETECTED")
73
74 @JvmField
75 internal val VALUES: List<Type> = listOf(UNKNOWN, FALL_DETECTED)
76
77 internal fun fromProto(proto: DataProto.HealthEvent.HealthEventType): Type =
78 VALUES.firstOrNull { it.id == proto.number } ?: UNKNOWN
79 }
80 }
81
82 internal constructor(
83 proto: DataProto.HealthEvent
84 ) : this(
85 Type.fromProto(proto.type),
86 Instant.ofEpochMilli(proto.eventTimeEpochMs),
Sean Kelleyb9f566f2022-04-25 10:24:17 -070087 fromHealthEventProto(proto)
Sean Kelley125448c2022-04-26 17:26:10 -070088 )
89
Hayley Ferr06230da2022-08-08 20:35:16 +000090 internal val proto: DataProto.HealthEvent =
Sean Kelley125448c2022-04-26 17:26:10 -070091 DataProto.HealthEvent.newBuilder()
92 .setType(type.toProto())
93 .setEventTimeEpochMs(eventTime.toEpochMilli())
Sean Kelleyb9f566f2022-04-25 10:24:17 -070094 .addAllMetrics(toEventProtoList(metrics))
Sean Kelley125448c2022-04-26 17:26:10 -070095 .build()
Sean Kelleyb9f566f2022-04-25 10:24:17 -070096
97 override fun equals(other: Any?): Boolean {
98 if (this === other) return true
99 if (other !is HealthEvent) return false
100 if (type != other.type) return false
101 if (eventTime != other.eventTime) return false
102 if (metrics != other.metrics) return false
103
104 return true
105 }
106
107 override fun hashCode(): Int {
108 var result = type.hashCode()
109 result = 31 * result + eventTime.hashCode()
110 result = 31 * result + metrics.hashCode()
111 return result
112 }
113
114 internal companion object {
115 internal fun toEventProtoList(container: DataPointContainer): List<MetricsEntry> {
116 val list = mutableListOf<MetricsEntry>()
117
118 for (entry in container.dataPoints) {
119 if (entry.value.isEmpty()) {
120 continue
121 }
122
123 when (entry.key.timeType) {
124 DataType.TimeType.SAMPLE -> {
125 list.add(
126 MetricsEntry.newBuilder()
127 .setDataType(entry.key.proto)
128 .addAllDataPoints(entry.value.map { (it as SampleDataPoint).proto })
129 .build()
130 )
131 }
132 DataType.TimeType.INTERVAL -> {
133 list.add(
134 MetricsEntry.newBuilder()
135 .setDataType(entry.key.proto)
136 .addAllDataPoints(entry.value.map {
137 (it as IntervalDataPoint).proto
138 })
139 .build()
140 )
141 }
142 }
143 }
144 return list.sortedBy { it.dataType.name } // Required to ensure equals() works
145 }
146
147 internal fun fromHealthEventProto(
148 proto: DataProto.HealthEvent
149 ): DataPointContainer {
150 val dataTypeToDataPoints: Map<DataType<*, *>, List<DataPoint<*>>> =
151 proto.metricsList.associate { entry ->
152 DataType.deltaFromProto(entry.dataType) to entry.dataPointsList.map {
153 DataPoint.fromProto(it)
154 }
155 }
156 return DataPointContainer(dataTypeToDataPoints)
157 }
158 }
Aurimas Liutikas4d534002023-07-06 15:51:33 -0700159}