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 | |
| 19 | import androidx.health.services.client.proto.DataProto |
| 20 | import java.time.Instant |
| 21 | |
| 22 | /** Represents a user's health event. */ |
| 23 | public class HealthEvent( |
| 24 | /** Gets the type of event. */ |
| 25 | public val type: Type, |
| 26 | |
| 27 | /** Returns the time of the health event. */ |
| 28 | public val eventTime: Instant, |
| 29 | |
| 30 | /** Gets metrics associated to the event. */ |
| 31 | public val metrics: Map<DataType, List<DataPoint>>, |
| 32 | ) { |
| 33 | |
| 34 | /** Health event types. */ |
| 35 | public class Type private constructor(public val id: Int, public val name: String) { |
| 36 | |
| 37 | override fun equals(other: Any?): Boolean { |
| 38 | if (this === other) return true |
| 39 | if (other !is Type) return false |
| 40 | if (id != other.id) return false |
| 41 | |
| 42 | return true |
| 43 | } |
| 44 | |
| 45 | override fun hashCode(): Int = id |
| 46 | |
| 47 | override fun toString(): String = name |
| 48 | |
| 49 | internal fun toProto(): DataProto.HealthEvent.HealthEventType = |
| 50 | DataProto.HealthEvent.HealthEventType.forNumber(id) |
| 51 | ?: DataProto.HealthEvent.HealthEventType.HEALTH_EVENT_TYPE_UNKNOWN |
| 52 | |
| 53 | public companion object { |
| 54 | /** |
| 55 | * The Health Event is unknown, or is represented by a value too new for this library |
| 56 | * version to parse. |
| 57 | */ |
| 58 | @JvmField |
| 59 | public val UNKNOWN: Type = Type(0, "UNKNOWN") |
| 60 | |
| 61 | /** Health Event signifying the device detected that the user fell. */ |
| 62 | @JvmField |
| 63 | public val FALL_DETECTED: Type = Type(3, "FALL_DETECTED") |
| 64 | |
| 65 | @JvmField |
| 66 | internal val VALUES: List<Type> = listOf(UNKNOWN, FALL_DETECTED) |
| 67 | |
| 68 | internal fun fromProto(proto: DataProto.HealthEvent.HealthEventType): Type = |
| 69 | VALUES.firstOrNull { it.id == proto.number } ?: UNKNOWN |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | internal constructor( |
| 74 | proto: DataProto.HealthEvent |
| 75 | ) : this( |
| 76 | Type.fromProto(proto.type), |
| 77 | Instant.ofEpochMilli(proto.eventTimeEpochMs), |
| 78 | proto |
| 79 | .metricsList |
| 80 | .map { entry -> DataType(entry.dataType) to entry.dataPointsList.map { DataPoint(it) } } |
| 81 | .toMap() |
| 82 | ) |
| 83 | |
| 84 | internal val proto: DataProto.HealthEvent by lazy { |
| 85 | DataProto.HealthEvent.newBuilder() |
| 86 | .setType(type.toProto()) |
| 87 | .setEventTimeEpochMs(eventTime.toEpochMilli()) |
| 88 | .addAllMetrics( |
| 89 | metrics |
| 90 | .map { entry -> |
| 91 | DataProto.HealthEvent.MetricsEntry.newBuilder() |
| 92 | .setDataType(entry.key.proto) |
| 93 | .addAllDataPoints(entry.value.map { it.proto }) |
| 94 | .build() |
| 95 | } |
| 96 | .sortedBy { it.dataType.name } // Required to ensure equals() works |
| 97 | ) |
| 98 | .build() |
| 99 | } |
| 100 | } |