Remove network communication from main thread

This commit is contained in:
tzugen 2021-11-18 19:07:18 +01:00
parent 744282f10a
commit cddbe72752
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 13 additions and 7 deletions

View File

@ -43,7 +43,6 @@ import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.ArrayList import java.util.ArrayList
import java.util.Date import java.util.Date
import java.util.LinkedList
import java.util.Locale import java.util.Locale
import java.util.concurrent.CancellationException import java.util.concurrent.CancellationException
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -116,6 +115,7 @@ class PlayerFragment :
private var currentPlaying: DownloadFile? = null private var currentPlaying: DownloadFile? = null
private var currentSong: MusicDirectory.Entry? = null private var currentSong: MusicDirectory.Entry? = null
private var rxBusSubscription: Disposable? = null private var rxBusSubscription: Disposable? = null
private var ioScope = CoroutineScope(Dispatchers.IO)
// Views and UI Elements // Views and UI Elements
private lateinit var visualizerViewLayout: LinearLayout private lateinit var visualizerViewLayout: LinearLayout
@ -260,6 +260,7 @@ class PlayerFragment :
val incrementTime = Settings.incrementTime val incrementTime = Settings.incrementTime
changeProgress(incrementTime) changeProgress(incrementTime)
} }
pauseButton.setOnClickListener { pauseButton.setOnClickListener {
launch(CommunicationError.getHandler(context)) { launch(CommunicationError.getHandler(context)) {
mediaPlayerController.pause() mediaPlayerController.pause()
@ -267,6 +268,7 @@ class PlayerFragment :
onSliderProgressChanged() onSliderProgressChanged()
} }
} }
stopButton.setOnClickListener { stopButton.setOnClickListener {
launch(CommunicationError.getHandler(context)) { launch(CommunicationError.getHandler(context)) {
mediaPlayerController.reset() mediaPlayerController.reset()
@ -389,8 +391,8 @@ class PlayerFragment :
onPlaylistChanged() onPlaylistChanged()
} }
// Query the Jukebox state off-thread // Query the Jukebox state in an IO Context
launch(CommunicationError.getHandler(context)) { ioScope.launch(CommunicationError.getHandler(context)) {
try { try {
jukeboxAvailable = mediaPlayerController.isJukeboxAvailable jukeboxAvailable = mediaPlayerController.isJukeboxAvailable
} catch (all: Exception) { } catch (all: Exception) {
@ -782,10 +784,10 @@ class PlayerFragment :
Util.toast(context, resources.getString(R.string.download_playlist_saving, playlistName)) Util.toast(context, resources.getString(R.string.download_playlist_saving, playlistName))
mediaPlayerController.suggestedPlaylistName = playlistName mediaPlayerController.suggestedPlaylistName = playlistName
launch { ioScope.launch {
val entries: MutableList<MusicDirectory.Entry> = LinkedList()
for (downloadFile in mediaPlayerController.playList) { val entries = mediaPlayerController.playList.map {
entries.add(downloadFile.song) it.song
} }
val musicService = getMusicService() val musicService = getMusicService()
musicService.createPlaylist(null, playlistName, entries) musicService.createPlaylist(null, playlistName, entries)

View File

@ -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 val isJukeboxAvailable: Boolean
get() { get() {
try { try {