DownloaderConnection.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.files.downloader
  21. import android.content.Context
  22. import android.content.Intent
  23. import android.os.IBinder
  24. import com.nextcloud.client.account.User
  25. import com.nextcloud.client.core.LocalConnection
  26. import com.owncloud.android.datamodel.OCFile
  27. import java.util.UUID
  28. class DownloaderConnection(context: Context, val user: User) : LocalConnection<DownloaderService>(context), Downloader {
  29. private var downloadListeners: MutableSet<(Transfer) -> Unit> = mutableSetOf()
  30. private var statusListeners: MutableSet<(Downloader.Status) -> Unit> = mutableSetOf()
  31. private var binder: DownloaderService.Binder? = null
  32. private val downloadsRequiringStatusRedelivery: MutableSet<UUID> = mutableSetOf()
  33. override val isRunning: Boolean
  34. get() = binder?.isRunning ?: false
  35. override val status: Downloader.Status
  36. get() = binder?.status ?: Downloader.Status.EMPTY
  37. override fun getDownload(uuid: UUID): Transfer? = binder?.getDownload(uuid)
  38. override fun getDownload(file: OCFile): Transfer? = binder?.getDownload(file)
  39. override fun download(request: Request) {
  40. val intent = DownloaderService.createDownloadIntent(context, request)
  41. context.startService(intent)
  42. if (!isConnected && downloadListeners.size > 0) {
  43. downloadsRequiringStatusRedelivery.add(request.uuid)
  44. }
  45. }
  46. override fun registerDownloadListener(listener: (Transfer) -> Unit) {
  47. downloadListeners.add(listener)
  48. binder?.registerDownloadListener(listener)
  49. }
  50. override fun removeDownloadListener(listener: (Transfer) -> Unit) {
  51. downloadListeners.remove(listener)
  52. binder?.removeDownloadListener(listener)
  53. }
  54. override fun registerStatusListener(listener: (Downloader.Status) -> Unit) {
  55. statusListeners.add(listener)
  56. binder?.registerStatusListener(listener)
  57. }
  58. override fun removeStatusListener(listener: (Downloader.Status) -> Unit) {
  59. statusListeners.remove(listener)
  60. binder?.removeStatusListener(listener)
  61. }
  62. override fun createBindIntent(): Intent {
  63. return DownloaderService.createBindIntent(context, user)
  64. }
  65. override fun onBound(binder: IBinder) {
  66. super.onBound(binder)
  67. this.binder = binder as DownloaderService.Binder
  68. downloadListeners.forEach { listener ->
  69. binder.registerDownloadListener(listener)
  70. }
  71. statusListeners.forEach { listener ->
  72. binder.registerStatusListener(listener)
  73. }
  74. deliverMissedUpdates()
  75. }
  76. /**
  77. * Since binding and download start are both asynchronous and the order
  78. * is not guaranteed, some downloads might already finish when service is bound,
  79. * resulting in lost notifications.
  80. *
  81. * Deliver all updates for pending downloads that were scheduled
  82. * before service has been bound.
  83. */
  84. private fun deliverMissedUpdates() {
  85. val downloadUpdates = downloadsRequiringStatusRedelivery.mapNotNull { uuid ->
  86. binder?.getDownload(uuid)
  87. }
  88. downloadListeners.forEach { listener ->
  89. downloadUpdates.forEach { update ->
  90. listener.invoke(update)
  91. }
  92. }
  93. downloadsRequiringStatusRedelivery.clear()
  94. if (statusListeners.isNotEmpty()) {
  95. binder?.status?.let { status ->
  96. statusListeners.forEach { it.invoke(status) }
  97. }
  98. }
  99. }
  100. override fun onUnbind() {
  101. super.onUnbind()
  102. downloadListeners.forEach { binder?.removeDownloadListener(it) }
  103. statusListeners.forEach { binder?.removeStatusListener(it) }
  104. }
  105. }