2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.activities
|
2020-06-13 16:09:48 +02:00
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-06-25 01:26:15 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-06-13 16:09:48 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2021-07-16 10:03:52 +02:00
|
|
|
import audio.funkwhale.ffa.adapters.DownloadsAdapter
|
|
|
|
import audio.funkwhale.ffa.databinding.ActivityDownloadsBinding
|
|
|
|
import audio.funkwhale.ffa.utils.Event
|
|
|
|
import audio.funkwhale.ffa.utils.EventBus
|
|
|
|
import audio.funkwhale.ffa.utils.getMetadata
|
2020-06-14 14:59:50 +02:00
|
|
|
import com.google.android.exoplayer2.offline.Download
|
2021-08-09 06:50:46 +02:00
|
|
|
import com.google.android.exoplayer2.offline.DownloadManager
|
2020-06-25 01:26:15 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Default
|
2020-06-13 16:09:48 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2020-06-22 22:24:34 +02:00
|
|
|
import kotlinx.coroutines.delay
|
2020-06-13 16:09:48 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2020-06-14 14:59:50 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.java.KoinJavaComponent.inject
|
2020-06-13 16:09:48 +02:00
|
|
|
|
|
|
|
class DownloadsActivity : AppCompatActivity() {
|
2021-07-16 10:03:52 +02:00
|
|
|
|
|
|
|
private lateinit var adapter: DownloadsAdapter
|
|
|
|
private lateinit var binding: ActivityDownloadsBinding
|
2021-08-09 06:50:46 +02:00
|
|
|
private val exoDownloadManager: DownloadManager by inject(DownloadManager::class.java)
|
2020-06-13 16:09:48 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding = ActivityDownloadsBinding.inflate(layoutInflater)
|
2020-06-13 16:09:48 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
setContentView(binding.root)
|
2020-06-22 22:24:34 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.downloads.itemAnimator = null
|
|
|
|
|
|
|
|
adapter = DownloadsAdapter(layoutInflater, this, DownloadChangedListener()).also {
|
2020-06-22 22:24:34 +02:00
|
|
|
it.setHasStableIds(true)
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.downloads.layoutManager = LinearLayoutManager(this)
|
|
|
|
binding.downloads.adapter = it
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
2020-06-22 22:24:34 +02:00
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Default) {
|
2020-06-22 22:24:34 +02:00
|
|
|
while (true) {
|
2020-06-22 22:25:03 +02:00
|
|
|
delay(1000)
|
2020-06-22 22:24:34 +02:00
|
|
|
refreshProgress()
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
}
|
2020-06-13 16:09:48 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Default) {
|
2020-06-14 14:59:50 +02:00
|
|
|
EventBus.get().collect { event ->
|
|
|
|
if (event is Event.DownloadChanged) {
|
|
|
|
refreshTrack(event.download)
|
|
|
|
}
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
|
|
|
|
refresh()
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun refresh() {
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2020-06-22 21:48:31 +02:00
|
|
|
adapter.downloads.clear()
|
2022-06-21 10:03:46 +02:00
|
|
|
exoDownloadManager.downloadIndex.getDownloads()
|
|
|
|
.use { cursor ->
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
val download = cursor.download
|
|
|
|
|
|
|
|
download.getMetadata()?.let { info ->
|
|
|
|
adapter.downloads.add(
|
|
|
|
info.apply { this.download = download }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 21:48:31 +02:00
|
|
|
}
|
|
|
|
adapter.notifyDataSetChanged()
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
private suspend fun refreshTrack(download: Download) {
|
2020-06-14 21:15:13 +02:00
|
|
|
download.getMetadata()?.let { info ->
|
2021-07-16 10:03:52 +02:00
|
|
|
adapter.downloads.withIndex().associate { it.value to it.index }
|
|
|
|
.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
|
|
|
|
if (download.state != info.download?.state) {
|
|
|
|
withContext(Main) {
|
|
|
|
adapter.downloads[match.second] = info.apply {
|
|
|
|
this.download = download
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
adapter.notifyItemChanged(match.second)
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:24:34 +02:00
|
|
|
private suspend fun refreshProgress() {
|
2022-06-21 10:03:46 +02:00
|
|
|
exoDownloadManager.downloadIndex.getDownloads()
|
|
|
|
.use { cursor ->
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
val download = cursor.download
|
|
|
|
|
|
|
|
download.getMetadata()?.let { info ->
|
|
|
|
adapter.downloads.withIndex().associate { it.value to it.index }
|
|
|
|
.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
|
|
|
|
if (download.state == Download.STATE_DOWNLOADING
|
|
|
|
&& download.percentDownloaded != (info.download?.percentDownloaded ?: 0)
|
|
|
|
) {
|
|
|
|
withContext(Main) {
|
|
|
|
adapter.downloads[match.second] = info.apply {
|
|
|
|
this.download = download
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.notifyItemChanged(match.second)
|
|
|
|
}
|
2021-07-16 10:03:52 +02:00
|
|
|
}
|
2020-06-22 22:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 10:03:46 +02:00
|
|
|
}
|
2020-06-22 22:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
inner class DownloadChangedListener : DownloadsAdapter.OnDownloadChangedListener {
|
|
|
|
override fun onItemRemoved(index: Int) {
|
|
|
|
adapter.downloads.removeAt(index)
|
|
|
|
adapter.notifyDataSetChanged()
|
2020-06-13 16:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|