health: bucket awake-in-bed and generic sleeping sleep stages
- Commit
288179293840fb1fa94933184133e668307001ad- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 18:53 UTC (4 days ago)
- Parents
0e0bff7
Health Connect has eight sleep stage kinds, but the Health Connect to Room conversion in HealthSyncWorker only bucketed six of them. STAGE_TYPE_AWAKE_IN_BED and the generic STAGE_TYPE_SLEEPING fell out of every SleepData total, so that time vanished from the sleep stage breakdown: a tracker reporting only generic sleeping stages produced a breakdown of all zeroes under a correct session total, and awake-in-bed time was lost rather than shown as awake. The hypnogram also drew awake-in-bed stages at the core-sleep level. Bucket awake-in-bed with the other awake stages and generic sleeping with light (core) sleep in toSleepRecord, matching the stage-to-level mapping SleepStageGraph already used for unclassified stages, and make SleepStageGraph treat awake-in-bed as awake both when drawing and when summing fallback durations for older records. Verified with a new SleepStageBucketingTest: both tests fail on the previous conversion (awake and light totals come back zero) and pass with the fix. Full :health:testDevUnitTest (658 tests) and :health:lint pass.
Diff
health/src/main/java/com/vayunmathur/health/sync/HealthSyncWorker.kt
diff --git a/health/src/main/java/com/vayunmathur/health/sync/HealthSyncWorker.kt b/health/src/main/java/com/vayunmathur/health/sync/HealthSyncWorker.ktindex 0a54d1f605..644857b736 100644--- a/health/src/main/java/com/vayunmathur/health/sync/HealthSyncWorker.kt+++ b/health/src/main/java/com/vayunmathur/health/sync/HealthSyncWorker.kt@@ -407,10 +407,17 @@ private fun SleepSessionRecord.toSleepRecord(): List<Record> { Duration.between(this.startTime, this.endTime).toMillis().toDouble() / MILLIS_PER_SECOND / SECONDS_PER_MINUTE / MINUTES_PER_HOUR, sleepData = com.vayunmathur.health.data.SleepData(+ // Health Connect has eight stage kinds and every one must land in a+ // bucket, or that time vanishes from the stage breakdown: awake-in-bed+ // is awake time, and the generic SLEEPING stage (trackers that report+ // no substages) is core sleep, the same mapping SleepStageGraph's+ // fallback uses. awakeDurationMillis = (durations[SleepSessionRecord.STAGE_TYPE_AWAKE] ?: 0) ++ (durations[SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED] ?: 0) + (durations[SleepSessionRecord.STAGE_TYPE_OUT_OF_BED] ?: 0), remDurationMillis = durations[SleepSessionRecord.STAGE_TYPE_REM] ?: 0,- lightDurationMillis = durations[SleepSessionRecord.STAGE_TYPE_LIGHT] ?: 0,+ lightDurationMillis = (durations[SleepSessionRecord.STAGE_TYPE_LIGHT] ?: 0) ++ (durations[SleepSessionRecord.STAGE_TYPE_SLEEPING] ?: 0), deepDurationMillis = durations[SleepSessionRecord.STAGE_TYPE_DEEP] ?: 0, unknownDurationMillis = durations[SleepSessionRecord.STAGE_TYPE_UNKNOWN] ?: 0, stagesJson = Json.encodeToString(stages)
health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt
diff --git a/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt b/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.ktindex 7e5d3316f3..2230537aa0 100644--- a/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt+++ b/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt@@ -69,11 +69,11 @@ fun SleepStageGraph(record: Record) { stages.filter { predicate(it.stage) }.sumOf { it.endTimeMillis - it.startTimeMillis } val sleepData = record.sleepData val awakeMillis = sleepData?.awakeDurationMillis?.takeIf { it > 0 }- ?: stageMillis { it == SleepSessionRecord.STAGE_TYPE_AWAKE || it == SleepSessionRecord.STAGE_TYPE_OUT_OF_BED }+ ?: stageMillis { it == SleepSessionRecord.STAGE_TYPE_AWAKE || it == SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED || it == SleepSessionRecord.STAGE_TYPE_OUT_OF_BED } val remMillis = sleepData?.remDurationMillis?.takeIf { it > 0 } ?: stageMillis { it == SleepSessionRecord.STAGE_TYPE_REM } val coreMillis = sleepData?.lightDurationMillis?.takeIf { it > 0 }- ?: stageMillis { it != SleepSessionRecord.STAGE_TYPE_AWAKE && it != SleepSessionRecord.STAGE_TYPE_OUT_OF_BED && it != SleepSessionRecord.STAGE_TYPE_REM && it != SleepSessionRecord.STAGE_TYPE_DEEP }+ ?: stageMillis { it != SleepSessionRecord.STAGE_TYPE_AWAKE && it != SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED && it != SleepSessionRecord.STAGE_TYPE_OUT_OF_BED && it != SleepSessionRecord.STAGE_TYPE_REM && it != SleepSessionRecord.STAGE_TYPE_DEEP } val deepMillis = sleepData?.deepDurationMillis?.takeIf { it > 0 } ?: stageMillis { it == SleepSessionRecord.STAGE_TYPE_DEEP } val graphDescription = stringResource(@@ -145,7 +145,7 @@ fun SleepStageGraph(record: Record) { ((stage.endTimeMillis - startTime).toFloat() / duration) * canvasWidth val (yPos, color) = when (stage.stage) {- SleepSessionRecord.STAGE_TYPE_AWAKE, SleepSessionRecord.STAGE_TYPE_OUT_OF_BED -> 0f to awakeColor+ SleepSessionRecord.STAGE_TYPE_AWAKE, SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED, SleepSessionRecord.STAGE_TYPE_OUT_OF_BED -> 0f to awakeColor SleepSessionRecord.STAGE_TYPE_REM -> levelHeight to remColor SleepSessionRecord.STAGE_TYPE_LIGHT -> levelHeight * 2 to coreColor SleepSessionRecord.STAGE_TYPE_DEEP -> levelHeight * 3 to deepColor
health/src/test/java/com/vayunmathur/health/util/SleepStageBucketingTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/SleepStageBucketingTest.kt b/health/src/test/java/com/vayunmathur/health/util/SleepStageBucketingTest.ktnew file mode 100644index 0000000000..fcd6ed6eed--- /dev/null+++ b/health/src/test/java/com/vayunmathur/health/util/SleepStageBucketingTest.kt@@ -0,0 +1,70 @@+package com.vayunmathur.health.util++import androidx.health.connect.client.records.SleepSessionRecord+import androidx.health.connect.client.records.metadata.Metadata+import com.vayunmathur.health.sync.toRecord+import java.time.Instant+import java.time.ZoneOffset+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: Health Connect has eight sleep stage kinds, but the Room+ * conversion bucketed only six of them. `STAGE_TYPE_AWAKE_IN_BED` and the+ * generic `STAGE_TYPE_SLEEPING` fell out of every SleepData total, so the+ * stage breakdown summed to less than the session (all zero for a tracker+ * that reports only generic sleeping stages) and awake-in-bed time was+ * silently lost rather than shown as awake.+ */+class SleepStageBucketingTest {++ private val start: Instant = Instant.parse("2026-03-04T23:00:00Z")+ private val zone: ZoneOffset = ZoneOffset.UTC++ private fun stage(kind: Int, fromSec: Long, toSec: Long) =+ SleepSessionRecord.Stage(+ startTime = start.plusSeconds(fromSec),+ endTime = start.plusSeconds(toSec),+ stage = kind,+ )++ private fun sleepWith(stages: List<SleepSessionRecord.Stage>) =+ SleepSessionRecord(+ metadata = Metadata.manualEntry("sleep-buckets"),+ startTime = start,+ startZoneOffset = zone,+ endTime = start.plusSeconds(7_200),+ endZoneOffset = zone,+ stages = stages,+ ).toRecord().single()++ @Test+ fun `awake in bed counts as awake, not nothing`() {+ val data = sleepWith(+ listOf(+ stage(SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED, 0, 600),+ stage(SleepSessionRecord.STAGE_TYPE_LIGHT, 600, 2_400),+ )+ ).sleepData!!+ assertEquals(600_000L, data.awakeDurationMillis)+ assertEquals(1_800_000L, data.lightDurationMillis)+ }++ @Test+ fun `generic sleeping counts as core sleep, so the buckets cover the session`() {+ val data = sleepWith(+ listOf(+ stage(SleepSessionRecord.STAGE_TYPE_SLEEPING, 0, 3_600),+ stage(SleepSessionRecord.STAGE_TYPE_AWAKE_IN_BED, 3_600, 3_900),+ stage(SleepSessionRecord.STAGE_TYPE_DEEP, 3_900, 4_500),+ stage(SleepSessionRecord.STAGE_TYPE_UNKNOWN, 4_500, 4_800),+ )+ ).sleepData!!+ assertEquals(3_600_000L, data.lightDurationMillis)+ assertEquals(300_000L, data.awakeDurationMillis)+ // Every stage kind lands in exactly one bucket: nothing is dropped.+ val bucketed = data.awakeDurationMillis + data.remDurationMillis ++ data.lightDurationMillis + data.deepDurationMillis + data.unknownDurationMillis+ assertEquals(4_800_000L, bucketed)+ }+}