[go: nahoru, domu]

blob: 5650cffce5a75ea02b08026414b8a8ed1634fde5 [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
82 internal val proto: DataProto.HealthEvent by lazy {
83 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()
88 }
Sean Kelleyb9f566f2022-04-25 10:24:17 -070089
90 override fun equals(other: Any?): Boolean {
91 if (this === other) return true
92 if (other !is HealthEvent) return false
93 if (type != other.type) return false
94 if (eventTime != other.eventTime) return false
95 if (metrics != other.metrics) return false
96
97 return true
98 }
99
100 override fun hashCode(): Int {
101 var result = type.hashCode()
102 result = 31 * result + eventTime.hashCode()
103 result = 31 * result + metrics.hashCode()
104 return result
105 }
106
107 internal companion object {
108 internal fun toEventProtoList(container: DataPointContainer): List<MetricsEntry> {
109 val list = mutableListOf<MetricsEntry>()
110
111 for (entry in container.dataPoints) {
112 if (entry.value.isEmpty()) {
113 continue
114 }
115
116 when (entry.key.timeType) {
117 DataType.TimeType.SAMPLE -> {
118 list.add(
119 MetricsEntry.newBuilder()
120 .setDataType(entry.key.proto)
121 .addAllDataPoints(entry.value.map { (it as SampleDataPoint).proto })
122 .build()
123 )
124 }
125 DataType.TimeType.INTERVAL -> {
126 list.add(
127 MetricsEntry.newBuilder()
128 .setDataType(entry.key.proto)
129 .addAllDataPoints(entry.value.map {
130 (it as IntervalDataPoint).proto
131 })
132 .build()
133 )
134 }
135 }
136 }
137 return list.sortedBy { it.dataType.name } // Required to ensure equals() works
138 }
139
140 internal fun fromHealthEventProto(
141 proto: DataProto.HealthEvent
142 ): DataPointContainer {
143 val dataTypeToDataPoints: Map<DataType<*, *>, List<DataPoint<*>>> =
144 proto.metricsList.associate { entry ->
145 DataType.deltaFromProto(entry.dataType) to entry.dataPointsList.map {
146 DataPoint.fromProto(it)
147 }
148 }
149 return DataPointContainer(dataTypeToDataPoints)
150 }
151 }
Sean Kelley125448c2022-04-26 17:26:10 -0700152}