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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import com.metrolist.music.constants.AndroidAutoYouTubePlaylistsKey
import com.metrolist.music.ui.screens.settings.AndroidAutoSection
import com.metrolist.music.ui.screens.settings.deserializeSections
import com.metrolist.music.ui.screens.settings.serializeSections
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.withContext

class MediaLibrarySessionCallback
@Inject
Expand All @@ -74,10 +76,10 @@ constructor(
) : MediaLibrarySession.Callback {
private val scope = CoroutineScope(Dispatchers.Main) + Job()
lateinit var service: MusicService
var toggleLike: () -> Unit = {}
var toggleStartRadio: () -> Unit = {}
var toggleLibrary: () -> Unit = {}
var addToTargetPlaylist: () -> Unit = {}
var toggleLike: suspend () -> Unit = {}
var toggleStartRadio: suspend () -> Unit = {}
var toggleLibrary: suspend () -> Unit = {}
var addToTargetPlaylist: suspend () -> Unit = {}

fun release() {
scope.cancel()
Expand Down Expand Up @@ -107,19 +109,47 @@ constructor(
controller: MediaSession.ControllerInfo,
customCommand: SessionCommand,
args: Bundle,
): ListenableFuture<SessionResult> {
when (customCommand.customAction) {
MediaSessionConstants.ACTION_TOGGLE_LIKE -> toggleLike()
MediaSessionConstants.ACTION_TOGGLE_START_RADIO -> toggleStartRadio()
MediaSessionConstants.ACTION_TOGGLE_LIBRARY -> toggleLibrary()
MediaSessionConstants.ACTION_TOGGLE_SHUFFLE -> session.player.shuffleModeEnabled =
!session.player.shuffleModeEnabled

MediaSessionConstants.ACTION_TOGGLE_REPEAT_MODE -> session.player.toggleRepeatMode()
MediaSessionConstants.ACTION_ADD_TO_TARGET_PLAYLIST -> addToTargetPlaylist()
): ListenableFuture<SessionResult> =
scope.future {
try {
when (customCommand.customAction) {
MediaSessionConstants.ACTION_TOGGLE_LIKE -> toggleLike()
MediaSessionConstants.ACTION_TOGGLE_START_RADIO -> toggleStartRadio()
MediaSessionConstants.ACTION_TOGGLE_LIBRARY -> toggleLibrary()
MediaSessionConstants.ACTION_TOGGLE_SHUFFLE ->
withContext(Dispatchers.Main.immediate) {
session.player.shuffleModeEnabled = !session.player.shuffleModeEnabled
}

MediaSessionConstants.ACTION_TOGGLE_REPEAT_MODE ->
withContext(Dispatchers.Main.immediate) {
session.player.toggleRepeatMode()
}

MediaSessionConstants.ACTION_ADD_TO_TARGET_PLAYLIST -> addToTargetPlaylist()
else -> {
return@future SessionResult(
SessionError(
SessionError.ERROR_BAD_VALUE,
"Unsupported command: ${customCommand.customAction}",
),
)
}
}

SessionResult(SessionResult.RESULT_SUCCESS)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
reportException(e)
SessionResult(
SessionError(
SessionError.ERROR_UNKNOWN,
e.message ?: "Failed to execute command",
),
)
}
}
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}

@Deprecated("Deprecated in MediaLibrarySession.Callback")
override fun onPlaybackResumption(
Expand Down
Loading
Loading