From cddbe72752a54dae00b8452c436fd87c9ee7cfd2 Mon Sep 17 00:00:00 2001 From: tzugen Date: Thu, 18 Nov 2021 19:07:18 +0100 Subject: [PATCH] Remove network communication from main thread --- .../moire/ultrasonic/fragment/PlayerFragment.kt | 16 +++++++++------- .../ultrasonic/service/MediaPlayerController.kt | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt index e671a940..7556d148 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt @@ -43,7 +43,6 @@ import java.text.DateFormat import java.text.SimpleDateFormat import java.util.ArrayList import java.util.Date -import java.util.LinkedList import java.util.Locale import java.util.concurrent.CancellationException import java.util.concurrent.Executors @@ -116,6 +115,7 @@ class PlayerFragment : private var currentPlaying: DownloadFile? = null private var currentSong: MusicDirectory.Entry? = null private var rxBusSubscription: Disposable? = null + private var ioScope = CoroutineScope(Dispatchers.IO) // Views and UI Elements private lateinit var visualizerViewLayout: LinearLayout @@ -260,6 +260,7 @@ class PlayerFragment : val incrementTime = Settings.incrementTime changeProgress(incrementTime) } + pauseButton.setOnClickListener { launch(CommunicationError.getHandler(context)) { mediaPlayerController.pause() @@ -267,6 +268,7 @@ class PlayerFragment : onSliderProgressChanged() } } + stopButton.setOnClickListener { launch(CommunicationError.getHandler(context)) { mediaPlayerController.reset() @@ -389,8 +391,8 @@ class PlayerFragment : onPlaylistChanged() } - // Query the Jukebox state off-thread - launch(CommunicationError.getHandler(context)) { + // Query the Jukebox state in an IO Context + ioScope.launch(CommunicationError.getHandler(context)) { try { jukeboxAvailable = mediaPlayerController.isJukeboxAvailable } catch (all: Exception) { @@ -782,10 +784,10 @@ class PlayerFragment : Util.toast(context, resources.getString(R.string.download_playlist_saving, playlistName)) mediaPlayerController.suggestedPlaylistName = playlistName - launch { - val entries: MutableList = LinkedList() - for (downloadFile in mediaPlayerController.playList) { - entries.add(downloadFile.song) + ioScope.launch { + + val entries = mediaPlayerController.playList.map { + it.song } val musicService = getMusicService() musicService.createPlaylist(null, playlistName, entries) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt index 05e80330..e3322213 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -413,6 +413,10 @@ class MediaPlayerController( } } + /** + * This function calls the music service directly and + * therefore can't be called from the main thread + */ val isJukeboxAvailable: Boolean get() { try {