[go: nahoru, domu]

blob: 19bdd36fca49c160d9e35d09b4a1c44289c986cb [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
19import androidx.health.services.client.proto.DataProto
Sean Kelleyb9f566f2022-04-25 10:24:17 -070020import androidx.health.services.client.proto.DataProto.HealthEvent.MetricsEntry
Sean Kelley125448c2022-04-26 17:26:10 -070021import java.time.Instant
22
23/** Represents a user's health event. */
24public class HealthEvent(
25 /** Gets the type of event. */
26 public val type: Type,
27
28 /** Returns the time of the health event. */
29 public val eventTime: Instant,
30
31 /** Gets metrics associated to the event. */
Sean Kelleyb9f566f2022-04-25 10:24:17 -070032 public val metrics: DataPointContainer,
Sean Kelley125448c2022-04-26 17:26:10 -070033) {
34
35 /** Health event types. */
36 public class Type private constructor(public val id: Int, public val name: String) {
37
38 override fun equals(other: Any?): Boolean {
39 if (this === other) return true
40 if (other !is Type) return false
41 if (id != other.id) return false
42
43 return true
44 }
45
46 override fun hashCode(): Int = id
47
48 override fun toString(): String = name
49
50 internal fun toProto(): DataProto.HealthEvent.HealthEventType =
51 DataProto.HealthEvent.HealthEventType.forNumber(id)
52 ?: DataProto.HealthEvent.HealthEventType.HEALTH_EVENT_TYPE_UNKNOWN
53
54 public companion object {
55 /**
56 * The Health Event is unknown, or is represented by a value too new for this library
57 * version to parse.
58 */
59 @JvmField
60 public val UNKNOWN: Type = Type(0, "UNKNOWN")
61
62 /** Health Event signifying the device detected that the user fell. */
63 @JvmField
64 public val FALL_DETECTED: Type = Type(3, "FALL_DETECTED")
65
66 @JvmField
67 internal val VALUES: List<Type> = listOf(UNKNOWN, FALL_DETECTED)
68
69 internal fun fromProto(proto: DataProto.HealthEvent.HealthEventType): Type =
70 VALUES.firstOrNull { it.id == proto.number } ?: UNKNOWN
71 }
72 }
73
74 internal constructor(
75 proto: DataProto.HealthEvent
76 ) : this(
77 Type.fromProto(proto.type),
78 Instant.ofEpochMilli(proto.eventTimeEpochMs),
Sean Kelleyb9f566f2022-04-25 10:24:17 -070079 fromHealthEventProto(proto)
Sean Kelley125448c2022-04-26 17:26:10 -070080 )
81
Hayley Ferr06230da2022-08-08 20:35:16 +000082 internal val proto: DataProto.HealthEvent =
Sean Kelley125448c2022-04-26 17:26:10 -070083 DataProto.HealthEvent.newBuilder()
84 .setType(type.toProto())
85 .setEventTimeEpochMs(eventTime.toEpochMilli())
Sean Kelleyb9f566f2022-04-25 10:24:17 -070086 .addAllMetrics(toEventProtoList(metrics))
Sean Kelley125448c2022-04-26 17:26:10 -070087 .build()
Sean Kelleyb9f566f2022-04-25 10:24:17 -070088
89 override fun equals(other: Any?): Boolean {
90 if (this === other) return true
91 if (other !is HealthEvent) return false
92 if (type != other.type) return false
93 if (eventTime != other.eventTime) return false
94 if (metrics != other.metrics) return false
95
96 return true
97 }
98
99 override fun hashCode(): Int {
100 var result = type.hashCode()
101 result = 31 * result + eventTime.hashCode()
102 result = 31 * result + metrics.hashCode()
103 return result
104 }
105
106 internal companion object {
107 internal fun toEventProtoList(container: DataPointContainer): List<MetricsEntry> {
108 val list = mutableListOf<MetricsEntry>()
109
110 for (entry in container.dataPoints) {
111 if (entry.value.isEmpty()) {
112 continue
113 }
114
115 when (entry.key.timeType) {
116 DataType.TimeType.SAMPLE -> {
117 list.add(
118 MetricsEntry.newBuilder()
119 .setDataType(entry.key.proto)
120 .addAllDataPoints(entry.value.map { (it as SampleDataPoint).proto })
121 .build()
122 )
123 }
124 DataType.TimeType.INTERVAL -> {
125 list.add(
126 MetricsEntry.newBuilder()
127 .setDataType(entry.key.proto)
128 .addAllDataPoints(entry.value.map {
129 (it as IntervalDataPoint).proto
130 })
131 .build()
132 )
133 }
134 }
135 }
136 return list.sortedBy { it.dataType.name } // Required to ensure equals() works
137 }
138
139 internal fun fromHealthEventProto(
140 proto: DataProto.HealthEvent
141 ): DataPointContainer {
142 val dataTypeToDataPoints: Map<DataType<*, *>, List<DataPoint<*>>> =
143 proto.metricsList.associate { entry ->
144 DataType.deltaFromProto(entry.dataType) to entry.dataPointsList.map {
145 DataPoint.fromProto(it)
146 }
147 }
148 return DataPointContainer(dataTypeToDataPoints)
149 }
150 }
Sean Kelley125448c2022-04-26 17:26:10 -0700151}