Set up for android widget

This commit is contained in:
advplyr 2023-02-19 11:14:30 -06:00
parent 750c370692
commit 28989f536a
7 changed files with 53 additions and 34 deletions

View file

@ -22,6 +22,8 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true" > android:usesCleartextTraffic="true" >
<!-- TODO: Incomplete desktop widget -->
<!-- <receiver--> <!-- <receiver-->
<!-- android:name=".NewAppWidget"--> <!-- android:name=".NewAppWidget"-->
<!-- android:exported="true" >--> <!-- android:exported="true" >-->
@ -74,7 +76,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<receiver android:name="androidx.media.session.MediaButtonReceiver" android:exported="false"> <receiver android:name="androidx.media.session.MediaButtonReceiver" android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" /> <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter> </intent-filter>
@ -83,7 +85,8 @@
<service <service
android:name=".player.PlayerNotificationService" android:name=".player.PlayerNotificationService"
android:enabled="true" android:enabled="true"
android:exported="true" > android:exported="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter> <intent-filter>
<action android:name="android.media.browse.MediaBrowserService" /> <action android:name="android.media.browse.MediaBrowserService" />
</intent-filter> </intent-filter>

View file

@ -158,8 +158,8 @@ data class DeviceSettings(
data class DeviceData( data class DeviceData(
var serverConnectionConfigs:MutableList<ServerConnectionConfig>, var serverConnectionConfigs:MutableList<ServerConnectionConfig>,
var lastServerConnectionConfigId:String?, 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 @JsonIgnore
fun getLastServerConnectionConfig(): ServerConnectionConfig? { fun getLastServerConnectionConfig(): ServerConnectionConfig? {

View file

@ -67,4 +67,9 @@ object DeviceManager {
} }
return false return false
} }
fun setLastPlaybackSession(playbackSession:PlaybackSession) {
deviceData.lastPlaybackSession = playbackSession
dbManager.saveDeviceData(deviceData)
}
} }

View file

@ -22,7 +22,7 @@ class DbManager {
} }
fun getDeviceData(): DeviceData { 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) { fun saveDeviceData(deviceData: DeviceData) {
Paper.book("device").write("data", deviceData) Paper.book("device").write("data", deviceData)

View file

@ -8,6 +8,7 @@ import android.util.Log
import android.view.KeyEvent import android.view.KeyEvent
import com.audiobookshelf.app.data.LibraryItemWrapper import com.audiobookshelf.app.data.LibraryItemWrapper
import com.audiobookshelf.app.data.PodcastEpisode import com.audiobookshelf.app.data.PodcastEpisode
import com.audiobookshelf.app.device.DeviceManager
import java.util.* import java.util.*
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
@ -157,22 +158,41 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi
Log.d(tag, "handleCallMediaButton keyEvent = $keyEvent | action ${keyEvent?.action}") Log.d(tag, "handleCallMediaButton keyEvent = $keyEvent | action ${keyEvent?.action}")
// TODO: Widget was only sending this event on key down // Widget button intent is only sending the action down event
// but this cannot be defined in both key down and key up if (keyEvent?.action == KeyEvent.ACTION_DOWN) {
// if (keyEvent?.action == KeyEvent.ACTION_DOWN) { Log.d(tag, "handleCallMediaButton: key action_down for ${keyEvent.keyCode}")
// Log.d(tag, "handleCallMediaButton: key action_down for ${keyEvent.keyCode}") when (keyEvent.keyCode) {
// when (keyEvent.keyCode) { KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {
// KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { Log.d(tag, "handleCallMediaButton: Media Play/Pause")
// Log.d(tag, "handleCallMediaButton: Media Play/Pause")
// if (playerNotificationService.mPlayer.isPlaying) { // TODO: Play/pause event sent from widget when app is closed. Currently the service gets destroyed before anything can happen
// playerNotificationService.pause() // if (playerNotificationService.currentPlaybackSession == null && DeviceManager.deviceData.lastPlaybackSession != null) {
// } else { // Log.i(tag, "No playback session but had one in the db")
// playerNotificationService.play() //
// } // 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) { if (keyEvent?.action == KeyEvent.ACTION_UP) {
Log.d(tag, "handleCallMediaButton: key action_up for ${keyEvent.keyCode}") Log.d(tag, "handleCallMediaButton: key action_up for ${keyEvent.keyCode}")
when (keyEvent.keyCode) { when (keyEvent.keyCode) {
@ -213,18 +233,6 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi
KeyEvent.KEYCODE_MEDIA_STOP -> { KeyEvent.KEYCODE_MEDIA_STOP -> {
playerNotificationService.closePlayback() 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 -> { else -> {
Log.d(tag, "KeyCode:${keyEvent.keyCode}") Log.d(tag, "KeyCode:${keyEvent.keyCode}")
return false return false

View file

@ -407,6 +407,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
} }
currentPlaybackSession = playbackSession currentPlaybackSession = playbackSession
DeviceManager.setLastPlaybackSession(playbackSession) // Save playback session to use when app is closed
Log.d(tag, "Set CurrentPlaybackSession MediaPlayer ${currentPlaybackSession?.mediaPlayer}") Log.d(tag, "Set CurrentPlaybackSession MediaPlayer ${currentPlaybackSession?.mediaPlayer}")
clientEventEmitter?.onPlaybackSession(playbackSession) clientEventEmitter?.onPlaybackSession(playbackSession)
@ -542,8 +544,10 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
} }
} }
private fun startNewPlaybackSession() { fun startNewPlaybackSession() {
currentPlaybackSession?.let { playbackSession -> currentPlaybackSession?.let { playbackSession ->
Log.i(tag, "Starting new playback session for ${playbackSession.displayTitle}")
val forceTranscode = playbackSession.isHLS // If already HLS then force val forceTranscode = playbackSession.isHLS // If already HLS then force
val playItemRequestPayload = getPlayItemRequestPayload(forceTranscode) val playItemRequestPayload = getPlayItemRequestPayload(forceTranscode)
@ -910,7 +914,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
// //
// MEDIA BROWSER STUFF (ANDROID AUTO) // 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 AUTO_MEDIA_ROOT = "/"
private val LIBRARIES_ROOT = "__LIBRARIES__" private val LIBRARIES_ROOT = "__LIBRARIES__"

View file

@ -51,7 +51,6 @@ class AbsDownloader : Plugin() {
folderScanner = FolderScanner(mainActivity) folderScanner = FolderScanner(mainActivity)
apiHandler = ApiHandler(mainActivity) apiHandler = ApiHandler(mainActivity)
downloadItemManager = DownloadItemManager(downloadManager, folderScanner, mainActivity, clientEventEmitter) downloadItemManager = DownloadItemManager(downloadManager, folderScanner, mainActivity, clientEventEmitter)
Log.d(tag, "Build SDK ${Build.VERSION.SDK_INT}")
} }
@PluginMethod @PluginMethod