watch: auto-enable Do-Not-Disturb during workouts and detected sleep
- Commit
72dbc450c08ff28d05cfe831eb4dd6dec6943d19- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-06 17:48 UTC (5 months ago)
- Parents
95630b7
Diff
watch/watch/src/main/java/com/vayunmathur/watch/watch/service/AutoDndManager.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/AutoDndManager.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/AutoDndManager.ktnew file mode 100644index 0000000000..b4e52b20a0--- /dev/null+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/AutoDndManager.kt@@ -0,0 +1,49 @@+package com.vayunmathur.watch.watch.service++import android.app.NotificationManager+import android.content.Context++/**+ * Coordinates automatic Do-Not-Disturb between the workout and sleep triggers so+ * they don't fight. Only one trigger "owns" auto-DnD at a time; the first to+ * [engage] wins until it [release]s. The prior interruption filter is remembered+ * and restored on release, but only if the filter is still the value we applied —+ * a manual override in between therefore wins.+ *+ * Setting only changes the *local* filter; the DndController then propagates it to+ * the paired phone over BLE for free.+ */+object AutoDndManager {++ const val OWNER_WORKOUT = "workout"+ const val OWNER_SLEEP = "sleep"++ private var owner: String? = null+ private var priorFilter: Int = -1+ private var appliedFilter: Int = -1++ @Synchronized+ fun engage(context: Context, owner: String, filter: Int) {+ if (this.owner != null) return+ val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager+ if (!nm.isNotificationPolicyAccessGranted) return+ priorFilter = nm.currentInterruptionFilter+ appliedFilter = filter+ this.owner = owner+ if (nm.currentInterruptionFilter != filter) {+ runCatching { nm.setInterruptionFilter(filter) }+ }+ }++ @Synchronized+ fun release(context: Context, owner: String) {+ if (this.owner != owner) return+ val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager+ if (nm.isNotificationPolicyAccessGranted && nm.currentInterruptionFilter == appliedFilter) {+ runCatching { nm.setInterruptionFilter(priorFilter) }+ }+ this.owner = null+ priorFilter = -1+ appliedFilter = -1+ }+}
watch/watch/src/main/java/com/vayunmathur/watch/watch/service/ExerciseService.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/ExerciseService.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/ExerciseService.ktindex 2f86c2b274..8f2f5f4eb9 100644--- a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/ExerciseService.kt+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/ExerciseService.kt@@ -90,8 +90,17 @@ class ExerciseService : Service() { if (uiState.value == UiState.Active && activeStartEpochMs.value == 0L) { activeStartEpochMs.value = System.currentTimeMillis() - activeDurationMs.value }+ // Auto-DnD: silence notifications for the duration of the workout.+ if (uiState.value == UiState.Active) {+ AutoDndManager.engage(+ this,+ AutoDndManager.OWNER_WORKOUT,+ NotificationManager.INTERRUPTION_FILTER_PRIORITY,+ )+ } requestTileUpdate() if (state.isEnded) {+ AutoDndManager.release(this, AutoDndManager.OWNER_WORKOUT) resetLiveState() stopForeground(STOP_FOREGROUND_REMOVE) stopSelf()
watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.ktindex eada4e25bd..fc70cf2ab8 100644--- a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt@@ -44,6 +44,8 @@ class SensorBackgroundService : Service(), SensorEventListener { private var lastStepCount = -1.0 // Current motion state, maintained via the one-shot trigger sensors. @Volatile private var isStationary = false+ // Wall-clock ms the wearer most recently became stationary (0 = moving).+ @Volatile private var stationarySinceMs = 0L override fun onCreate() { super.onCreate()@@ -97,12 +99,16 @@ class SensorBackgroundService : Service(), SensorEventListener { when (event.sensor.type) { Sensor.TYPE_STATIONARY_DETECT -> { isStationary = true+ stationarySinceMs = now persist(SensorRecord(type = MetricType.Motion, timestamp = now, value = 1.0)) // Trigger sensors are one-shot; arm the opposite one. armMotionDetect() } Sensor.TYPE_MOTION_DETECT -> { isStationary = false+ stationarySinceMs = 0L+ // Movement ends any auto-sleep DnD.+ AutoDndManager.release(this@SensorBackgroundService, AutoDndManager.OWNER_SLEEP) persist(SensorRecord(type = MetricType.Motion, timestamp = now, value = 0.0)) armStationaryDetect() }@@ -127,6 +133,7 @@ class SensorBackgroundService : Service(), SensorEventListener { stationary = isStationary, ), )+ evaluateSleep(bpm, now) } Sensor.TYPE_STEP_COUNTER -> { val cumulative = event.values.firstOrNull()?.toDouble() ?: return@@ -162,6 +169,24 @@ class SensorBackgroundService : Service(), SensorEventListener { override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} + // Approximate sleep heuristic from signals already collected here: sustained+ // stillness plus a resting-low heart rate engages auto-DnD; a heart-rate rise+ // (or motion, handled in the trigger listener) releases it. Documented as+ // approximate — refine later with a proper sleep-staging signal.+ private fun evaluateSleep(bpm: Double, now: Long) {+ val stationaryLongEnough = isStationary && stationarySinceMs > 0L &&+ now - stationarySinceMs >= SLEEP_STATIONARY_MS+ if (stationaryLongEnough && bpm > 0.0 && bpm < RESTING_HR_THRESHOLD) {+ AutoDndManager.engage(+ this,+ AutoDndManager.OWNER_SLEEP,+ NotificationManager.INTERRUPTION_FILTER_PRIORITY,+ )+ } else if (bpm >= WAKE_HR_THRESHOLD) {+ AutoDndManager.release(this, AutoDndManager.OWNER_SLEEP)+ }+ }+ private fun buildNotification(): Notification { val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channel = NotificationChannel(@@ -191,6 +216,7 @@ class SensorBackgroundService : Service(), SensorEventListener { gattServer.stop() healthServices.stop() dndController.unregister()+ AutoDndManager.release(this, AutoDndManager.OWNER_SLEEP) scope.cancel() super.onDestroy() }@@ -202,6 +228,10 @@ class SensorBackgroundService : Service(), SensorEventListener { private const val CHANNEL_ID = "sensor_collection" private const val NOTIFICATION_ID = 1 private const val HEART_RATE_INTERVAL_MS = 60_000L+ // Sleep heuristic thresholds.+ private const val SLEEP_STATIONARY_MS = 20L * 60 * 1000+ private const val RESTING_HR_THRESHOLD = 55.0+ private const val WAKE_HR_THRESHOLD = 70.0 fun start(context: Context) { val intent = Intent(context, SensorBackgroundService::class.java)