Skip to content

feat: shake-to-revive after the sleep timer stops (#1618) - #1696

Open
jphein wants to merge 1 commit into
mainfrom
feat/1618-shake-to-revive
Open

feat: shake-to-revive after the sleep timer stops (#1618)#1696
jphein wants to merge 1 commit into
mainfrom
feat/1618-shake-to-revive

Conversation

@jphein

@jphein jphein commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

When the sleep timer fires and pauses, a quick shake within a short grace window should bring playback back — instead of forcing the user to unlock, find the app, and hit play. Builds on #1595 (the ShakeDetector + 12 m/s² threshold, reused as-is) and #1609 (main-marshalled pause/resume).

Behavior (per the issue)

  • Timer fires + pauses → the accelerometer keeps listening for a 60s grace window (SHAKE_GRACE_WINDOW_MS, tunable const).
  • A qualifying shake in-windowresume from the paused position + re-arm a fresh timer at the same mode that just elapsed.
  • Window elapses with no shake → release the accelerometer (no overnight battery drain).
  • A user-cancelled timer opens no window.

Design

  • SleepTimer.timerFired: SharedFlow<SleepTimerMode>replay = 0 (an event, not state) so a late subscriber can't open a spurious window; emitted at the single fire point (fadeAndPause), carrying the fired mode. cancel() emits nothing. One signal solves fire-vs-cancel and "same duration as last".
  • DefaultPlaybackController.timerFired — a concrete-only val delegating to SleepTimer. The PlaybackController interface is deliberately untouched, so its fakes need no change.
  • StoryvoxPlaybackService — one accelerometer owner (refreshShakeListening()) armed while in the fade tail OR the grace window; shakeGraceJob opens the window on timerFired and releases the sensor after it; onShake revives (resume() on the Main scope per fix(sleep): marshal timer-fire pause to Main — stop Player-wrong-thread crash (#1606) #1609 + startSleepTimer(firedMode)).

CI proves (compile + unit)

Pure decisions shouldListenInGraceWindow / shouldReviveOnShake in SleepTimerDecisions, with JVM cases in SleepTimerAutoArmTest (fire→listen, partway, end-boundary, past-boundary, no-fire).

📱 Device-verify — for JP (sensor + resume are runtime; do NOT merge until checked)

  • Sleep timer stops → shake within ~60s → playback resumes and a fresh timer re-arms at the same duration.
  • Timer stops → wait >60s, then shake → nothing resumes, and the accelerometer is released (no battery drain).
  • Cancel the timer manually (don't let it fire) → a shake does not resume (no grace window opened).
  • Shake-to-extend during the fade tail (bug: shake-to-extend sleep timer not working (reported on-device) #1595) still works (unregression).
  • Shake-to-extend toggle OFF → neither extend nor revive listens.

Closes #1618
Refs #1595 #1609

🤖 Generated with Claude Code

Builds on #1595 (ShakeDetector + 12 m/s² threshold, reused as-is) and #1609
(main-marshalled pause/resume). After the sleep timer fires and pauses, the
accelerometer keeps listening for a ~60s grace window; a qualifying shake
resumes from the paused position and re-arms a fresh timer at the SAME mode
that just elapsed. After the window with no shake, the sensor is released so
a stopped timer never drains battery overnight.

- SleepTimer: new `timerFired: SharedFlow<SleepTimerMode>` (replay=0 event;
  cancel() emits nothing) carrying the fired mode — solves fire-vs-cancel AND
  "same duration as last" in one signal. Emitted at the single fire point
  (fadeAndPause). Concrete class → no fakes affected.
- DefaultPlaybackController: concrete-only `timerFired` val delegating to
  SleepTimer — the PlaybackController interface is untouched, so its fakes
  need no change.
- StoryvoxPlaybackService: one accelerometer owner (`refreshShakeListening`)
  armed for fade-tail OR grace window; `shakeGraceJob` opens/closes the grace
  window on `timerFired`; onShake revives (resume + re-arm mode) when in-window.
- SleepTimerDecisions: pure `shouldListenInGraceWindow` / `shouldReviveOnShake`
  + JVM tests in SleepTimerAutoArmTest (fire→listen, boundary, expiry, no-fire).

CI proves compile + the pure decisions; sensor-listening + resume + re-arm are
runtime → device-verify by JP (shake is physical). green+OPEN for JP.

Device-verify (JP): timer stops → shake within 60s resumes + re-arms same
duration; no shake within 60s → accelerometer released (no drain); user-
cancelled timer opens no window.

Closes #1618
Refs #1595 #1609

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jphein
jphein requested a review from a team as a code owner July 12, 2026 16:30
Copilot AI review requested due to automatic review settings July 12, 2026 16:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@jphein, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 21 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: de3ff011-673d-4007-a312-d8d1dc0c84bb

📥 Commits

Reviewing files that changed from the base of the PR and between 8587b18 and dfcb6d6.

📒 Files selected for processing (5)
  • core-playback/src/main/kotlin/in/jphe/storyvox/playback/PlaybackController.kt
  • core-playback/src/main/kotlin/in/jphe/storyvox/playback/SleepTimer.kt
  • core-playback/src/main/kotlin/in/jphe/storyvox/playback/SleepTimerDecisions.kt
  • core-playback/src/main/kotlin/in/jphe/storyvox/playback/StoryvoxPlaybackService.kt
  • core-playback/src/test/kotlin/in/jphe/storyvox/playback/SleepTimerAutoArmTest.kt
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/1618-shake-to-revive

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a post-stop shake-to-revive grace window (Issue #1618), allowing users to shake their device within 60 seconds after the sleep timer stops to resume playback and re-arm the same timer mode. It consolidates accelerometer management under a single refreshShakeListening function and adds corresponding unit tests. The review comments identify two critical issues with this implementation: first, the accelerometer state flow (shakeJob) does not monitor isPlaying, meaning the sensor might remain active even after manual playback resumes; second, the grace window state is not cleared when the user manually resumes playback or starts a new timer, which could lead to accidental shake gestures overwriting manual actions. Code suggestions are provided to address both issues.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 242 to +247
shakeJob = scope.launch {
controller.state
.map { it.sleepTimerRemainingMs to it.shakeToExtendEnabled }
.distinctUntilChanged()
.collect { (remaining, enabled) ->
// Issue #1595 — pure [shouldListenForShake] gate: listen
// only while the user opted in AND an armed timer is in
// its 10s fade tail.
val inFadeWindow = shouldListenForShake(remaining, enabled, SHAKE_FADE_WINDOW_MS)
if (inFadeWindow && !shakeListening) {
val started = shakeDetector?.start() ?: false
shakeListening = true
Log.i(
TAG,
"Shake-to-extend: fade tail entered (remaining=${remaining}ms) — " +
"accelerometer ${if (started) "registered" else "FAILED to register"}",
)
} else if (!inFadeWindow && shakeListening) {
shakeDetector?.stop()
shakeListening = false
Log.d(TAG, "Shake-to-extend: fade tail exited — accelerometer released")
.collect { refreshShakeListening() }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The shakeJob flow mapping only monitors sleepTimerRemainingMs and shakeToExtendEnabled. When a user manually resumes playback or starts a new timer, isPlaying changes, but this flow does not emit. As a result, refreshShakeListening() is never called, and the accelerometer remains active and listening for shakes even while the app is actively playing.

This can lead to accidental shake-to-revive triggers that overwrite the active playback state or newly started sleep timer if the user shakes the phone within 60 seconds of the last timer stop.

Including isPlaying in the shakeJob flow mapping ensures that any change in playback state correctly triggers refreshShakeListening() to release the accelerometer.

Suggested change
shakeJob = scope.launch {
controller.state
.map { it.sleepTimerRemainingMs to it.shakeToExtendEnabled }
.distinctUntilChanged()
.collect { (remaining, enabled) ->
// Issue #1595 — pure [shouldListenForShake] gate: listen
// only while the user opted in AND an armed timer is in
// its 10s fade tail.
val inFadeWindow = shouldListenForShake(remaining, enabled, SHAKE_FADE_WINDOW_MS)
if (inFadeWindow && !shakeListening) {
val started = shakeDetector?.start() ?: false
shakeListening = true
Log.i(
TAG,
"Shake-to-extend: fade tail entered (remaining=${remaining}ms) — " +
"accelerometer ${if (started) "registered" else "FAILED to register"}",
)
} else if (!inFadeWindow && shakeListening) {
shakeDetector?.stop()
shakeListening = false
Log.d(TAG, "Shake-to-extend: fade tail exited — accelerometer released")
.collect { refreshShakeListening() }
}
shakeJob = scope.launch {
controller.state
.map { Triple(it.sleepTimerRemainingMs, it.shakeToExtendEnabled, it.isPlaying) }
.distinctUntilChanged()
.collect { refreshShakeListening() }
}

Comment on lines +505 to +507
private fun refreshShakeListening() {
val s = controller.state.value
val enabled = s.shakeToExtendEnabled

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The post-stop grace window (sleepTimerFiredAtMs and lastFiredSleepMode) is never cleared when the user manually resumes playback or starts a new sleep timer.

If the user manually resumes playback or starts a new timer, the grace window remains active. A subsequent shake within the 60-second window will still trigger onShake, which will call controller.resume() and controller.startSleepTimer(mode), overwriting the user's manual actions (e.g., overwriting a newly started 30-minute timer with the old EndOfChapter mode).

Clearing the grace window state inside refreshShakeListening() if the app is currently playing or if a new sleep timer is already running prevents this issue.

Suggested change
private fun refreshShakeListening() {
val s = controller.state.value
val enabled = s.shakeToExtendEnabled
private fun refreshShakeListening() {
val s = controller.state.value
if (s.isPlaying || s.sleepTimerRemainingMs != null) {
sleepTimerFiredAtMs = null
lastFiredSleepMode = null
}
val enabled = s.shakeToExtendEnabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(sleep): shake to revive playback after the timer stops (post-stop grace window)

2 participants