diff --git a/AGENTS.md b/AGENTS.md index 16fefc45..716b4fe4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1243,6 +1243,24 @@ clutter came from ignoring one of them: - **A goal saved anywhere goes through `GoalSync.afterManualSave`** — it is what stops the field following the coach, sends the step goal to the ring and republishes the widget. +## Detected workouts: guess the type, ask, learn (docs/activity-recognition-plan.md) + +The rings give no raw accelerometer, so `ActivityClassifier` guesses from step cadence and +burstiness and from heart-rate level and unevenness. Every type is ranked; the user always picks. +Rules a change must keep: + +- **Nothing is logged without a tap.** A wrong workout reaches Health Connect and Strava. +- **The user's answers win.** A confirmed suggestion stores its features with the chosen type + (`activity_detection` → `learned`, newest 200, in the backup). Close earlier answers outweigh + the rules, so don't tune the rules to override them. +- **"Not this" holds by overlap, not by key** (`ActivityDetection.open`): a re-detection with + shifted bounds or a changed guess must not resurface a dismissed stretch. +- **Steps and heart stretches merge into one window** (`mergeActive`), so a run is never also + offered as cardio. +- **Notify once, recent only** (`ActivityDetectionNotifier`): after a sync's `done`, for + activity ending in the last 12 h, never twice for the same stretch, behind its own switch and + channel. Today shows the newest as one line; the Activity tab's card is where it is answered. + ## Logging a food again: suggestions come from the user's own history Typing a meal's name in the Log Meal form offers foods logged before (`service/FoodHistory.kt`, diff --git a/app/src/main/assets/i18n/pt-BR.json b/app/src/main/assets/i18n/pt-BR.json index 8aa5696e..26d66c75 100644 --- a/app/src/main/assets/i18n/pt-BR.json +++ b/app/src/main/assets/i18n/pt-BR.json @@ -154,6 +154,7 @@ "After 3 or more": "Depois de 3 ou mais", "After 3 or more drinks, your HRV runs {0}% {1} than on dry nights.": "Depois de 3 ou mais bebidas, sua HRV fica {0}% {1} do que nas noites sem álcool.", "After a session": "Depois de uma sessão", + "After a sync, what your ring saw you doing (a walk, a ride, strength training), to add with a tap. Nothing is logged without you.": "Depois de uma sincronização, o que seu anel viu você fazendo (uma caminhada, um pedal, musculação), para adicionar com um toque. Nada é registrado sem você.", "After birth, sleep is broken rather than short. The longest unbroken stretch is the number to watch; it usually grows over the first months.": "Depois do parto, o sono fica fragmentado, mais do que curto. O maior trecho sem interrupção é o número a acompanhar; ele costuma crescer nos primeiros meses.", "After the period, until about two weeks before the next one.": "Depois da menstruação, até cerca de duas semanas antes da próxima.", "Against your {0} dry nights since you started. A pattern in your data, not proof of cause.": "Comparado com suas {0} noites sem álcool desde que começou. Um padrão nos seus dados, não prova de causa.", @@ -476,6 +477,8 @@ "Details": "Detalhes", "Detect server & configure": "Detectar servidor e configurar", "Detected by PulseLoop and confirmed": "Detectado pelo PulseLoop e confirmado", + "Detected workout notifications": "Notificações de treinos detectados", + "Detected workouts": "Treinos detectados", "Detecting…": "Detectando…", "Developer": "Desenvolvedor", "Developer options are already enabled.": "As opções de desenvolvedor já estão ativadas.", @@ -878,7 +881,13 @@ "Long-term insights appear as more nights are tracked.": "Insights de longo prazo aparecem conforme mais noites são registradas.", "Look again": "Verificar novamente", "Looking for apps writing to Health Connect…": "Procurando apps que gravam no Health Connect…", + "Looks like a hike": "Parece uma trilha", + "Looks like a ride": "Parece um pedal", + "Looks like a run": "Parece uma corrida", "Looks like a strong night after a more active day. Your duration was solid and deep sleep made up a healthy part of the night, which kept the score high.": "Parece uma noite forte depois de um dia mais ativo. A duração foi boa e o sono profundo ocupou uma parte saudável da noite, o que manteve a pontuação alta.", + "Looks like a walk": "Parece uma caminhada", + "Looks like a workout": "Parece um treino", + "Looks like strength training": "Parece musculação", "Low": "Baixo", "Low (medication)": "Baixa (medicação)", "Low Battery Notifications": "Notificações de bateria fraca", @@ -1292,6 +1301,7 @@ "Resume": "Retomar", "Retake (every 3 months)": "Refazer (a cada 3 meses)", "Retry": "Tentar novamente", + "Review": "Revisar", "Review & adjust": "Revisar e ajustar", "Ride it out": "Aguentar firme", "Right at your limit": "Exatamente no seu limite", @@ -1730,6 +1740,7 @@ "What affects you": "O que afeta você", "What changed": "O que mudou", "What did you do?": "O que você fez?", + "What your ring saw you doing, to add with a tap": "O que seu anel viu você fazendo, para adicionar com um toque", "What's your goal?": "Qual é a sua meta?", "When": "Quando", "When did it end?": "Quando terminou?", @@ -1921,6 +1932,7 @@ "{0} markers outside the lab's range in your latest report. Talk them over with your doctor.": "{0} marcadores fora da faixa do laboratório no seu último laudo. Converse sobre eles com seu médico.", "{0} min a night less than your usual over the last 7 nights.": "{0} min por noite a menos que o seu habitual nas últimas 7 noites.", "{0} min a night more than your usual over the last 7 nights.": "{0} min por noite a mais que o seu habitual nas últimas 7 noites.", + "{0} min at {1}. Tap to add it, or say it wasn't.": "{0} min às {1}. Toque para adicionar, ou diga que não foi.", "{0} min {1} sleep": "{0} min de sono {1}", "{0} ml at {1}% · about {2} kcal": "{0} ml a {1}% · cerca de {2} kcal", "{0} more dry day{1} to reach {2}": "Mais {0} dia{1} sem álcool para chegar a {2}", diff --git a/app/src/main/java/com/pulseloop/MainActivity.kt b/app/src/main/java/com/pulseloop/MainActivity.kt index a8739a39..613bb25b 100644 --- a/app/src/main/java/com/pulseloop/MainActivity.kt +++ b/app/src/main/java/com/pulseloop/MainActivity.kt @@ -157,6 +157,10 @@ class MainActivity : ComponentActivity() { } else if (intent?.action == ACTION_OPEN_ACCURACY_CHECK) { com.pulseloop.ui.LaunchRequests.route.value = "settings/accuracy-check" intent.action = null + } else if (intent?.action == ACTION_OPEN_ACTIVITY) { + // A detected-workout notification: the suggestion waits on the Activity tab. + com.pulseloop.ui.LaunchRequests.route.value = "activity" + intent.action = null } } @@ -180,6 +184,8 @@ class MainActivity : ComponentActivity() { const val ACTION_LOG_MEAL = "com.pulseloop.action.LOG_MEAL" /** The accuracy-report-ready notification. */ const val ACTION_OPEN_ACCURACY_CHECK = "com.pulseloop.action.OPEN_ACCURACY_CHECK" + /** A detected-workout notification (ActivityDetectionNotifier). */ + const val ACTION_OPEN_ACTIVITY = "com.pulseloop.action.OPEN_ACTIVITY" } private fun handleStravaRedirect(intent: Intent) { diff --git a/app/src/main/java/com/pulseloop/PulseLoopApplication.kt b/app/src/main/java/com/pulseloop/PulseLoopApplication.kt index 39434c54..8fd3574b 100644 --- a/app/src/main/java/com/pulseloop/PulseLoopApplication.kt +++ b/app/src/main/java/com/pulseloop/PulseLoopApplication.kt @@ -37,6 +37,9 @@ class PulseLoopApplication : Application() { // Accuracy check: a different ring ends a running check at once (it can't be told apart later). com.pulseloop.health.accuracy.AccuracyRingWatcher.start(this, appScope) com.pulseloop.health.accuracy.AccuracyNotifications.createChannel(this) + // What the ring saw the user doing, said after each sync (docs/activity-recognition-plan.md). + com.pulseloop.notifications.ActivityDetectionNotifier.createChannel(this) + com.pulseloop.notifications.ActivityDetectionNotifier.start(this, appScope) // One-time cleanup of activity totals corrupted by the old bucket handling; runs off the // main thread and is prefs-gated so it executes once. Safe to race the first sync — the // ring can't connect before this completes a couple of DELETE statements. diff --git a/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt b/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt index 10ca0372..edb18664 100644 --- a/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt +++ b/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt @@ -37,8 +37,9 @@ object ArchivedSettings { "mindful_sessions" to emptySet(), // The phone-step calibration and its daily counts. The raw step counter is this phone's. "step_calibration" to setOf("lastCounter"), - // Detected workouts and naps already answered, so they aren't asked again. - "activity_detection" to emptySet(), + // Detected workouts: what was answered (not asked again), the types the user taught the + // classifier ("learned"), and the notification switch. Which ones notified stays here. + "activity_detection" to setOf("notified"), // Rest Mode, while it lasts. "rest_mode" to emptySet(), // Whether the coach may use the lab results. diff --git a/app/src/main/java/com/pulseloop/notifications/ActivityDetectionNotifier.kt b/app/src/main/java/com/pulseloop/notifications/ActivityDetectionNotifier.kt new file mode 100644 index 00000000..6e234748 --- /dev/null +++ b/app/src/main/java/com/pulseloop/notifications/ActivityDetectionNotifier.kt @@ -0,0 +1,103 @@ +package com.pulseloop.notifications + +import android.Manifest +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import androidx.core.content.ContextCompat +import com.pulseloop.MainActivity +import com.pulseloop.data.PulseLoopDatabase +import com.pulseloop.ring.PulseEvent +import com.pulseloop.ring.PulseEventBus +import com.pulseloop.service.ActivityDetection +import com.pulseloop.ui.i18n.tr +import com.pulseloop.ui.i18n.trf +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.launch +import java.time.Instant +import java.time.ZoneId +import java.time.format.DateTimeFormatter + +/** + * After each completed sync, says what the ring saw the user doing ("Looks like strength + * training · 45 min at 18:10"). One notification per new suggestion, only for activity that ended + * in the last 12 hours, never twice for the same stretch. Tapping opens the Activity tab, where + * the user confirms, changes the type or dismisses it. See docs/activity-recognition-plan.md. + */ +object ActivityDetectionNotifier { + private const val CHANNEL_ID = "detected_workouts" + private const val RECENT_MS = 12 * 60 * 60_000L + + fun createChannel(context: Context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel(CHANNEL_ID, tr("Detected workouts"), NotificationManager.IMPORTANCE_DEFAULT) + .apply { description = tr("What your ring saw you doing, to add with a tap") } + (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel) + } + } + + /** Application-scoped, like the accuracy watcher: runs whether or not a screen is open. */ + fun start(context: Context, scope: CoroutineScope) { + val app = context.applicationContext + scope.launch { + PulseEventBus.events + .filter { it is PulseEvent.SyncProgress && it.stage == CoachNotificationDataTrigger.DONE_STAGE } + .collectLatest { + // Let the sync's last rows land and back-to-back completions coalesce. + delay(5_000L) + runCatching { kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { check(app) } } + } + } + } + + suspend fun check(context: Context, now: Long = System.currentTimeMillis()) { + if (!ActivityDetection.notifyEnabled(context)) return + val db = PulseLoopDatabase.getInstance(context) + val already = ActivityDetection.notified(context) + val fresh = ActivityDetection.load(context, db, days = 1, now = now) + .filter { it.kind != ActivityDetection.Proposal.Kind.NAP && it.endAt >= now - RECENT_MS && it.key !in already } + if (fresh.isEmpty()) return + // Marked before delivery: a missing permission must not make every later sync retry it. + ActivityDetection.markNotified(context, fresh.map { it.key }) + fresh.take(2).forEach { deliver(context, it) } + } + + /** "a ride", "strength training": how the notification names a guessed type. */ + fun phrase(type: String): String = when (type) { + "walk" -> tr("Looks like a walk") + "run" -> tr("Looks like a run") + "cycle" -> tr("Looks like a ride") + "gym" -> tr("Looks like strength training") + "hike" -> tr("Looks like a hike") + else -> tr("Looks like a workout") + } + + private fun deliver(context: Context, p: ActivityDetection.Proposal) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED + ) return + val time = Instant.ofEpochMilli(p.startAt).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("HH:mm")) + val intent = Intent(context, MainActivity::class.java) + .setAction(MainActivity.ACTION_OPEN_ACTIVITY) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + val pending = PendingIntent.getActivity(context, p.key.hashCode(), intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + val n = NotificationCompat.Builder(context, CHANNEL_ID) + .setContentTitle(phrase(p.bestType)) + .setContentText(trf("{0} min at {1}. Tap to add it, or say it wasn't.", p.minutes, time)) + .setSmallIcon(android.R.drawable.ic_dialog_info) + .setAutoCancel(true) + .setContentIntent(pending) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .build() + NotificationManagerCompat.from(context).notify(p.key.hashCode(), n) + } +} diff --git a/app/src/main/java/com/pulseloop/service/ActivityClassifier.kt b/app/src/main/java/com/pulseloop/service/ActivityClassifier.kt new file mode 100644 index 00000000..a305f422 --- /dev/null +++ b/app/src/main/java/com/pulseloop/service/ActivityClassifier.kt @@ -0,0 +1,112 @@ +package com.pulseloop.service + +import kotlin.math.sqrt + +/** + * What a stretch of movement probably was, from what every ring syncs: step cadence and how + * bursty it is, and how high and how uneven heart rate ran. See docs/activity-recognition-plan.md. + * + * Rules give the first guess; the user's own earlier answers win once there are some close to + * this stretch. Every type is returned, best first, because the user always picks: this only + * decides which one is selected and what order the rest come in. + */ +object ActivityClassifier { + + data class Features( + val minutes: Double, + /** Steps per minute over the whole stretch. */ + val cadence: Double, + /** Coefficient of variation of the per-bucket cadence: sets with steps between them read high. */ + val stepBurst: Double, + /** Mean heart rate as a share of heart-rate reserve (0 = quiet level, 1 = HRmax). */ + val hrMeanPct: Double, + /** 90th-percentile heart rate as a share of heart-rate reserve. */ + val hrPeakPct: Double, + /** Coefficient of variation of the heart-rate readings: intervals and sets read high. */ + val hrCv: Double, + ) + + /** A stretch the user labelled, kept to steer later guesses. */ + data class Example(val features: Features, val type: String) + + /** The types a guess can name, in ActivityMeta's own order for the ones rules can't tell apart. */ + val TYPES = listOf("walk", "run", "cycle", "gym", "sport", "hike", "dance", "yoga", "squash", "other") + + fun features( + startAt: Long, + endAt: Long, + steps: List>, + hr: List>, + quiet: Double, + hrMax: Double, + ): Features { + val minutes = ((endAt - startAt) / 60_000.0).coerceAtLeast(1.0) + val inSteps = steps.filter { it.first in startAt until endAt }.sortedBy { it.first } + val cadence = inSteps.sumOf { it.second } / minutes + // Bucket length from the data itself: families sync 1- to 15-minute buckets. + val bucketMin = inSteps.zipWithNext { a, b -> (b.first - a.first) / 60_000.0 }.filter { it > 0 }.minOrNull() ?: 15.0 + val perBucket = inSteps.map { it.second / bucketMin } + val stepBurst = cv(perBucket).takeIf { perBucket.size >= 3 } ?: 0.0 + val bpm = hr.filter { it.first in startAt..endAt }.map { it.second } + val reserve = (hrMax - quiet).coerceAtLeast(20.0) + fun pct(v: Double) = ((v - quiet) / reserve).coerceIn(-0.5, 1.2) + val mean = if (bpm.isEmpty()) 0.0 else pct(bpm.average()) + val peak = if (bpm.isEmpty()) 0.0 else pct(bpm.sorted()[((bpm.size - 1) * 0.9).toInt()]) + val hrCv = cv(bpm).takeIf { bpm.size >= 3 } ?: 0.0 + return Features(minutes, cadence, stepBurst, mean, peak, hrCv) + } + + /** Every type, most likely first. */ + fun rank(f: Features, learned: List = emptyList()): List { + val rules = ruleScores(f) + val personal = personalScores(f, learned) + val weight = personal.second + val blended = TYPES.associateWith { t -> (1 - weight) * (rules[t] ?: 0.0) + weight * (personal.first[t] ?: 0.0) } + return TYPES.sortedWith(compareByDescending { blended[it] ?: 0.0 }.thenBy { TYPES.indexOf(it) }) + } + + internal fun ruleScores(f: Features): Map { + fun ramp(x: Double, from: Double, to: Double) = ((x - from) / (to - from)).coerceIn(0.0, 1.0) + val low = ramp(f.cadence, 60.0, 20.0) // few steps for the time spent + val long = ramp(f.minutes, 15.0, 40.0) + val scores = mapOf( + "run" to ramp(f.cadence, 110.0, 145.0), + "walk" to (ramp(f.cadence, 55.0, 85.0) * ramp(f.cadence, 140.0, 115.0)), + // A ride: heart rate held up and steady, for a while, with few steps. + "cycle" to low * ramp(f.hrMeanPct, 0.25, 0.5) * ramp(f.hrCv, 0.15, 0.07) * long, + // Strength: heart rate up but uneven (sets and rests), often with bursts of steps between sets. + "gym" to low * ramp(f.hrMeanPct, 0.12, 0.35) * (0.7 * ramp(f.hrCv, 0.07, 0.15) + 0.3 * ramp(f.stepBurst, 0.4, 1.2)), + // Intervals or a game: high peaks with deep drops. + "sport" to ramp(f.cadence, 100.0, 60.0) * ramp(f.hrPeakPct, 0.6, 0.85) * ramp(f.hrCv, 0.1, 0.2), + "other" to 0.12, + ) + val sum = scores.values.sum().takeIf { it > 0 } ?: 1.0 + return scores.mapValues { it.value / sum } + } + + /** Votes from the nearest earlier answers, and how much they should count (0..0.8). */ + internal fun personalScores(f: Features, learned: List): Pair, Double> { + if (learned.isEmpty()) return emptyMap() to 0.0 + val near = learned.map { it to distance(f, it.features) }.filter { it.second < 1.5 }.sortedBy { it.second }.take(5) + if (near.isEmpty()) return emptyMap() to 0.0 + val votes = near.groupBy { it.first.type }.mapValues { (_, v) -> v.sumOf { 1.0 / (it.second + 0.25) } } + val sum = votes.values.sum() + return votes.mapValues { it.value / sum } to (0.25 * near.size).coerceAtMost(0.8) + } + + internal fun distance(a: Features, b: Features): Double { + fun d(x: Double, y: Double, scale: Double) = (x - y) / scale + val parts = listOf( + d(a.minutes, b.minutes, 45.0), d(a.cadence, b.cadence, 30.0), d(a.stepBurst, b.stepBurst, 0.8), + d(a.hrMeanPct, b.hrMeanPct, 0.2), d(a.hrPeakPct, b.hrPeakPct, 0.2), d(a.hrCv, b.hrCv, 0.08), + ) + return sqrt(parts.sumOf { it * it }) + } + + private fun cv(v: List): Double { + if (v.isEmpty()) return 0.0 + val m = v.average() + if (m <= 0) return 0.0 + return sqrt(v.sumOf { (it - m) * (it - m) } / v.size) / m + } +} diff --git a/app/src/main/java/com/pulseloop/service/ActivityDetection.kt b/app/src/main/java/com/pulseloop/service/ActivityDetection.kt index 184e011b..7f6b728b 100644 --- a/app/src/main/java/com/pulseloop/service/ActivityDetection.kt +++ b/app/src/main/java/com/pulseloop/service/ActivityDetection.kt @@ -17,8 +17,18 @@ import java.time.ZoneId */ object ActivityDetection { - data class Proposal(val kind: Kind, val startAt: Long, val endAt: Long, val detail: String) { + data class Proposal( + val kind: Kind, val startAt: Long, val endAt: Long, val detail: String, + /** Every activity type, most likely first (ActivityClassifier); empty for a nap. */ + val guesses: List = emptyList(), + /** What the guess was made from, kept with the user's answer so later guesses learn. */ + val features: ActivityClassifier.Features? = null, + ) { enum class Kind { WALK, RUN, CARDIO, NAP } + /** The selected type: the classifier's first choice, else the kind's own. */ + val bestType: String get() = guesses.firstOrNull() ?: when (kind) { + Kind.WALK -> "walk"; Kind.RUN -> "run"; else -> "other" + } val key: String get() = "${kind.name}:$startAt:$endAt" val minutes: Int get() = ((endAt - startAt) / 60_000L).toInt() } @@ -107,13 +117,73 @@ object ActivityDetection { return out } - /** Proposals that overlap anything already recorded, or that the user already answered, are dropped. */ - fun open(proposals: List, recorded: List, answered: Set): List = - proposals.filter { p -> p.key !in answered && recorded.none { it.first < p.endAt && p.startAt < it.last } } + /** + * Proposals that overlap anything already recorded, or a stretch the user already answered, are + * dropped. Answers match by overlap, not by key: a re-detection (more data, a changed guess) + * can shift a stretch's bounds or kind, and "Not this" must still hold for it. + */ + fun open(proposals: List, recorded: List, answered: Set): List { + val answeredRanges = answered.mapNotNull { k -> + val parts = k.split(':') + val a = parts.getOrNull(1)?.toLongOrNull(); val b = parts.getOrNull(2)?.toLongOrNull() + if (a != null && b != null) a..b else null + } + fun overlaps(r: LongRange, p: Proposal) = r.first < p.endAt && p.startAt < r.last + return proposals.filter { p -> + p.key !in answered && recorded.none { overlaps(it, p) } && answeredRanges.none { overlaps(it, p) } + } + } + + /** + * Walk/run stretches and raised-heart stretches that touch (within 10 minutes) are one + * activity: a run is not also offered as "cardio", and a ride with a walk to the bike is one. + */ + fun mergeActive(ranges: List): List { + val out = mutableListOf() + for (r in ranges.sortedBy { it.first }) { + val last = out.lastOrNull() + if (last != null && r.first <= last.last + 10 * 60_000L) out[out.size - 1] = last.first..maxOf(last.last, r.last) + else out += r + } + return out + } // ── Persistence of answers ─────────────────────────────────────────────────────────────── private fun prefs(c: Context) = c.applicationContext.getSharedPreferences("activity_detection", Context.MODE_PRIVATE) + + // ── Learning: confirmed stretches with the type the user chose (rides in the backup) ── + fun learned(c: Context): List = + runCatching { decodeLearned(prefs(c).getString("learned", null)) }.getOrDefault(emptyList()) + + fun learn(c: Context, f: ActivityClassifier.Features, type: String) { + val all = (learned(c) + ActivityClassifier.Example(f, type)).takeLast(200) + prefs(c).edit().putString("learned", encodeLearned(all)).apply() + } + + /** One line per example: type and the six features, comma-separated. */ + internal fun encodeLearned(all: List): String = all.joinToString("\n") { e -> + val f = e.features + listOf(e.type, f.minutes, f.cadence, f.stepBurst, f.hrMeanPct, f.hrPeakPct, f.hrCv).joinToString(",") + } + + internal fun decodeLearned(raw: String?): List = + raw.orEmpty().lines().mapNotNull { line -> + val p = line.split(',') + val n = p.drop(1).mapNotNull { it.toDoubleOrNull() } + if (p.size != 7 || n.size != 6) null + else ActivityClassifier.Example(ActivityClassifier.Features(n[0], n[1], n[2], n[3], n[4], n[5]), p[0]) + } + + /** Whether a new suggestion may notify (Settings → Notifications → Detected workouts). */ + fun notifyEnabled(c: Context): Boolean = prefs(c).getBoolean("notify", true) + fun setNotifyEnabled(c: Context, on: Boolean) { prefs(c).edit().putBoolean("notify", on).apply() } + fun notified(c: Context): Set = prefs(c).getStringSet("notified", emptySet()).orEmpty() + fun markNotified(c: Context, keys: Collection) { + val keep = (notified(c) + keys).sortedBy { it.substringAfter(':').substringBefore(':').toLongOrNull() ?: 0 }.takeLast(300).toSet() + prefs(c).edit().putStringSet("notified", keep).apply() + } + fun answered(c: Context): Set = prefs(c).getStringSet("answered", emptySet()).orEmpty() fun answer(c: Context, p: Proposal) { val keep = (answered(c) + p.key).sortedBy { it.substringAfter(':').substringBefore(':').toLongOrNull() ?: 0 }.takeLast(300).toSet() @@ -140,18 +210,26 @@ object ActivityDetection { val buckets = (0 until days).flatMap { db.activityBucketDao().byDay(start(today.minusDays(it))) } .map { it.startEpoch to it.steps } val recentHr = hr29.filter { it.first >= from && nights.none { n -> it.first in n } } - val proposals = buildList { - addAll(stepSessions(buckets.filter { it.first >= from })) - if (quiet != null) { - addAll(heartSessions(recentHr, quiet, CardioLoad.hrMax(profile?.age))) - addAll(naps(recentHr, buckets, quiet, zone)) - } + val recentSteps = buckets.filter { it.first >= from } + val hrMax = CardioLoad.hrMax(profile?.age) + val active = stepSessions(recentSteps) + (if (quiet != null) heartSessions(recentHr, quiet, hrMax) else emptyList()) + val learned = learned(c) + // One window per activity, then a guess at what it was (ActivityClassifier). + val activities = mergeActive(active.map { it.startAt..it.endAt }).map { w -> + val f = ActivityClassifier.features(w.first, w.last, recentSteps, recentHr, quiet ?: 60.0, hrMax) + val guesses = ActivityClassifier.rank(f, learned) + val kind = when (guesses.first()) { "walk" -> Proposal.Kind.WALK; "run" -> Proposal.Kind.RUN; else -> Proposal.Kind.CARDIO } + val stepsIn = recentSteps.filter { it.first in w.first until w.last }.sumOf { it.second } + val peak = recentHr.filter { it.first in w.first..w.last }.maxOfOrNull { it.second } + val detail = listOfNotNull( + stepsIn.takeIf { it >= 300 }?.let { tr("%,d steps").format(it) }, + peak?.let { trf("Heart rate up to {0} bpm", it.toInt()) }, + ).joinToString(" · ") + Proposal(kind, w.first, w.last, detail, guesses, f) } + val proposals = activities + (if (quiet != null) naps(recentHr, buckets, quiet, zone) else emptyList()) val sessions = db.activitySessionDao().recent(50).map { it.startedAt..(it.endedAt ?: it.startedAt) } - // A cardio stretch inside a walk or run is the same activity. - val stepRanges = proposals.filter { it.kind == Proposal.Kind.WALK || it.kind == Proposal.Kind.RUN }.map { it.startAt..it.endAt } - val deduped = proposals.filter { p -> p.kind != Proposal.Kind.CARDIO || stepRanges.none { it.first < p.endAt && p.startAt < it.last } } - return open(deduped, sessions + nights, answered(c)).sortedByDescending { it.startAt } + return open(proposals, sessions + nights, answered(c)).sortedByDescending { it.startAt } } } @@ -164,6 +242,8 @@ suspend fun ActivityDetection.accept(c: Context, db: PulseLoopDatabase, p: Activ db.journalDao().upsert(com.pulseloop.data.entity.JournalEntryEntity(com.pulseloop.data.entity.JournalEntryEntity.idFor(day, tag), day, tag)) } else { ManualActivityService.create(db, type, p.startAt, p.minutes.toDouble(), notes = tr("Detected by PulseLoop and confirmed")) + // The answer teaches the next guess for a stretch like this one. + p.features?.let { learn(c, it, type) } } answer(c, p) } diff --git a/app/src/main/java/com/pulseloop/ui/screens/ActivityScreen.kt b/app/src/main/java/com/pulseloop/ui/screens/ActivityScreen.kt index 94b3951c..16664eba 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/ActivityScreen.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/ActivityScreen.kt @@ -947,48 +947,37 @@ private fun DetectedActivityCard() { ) { Text(tr("DETECTED · REVIEW"), style = com.pulseloop.ui.theme.PulseType.eyebrow, color = PulseColors.textSecondary) proposals.forEach { p -> - val defaultType = when (p.kind) { - com.pulseloop.service.ActivityDetection.Proposal.Kind.WALK -> "walk" - com.pulseloop.service.ActivityDetection.Proposal.Kind.RUN -> "run" - else -> "cycle" - } - var type by remember(p.key) { mutableStateOf(defaultType) } + // The classifier's best guess is selected; the rest follow in order of likelihood. + var type by remember(p.key) { mutableStateOf(p.bestType) } val isNap = p.kind == com.pulseloop.service.ActivityDetection.Proposal.Kind.NAP Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { Text( - (if (isNap) tr("Possible nap") else trf("Possible {0}", ActivityMeta.label(type).lowercase())) + " · ${p.minutes} min", + (if (isNap) tr("Possible nap") else com.pulseloop.notifications.ActivityDetectionNotifier.phrase(type)) + " · ${p.minutes} min", fontSize = 16.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.textPrimary, ) Text( "${java.time.Instant.ofEpochMilli(p.startAt).atZone(zone).format(fmt)} · ${p.detail}", fontSize = 12.sp, color = PulseColors.textMuted, ) - if (p.kind == com.pulseloop.service.ActivityDetection.Proposal.Kind.CARDIO) { + if (!isNap) { FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { - listOf("cycle", "gym", "sport", "dance", "other").forEach { t -> - val on = t == type - Text( - ActivityMeta.label(t), fontSize = 12.sp, - color = if (on) PulseColors.onAccent else PulseColors.textSecondary, - modifier = Modifier.clip(RoundedCornerShape(16.dp)) - .background(if (on) PulseColors.accent else PulseColors.cardSoft) - .clickable(role = Role.Button) { type = t }.padding(horizontal = 12.dp, vertical = 8.dp), + p.guesses.ifEmpty { listOf(p.bestType) }.take(6).forEach { t -> + androidx.compose.material3.FilterChip( + selected = t == type, + onClick = { type = t }, + label = { Text(ActivityMeta.label(t), fontSize = 14.sp) }, ) } } } // FlowRow: the two translated actions wrap rather than overflow the card. - FlowRow(horizontalArrangement = Arrangement.spacedBy(16.dp)) { - Text( - if (isNap) tr("Yes, a nap") else tr("Add workout"), fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.accent, - modifier = Modifier.clickable(role = Role.Button) { - scope.launch { runCatching { com.pulseloop.service.ActivityDetection.accept(context, db, p, type) }; tick++ } - }.padding(vertical = 4.dp), - ) - Text( - tr("Not this"), fontSize = 14.sp, color = PulseColors.textMuted, - modifier = Modifier.clickable(role = Role.Button) { com.pulseloop.service.ActivityDetection.answer(context, p); tick++ }.padding(vertical = 4.dp), - ) + FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + androidx.compose.material3.FilledTonalButton(onClick = { + scope.launch { runCatching { com.pulseloop.service.ActivityDetection.accept(context, db, p, type) }; tick++ } + }) { Text(if (isNap) tr("Yes, a nap") else tr("Add workout"), fontSize = 14.sp, fontWeight = FontWeight.SemiBold) } + TextButton(onClick = { com.pulseloop.service.ActivityDetection.answer(context, p); tick++ }) { + Text(tr("Not this"), fontSize = 14.sp, color = PulseColors.textSecondary) + } } } } diff --git a/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt b/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt index 5164bff4..84a331c4 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt @@ -957,11 +957,44 @@ fun CheckInsSettingsScreen(onBack: () -> Unit) { } } + // Detected workouts (docs/activity-recognition-plan.md): after a sync, what the ring saw. + var detectedEnabled by remember { mutableStateOf(com.pulseloop.service.ActivityDetection.notifyEnabled(context)) } + val detectedPermLauncher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> + detectedEnabled = granted + com.pulseloop.service.ActivityDetection.setNotifyEnabled(context, granted) + } + fun setDetected(on: Boolean) { + if (on && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + detectedPermLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } else { + detectedEnabled = on + com.pulseloop.service.ActivityDetection.setNotifyEnabled(context, on) + } + } + SettingsSubScreen(title = tr("Notifications"), onBack = onBack) { // Guidance and battery alerts work without the coach, so they lead; the coach's own // check-ins come last and explain themselves when the coach is off. DailyGuidanceSettingsCard() + Card(Modifier.fillMaxWidth()) { + Column(Modifier.padding(16.dp)) { + Text(tr("Detected workouts"), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) + Spacer(Modifier.height(4.dp)) + Text( + tr("After a sync, what your ring saw you doing (a walk, a ride, strength training), to add with a tap. Nothing is logged without you."), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Spacer(Modifier.height(12.dp)) + Row(Modifier.fillMaxWidth() + .switchRow(checked = detectedEnabled, onCheckedChange = { setDetected(it) }), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically) { + Text(tr("Detected workout notifications"), Modifier.weight(1f)) + Switch(checked = detectedEnabled, onCheckedChange = null) + } + } + } + // Ring battery alerts — independent of the coach toggle (iOS #61a puts this in its own // NotificationsSettingsView section): a local notification at <20% / <10%, once per day. Card(Modifier.fillMaxWidth()) { diff --git a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt index 4c82af72..03bf3d53 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt @@ -245,6 +245,9 @@ fun TodayScreen( // Accuracy check in progress (docs/accuracy-check-plan.md): say so, since it pauses export. accuracyOpen?.let { c -> item { AccuracyCheckBanner(c) { navController?.navigate("settings/accuracy-check") } } } + // What the ring saw the user doing since the last look (docs/activity-recognition-plan.md): + // one line, opening the Activity tab where it is confirmed, retyped or dismissed. + item { DetectedActivityLine { navController?.navigate("activity") { launchSingleTop = true } } } // The accuracy-check correction note lives in Settings → Accuracy check, not here: // on Today it was a line shown on every visit for something set once. @@ -729,3 +732,39 @@ private fun ColumnScope.NutritionTileBody(food: FoodTotals?, onLog: () -> Unit) Text(tr("+ Log meal"), fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.accent) } } + + +/** The newest detected workout as one line on Today, or nothing. */ +@Composable +private fun DetectedActivityLine(onOpen: () -> Unit) { + val context = androidx.compose.ui.platform.LocalContext.current + val db = remember { com.pulseloop.data.PulseLoopDatabase.getInstance(context) } + val newest by androidx.compose.runtime.produceState(null) { + value = runCatching { + kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { + com.pulseloop.service.ActivityDetection.load(context, db, days = 2) + .firstOrNull { it.kind != com.pulseloop.service.ActivityDetection.Proposal.Kind.NAP } + } + }.getOrNull() + } + val p = newest ?: return + val time = remember(p.startAt) { + java.time.Instant.ofEpochMilli(p.startAt).atZone(java.time.ZoneId.systemDefault()) + .format(java.time.format.DateTimeFormatter.ofPattern("EEE HH:mm")) + } + Row( + Modifier.fillMaxWidth().heightIn(min = 48.dp).clip(androidx.compose.foundation.shape.RoundedCornerShape(8.dp)) + .background(PulseColors.card).clickable(role = Role.Button, onClickLabel = tr("Review"), onClick = onOpen) + .padding(horizontal = 16.dp, vertical = 12.dp), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(8.dp), + ) { + Box(Modifier.size(8.dp).clip(androidx.compose.foundation.shape.CircleShape).background(PulseColors.steps)) + Text( + com.pulseloop.notifications.ActivityDetectionNotifier.phrase(p.bestType) + " · $time · ${p.minutes} min", + fontSize = 14.sp, color = PulseColors.textPrimary, maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.weight(1f), + ) + com.pulseloop.ui.components.Chevron() + } +} diff --git a/app/src/test/java/com/pulseloop/service/ActivityClassifierTest.kt b/app/src/test/java/com/pulseloop/service/ActivityClassifierTest.kt new file mode 100644 index 00000000..6e660d3d --- /dev/null +++ b/app/src/test/java/com/pulseloop/service/ActivityClassifierTest.kt @@ -0,0 +1,85 @@ +package com.pulseloop.service + +import com.pulseloop.service.ActivityClassifier.Example +import com.pulseloop.service.ActivityClassifier.Features +import org.junit.Assert.assertEquals +import org.junit.Test + +/** Guessing what a stretch of movement was, from cadence and heart-rate shape. */ +class ActivityClassifierTest { + private fun f(minutes: Double = 40.0, cadence: Double = 0.0, burst: Double = 0.0, mean: Double = 0.0, peak: Double = 0.0, cv: Double = 0.0) = + Features(minutes, cadence, burst, mean, peak, cv) + + @Test + fun cadenceSeparatesRunsFromWalks() { + assertEquals("run", ActivityClassifier.rank(f(cadence = 155.0, mean = 0.6, peak = 0.75, cv = 0.05)).first()) + assertEquals("walk", ActivityClassifier.rank(f(cadence = 95.0, mean = 0.2, peak = 0.3, cv = 0.05)).first()) + } + + @Test + fun aSteadyRaisedHeartWithFewStepsIsARide() { + assertEquals("cycle", ActivityClassifier.rank(f(minutes = 60.0, cadence = 4.0, mean = 0.55, peak = 0.65, cv = 0.05)).first()) + } + + @Test + fun anUnevenHeartWithBurstsOfStepsIsStrength() { + assertEquals("gym", ActivityClassifier.rank(f(minutes = 45.0, cadence = 12.0, burst = 1.3, mean = 0.3, peak = 0.5, cv = 0.16)).first()) + } + + @Test + fun highPeaksWithDeepDropsAreIntervalsOrAGame() { + assertEquals("sport", ActivityClassifier.rank(f(minutes = 50.0, cadence = 45.0, mean = 0.5, peak = 0.9, cv = 0.22)).first()) + } + + @Test + fun everyTypeIsOfferedOnce() { + val ranked = ActivityClassifier.rank(f(cadence = 20.0)) + assertEquals(ActivityClassifier.TYPES.toSet(), ranked.toSet()) + assertEquals(ActivityClassifier.TYPES.size, ranked.size) + } + + @Test + fun theUsersOwnAnswersOutweighTheRules() { + // The rules call this a ride; this user has said "dance" three times for stretches like it. + val stretch = f(minutes = 60.0, cadence = 4.0, mean = 0.55, peak = 0.65, cv = 0.05) + val learned = List(3) { Example(stretch.copy(minutes = 55.0 + it), "dance") } + assertEquals("dance", ActivityClassifier.rank(stretch, learned).first()) + // Answers for very different stretches don't move the guess. + val far = List(3) { Example(f(cadence = 160.0, mean = 0.8), "dance") } + assertEquals("cycle", ActivityClassifier.rank(stretch, far).first()) + } + + @Test + fun featuresReadCadenceBurstinessAndHeartShape() { + val t0 = 0L + val steps = listOf(0 to 300, 5 to 0, 10 to 300, 15 to 0).map { (m, s) -> (t0 + m * 60_000L) to s } + val hr = listOf(100.0, 140.0, 100.0, 140.0).mapIndexed { i, b -> (t0 + i * 5 * 60_000L) to b } + val x = ActivityClassifier.features(t0, t0 + 20 * 60_000L, steps, hr, quiet = 60.0, hrMax = 180.0) + assertEquals(30.0, x.cadence, 0.01) + assertEquals(1.0, x.stepBurst, 0.01) + assertEquals((120.0 - 60.0) / 120.0, x.hrMeanPct, 0.01) + assertEquals(20.0 / 120.0, x.hrCv, 0.01) + } + + @Test + fun touchingStretchesAreOneActivity() { + val m = 60_000L + val merged = ActivityDetection.mergeActive(listOf(0L..30 * m, 35 * m..60 * m, 90 * m..120 * m)) + assertEquals(listOf(0L..60 * m, 90 * m..120 * m), merged) + } + + @Test + fun aDismissedStretchStaysDismissedWhenItsBoundsShift() { + val m = 60_000L + val first = ActivityDetection.Proposal(ActivityDetection.Proposal.Kind.CARDIO, 0, 40 * m, "") + val redetected = ActivityDetection.Proposal(ActivityDetection.Proposal.Kind.WALK, 5 * m, 50 * m, "") + assertEquals(0, ActivityDetection.open(listOf(redetected), emptyList(), setOf(first.key)).size) + } + + @Test + fun learnedAnswersRoundTrip() { + val ex = listOf(Example(f(minutes = 45.0, cadence = 12.0, burst = 1.3, mean = 0.3, peak = 0.5, cv = 0.16), "gym")) + assertEquals(ex, ActivityDetection.decodeLearned(ActivityDetection.encodeLearned(ex))) + assertEquals(emptyList(), ActivityDetection.decodeLearned("garbage\ngym,1,2")) + } +} diff --git a/docs/activity-recognition-plan.md b/docs/activity-recognition-plan.md new file mode 100644 index 00000000..32cbc0ad --- /dev/null +++ b/docs/activity-recognition-plan.md @@ -0,0 +1,82 @@ +# Activity recognition: what the ring saw you doing + +**Goal.** After a sync, and when the app opens, the app says what the user was probably doing, +so it can be logged with a tap. Examples: "Looks like strength training · 45 min at 18:10", or +"Looks like a ride". The model is Oura's automatic activity detection: a stretch of movement is +noticed, a type is guessed, and the user confirms, changes the type, or dismisses it. + +## What we have to work with + +The rings PulseLoop supports don't expose raw accelerometer data. Detection has to work from +what every family syncs: + +| Signal | Resolution | Source | +|---|---|---| +| Steps | buckets of 1–15 min (per family) | `activity_buckets` | +| Heart rate | all-day grid, usually 5 min, denser during spot measures | `measurements` (HEART_RATE, real rows) | +| Quiet HR / HRmax | per user | `CardioLoad.quietLevel`, `CardioLoad.hrMax(age)` | +| Nights | per night | sleep sessions (excluded from detection) | + +What that can and can't tell apart: + +- **Walking vs running:** cadence. Reliable. +- **Cycling:** heart rate held up for a long stretch, with few steps and a steady line. Usually + right; a rowing machine or an elliptical looks the same. +- **Strength training:** heart rate up but uneven (sets and rests), few steps, often short bursts + of steps between sets. The 5-minute HR grid blurs the sets, so on its own this is the weakest + call. +- **High-intensity intervals:** high peaks with deep drops between them. +- **Everything else** (sport, dance, yoga) can't be told apart from these signals. The user + chooses, and the app learns. + +That is why the app always asks and never logs on its own. It is also why the user's own answers +are the strongest signal it has. + +## Phase 1: shipped first + +1. **Guess the type** (`service/ActivityClassifier.kt`). + - **Features of a stretch:** minutes, cadence (steps per minute), how bursty the steps are, + mean and peak heart rate as a share of heart-rate reserve, and how uneven the heart rate is + (coefficient of variation). + - **Rule scores** for walk, run, cycle, gym (strength), high-intensity and other. Every type + is offered, best first. + - **Learning:** each confirmed suggestion stores its features with the type the user chose + (`activity_detection` prefs, `learned`, newest 200, carried in the backup). Once there are + a few, the nearest earlier answers vote, and an answer close to this stretch outweighs the + rules. Someone who always lifts at 18:00 gets "Gym" first, even when the rules lean + towards cycling. +2. **One candidate per stretch.** Step stretches and raised-heart-rate stretches merge into one + window, so a run is not also offered as "cardio". +3. **After every sync, a notification.** `ActivityDetectionTrigger` listens for the sync's + `done` event, runs detection, and posts one notification per new suggestion. It only + notifies for activity that ended within the last 12 hours, and only once per suggestion. + Tapping it opens the Activity tab. The trigger has its own channel and its own switch + (Settings → Notifications → Detected workouts). +4. **When the app opens, a line on Today.** It names the newest suggestion ("Looks like strength + training · 18:10 · 45 min") and opens the Activity tab. There it is the same "Detected" card + as before, with the guessed type selected and the other types offered in order of + likelihood. +5. **Rules kept from the first version:** nothing is ever logged without a tap, "Not this" is + remembered, and a stretch that overlaps a recorded workout or a night is never offered. + +## Phase 2: workouts the ring recorded itself + +Several families keep their own exercise records when the user starts a sport on the ring, or +when the ring auto-detects one: Colmi's sport records (`0x77`/`0x78` sessions), YCBT sport +history, CRP movement records. Their type comes from the ring, so it beats any guess. Import them +as suggestions that carry the ring's type and its own HR and distance figures. Match the vendor +app's decode for each family; per AGENTS.md, don't guess opcodes. + +## Phase 3: sharper heart rate during a stretch + +When a stretch looks active, request the ring's finer HR history for that window where a family +offers one, or temporarily raise the all-day measuring interval. Sets and rests then become +visible, and the strength call stops depending on a 5-minute grid. This needs per-family protocol +work and hardware validation with the ring owners. + +## Not planned + +- **Logging automatically.** A wrong workout is worse than a missed one: it lands in Health + Connect and Strava. +- **A population-trained model.** We have no labelled dataset, and the user's own answers are + the better one.