Incremented max playlist size

Fixed DownloadService start
Minor fixes
This commit is contained in:
Nite 2022-04-24 08:44:36 +02:00
parent 707339b88b
commit 46846bd5c9
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
4 changed files with 24 additions and 23 deletions

View File

@ -30,7 +30,9 @@ class LegacyPlaylistManager : KoinComponent {
@JvmField @JvmField
var currentPlaying: DownloadFile? = null var currentPlaying: DownloadFile? = null
private val mediaItemCache = LRUCache<String, DownloadFile>(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<String, DownloadFile>(2000)
val jukeboxMediaPlayer: JukeboxMediaPlayer by inject() val jukeboxMediaPlayer: JukeboxMediaPlayer by inject()
val downloader: Downloader by inject() val downloader: Downloader by inject()

View File

@ -26,6 +26,8 @@ import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.SimpleServiceBinder import org.moire.ultrasonic.util.SimpleServiceBinder
import org.moire.ultrasonic.util.Util import org.moire.ultrasonic.util.Util
import timber.log.Timber 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 * Android Foreground service which is used to download tracks even when the app is not visible
@ -56,6 +58,7 @@ class DownloadService : Service() {
updateNotification() updateNotification()
instance = this instance = this
startedSemaphore.release()
Timber.i("DownloadService initiated") Timber.i("DownloadService initiated")
} }
@ -176,14 +179,11 @@ class DownloadService : Service() {
@Volatile @Volatile
private var instance: DownloadService? = null private var instance: DownloadService? = null
private val instanceLock = Any() private val instanceLock = Any()
private val startedSemaphore: Semaphore = Semaphore(0)
@JvmStatic @JvmStatic
fun getInstance(): DownloadService? { fun getInstance(): DownloadService? {
val context = UApp.applicationContext() 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 if (instance != null) return instance
synchronized(instanceLock) { synchronized(instanceLock) {
if (instance != null) return instance if (instance != null) return instance
@ -194,12 +194,15 @@ class DownloadService : Service() {
} else { } else {
context.startService(Intent(context, DownloadService::class.java)) context.startService(Intent(context, DownloadService::class.java))
} }
Timber.i("DownloadService starting...")
if (startedSemaphore.tryAcquire(10, TimeUnit.SECONDS)) {
Timber.i("DownloadService started") Timber.i("DownloadService started")
}
Util.sleepQuietly(100L)
}
return instance return instance
} }
Timber.w("DownloadService failed to start!")
return null
}
}
@JvmStatic @JvmStatic
val runningInstance: DownloadService? val runningInstance: DownloadService?
@ -208,7 +211,7 @@ class DownloadService : Service() {
} }
@JvmStatic @JvmStatic
fun executeOnStartedMediaPlayerService( fun executeOnStartedDownloadService(
taskToExecute: (DownloadService) -> Unit taskToExecute: (DownloadService) -> Unit
) { ) {

View File

@ -205,7 +205,7 @@ class Downloader(
private fun startDownloadOnService(file: DownloadFile) { private fun startDownloadOnService(file: DownloadFile) {
if (file.isDownloading) return if (file.isDownloading) return
file.prepare() file.prepare()
DownloadService.executeOnStartedMediaPlayerService { DownloadService.executeOnStartedDownloadService {
FileUtil.createDirectoryForParent(file.pinnedFile) FileUtil.createDirectoryForParent(file.pinnedFile)
file.isFailed = false file.isFailed = false
file.downloadTask = DownloadTask(file) file.downloadTask = DownloadTask(file)

View File

@ -347,10 +347,6 @@ class MediaPlayerController(
) { ) {
var insertAt = 0 var insertAt = 0
if (insertionMode == InsertionMode.CLEAR) {
clear()
}
when (insertionMode) { when (insertionMode) {
InsertionMode.CLEAR -> clear() InsertionMode.CLEAR -> clear()
InsertionMode.APPEND -> insertAt = mediaItemCount InsertionMode.APPEND -> insertAt = mediaItemCount