From 3b86d0471ca17dcb0928ad5e038d1969fa283cae Mon Sep 17 00:00:00 2001 From: MiMoHo <37556964+MiMoHo@users.noreply.github.com> Date: Fri, 3 Jul 2026 20:57:09 +0200 Subject: [PATCH] fix: prevent sleep timer freeze and app exit on cancel after re-set The sleep timer was driven by a single TOGGLE_SLEEP_TIMER command whose behavior depended on a service-side isActive flag. Setting the timer twice desynced client intent from the service state: the second set toggled the timer off (zeroing config.sleepInTS), and a subsequent cancel toggled it back on, building a CountDownTimer from a stale, zeroed timestamp. That produced a non-positive duration, firing onFinish() immediately, which posted SleepTimerChanged(0) and made MainActivity call finish(). Split the ambiguous toggle into explicit START_SLEEP_TIMER and STOP_SLEEP_TIMER commands so setting always (re)starts and canceling always stops, removing the isActive flag and the toggle indirection. Closes #345 --- CHANGELOG.md | 3 +++ .../fossify/musicplayer/activities/MainActivity.kt | 4 ++-- .../fossify/musicplayer/playback/CustomCommands.kt | 3 ++- .../musicplayer/playback/MediaSessionCallback.kt | 3 ++- .../org/fossify/musicplayer/playback/SleepTimer.kt | 11 ----------- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a95b2901d..3b4d5528b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fixed the sleep timer freezing and the app closing when the timer was set again and then canceled ([#345]) ## [1.8.1] - 2026-02-14 ### Changed @@ -124,6 +126,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#261]: https://github.com/FossifyOrg/Music-Player/issues/261 [#269]: https://github.com/FossifyOrg/Music-Player/issues/269 [#298]: https://github.com/FossifyOrg/Music-Player/issues/298 +[#345]: https://github.com/FossifyOrg/Music-Player/issues/345 [#361]: https://github.com/FossifyOrg/Music-Player/issues/361 [Unreleased]: https://github.com/FossifyOrg/Music-Player/compare/1.8.1...HEAD diff --git a/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt index 9865320bf..13437365b 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt @@ -481,14 +481,14 @@ class MainActivity : SimpleMusicActivity() { private fun startSleepTimer() { binding.sleepTimerHolder.fadeIn() withPlayer { - sendCommand(CustomCommands.TOGGLE_SLEEP_TIMER) + sendCommand(CustomCommands.START_SLEEP_TIMER) } } private fun stopSleepTimer() { binding.sleepTimerHolder.fadeOut() withPlayer { - sendCommand(CustomCommands.TOGGLE_SLEEP_TIMER) + sendCommand(CustomCommands.STOP_SLEEP_TIMER) } } diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt index 2b01e9347..63418094f 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt @@ -12,7 +12,8 @@ import org.fossify.musicplayer.helpers.PATH enum class CustomCommands(val customAction: String) { CLOSE_PLAYER(customAction = PATH + "CLOSE_PLAYER"), RELOAD_CONTENT(customAction = PATH + "RELOAD_CONTENT"), - TOGGLE_SLEEP_TIMER(customAction = PATH + "TOGGLE_SLEEP_TIMER"), + START_SLEEP_TIMER(customAction = PATH + "START_SLEEP_TIMER"), + STOP_SLEEP_TIMER(customAction = PATH + "STOP_SLEEP_TIMER"), SET_NEXT_ITEM(customAction = PATH + "SET_NEXT_ITEM"), SET_SHUFFLE_ORDER(customAction = PATH + "SET_SHUFFLE_ORDER"); diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt index 82f589ba7..8dc635c09 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt @@ -74,7 +74,8 @@ internal fun PlaybackService.getMediaSessionCallback() = object : MediaLibrarySe when (command) { CustomCommands.CLOSE_PLAYER -> stopService() CustomCommands.RELOAD_CONTENT -> reloadContent() - CustomCommands.TOGGLE_SLEEP_TIMER -> toggleSleepTimer() + CustomCommands.START_SLEEP_TIMER -> startSleepTimer() + CustomCommands.STOP_SLEEP_TIMER -> stopSleepTimer() CustomCommands.SET_SHUFFLE_ORDER -> setShuffleOrder(args) CustomCommands.SET_NEXT_ITEM -> setNextItem(args) } diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt index c468dd514..cfbb1a711 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt @@ -5,17 +5,8 @@ import org.fossify.musicplayer.extensions.config import org.fossify.musicplayer.models.Events import org.greenrobot.eventbus.EventBus -private var isActive = false private var sleepTimer: CountDownTimer? = null -internal fun PlaybackService.toggleSleepTimer() { - if (isActive) { - stopSleepTimer() - } else { - startSleepTimer() - } -} - internal fun PlaybackService.startSleepTimer() { val millisInFuture = config.sleepInTS - System.currentTimeMillis() + 1000L sleepTimer?.cancel() @@ -34,12 +25,10 @@ internal fun PlaybackService.startSleepTimer() { } sleepTimer?.start() - isActive = true } internal fun PlaybackService.stopSleepTimer() { sleepTimer?.cancel() sleepTimer = null - isActive = false config.sleepInTS = 0 }