Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.health.services.client.data |
| 18 | |
Batuhan Ertas | dde12cc | 2023-11-22 18:08:14 +0000 | [diff] [blame] | 19 | import androidx.annotation.RestrictTo |
| 20 | import androidx.annotation.RestrictTo.Scope |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 21 | import androidx.health.services.client.proto.DataProto |
Sean Kelley | b9f566f | 2022-04-25 10:24:17 -0700 | [diff] [blame] | 22 | import androidx.health.services.client.proto.DataProto.HealthEvent.MetricsEntry |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 23 | import java.time.Instant |
| 24 | |
| 25 | /** Represents a user's health event. */ |
| 26 | public 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 Kelley | b9f566f | 2022-04-25 10:24:17 -0700 | [diff] [blame] | 34 | public val metrics: DataPointContainer, |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 35 | ) { |
| 36 | |
| 37 | /** Health event types. */ |
Batuhan Ertas | dde12cc | 2023-11-22 18:08:14 +0000 | [diff] [blame] | 38 | 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 Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 45 | |
| 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 Ertas | baea0e3 | 2023-11-30 22:00:55 +0000 | [diff] [blame^] | 58 | internal fun toProto(): Int = id |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 59 | |
| 60 | public companion object { |
Batuhan Ertas | baea0e3 | 2023-11-30 22:00:55 +0000 | [diff] [blame^] | 61 | private const val CUSTOM_TYPE_NAME_PREFIX = "health_services.device_private." |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 62 | /** |
| 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 Ertas | baea0e3 | 2023-11-30 22:00:55 +0000 | [diff] [blame^] | 78 | |
| 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 Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | internal constructor( |
| 92 | proto: DataProto.HealthEvent |
| 93 | ) : this( |
Batuhan Ertas | baea0e3 | 2023-11-30 22:00:55 +0000 | [diff] [blame^] | 94 | Type.fromProto(proto.healthEventTypeId), |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 95 | Instant.ofEpochMilli(proto.eventTimeEpochMs), |
Sean Kelley | b9f566f | 2022-04-25 10:24:17 -0700 | [diff] [blame] | 96 | fromHealthEventProto(proto) |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 97 | ) |
| 98 | |
Hayley Ferr | 06230da | 2022-08-08 20:35:16 +0000 | [diff] [blame] | 99 | internal val proto: DataProto.HealthEvent = |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 100 | DataProto.HealthEvent.newBuilder() |
Batuhan Ertas | baea0e3 | 2023-11-30 22:00:55 +0000 | [diff] [blame^] | 101 | .setHealthEventTypeId(type.toProto()) |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 102 | .setEventTimeEpochMs(eventTime.toEpochMilli()) |
Sean Kelley | b9f566f | 2022-04-25 10:24:17 -0700 | [diff] [blame] | 103 | .addAllMetrics(toEventProtoList(metrics)) |
Sean Kelley | 125448c | 2022-04-26 17:26:10 -0700 | [diff] [blame] | 104 | .build() |
Sean Kelley | b9f566f | 2022-04-25 10:24:17 -0700 | [diff] [blame] | 105 | |
| 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 Liutikas | 4d53400 | 2023-07-06 15:51:33 -0700 | [diff] [blame] | 168 | } |