2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.playback
|
2020-06-10 16:25:20 +02:00
|
|
|
|
|
|
|
import android.app.Notification
|
2020-06-14 14:59:50 +02:00
|
|
|
import android.content.Context
|
2020-06-13 16:09:48 +02:00
|
|
|
import android.content.Intent
|
2021-08-29 15:41:50 +02:00
|
|
|
import androidx.core.net.toUri
|
2021-07-21 09:11:07 +02:00
|
|
|
import audio.funkwhale.ffa.R
|
2021-08-22 09:48:33 +02:00
|
|
|
import audio.funkwhale.ffa.model.DownloadInfo
|
|
|
|
import audio.funkwhale.ffa.model.Track
|
2022-06-17 11:50:39 +02:00
|
|
|
import audio.funkwhale.ffa.utils.*
|
2020-06-14 14:59:50 +02:00
|
|
|
import com.google.android.exoplayer2.offline.Download
|
|
|
|
import com.google.android.exoplayer2.offline.DownloadManager
|
|
|
|
import com.google.android.exoplayer2.offline.DownloadRequest
|
|
|
|
import com.google.android.exoplayer2.offline.DownloadService
|
2020-06-10 16:25:20 +02:00
|
|
|
import com.google.android.exoplayer2.scheduler.Scheduler
|
|
|
|
import com.google.android.exoplayer2.ui.DownloadNotificationHelper
|
2020-06-14 14:59:50 +02:00
|
|
|
import com.google.gson.Gson
|
2020-06-25 01:26:15 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2020-06-13 16:09:48 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2020-06-25 01:26:15 +02:00
|
|
|
import kotlinx.coroutines.Job
|
2020-06-13 16:09:48 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.java.KoinJavaComponent
|
2022-06-17 11:50:39 +02:00
|
|
|
import java.util.*
|
2020-06-10 16:25:20 +02:00
|
|
|
|
|
|
|
class PinService : DownloadService(AppContext.NOTIFICATION_DOWNLOADS) {
|
2021-08-09 06:50:46 +02:00
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
private val scope: CoroutineScope = CoroutineScope(Job() + Main)
|
2021-08-09 06:50:46 +02:00
|
|
|
private val exoDownloadManager: DownloadManager by KoinJavaComponent.inject(DownloadManager::class.java)
|
2020-06-25 01:26:15 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
companion object {
|
2022-06-17 11:50:39 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
fun download(context: Context, track: Track) {
|
|
|
|
track.bestUpload()?.let { upload ->
|
|
|
|
val url = mustNormalizeUrl(upload.listen_url)
|
|
|
|
val data = Gson().toJson(
|
|
|
|
DownloadInfo(
|
|
|
|
track.id,
|
|
|
|
url,
|
|
|
|
track.title,
|
|
|
|
track.artist.name,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
).toByteArray()
|
2020-06-10 16:25:20 +02:00
|
|
|
|
2022-06-17 11:50:39 +02:00
|
|
|
val request = DownloadRequest.Builder(url.toUri().toString(), url.toUri())
|
2021-08-29 15:41:50 +02:00
|
|
|
.setData(data)
|
|
|
|
.setStreamKeys(Collections.emptyList())
|
|
|
|
.build()
|
|
|
|
|
|
|
|
sendAddDownload(context, PinService::class.java, request, false)
|
2020-06-14 14:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:25:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 16:09:48 +02:00
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
|
buildResumeDownloadsIntent(this, PinService::class.java, true)
|
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
scope.launch(Main) {
|
2020-06-13 16:09:48 +02:00
|
|
|
RequestBus.get().collect { request ->
|
|
|
|
when (request) {
|
2021-07-21 09:11:07 +02:00
|
|
|
is Request.GetDownloads -> request.channel?.trySend(Response.Downloads(getDownloads()))?.isSuccess
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onStartCommand(intent, flags, startId)
|
|
|
|
}
|
|
|
|
|
2022-06-17 11:50:39 +02:00
|
|
|
override fun getDownloadManager(): DownloadManager {
|
|
|
|
return exoDownloadManager.apply {
|
|
|
|
addListener(DownloadListener())
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
}
|
2020-06-10 16:25:20 +02:00
|
|
|
|
|
|
|
override fun getScheduler(): Scheduler? = null
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
override fun getForegroundNotification(downloads: MutableList<Download>): Notification {
|
2021-07-02 13:55:49 +02:00
|
|
|
val description =
|
|
|
|
resources.getQuantityString(R.plurals.downloads_description, downloads.size, downloads.size)
|
2020-06-13 17:44:50 +02:00
|
|
|
|
2021-07-02 13:55:49 +02:00
|
|
|
return DownloadNotificationHelper(
|
|
|
|
this,
|
|
|
|
AppContext.NOTIFICATION_CHANNEL_DOWNLOADS
|
2021-08-29 15:41:50 +02:00
|
|
|
).buildProgressNotification(this, R.drawable.downloads, null, description, downloads)
|
2020-06-13 17:44:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
private fun getDownloads() = downloadManager.downloadIndex.getDownloads()
|
2020-06-13 17:44:50 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
inner class DownloadListener : DownloadManager.Listener {
|
2021-08-29 15:41:50 +02:00
|
|
|
|
|
|
|
override fun onDownloadChanged(
|
|
|
|
downloadManager: DownloadManager,
|
|
|
|
download: Download,
|
|
|
|
finalException: Exception?
|
|
|
|
) {
|
|
|
|
super.onDownloadChanged(downloadManager, download, finalException)
|
2020-06-10 16:25:20 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
EventBus.send(Event.DownloadChanged(download))
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|