Handle WifiLock in Download manager class, instead of creating an individual lock per task.

Also only stop the executor when done.
This commit is contained in:
tzugen 2021-09-12 13:45:00 +02:00
parent 5ff4d21abc
commit 611539be55
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 36 additions and 21 deletions

View File

@ -1,5 +1,6 @@
package org.moire.ultrasonic.service
import android.net.wifi.WifiManager
import java.util.ArrayList
import java.util.PriorityQueue
import java.util.concurrent.Executors
@ -11,6 +12,7 @@ import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.PlayerState
import org.moire.ultrasonic.util.LRUCache
import org.moire.ultrasonic.util.ShufflePlayBuffer
import org.moire.ultrasonic.util.Util
import org.moire.ultrasonic.util.Util.getMaxSongs
import org.moire.ultrasonic.util.Util.getPreloadCount
import org.moire.ultrasonic.util.Util.isExternalStoragePresent
@ -39,27 +41,21 @@ class Downloader(
private val downloadFileCache = LRUCache<MusicDirectory.Entry, DownloadFile>(100)
private var executorService: ScheduledExecutorService? = null
private var wifiLock: WifiManager.WifiLock? = null
var playlistUpdateRevision: Long = 0
private set
val downloadChecker = Runnable {
try {
Timber.w("checking Downloads")
Timber.w("Checking Downloads")
checkDownloadsInternal()
} catch (all: Exception) {
Timber.e(all, "checkDownloads() failed.")
}
}
fun onCreate() {
executorService = Executors.newSingleThreadScheduledExecutor()
executorService!!.scheduleWithFixedDelay(
downloadChecker, CHECK_INTERVAL, CHECK_INTERVAL, TimeUnit.SECONDS
)
Timber.i("Downloader created")
}
fun onDestroy() {
stop()
clearPlaylist()
@ -67,16 +63,39 @@ class Downloader(
Timber.i("Downloader destroyed")
}
fun start() {
if (executorService == null) {
executorService = Executors.newSingleThreadScheduledExecutor()
executorService!!.scheduleWithFixedDelay(
downloadChecker, 0L, CHECK_INTERVAL, TimeUnit.SECONDS
)
Timber.i("Downloader started")
}
if (wifiLock == null) {
wifiLock = Util.createWifiLock(toString())
wifiLock?.acquire()
}
}
fun stop() {
if (executorService != null) executorService!!.shutdown()
executorService?.shutdown()
executorService = null
wifiLock?.release()
wifiLock = null
Timber.i("Downloader stopped")
}
fun checkDownloads() {
executorService?.execute(downloadChecker)
if (executorService == null || executorService!!.isTerminated) {
start()
} else {
executorService?.execute(downloadChecker)
}
}
@Synchronized
@Suppress("ComplexMethod")
fun checkDownloadsInternal() {
if (!isExternalStoragePresent() || !externalStorageMonitor.isExternalStorageAvailable) {
return
@ -126,6 +145,11 @@ class Downloader(
localMediaPlayer.setNextPlayerState(PlayerState.DOWNLOADING)
}
}
// Stop Executor service when done downloading
if (activelyDownloading.size == 0) {
stop()
}
}
private fun startDownloadOnService(task: DownloadFile) {

View File

@ -28,10 +28,7 @@ import org.moire.ultrasonic.util.Util
import timber.log.Timber
/**
* This class represents a singe Song or Video that can be downloaded.
*
* @author Sindre Mehus
* @version $Id$
* This class represents a single Song or Video that can be downloaded.
*/
class DownloadFile(
val song: MusicDirectory.Entry,
@ -204,11 +201,7 @@ class DownloadFile(
override fun execute() {
var inputStream: InputStream? = null
var outputStream: FileOutputStream? = null
var wifiLock: WifiLock? = null
try {
wifiLock = Util.createWifiLock(toString())
wifiLock.acquire()
if (saveFile.exists()) {
Timber.i("%s already exists. Skipping.", saveFile)
return
@ -300,7 +293,6 @@ class DownloadFile(
} finally {
Util.close(inputStream)
Util.close(outputStream)
wifiLock?.release()
CacheCleaner().cleanSpace()
downloader.checkDownloads()
}

View File

@ -81,7 +81,6 @@ class MediaPlayerService : Service() {
override fun onCreate() {
super.onCreate()
downloader.onCreate()
shufflePlayBuffer.onCreate()
localMediaPlayer.init()