Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ class MainActivity : AppCompatActivity() {
viewModel.appStart(intent)
}

internal fun handleRemotePlaybackIntent(intent: Intent) {
viewModel.appStart(intent)
}

fun changeDisplayMode(modeId: Int) {
lifecycleScope.launch(WholphinDispatchers.Main + ExceptionHandler(autoToast = true)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class IntentService
const val INTENT_SEASON_ID = "seaId"
const val INTENT_SERVER_ID = "serverId"
const val INTENT_USER_ID = "userId"
const val INTENT_POSITION = "position"

const val ACTION_PLAYBACK = "com.github.damontecres.wholphin.PLAYBACK"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.damontecres.wholphin.services

import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.github.damontecres.wholphin.MainActivity
import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.JellyfinServer
import com.github.damontecres.wholphin.data.model.JellyfinUser
Expand All @@ -22,12 +24,14 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.jellyfin.sdk.api.client.ApiClient
import org.jellyfin.sdk.api.client.extensions.sessionApi
import org.jellyfin.sdk.api.sockets.subscribe
import org.jellyfin.sdk.model.api.GeneralCommandMessage
import org.jellyfin.sdk.model.api.GeneralCommandType
import org.jellyfin.sdk.model.api.MediaType
import org.jellyfin.sdk.model.api.PlayMessage
import org.jellyfin.sdk.model.api.UserUpdatedMessage
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -86,6 +90,29 @@ class ServerEventListener
try {
// Launch multiple listeners, but stop all if one fails
coroutineScope {
api.webSocket
.subscribe<PlayMessage>()
.onEach { message ->
val intent = message.toPlaybackIntent()
if (intent == null) {
Timber.w("Ignoring remote play message with no item IDs")
} else {
val itemId = intent.getStringExtra(IntentService.INTENT_ITEM_ID)
Timber.i(
"Received remote play command %s for item %s",
message.data?.playCommand,
itemId,
)
activity.lifecycleScope.launch {
(activity as? MainActivity)?.handleRemotePlaybackIntent(
intent,
)
}
}
}.catch { ex ->
Timber.e(ex, "Error in remote play websocket subscription")
}.launchIn(this@coroutineScope)

api.webSocket
.subscribe<GeneralCommandMessage>()
.onEach { message ->
Expand Down Expand Up @@ -140,6 +167,20 @@ class ServerEventListener
}
}

private fun PlayMessage.toPlaybackIntent(): Intent? {
val request = data ?: return null
val itemIds = request.itemIds.orEmpty()
val startIndex = (request.startIndex ?: 0).coerceAtLeast(0)
val itemId = itemIds.getOrNull(startIndex) ?: itemIds.firstOrNull() ?: return null

return Intent(IntentService.ACTION_PLAYBACK).apply {
putExtra(IntentService.INTENT_ITEM_ID, itemId.toString())
request.startPositionTicks
?.coerceAtLeast(0L)
?.let { putExtra(IntentService.INTENT_POSITION, it / 10_000L) }
}
}

override fun onResume(owner: LifecycleOwner) {
serverRepository.current.value?.let { init(it.server, it.user) }
}
Expand Down