Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions android/app/src/main/java/com/audiobookshelf/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.view.ViewGroup
import android.view.WindowInsets
Expand Down Expand Up @@ -110,6 +112,34 @@ class MainActivity : BridgeActivity() {
super.onDestroy()
}

override fun onStart() {
super.onStart()
Log.d(tag, "onStart MainActivity")
// Additional sync point for when activity becomes visible
if (::foregroundService.isInitialized) {
try {
val absAudioPlayer = bridge.getPlugin("AbsAudioPlayer").instance as AbsAudioPlayer
absAudioPlayer.syncCurrentPlaybackStateWhenReady()
} catch (e: Exception) {
Log.e(tag, "Failed to sync playback state on start: ${e.message}")
}
}
}

override fun onResume() {
super.onResume()
Log.d(tag, "onResume MainActivity")
// Trigger UI sync when app comes to foreground, waiting for UI to be ready
if (::foregroundService.isInitialized) {
try {
val absAudioPlayer = bridge.getPlugin("AbsAudioPlayer").instance as AbsAudioPlayer
absAudioPlayer.syncCurrentPlaybackStateWhenReady() // Smart sync that waits for readiness
} catch (e: Exception) {
Log.e(tag, "Failed to sync playback state on resume: ${e.message}")
}
}
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
Log.d(tag, "onPostCreate MainActivity")
Expand All @@ -129,6 +159,25 @@ class MainActivity : BridgeActivity() {

// Let NativeAudio know foreground service is ready and setup event listener
pluginCallback()

// Also trigger UI sync when service connects on activity creation
try {
val absAudioPlayer = bridge.getPlugin("AbsAudioPlayer").instance as AbsAudioPlayer
absAudioPlayer.syncCurrentPlaybackStateWhenReady() // Smart sync that waits for readiness

// Add a delayed fallback sync for fresh installs/updates where timing might be critical
Handler(Looper.getMainLooper()).postDelayed({
try {
Log.d(tag, "Fallback sync attempt after service connection")
absAudioPlayer.syncCurrentPlaybackStateWhenReady()
} catch (e: Exception) {
Log.e(tag, "Fallback sync failed: ${e.message}")
}
}, 3000) // 3 second delay

} catch (e: Exception) {
Log.e(tag, "Failed to sync playback state on service connect: ${e.message}")
}
}
}

Expand Down
Loading