diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt index 0afaccd2..88d5dc13 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt @@ -30,7 +30,9 @@ class LegacyPlaylistManager : KoinComponent { @JvmField var currentPlaying: DownloadFile? = null - private val mediaItemCache = LRUCache(1000) + // TODO This limits the maximum size of the playlist. + // This will be fixed when this class is refactored and removed + private val mediaItemCache = LRUCache(2000) val jukeboxMediaPlayer: JukeboxMediaPlayer by inject() val downloader: Downloader by inject() diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt index 4f585402..e3c75d54 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt @@ -26,6 +26,8 @@ import org.moire.ultrasonic.util.Constants import org.moire.ultrasonic.util.SimpleServiceBinder import org.moire.ultrasonic.util.Util import timber.log.Timber +import java.util.concurrent.Semaphore +import java.util.concurrent.TimeUnit /** * Android Foreground service which is used to download tracks even when the app is not visible @@ -56,6 +58,7 @@ class DownloadService : Service() { updateNotification() instance = this + startedSemaphore.release() Timber.i("DownloadService initiated") } @@ -176,29 +179,29 @@ class DownloadService : Service() { @Volatile private var instance: DownloadService? = null private val instanceLock = Any() + private val startedSemaphore: Semaphore = Semaphore(0) @JvmStatic fun getInstance(): DownloadService? { val context = UApp.applicationContext() - // Try for twenty times to retrieve a running service, - // sleep 100 millis between each try, - // and run the block that creates a service only synchronized. - for (i in 0..19) { + if (instance != null) return instance + synchronized(instanceLock) { if (instance != null) return instance - synchronized(instanceLock) { - if (instance != null) return instance - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService( - Intent(context, DownloadService::class.java) - ) - } else { - context.startService(Intent(context, DownloadService::class.java)) - } - Timber.i("DownloadService started") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService( + Intent(context, DownloadService::class.java) + ) + } else { + context.startService(Intent(context, DownloadService::class.java)) } - Util.sleepQuietly(100L) + Timber.i("DownloadService starting...") + if (startedSemaphore.tryAcquire(10, TimeUnit.SECONDS)) { + Timber.i("DownloadService started") + return instance + } + Timber.w("DownloadService failed to start!") + return null } - return instance } @JvmStatic @@ -208,7 +211,7 @@ class DownloadService : Service() { } @JvmStatic - fun executeOnStartedMediaPlayerService( + fun executeOnStartedDownloadService( taskToExecute: (DownloadService) -> Unit ) { diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt index f16a2d39..7532c263 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt @@ -205,7 +205,7 @@ class Downloader( private fun startDownloadOnService(file: DownloadFile) { if (file.isDownloading) return file.prepare() - DownloadService.executeOnStartedMediaPlayerService { + DownloadService.executeOnStartedDownloadService { FileUtil.createDirectoryForParent(file.pinnedFile) file.isFailed = false file.downloadTask = DownloadTask(file) 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 1ff9aa0b..a712915b 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -347,10 +347,6 @@ class MediaPlayerController( ) { var insertAt = 0 - if (insertionMode == InsertionMode.CLEAR) { - clear() - } - when (insertionMode) { InsertionMode.CLEAR -> clear() InsertionMode.APPEND -> insertAt = mediaItemCount