diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 4738131c..5e4a8f29 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -22,6 +22,8 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true" > + + @@ -74,7 +76,7 @@ - + @@ -83,7 +85,8 @@ + android:exported="true" + android:foregroundServiceType="mediaPlayback"> diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/DeviceClasses.kt b/android/app/src/main/java/com/audiobookshelf/app/data/DeviceClasses.kt index fd9ea633..7f4d4780 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/DeviceClasses.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/DeviceClasses.kt @@ -158,8 +158,8 @@ data class DeviceSettings( data class DeviceData( var serverConnectionConfigs:MutableList, var lastServerConnectionConfigId:String?, - var currentLocalPlaybackSession: PlaybackSession?, // Stored to open up where left off for local media. TODO: Old - var deviceSettings: DeviceSettings? + var deviceSettings: DeviceSettings?, + var lastPlaybackSession: PlaybackSession? ) { @JsonIgnore fun getLastServerConnectionConfig(): ServerConnectionConfig? { diff --git a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt index bd7b5f58..57245e19 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt @@ -67,4 +67,9 @@ object DeviceManager { } return false } + + fun setLastPlaybackSession(playbackSession:PlaybackSession) { + deviceData.lastPlaybackSession = playbackSession + dbManager.saveDeviceData(deviceData) + } } diff --git a/android/app/src/main/java/com/audiobookshelf/app/managers/DbManager.kt b/android/app/src/main/java/com/audiobookshelf/app/managers/DbManager.kt index be201946..e681eb91 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/managers/DbManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/managers/DbManager.kt @@ -22,7 +22,7 @@ class DbManager { } fun getDeviceData(): DeviceData { - return Paper.book("device").read("data") ?: DeviceData(mutableListOf(), null, null, DeviceSettings.default()) + return Paper.book("device").read("data") ?: DeviceData(mutableListOf(), null, DeviceSettings.default(), null) } fun saveDeviceData(deviceData: DeviceData) { Paper.book("device").write("data", deviceData) diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/MediaSessionCallback.kt b/android/app/src/main/java/com/audiobookshelf/app/player/MediaSessionCallback.kt index 12e20357..222bd5f6 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/MediaSessionCallback.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/MediaSessionCallback.kt @@ -8,6 +8,7 @@ import android.util.Log import android.view.KeyEvent import com.audiobookshelf.app.data.LibraryItemWrapper import com.audiobookshelf.app.data.PodcastEpisode +import com.audiobookshelf.app.device.DeviceManager import java.util.* import kotlin.concurrent.schedule @@ -157,21 +158,40 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi Log.d(tag, "handleCallMediaButton keyEvent = $keyEvent | action ${keyEvent?.action}") - // TODO: Widget was only sending this event on key down - // but this cannot be defined in both key down and key up -// if (keyEvent?.action == KeyEvent.ACTION_DOWN) { -// Log.d(tag, "handleCallMediaButton: key action_down for ${keyEvent.keyCode}") -// when (keyEvent.keyCode) { -// KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { -// Log.d(tag, "handleCallMediaButton: Media Play/Pause") -// if (playerNotificationService.mPlayer.isPlaying) { -// playerNotificationService.pause() -// } else { -// playerNotificationService.play() + // Widget button intent is only sending the action down event + if (keyEvent?.action == KeyEvent.ACTION_DOWN) { + Log.d(tag, "handleCallMediaButton: key action_down for ${keyEvent.keyCode}") + when (keyEvent.keyCode) { + KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { + Log.d(tag, "handleCallMediaButton: Media Play/Pause") + + // TODO: Play/pause event sent from widget when app is closed. Currently the service gets destroyed before anything can happen +// if (playerNotificationService.currentPlaybackSession == null && DeviceManager.deviceData.lastPlaybackSession != null) { +// Log.i(tag, "No playback session but had one in the db") +// +// val connectionConfig = DeviceManager.deviceData.serverConnectionConfigs.find { it.id == DeviceManager.deviceData.lastPlaybackSession?.serverConnectionConfigId } +// connectionConfig?.let { +// Log.i(tag, "Setting playback session from db $it") +// DeviceManager.serverConnectionConfig = it +// +// playerNotificationService.currentPlaybackSession = DeviceManager.deviceData.lastPlaybackSession +// playerNotificationService.startNewPlaybackSession() +// return true +// } // } -// } -// } -// } + + if (playerNotificationService.mPlayer.isPlaying) { + if (0 == mediaButtonClickCount) playerNotificationService.pause() + handleMediaButtonClickCount() + } else { + if (0 == mediaButtonClickCount) { + playerNotificationService.play() + } + handleMediaButtonClickCount() + } + } + } + } if (keyEvent?.action == KeyEvent.ACTION_UP) { Log.d(tag, "handleCallMediaButton: key action_up for ${keyEvent.keyCode}") @@ -213,18 +233,6 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi KeyEvent.KEYCODE_MEDIA_STOP -> { playerNotificationService.closePlayback() } - KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { - Log.d(tag, "handleCallMediaButton: Media Play/Pause") - if (playerNotificationService.mPlayer.isPlaying) { - if (0 == mediaButtonClickCount) playerNotificationService.pause() - handleMediaButtonClickCount() - } else { - if (0 == mediaButtonClickCount) { - playerNotificationService.play() - } - handleMediaButtonClickCount() - } - } else -> { Log.d(tag, "KeyCode:${keyEvent.keyCode}") return false diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt index 079a1c56..117a350b 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt @@ -407,6 +407,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { } currentPlaybackSession = playbackSession + DeviceManager.setLastPlaybackSession(playbackSession) // Save playback session to use when app is closed + Log.d(tag, "Set CurrentPlaybackSession MediaPlayer ${currentPlaybackSession?.mediaPlayer}") clientEventEmitter?.onPlaybackSession(playbackSession) @@ -542,8 +544,10 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { } } - private fun startNewPlaybackSession() { + fun startNewPlaybackSession() { currentPlaybackSession?.let { playbackSession -> + Log.i(tag, "Starting new playback session for ${playbackSession.displayTitle}") + val forceTranscode = playbackSession.isHLS // If already HLS then force val playItemRequestPayload = getPlayItemRequestPayload(forceTranscode) @@ -910,7 +914,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { // // MEDIA BROWSER STUFF (ANDROID AUTO) // - private val VALID_MEDIA_BROWSERS = mutableListOf("com.audiobookshelf.app", ANDROID_AUTO_PKG_NAME, ANDROID_AUTO_SIMULATOR_PKG_NAME, ANDROID_WEARABLE_PKG_NAME, ANDROID_GSEARCH_PKG_NAME, ANDROID_AUTOMOTIVE_PKG_NAME) + private val VALID_MEDIA_BROWSERS = mutableListOf("com.audiobookshelf.app", "com.audiobookshelf.app.debug", "com.android.systemui", ANDROID_AUTO_PKG_NAME, ANDROID_AUTO_SIMULATOR_PKG_NAME, ANDROID_WEARABLE_PKG_NAME, ANDROID_GSEARCH_PKG_NAME, ANDROID_AUTOMOTIVE_PKG_NAME) private val AUTO_MEDIA_ROOT = "/" private val LIBRARIES_ROOT = "__LIBRARIES__" diff --git a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt index 598a21f9..3121af6f 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt @@ -51,7 +51,6 @@ class AbsDownloader : Plugin() { folderScanner = FolderScanner(mainActivity) apiHandler = ApiHandler(mainActivity) downloadItemManager = DownloadItemManager(downloadManager, folderScanner, mainActivity, clientEventEmitter) - Log.d(tag, "Build SDK ${Build.VERSION.SDK_INT}") } @PluginMethod