Merge pull request #1464 from nichwall/sleep_timer_cleanup_2

Android Sleep Timer cleanup part 2
This commit is contained in:
advplyr 2025-02-02 16:04:52 -06:00 committed by GitHub
commit 02b83f02d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -264,7 +264,7 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
} }
/** /**
* Gets the chapter end time for use in End of Chapter timers. If less than 2 seconds remain in * Gets the chapter end time for use in End of Chapter timers. If less than 10 seconds remain in
* the chapter, then use the next chapter. * the chapter, then use the next chapter.
* @return Long? - the chapter end time in milliseconds, or null if there is no current session. * @return Long? - the chapter end time in milliseconds, or null if there is no current session.
*/ */
@ -276,8 +276,11 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
} }
val timeLeftInChapter = currentChapterEndTimeMs - getCurrentTime() val timeLeftInChapter = currentChapterEndTimeMs - getCurrentTime()
return if (timeLeftInChapter < 2000L) { // If less than 10 seconds remain in the chapter, set the timer to the next chapter or track
Log.i(tag, "Getting chapter sleep timer time and current chapter has less than 2s remaining") // This handles the auto-rewind from not playing media for a little bit to select the next
// chapter
return if (timeLeftInChapter < 10000L) {
Log.i(tag, "Getting chapter sleep timer time and current chapter has less than 10s remaining")
val nextChapterEndTimeMs = playerNotificationService.getEndTimeOfNextChapterOrTrack() val nextChapterEndTimeMs = playerNotificationService.getEndTimeOfNextChapterOrTrack()
if (nextChapterEndTimeMs == null || currentChapterEndTimeMs == nextChapterEndTimeMs) { if (nextChapterEndTimeMs == null || currentChapterEndTimeMs == nextChapterEndTimeMs) {
Log.e( Log.e(
@ -332,6 +335,14 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
return return
} }
// If timer was cleared by going negative on time, clear the sleep timer length so pressing
// play allows playback to continue without the sleep timer continuously setting for 1 second.
if (sleepTimerLength == 1000L) {
Log.d(tag, "Sleep timer cleared by manually subtracting time, clearing sleep timer")
sleepTimerFinishedAt = 0L
return
}
// Automatically rewind in the book if settings are enabled // Automatically rewind in the book if settings are enabled
tryRewindAutoSleepTimer() tryRewindAutoSleepTimer()
@ -343,9 +354,12 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
} }
} }
/** Handles the shake event to reset the sleep timer. */ /**
* Handles the shake event to reset the sleep timer. Shaking to reset only works during the 2
* minute grace period after the timer ends or while media is playing.
*/
fun handleShake() { fun handleShake() {
if (sleepTimerRunning || sleepTimerFinishedAt > 0L) { if ((sleepTimerRunning && getIsPlaying()) || sleepTimerFinishedAt > 0L) {
if (DeviceManager.deviceData.deviceSettings?.disableShakeToResetSleepTimer == true) { if (DeviceManager.deviceData.deviceSettings?.disableShakeToResetSleepTimer == true) {
Log.d(tag, "Shake to reset sleep timer is disabled") Log.d(tag, "Shake to reset sleep timer is disabled")
return return
@ -460,7 +474,7 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
// Start an auto sleep timer // Start an auto sleep timer
val currentHour = currentCalendar.get(Calendar.HOUR_OF_DAY) val currentHour = currentCalendar.get(Calendar.HOUR_OF_DAY)
val currentMin = currentCalendar.get(Calendar.MINUTE) val currentMin = currentCalendar.get(Calendar.MINUTE)
Log.i(tag, "Starting sleep timer at $currentHour:$currentMin") Log.i(tag, "Starting auto sleep timer at $currentHour:$currentMin")
// Automatically rewind in the book if settings is enabled // Automatically rewind in the book if settings is enabled
tryRewindAutoSleepTimer() tryRewindAutoSleepTimer()