mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-01 08:07:20 +02:00
Don't queue display events while backgrounded
When we are backgrounded, don't queue display events. This prevents a hang and sluggish replay of display events once we are re-foregrounded later
This commit is contained in:
@@ -527,4 +527,18 @@ constructor(private val playerNotificationService: PlayerNotificationService) {
|
|||||||
|
|
||||||
checkAutoSleepTimer()
|
checkAutoSleepTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the current sleep timer state to the client.
|
||||||
|
* Called when app resumes from background to sync UI state.
|
||||||
|
*/
|
||||||
|
fun sendCurrentSleepTimerState() {
|
||||||
|
if (sleepTimerRunning) {
|
||||||
|
val timeRemaining = getSleepTimerTimeRemainingSeconds(getPlaybackSpeed())
|
||||||
|
playerNotificationService.clientEventEmitter?.onSleepTimerSet(timeRemaining, isAutoSleepTimer)
|
||||||
|
} else {
|
||||||
|
// No timer running - send 0 to clear any stale UI state
|
||||||
|
playerNotificationService.clientEventEmitter?.onSleepTimerSet(0, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ class AbsAudioPlayer : Plugin() {
|
|||||||
|
|
||||||
private var isCastAvailable:Boolean = false
|
private var isCastAvailable:Boolean = false
|
||||||
|
|
||||||
|
// Track foreground state to avoid flooding WebView with events while backgrounded
|
||||||
|
private var isInForeground: Boolean = true
|
||||||
|
|
||||||
override fun load() {
|
override fun load() {
|
||||||
mainActivity = (activity as MainActivity)
|
mainActivity = (activity as MainActivity)
|
||||||
apiHandler = ApiHandler(mainActivity)
|
apiHandler = ApiHandler(mainActivity)
|
||||||
@@ -60,6 +63,8 @@ class AbsAudioPlayer : Plugin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onMetadata(metadata: PlaybackMetadata) {
|
override fun onMetadata(metadata: PlaybackMetadata) {
|
||||||
|
// Skip frequent metadata updates when app is backgrounded to prevent event queue buildup
|
||||||
|
if (!isInForeground) return
|
||||||
notifyListeners("onMetadata", JSObject(jacksonMapper.writeValueAsString(metadata)))
|
notifyListeners("onMetadata", JSObject(jacksonMapper.writeValueAsString(metadata)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +73,8 @@ class AbsAudioPlayer : Plugin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onSleepTimerSet(sleepTimeRemaining: Int, isAutoSleepTimer:Boolean) {
|
override fun onSleepTimerSet(sleepTimeRemaining: Int, isAutoSleepTimer:Boolean) {
|
||||||
|
// Skip sleep timer updates when app is backgrounded to prevent event queue buildup
|
||||||
|
if (!isInForeground) return
|
||||||
val ret = JSObject()
|
val ret = JSObject()
|
||||||
ret.put("value", sleepTimeRemaining)
|
ret.put("value", sleepTimeRemaining)
|
||||||
ret.put("isAuto", isAutoSleepTimer)
|
ret.put("isAuto", isAutoSleepTimer)
|
||||||
@@ -118,6 +125,26 @@ class AbsAudioPlayer : Plugin() {
|
|||||||
notifyListeners(evtName, ret)
|
notifyListeners(evtName, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun handleOnPause() {
|
||||||
|
super.handleOnPause()
|
||||||
|
isInForeground = false
|
||||||
|
Log.d(tag, "App paused - disabling frequent event emission")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun handleOnResume() {
|
||||||
|
super.handleOnResume()
|
||||||
|
isInForeground = true
|
||||||
|
Log.d(tag, "App resumed - enabling event emission and sending current state")
|
||||||
|
|
||||||
|
// Send current state to UI after resume to sync up
|
||||||
|
if (::playerNotificationService.isInitialized && playerNotificationService.currentPlaybackSession != null) {
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
playerNotificationService.sendClientMetadata(PlayerState.READY)
|
||||||
|
playerNotificationService.sleepTimerManager.sendCurrentSleepTimerState()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun initCastManager() {
|
private fun initCastManager() {
|
||||||
val googleApi = GoogleApiAvailability.getInstance()
|
val googleApi = GoogleApiAvailability.getInstance()
|
||||||
val statusCode = googleApi.isGooglePlayServicesAvailable(mainActivity)
|
val statusCode = googleApi.isGooglePlayServicesAvailable(mainActivity)
|
||||||
|
|||||||
Reference in New Issue
Block a user