Browse Source

Bugfix/122 fix resource leakage

technical/upgrade-exoplayer
Ryan Harg 2 years ago
parent
commit
8f1f565652
  1. 17
      app/src/main/java/audio/funkwhale/ffa/activities/DownloadsActivity.kt
  2. 7
      app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt
  3. 1
      changes/changelog.d/122.bugfix

17
app/src/main/java/audio/funkwhale/ffa/activities/DownloadsActivity.kt

@ -14,7 +14,6 @@ import com.google.android.exoplayer2.offline.DownloadManager
import kotlinx.coroutines.Dispatchers.Default import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.java.KoinJavaComponent.inject import org.koin.java.KoinJavaComponent.inject
@ -65,10 +64,9 @@ class DownloadsActivity : AppCompatActivity() {
private fun refresh() { private fun refresh() {
lifecycleScope.launch(Main) { lifecycleScope.launch(Main) {
val cursor = exoDownloadManager.downloadIndex.getDownloads()
adapter.downloads.clear() adapter.downloads.clear()
exoDownloadManager.downloadIndex.getDownloads()
.use { cursor ->
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val download = cursor.download val download = cursor.download
@ -78,7 +76,7 @@ class DownloadsActivity : AppCompatActivity() {
) )
} }
} }
}
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
} }
@ -101,15 +99,17 @@ class DownloadsActivity : AppCompatActivity() {
} }
private suspend fun refreshProgress() { private suspend fun refreshProgress() {
val cursor = exoDownloadManager.downloadIndex.getDownloads() exoDownloadManager.downloadIndex.getDownloads()
.use { cursor ->
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val download = cursor.download val download = cursor.download
download.getMetadata()?.let { info -> download.getMetadata()?.let { info ->
adapter.downloads.withIndex().associate { it.value to it.index } adapter.downloads.withIndex().associate { it.value to it.index }
.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match -> .filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
if (download.state == Download.STATE_DOWNLOADING && download.percentDownloaded != info.download?.percentDownloaded ?: 0) { if (download.state == Download.STATE_DOWNLOADING
&& download.percentDownloaded != (info.download?.percentDownloaded ?: 0)
) {
withContext(Main) { withContext(Main) {
adapter.downloads[match.second] = info.apply { adapter.downloads[match.second] = info.apply {
this.download = download this.download = download
@ -122,6 +122,7 @@ class DownloadsActivity : AppCompatActivity() {
} }
} }
} }
}
inner class DownloadChangedListener : DownloadsAdapter.OnDownloadChangedListener { inner class DownloadChangedListener : DownloadsAdapter.OnDownloadChangedListener {
override fun onItemRemoved(index: Int) { override fun onItemRemoved(index: Int) {

7
app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt

@ -42,19 +42,18 @@ class TracksRepository(override val context: Context?, albumId: Int) :
companion object { companion object {
fun getDownloadedIds(exoDownloadManager: DownloadManager): List<Int>? { fun getDownloadedIds(exoDownloadManager: DownloadManager): List<Int>? {
val cursor = exoDownloadManager.downloadIndex.getDownloads()
val ids: MutableList<Int> = mutableListOf() val ids: MutableList<Int> = mutableListOf()
exoDownloadManager.downloadIndex.getDownloads()
.use { cursor ->
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val download = cursor.download val download = cursor.download
download.getMetadata()?.let { download.getMetadata()?.let {
if (download.state == Download.STATE_COMPLETED) { if (download.state == Download.STATE_COMPLETED) {
ids.add(it.id) ids.add(it.id)
} }
} }
} }
}
return ids return ids
} }
} }

1
changes/changelog.d/122.bugfix

@ -0,0 +1 @@
Fix leaked database cursor resource
Loading…
Cancel
Save