funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/playback/PinService.kt

103 lines
3.3 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.playback
2020-06-10 16:25:20 +02:00
import android.app.Notification
import android.content.Context
import android.content.Intent
import android.net.Uri
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
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import audio.funkwhale.ffa.FFA
import audio.funkwhale.ffa.R
import audio.funkwhale.ffa.utils.AppContext
import audio.funkwhale.ffa.utils.DownloadInfo
import audio.funkwhale.ffa.utils.Event
import audio.funkwhale.ffa.utils.EventBus
import audio.funkwhale.ffa.utils.Request
import audio.funkwhale.ffa.utils.RequestBus
import audio.funkwhale.ffa.utils.Response
import audio.funkwhale.ffa.utils.Track
import audio.funkwhale.ffa.utils.mustNormalizeUrl
2021-07-02 13:55:49 +02:00
import java.util.Collections
2020-06-10 16:25:20 +02:00
class PinService : DownloadService(AppContext.NOTIFICATION_DOWNLOADS) {
private val scope: CoroutineScope = CoroutineScope(Job() + Main)
companion object {
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
2021-07-02 13:55:49 +02:00
DownloadRequest(
url,
DownloadRequest.TYPE_PROGRESSIVE,
Uri.parse(url),
Collections.emptyList(),
null,
data
).also {
sendAddDownload(context, PinService::class.java, it, false)
}
}
}
2020-06-10 16:25:20 +02:00
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
buildResumeDownloadsIntent(this, PinService::class.java, true)
scope.launch(Main) {
RequestBus.get().collect { request ->
when (request) {
is Request.GetDownloads -> request.channel?.offer(Response.Downloads(getDownloads()))
}
}
}
return super.onStartCommand(intent, flags, startId)
}
override fun getDownloadManager() = audio.funkwhale.ffa.FFA.get().exoDownloadManager.apply {
addListener(DownloadListener())
}
2020-06-10 16:25:20 +02:00
override fun getScheduler(): Scheduler? = null
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)
2021-07-02 13:55:49 +02:00
return DownloadNotificationHelper(
this,
AppContext.NOTIFICATION_CHANNEL_DOWNLOADS
).buildProgressNotification(R.drawable.downloads, null, description, downloads)
}
private fun getDownloads() = downloadManager.downloadIndex.getDownloads()
inner class DownloadListener : DownloadManager.Listener {
override fun onDownloadChanged(downloadManager: DownloadManager, download: Download) {
super.onDownloadChanged(downloadManager, download)
2020-06-10 16:25:20 +02:00
EventBus.send(Event.DownloadChanged(download))
}
}
2021-07-02 13:55:49 +02:00
}