@solutionforest/android-health-connect 0.0.0-stage → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AndroidHealthConnect.podspec +20 -0
- package/CHANGELOG.md +211 -0
- package/LICENSE +20 -0
- package/README.md +849 -2
- package/SECURITY.md +99 -0
- package/android/build.gradle +104 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/androidhealthconnect/AggregateMapper.kt +521 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectModule.kt +637 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectPackage.kt +31 -0
- package/android/src/main/java/com/androidhealthconnect/AvailabilitySupport.kt +149 -0
- package/android/src/main/java/com/androidhealthconnect/ChangesMapper.kt +105 -0
- package/android/src/main/java/com/androidhealthconnect/ExtendedRecordMapper.kt +665 -0
- package/android/src/main/java/com/androidhealthconnect/FeatureSupport.kt +88 -0
- package/android/src/main/java/com/androidhealthconnect/PhrMapper.kt +187 -0
- package/android/src/main/java/com/androidhealthconnect/PlannedExerciseMapper.kt +242 -0
- package/android/src/main/java/com/androidhealthconnect/RecordMapper.kt +1222 -0
- package/app.plugin.js +1 -0
- package/docs/assets/logo.jpg +0 -0
- package/ios/AndroidHealthConnect.h +5 -0
- package/ios/AndroidHealthConnect.mm +21 -0
- package/lib/module/NativeAndroidHealthConnect.js +5 -0
- package/lib/module/NativeAndroidHealthConnect.js.map +1 -0
- package/lib/module/foreground.js +117 -0
- package/lib/module/foreground.js.map +1 -0
- package/lib/module/helpers.js +280 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +354 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +392 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/upgrade.js +101 -0
- package/lib/module/upgrade.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/plugin/src/index.d.ts +14 -0
- package/lib/typescript/plugin/src/index.d.ts.map +1 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts +95 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts.map +1 -0
- package/lib/typescript/src/foreground.d.ts +43 -0
- package/lib/typescript/src/foreground.d.ts.map +1 -0
- package/lib/typescript/src/helpers.d.ts +108 -0
- package/lib/typescript/src/helpers.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +208 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +1209 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/upgrade.d.ts +26 -0
- package/lib/typescript/src/upgrade.d.ts.map +1 -0
- package/package.json +255 -4
- package/plugin/build/index.d.ts +13 -0
- package/plugin/build/index.js +57 -0
- package/plugin/src/index.ts +105 -0
- package/src/NativeAndroidHealthConnect.ts +133 -0
- package/src/foreground.ts +167 -0
- package/src/helpers.ts +373 -0
- package/src/index.tsx +623 -0
- package/src/types.ts +1525 -0
- package/src/upgrade.ts +155 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import androidx.health.connect.client.HealthConnectClient
|
|
4
|
+
import androidx.health.connect.client.aggregate.AggregateMetric
|
|
5
|
+
import androidx.health.connect.client.aggregate.AggregationResult
|
|
6
|
+
import androidx.health.connect.client.records.ActiveCaloriesBurnedRecord
|
|
7
|
+
import androidx.health.connect.client.records.ActivityIntensityRecord
|
|
8
|
+
import androidx.health.connect.client.records.BloodPressureRecord
|
|
9
|
+
import androidx.health.connect.client.records.BasalMetabolicRateRecord
|
|
10
|
+
import androidx.health.connect.client.records.CyclingPedalingCadenceRecord
|
|
11
|
+
import androidx.health.connect.client.records.DistanceRecord
|
|
12
|
+
import androidx.health.connect.client.records.ElevationGainedRecord
|
|
13
|
+
import androidx.health.connect.client.records.ExerciseSessionRecord
|
|
14
|
+
import androidx.health.connect.client.records.FloorsClimbedRecord
|
|
15
|
+
import androidx.health.connect.client.records.HeartRateRecord
|
|
16
|
+
import androidx.health.connect.client.records.HeightRecord
|
|
17
|
+
import androidx.health.connect.client.records.HydrationRecord
|
|
18
|
+
import androidx.health.connect.client.records.MindfulnessSessionRecord
|
|
19
|
+
import androidx.health.connect.client.records.NutritionRecord
|
|
20
|
+
import androidx.health.connect.client.records.PowerRecord
|
|
21
|
+
import androidx.health.connect.client.records.RestingHeartRateRecord
|
|
22
|
+
import androidx.health.connect.client.records.SleepSessionRecord
|
|
23
|
+
import androidx.health.connect.client.records.StepsRecord
|
|
24
|
+
import androidx.health.connect.client.records.StepsCadenceRecord
|
|
25
|
+
import androidx.health.connect.client.records.TotalCaloriesBurnedRecord
|
|
26
|
+
import androidx.health.connect.client.records.WeightRecord
|
|
27
|
+
import androidx.health.connect.client.records.SpeedRecord
|
|
28
|
+
import androidx.health.connect.client.records.WheelchairPushesRecord
|
|
29
|
+
import androidx.health.connect.client.records.metadata.DataOrigin
|
|
30
|
+
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
|
|
31
|
+
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
|
|
32
|
+
import androidx.health.connect.client.request.AggregateRequest
|
|
33
|
+
import androidx.health.connect.client.time.TimeRangeFilter
|
|
34
|
+
import androidx.health.connect.client.units.Energy
|
|
35
|
+
import androidx.health.connect.client.units.Length
|
|
36
|
+
import androidx.health.connect.client.units.Mass
|
|
37
|
+
import androidx.health.connect.client.units.Pressure
|
|
38
|
+
import androidx.health.connect.client.units.Volume
|
|
39
|
+
import androidx.health.connect.client.units.Velocity
|
|
40
|
+
import androidx.health.connect.client.units.Power
|
|
41
|
+
import org.json.JSONArray
|
|
42
|
+
import org.json.JSONObject
|
|
43
|
+
import java.time.Duration
|
|
44
|
+
import java.time.Instant
|
|
45
|
+
import java.time.LocalDateTime
|
|
46
|
+
import java.time.Period
|
|
47
|
+
import java.time.ZoneId
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Health Connect aggregate helpers.
|
|
51
|
+
*
|
|
52
|
+
* Duration buckets always use [Instant] time ranges to avoid Android 14+ UTC
|
|
53
|
+
* wall-clock shift bugs (matinzd/react-native-health-connect#278 / #279).
|
|
54
|
+
* Period buckets use [LocalDateTime] per official Health Connect docs.
|
|
55
|
+
*/
|
|
56
|
+
internal object AggregateMapper {
|
|
57
|
+
|
|
58
|
+
private data class MetricSpec(
|
|
59
|
+
val name: String,
|
|
60
|
+
val metric: AggregateMetric<*>,
|
|
61
|
+
val read: (AggregationResult) -> Any?,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
suspend fun aggregate(client: HealthConnectClient, options: JSONObject): JSONObject {
|
|
65
|
+
val recordType = options.getString("recordType")
|
|
66
|
+
val specs = specsFor(recordType)
|
|
67
|
+
val result =
|
|
68
|
+
client.aggregate(
|
|
69
|
+
AggregateRequest(
|
|
70
|
+
metrics = specs.map { it.metric }.toSet(),
|
|
71
|
+
timeRangeFilter = parseInstantTimeRange(options),
|
|
72
|
+
dataOriginFilter = parseDataOriginFilter(options),
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
return resultToJson(recordType, result, specs)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
suspend fun aggregateGroupByDuration(
|
|
79
|
+
client: HealthConnectClient,
|
|
80
|
+
options: JSONObject,
|
|
81
|
+
): JSONArray {
|
|
82
|
+
val recordType = options.getString("recordType")
|
|
83
|
+
val specs = specsFor(recordType)
|
|
84
|
+
val rows =
|
|
85
|
+
client.aggregateGroupByDuration(
|
|
86
|
+
AggregateGroupByDurationRequest(
|
|
87
|
+
metrics = specs.map { it.metric }.toSet(),
|
|
88
|
+
timeRangeFilter = parseInstantTimeRange(options),
|
|
89
|
+
timeRangeSlicer = parseDurationSlicer(options),
|
|
90
|
+
dataOriginFilter = parseDataOriginFilter(options),
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
val out = JSONArray()
|
|
94
|
+
rows.forEach { row ->
|
|
95
|
+
out.put(
|
|
96
|
+
JSONObject().apply {
|
|
97
|
+
put("startTime", row.startTime.toString())
|
|
98
|
+
put("endTime", row.endTime.toString())
|
|
99
|
+
put("zoneOffset", row.zoneOffset?.toString() ?: JSONObject.NULL)
|
|
100
|
+
put("result", resultToJson(recordType, row.result, specs))
|
|
101
|
+
}
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
return out
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
suspend fun aggregateGroupByPeriod(
|
|
108
|
+
client: HealthConnectClient,
|
|
109
|
+
options: JSONObject,
|
|
110
|
+
): JSONArray {
|
|
111
|
+
val recordType = options.getString("recordType")
|
|
112
|
+
val specs = specsFor(recordType)
|
|
113
|
+
val rows =
|
|
114
|
+
client.aggregateGroupByPeriod(
|
|
115
|
+
AggregateGroupByPeriodRequest(
|
|
116
|
+
metrics = specs.map { it.metric }.toSet(),
|
|
117
|
+
timeRangeFilter = parseLocalDateTimeRange(options),
|
|
118
|
+
timeRangeSlicer = parsePeriodSlicer(options),
|
|
119
|
+
dataOriginFilter = parseDataOriginFilter(options),
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
val out = JSONArray()
|
|
123
|
+
rows.forEach { row ->
|
|
124
|
+
out.put(
|
|
125
|
+
JSONObject().apply {
|
|
126
|
+
put("startTime", row.startTime.toString())
|
|
127
|
+
put("endTime", row.endTime.toString())
|
|
128
|
+
put("result", resultToJson(recordType, row.result, specs))
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
return out
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fun supportedRecordTypes(): Set<String> =
|
|
136
|
+
setOf(
|
|
137
|
+
"Steps",
|
|
138
|
+
"Distance",
|
|
139
|
+
"ActiveCaloriesBurned",
|
|
140
|
+
"TotalCaloriesBurned",
|
|
141
|
+
"HeartRate",
|
|
142
|
+
"RestingHeartRate",
|
|
143
|
+
"Weight",
|
|
144
|
+
"Height",
|
|
145
|
+
"Hydration",
|
|
146
|
+
"FloorsClimbed",
|
|
147
|
+
"ElevationGained",
|
|
148
|
+
"ExerciseSession",
|
|
149
|
+
"SleepSession",
|
|
150
|
+
"BloodPressure",
|
|
151
|
+
"MindfulnessSession",
|
|
152
|
+
"ActivityIntensity",
|
|
153
|
+
"Nutrition",
|
|
154
|
+
"Speed",
|
|
155
|
+
"Power",
|
|
156
|
+
"StepsCadence",
|
|
157
|
+
"CyclingPedalingCadence",
|
|
158
|
+
"WheelchairPushes",
|
|
159
|
+
"BasalMetabolicRate",
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
private fun specsFor(recordType: String): List<MetricSpec> =
|
|
163
|
+
when (recordType) {
|
|
164
|
+
"Steps" ->
|
|
165
|
+
listOf(MetricSpec("COUNT_TOTAL", StepsRecord.COUNT_TOTAL) { it[StepsRecord.COUNT_TOTAL] })
|
|
166
|
+
"Distance" ->
|
|
167
|
+
listOf(
|
|
168
|
+
MetricSpec("DISTANCE_TOTAL", DistanceRecord.DISTANCE_TOTAL) {
|
|
169
|
+
it[DistanceRecord.DISTANCE_TOTAL]?.let(::lengthJson)
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
"ActiveCaloriesBurned" ->
|
|
173
|
+
listOf(
|
|
174
|
+
MetricSpec(
|
|
175
|
+
"ACTIVE_CALORIES_TOTAL",
|
|
176
|
+
ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL,
|
|
177
|
+
) { it[ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL]?.let(::energyJson) }
|
|
178
|
+
)
|
|
179
|
+
"TotalCaloriesBurned" ->
|
|
180
|
+
listOf(
|
|
181
|
+
MetricSpec("ENERGY_TOTAL", TotalCaloriesBurnedRecord.ENERGY_TOTAL) {
|
|
182
|
+
it[TotalCaloriesBurnedRecord.ENERGY_TOTAL]?.let(::energyJson)
|
|
183
|
+
}
|
|
184
|
+
)
|
|
185
|
+
"HeartRate" ->
|
|
186
|
+
listOf(
|
|
187
|
+
MetricSpec("BPM_AVG", HeartRateRecord.BPM_AVG) { it[HeartRateRecord.BPM_AVG] },
|
|
188
|
+
MetricSpec("BPM_MIN", HeartRateRecord.BPM_MIN) { it[HeartRateRecord.BPM_MIN] },
|
|
189
|
+
MetricSpec("BPM_MAX", HeartRateRecord.BPM_MAX) { it[HeartRateRecord.BPM_MAX] },
|
|
190
|
+
MetricSpec("MEASUREMENTS_COUNT", HeartRateRecord.MEASUREMENTS_COUNT) {
|
|
191
|
+
it[HeartRateRecord.MEASUREMENTS_COUNT]
|
|
192
|
+
},
|
|
193
|
+
)
|
|
194
|
+
"RestingHeartRate" ->
|
|
195
|
+
listOf(
|
|
196
|
+
MetricSpec("BPM_AVG", RestingHeartRateRecord.BPM_AVG) {
|
|
197
|
+
it[RestingHeartRateRecord.BPM_AVG]
|
|
198
|
+
},
|
|
199
|
+
MetricSpec("BPM_MIN", RestingHeartRateRecord.BPM_MIN) {
|
|
200
|
+
it[RestingHeartRateRecord.BPM_MIN]
|
|
201
|
+
},
|
|
202
|
+
MetricSpec("BPM_MAX", RestingHeartRateRecord.BPM_MAX) {
|
|
203
|
+
it[RestingHeartRateRecord.BPM_MAX]
|
|
204
|
+
},
|
|
205
|
+
)
|
|
206
|
+
"Weight" ->
|
|
207
|
+
listOf(
|
|
208
|
+
MetricSpec("WEIGHT_AVG", WeightRecord.WEIGHT_AVG) {
|
|
209
|
+
it[WeightRecord.WEIGHT_AVG]?.let(::massJson)
|
|
210
|
+
},
|
|
211
|
+
MetricSpec("WEIGHT_MIN", WeightRecord.WEIGHT_MIN) {
|
|
212
|
+
it[WeightRecord.WEIGHT_MIN]?.let(::massJson)
|
|
213
|
+
},
|
|
214
|
+
MetricSpec("WEIGHT_MAX", WeightRecord.WEIGHT_MAX) {
|
|
215
|
+
it[WeightRecord.WEIGHT_MAX]?.let(::massJson)
|
|
216
|
+
},
|
|
217
|
+
)
|
|
218
|
+
"Height" ->
|
|
219
|
+
listOf(
|
|
220
|
+
MetricSpec("HEIGHT_AVG", HeightRecord.HEIGHT_AVG) {
|
|
221
|
+
it[HeightRecord.HEIGHT_AVG]?.let(::lengthJson)
|
|
222
|
+
},
|
|
223
|
+
MetricSpec("HEIGHT_MIN", HeightRecord.HEIGHT_MIN) {
|
|
224
|
+
it[HeightRecord.HEIGHT_MIN]?.let(::lengthJson)
|
|
225
|
+
},
|
|
226
|
+
MetricSpec("HEIGHT_MAX", HeightRecord.HEIGHT_MAX) {
|
|
227
|
+
it[HeightRecord.HEIGHT_MAX]?.let(::lengthJson)
|
|
228
|
+
},
|
|
229
|
+
)
|
|
230
|
+
"Hydration" ->
|
|
231
|
+
listOf(
|
|
232
|
+
MetricSpec("VOLUME_TOTAL", HydrationRecord.VOLUME_TOTAL) {
|
|
233
|
+
it[HydrationRecord.VOLUME_TOTAL]?.let(::volumeJson)
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
"FloorsClimbed" ->
|
|
237
|
+
listOf(
|
|
238
|
+
MetricSpec("FLOORS_CLIMBED_TOTAL", FloorsClimbedRecord.FLOORS_CLIMBED_TOTAL) {
|
|
239
|
+
it[FloorsClimbedRecord.FLOORS_CLIMBED_TOTAL]
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
"ElevationGained" ->
|
|
243
|
+
listOf(
|
|
244
|
+
MetricSpec("ELEVATION_GAINED_TOTAL", ElevationGainedRecord.ELEVATION_GAINED_TOTAL) {
|
|
245
|
+
it[ElevationGainedRecord.ELEVATION_GAINED_TOTAL]?.let(::lengthJson)
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
"ExerciseSession" ->
|
|
249
|
+
listOf(
|
|
250
|
+
MetricSpec("EXERCISE_DURATION_TOTAL", ExerciseSessionRecord.EXERCISE_DURATION_TOTAL) {
|
|
251
|
+
it[ExerciseSessionRecord.EXERCISE_DURATION_TOTAL]?.let { d ->
|
|
252
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
)
|
|
256
|
+
"SleepSession" ->
|
|
257
|
+
listOf(
|
|
258
|
+
MetricSpec("SLEEP_DURATION_TOTAL", SleepSessionRecord.SLEEP_DURATION_TOTAL) {
|
|
259
|
+
it[SleepSessionRecord.SLEEP_DURATION_TOTAL]?.let { d ->
|
|
260
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
"BloodPressure" ->
|
|
265
|
+
listOf(
|
|
266
|
+
MetricSpec("SYSTOLIC_AVG", BloodPressureRecord.SYSTOLIC_AVG) {
|
|
267
|
+
it[BloodPressureRecord.SYSTOLIC_AVG]?.let(::pressureJson)
|
|
268
|
+
},
|
|
269
|
+
MetricSpec("SYSTOLIC_MIN", BloodPressureRecord.SYSTOLIC_MIN) {
|
|
270
|
+
it[BloodPressureRecord.SYSTOLIC_MIN]?.let(::pressureJson)
|
|
271
|
+
},
|
|
272
|
+
MetricSpec("SYSTOLIC_MAX", BloodPressureRecord.SYSTOLIC_MAX) {
|
|
273
|
+
it[BloodPressureRecord.SYSTOLIC_MAX]?.let(::pressureJson)
|
|
274
|
+
},
|
|
275
|
+
MetricSpec("DIASTOLIC_AVG", BloodPressureRecord.DIASTOLIC_AVG) {
|
|
276
|
+
it[BloodPressureRecord.DIASTOLIC_AVG]?.let(::pressureJson)
|
|
277
|
+
},
|
|
278
|
+
MetricSpec("DIASTOLIC_MIN", BloodPressureRecord.DIASTOLIC_MIN) {
|
|
279
|
+
it[BloodPressureRecord.DIASTOLIC_MIN]?.let(::pressureJson)
|
|
280
|
+
},
|
|
281
|
+
MetricSpec("DIASTOLIC_MAX", BloodPressureRecord.DIASTOLIC_MAX) {
|
|
282
|
+
it[BloodPressureRecord.DIASTOLIC_MAX]?.let(::pressureJson)
|
|
283
|
+
},
|
|
284
|
+
)
|
|
285
|
+
"MindfulnessSession" ->
|
|
286
|
+
listOf(
|
|
287
|
+
MetricSpec(
|
|
288
|
+
"MINDFULNESS_DURATION_TOTAL",
|
|
289
|
+
MindfulnessSessionRecord.MINDFULNESS_DURATION_TOTAL,
|
|
290
|
+
) {
|
|
291
|
+
it[MindfulnessSessionRecord.MINDFULNESS_DURATION_TOTAL]?.let { d ->
|
|
292
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
)
|
|
296
|
+
"ActivityIntensity" ->
|
|
297
|
+
listOf(
|
|
298
|
+
MetricSpec("DURATION_TOTAL", ActivityIntensityRecord.DURATION_TOTAL) {
|
|
299
|
+
it[ActivityIntensityRecord.DURATION_TOTAL]?.let { d ->
|
|
300
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
MetricSpec("MODERATE_DURATION_TOTAL", ActivityIntensityRecord.MODERATE_DURATION_TOTAL) {
|
|
304
|
+
it[ActivityIntensityRecord.MODERATE_DURATION_TOTAL]?.let { d ->
|
|
305
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
MetricSpec("VIGOROUS_DURATION_TOTAL", ActivityIntensityRecord.VIGOROUS_DURATION_TOTAL) {
|
|
309
|
+
it[ActivityIntensityRecord.VIGOROUS_DURATION_TOTAL]?.let { d ->
|
|
310
|
+
JSONObject().put("inSeconds", d.seconds)
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
)
|
|
314
|
+
"Nutrition" ->
|
|
315
|
+
listOf(
|
|
316
|
+
massSpec("BIOTIN_TOTAL", NutritionRecord.BIOTIN_TOTAL),
|
|
317
|
+
massSpec("CAFFEINE_TOTAL", NutritionRecord.CAFFEINE_TOTAL),
|
|
318
|
+
massSpec("CALCIUM_TOTAL", NutritionRecord.CALCIUM_TOTAL),
|
|
319
|
+
energySpec("ENERGY_TOTAL", NutritionRecord.ENERGY_TOTAL),
|
|
320
|
+
energySpec("ENERGY_FROM_FAT_TOTAL", NutritionRecord.ENERGY_FROM_FAT_TOTAL),
|
|
321
|
+
massSpec("CHLORIDE_TOTAL", NutritionRecord.CHLORIDE_TOTAL),
|
|
322
|
+
massSpec("CHOLESTEROL_TOTAL", NutritionRecord.CHOLESTEROL_TOTAL),
|
|
323
|
+
massSpec("CHROMIUM_TOTAL", NutritionRecord.CHROMIUM_TOTAL),
|
|
324
|
+
massSpec("COPPER_TOTAL", NutritionRecord.COPPER_TOTAL),
|
|
325
|
+
massSpec("DIETARY_FIBER_TOTAL", NutritionRecord.DIETARY_FIBER_TOTAL),
|
|
326
|
+
massSpec("FOLATE_TOTAL", NutritionRecord.FOLATE_TOTAL),
|
|
327
|
+
massSpec("FOLIC_ACID_TOTAL", NutritionRecord.FOLIC_ACID_TOTAL),
|
|
328
|
+
massSpec("IODINE_TOTAL", NutritionRecord.IODINE_TOTAL),
|
|
329
|
+
massSpec("IRON_TOTAL", NutritionRecord.IRON_TOTAL),
|
|
330
|
+
massSpec("MAGNESIUM_TOTAL", NutritionRecord.MAGNESIUM_TOTAL),
|
|
331
|
+
massSpec("MANGANESE_TOTAL", NutritionRecord.MANGANESE_TOTAL),
|
|
332
|
+
massSpec("MOLYBDENUM_TOTAL", NutritionRecord.MOLYBDENUM_TOTAL),
|
|
333
|
+
massSpec("MONOUNSATURATED_FAT_TOTAL", NutritionRecord.MONOUNSATURATED_FAT_TOTAL),
|
|
334
|
+
massSpec("NIACIN_TOTAL", NutritionRecord.NIACIN_TOTAL),
|
|
335
|
+
massSpec("PANTOTHENIC_ACID_TOTAL", NutritionRecord.PANTOTHENIC_ACID_TOTAL),
|
|
336
|
+
massSpec("PHOSPHORUS_TOTAL", NutritionRecord.PHOSPHORUS_TOTAL),
|
|
337
|
+
massSpec("POLYUNSATURATED_FAT_TOTAL", NutritionRecord.POLYUNSATURATED_FAT_TOTAL),
|
|
338
|
+
massSpec("POTASSIUM_TOTAL", NutritionRecord.POTASSIUM_TOTAL),
|
|
339
|
+
massSpec("PROTEIN_TOTAL", NutritionRecord.PROTEIN_TOTAL),
|
|
340
|
+
massSpec("RIBOFLAVIN_TOTAL", NutritionRecord.RIBOFLAVIN_TOTAL),
|
|
341
|
+
massSpec("SATURATED_FAT_TOTAL", NutritionRecord.SATURATED_FAT_TOTAL),
|
|
342
|
+
massSpec("SELENIUM_TOTAL", NutritionRecord.SELENIUM_TOTAL),
|
|
343
|
+
massSpec("SODIUM_TOTAL", NutritionRecord.SODIUM_TOTAL),
|
|
344
|
+
massSpec("SUGAR_TOTAL", NutritionRecord.SUGAR_TOTAL),
|
|
345
|
+
massSpec("THIAMIN_TOTAL", NutritionRecord.THIAMIN_TOTAL),
|
|
346
|
+
massSpec("TOTAL_CARBOHYDRATE_TOTAL", NutritionRecord.TOTAL_CARBOHYDRATE_TOTAL),
|
|
347
|
+
massSpec("TOTAL_FAT_TOTAL", NutritionRecord.TOTAL_FAT_TOTAL),
|
|
348
|
+
massSpec("TRANS_FAT_TOTAL", NutritionRecord.TRANS_FAT_TOTAL),
|
|
349
|
+
massSpec("UNSATURATED_FAT_TOTAL", NutritionRecord.UNSATURATED_FAT_TOTAL),
|
|
350
|
+
massSpec("VITAMIN_A_TOTAL", NutritionRecord.VITAMIN_A_TOTAL),
|
|
351
|
+
massSpec("VITAMIN_B12_TOTAL", NutritionRecord.VITAMIN_B12_TOTAL),
|
|
352
|
+
massSpec("VITAMIN_B6_TOTAL", NutritionRecord.VITAMIN_B6_TOTAL),
|
|
353
|
+
massSpec("VITAMIN_C_TOTAL", NutritionRecord.VITAMIN_C_TOTAL),
|
|
354
|
+
massSpec("VITAMIN_D_TOTAL", NutritionRecord.VITAMIN_D_TOTAL),
|
|
355
|
+
massSpec("VITAMIN_E_TOTAL", NutritionRecord.VITAMIN_E_TOTAL),
|
|
356
|
+
massSpec("VITAMIN_K_TOTAL", NutritionRecord.VITAMIN_K_TOTAL),
|
|
357
|
+
massSpec("ZINC_TOTAL", NutritionRecord.ZINC_TOTAL),
|
|
358
|
+
)
|
|
359
|
+
"Speed" ->
|
|
360
|
+
listOf(
|
|
361
|
+
velocitySpec("SPEED_AVG", SpeedRecord.SPEED_AVG),
|
|
362
|
+
velocitySpec("SPEED_MIN", SpeedRecord.SPEED_MIN),
|
|
363
|
+
velocitySpec("SPEED_MAX", SpeedRecord.SPEED_MAX),
|
|
364
|
+
)
|
|
365
|
+
"Power" ->
|
|
366
|
+
listOf(
|
|
367
|
+
powerSpec("POWER_AVG", PowerRecord.POWER_AVG),
|
|
368
|
+
powerSpec("POWER_MIN", PowerRecord.POWER_MIN),
|
|
369
|
+
powerSpec("POWER_MAX", PowerRecord.POWER_MAX),
|
|
370
|
+
)
|
|
371
|
+
"StepsCadence" ->
|
|
372
|
+
listOf(
|
|
373
|
+
MetricSpec("RATE_AVG", StepsCadenceRecord.RATE_AVG) {
|
|
374
|
+
it[StepsCadenceRecord.RATE_AVG]
|
|
375
|
+
},
|
|
376
|
+
MetricSpec("RATE_MIN", StepsCadenceRecord.RATE_MIN) {
|
|
377
|
+
it[StepsCadenceRecord.RATE_MIN]
|
|
378
|
+
},
|
|
379
|
+
MetricSpec("RATE_MAX", StepsCadenceRecord.RATE_MAX) {
|
|
380
|
+
it[StepsCadenceRecord.RATE_MAX]
|
|
381
|
+
},
|
|
382
|
+
)
|
|
383
|
+
"CyclingPedalingCadence" ->
|
|
384
|
+
listOf(
|
|
385
|
+
MetricSpec("RPM_AVG", CyclingPedalingCadenceRecord.RPM_AVG) {
|
|
386
|
+
it[CyclingPedalingCadenceRecord.RPM_AVG]
|
|
387
|
+
},
|
|
388
|
+
MetricSpec("RPM_MIN", CyclingPedalingCadenceRecord.RPM_MIN) {
|
|
389
|
+
it[CyclingPedalingCadenceRecord.RPM_MIN]
|
|
390
|
+
},
|
|
391
|
+
MetricSpec("RPM_MAX", CyclingPedalingCadenceRecord.RPM_MAX) {
|
|
392
|
+
it[CyclingPedalingCadenceRecord.RPM_MAX]
|
|
393
|
+
},
|
|
394
|
+
)
|
|
395
|
+
"WheelchairPushes" ->
|
|
396
|
+
listOf(
|
|
397
|
+
MetricSpec("COUNT_TOTAL", WheelchairPushesRecord.COUNT_TOTAL) {
|
|
398
|
+
it[WheelchairPushesRecord.COUNT_TOTAL]
|
|
399
|
+
}
|
|
400
|
+
)
|
|
401
|
+
"BasalMetabolicRate" ->
|
|
402
|
+
listOf(
|
|
403
|
+
energySpec(
|
|
404
|
+
"BASAL_CALORIES_TOTAL",
|
|
405
|
+
BasalMetabolicRateRecord.BASAL_CALORIES_TOTAL,
|
|
406
|
+
)
|
|
407
|
+
)
|
|
408
|
+
else ->
|
|
409
|
+
throw IllegalArgumentException("Aggregation not supported for recordType=$recordType")
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
private fun resultToJson(
|
|
413
|
+
recordType: String,
|
|
414
|
+
result: AggregationResult,
|
|
415
|
+
specs: List<MetricSpec>,
|
|
416
|
+
): JSONObject =
|
|
417
|
+
JSONObject().apply {
|
|
418
|
+
put("recordType", recordType)
|
|
419
|
+
put("dataOrigins", JSONArray(result.dataOrigins.map { it.packageName }))
|
|
420
|
+
val metrics = JSONObject()
|
|
421
|
+
specs.forEach { spec ->
|
|
422
|
+
metrics.put(spec.name, spec.read(result) ?: JSONObject.NULL)
|
|
423
|
+
}
|
|
424
|
+
put("metrics", metrics)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private fun massSpec(name: String, metric: AggregateMetric<Mass>) =
|
|
428
|
+
MetricSpec(name, metric) { it[metric]?.let(::massJson) }
|
|
429
|
+
|
|
430
|
+
private fun energySpec(name: String, metric: AggregateMetric<Energy>) =
|
|
431
|
+
MetricSpec(name, metric) { it[metric]?.let(::energyJson) }
|
|
432
|
+
|
|
433
|
+
private fun velocitySpec(name: String, metric: AggregateMetric<Velocity>) =
|
|
434
|
+
MetricSpec(name, metric) { it[metric]?.let(::velocityJson) }
|
|
435
|
+
|
|
436
|
+
private fun powerSpec(name: String, metric: AggregateMetric<Power>) =
|
|
437
|
+
MetricSpec(name, metric) { it[metric]?.let(::powerJson) }
|
|
438
|
+
|
|
439
|
+
private fun parseInstantTimeRange(options: JSONObject): TimeRangeFilter {
|
|
440
|
+
val filter = options.getJSONObject("timeRangeFilter")
|
|
441
|
+
return TimeRangeFilter.between(
|
|
442
|
+
Instant.parse(filter.getString("startTime")),
|
|
443
|
+
Instant.parse(filter.getString("endTime")),
|
|
444
|
+
)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private fun parseLocalDateTimeRange(options: JSONObject): TimeRangeFilter {
|
|
448
|
+
val filter = options.getJSONObject("timeRangeFilter")
|
|
449
|
+
val zone = ZoneId.systemDefault()
|
|
450
|
+
val start = LocalDateTime.ofInstant(Instant.parse(filter.getString("startTime")), zone)
|
|
451
|
+
val end = LocalDateTime.ofInstant(Instant.parse(filter.getString("endTime")), zone)
|
|
452
|
+
return TimeRangeFilter.between(start, end)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private fun parseDataOriginFilter(options: JSONObject): Set<DataOrigin> {
|
|
456
|
+
val arr = options.optJSONArray("dataOriginFilter") ?: return emptySet()
|
|
457
|
+
return (0 until arr.length()).map { DataOrigin(arr.getString(it)) }.toSet()
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private fun parseDurationSlicer(options: JSONObject): Duration {
|
|
461
|
+
val slicer = options.getJSONObject("timeRangeSlicer")
|
|
462
|
+
val value = slicer.getLong("value")
|
|
463
|
+
return when (slicer.getString("unit")) {
|
|
464
|
+
"millis" -> Duration.ofMillis(value)
|
|
465
|
+
"seconds" -> Duration.ofSeconds(value)
|
|
466
|
+
"minutes" -> Duration.ofMinutes(value)
|
|
467
|
+
"hours" -> Duration.ofHours(value)
|
|
468
|
+
"days" -> Duration.ofDays(value)
|
|
469
|
+
else -> throw IllegalArgumentException("Unsupported duration unit")
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private fun parsePeriodSlicer(options: JSONObject): Period {
|
|
474
|
+
val slicer = options.getJSONObject("timeRangeSlicer")
|
|
475
|
+
val value = slicer.getInt("value")
|
|
476
|
+
return when (slicer.getString("unit")) {
|
|
477
|
+
"days" -> Period.ofDays(value)
|
|
478
|
+
"weeks" -> Period.ofWeeks(value)
|
|
479
|
+
"months" -> Period.ofMonths(value)
|
|
480
|
+
"years" -> Period.ofYears(value)
|
|
481
|
+
else -> throw IllegalArgumentException("Unsupported period unit")
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private fun energyJson(e: Energy) =
|
|
486
|
+
JSONObject()
|
|
487
|
+
.put("inKilocalories", e.inKilocalories)
|
|
488
|
+
.put("inCalories", e.inCalories)
|
|
489
|
+
.put("inJoules", e.inJoules)
|
|
490
|
+
|
|
491
|
+
private fun lengthJson(l: Length) =
|
|
492
|
+
JSONObject()
|
|
493
|
+
.put("inMeters", l.inMeters)
|
|
494
|
+
.put("inKilometers", l.inKilometers)
|
|
495
|
+
.put("inMiles", l.inMiles)
|
|
496
|
+
|
|
497
|
+
private fun massJson(m: Mass) =
|
|
498
|
+
JSONObject()
|
|
499
|
+
.put("inKilograms", m.inKilograms)
|
|
500
|
+
.put("inGrams", m.inGrams)
|
|
501
|
+
.put("inPounds", m.inPounds)
|
|
502
|
+
|
|
503
|
+
private fun volumeJson(v: Volume) =
|
|
504
|
+
JSONObject()
|
|
505
|
+
.put("inLiters", v.inLiters)
|
|
506
|
+
.put("inMilliliters", v.inMilliliters)
|
|
507
|
+
|
|
508
|
+
private fun pressureJson(p: Pressure) =
|
|
509
|
+
JSONObject().put("inMillimetersOfMercury", p.inMillimetersOfMercury)
|
|
510
|
+
|
|
511
|
+
private fun velocityJson(v: Velocity) =
|
|
512
|
+
JSONObject()
|
|
513
|
+
.put("inMetersPerSecond", v.inMetersPerSecond)
|
|
514
|
+
.put("inKilometersPerHour", v.inKilometersPerHour)
|
|
515
|
+
.put("inMilesPerHour", v.inMilesPerHour)
|
|
516
|
+
|
|
517
|
+
private fun powerJson(p: Power) =
|
|
518
|
+
JSONObject()
|
|
519
|
+
.put("inWatts", p.inWatts)
|
|
520
|
+
.put("inKilocaloriesPerDay", p.inKilocaloriesPerDay)
|
|
521
|
+
}
|