Fix DownloadService leak
This commit is contained in:
@@ -9,6 +9,8 @@ import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -41,7 +43,6 @@ class DownloadService : BaseService() {
|
||||
private val jobs = LinkedHashMap<Int, PausingProgressJob<DownloadState>>()
|
||||
private val jobCount = MutableStateFlow(0)
|
||||
private val controlReceiver = ControlReceiver()
|
||||
private var binder: DownloadBinder? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@@ -50,7 +51,7 @@ class DownloadService : BaseService() {
|
||||
val wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager)
|
||||
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "kotatsu:downloading")
|
||||
downloadManager = get<DownloadManager.Factory>().create(
|
||||
coroutineScope = lifecycleScope + WakeLockNode(wakeLock, TimeUnit.HOURS.toMillis(1))
|
||||
coroutineScope = lifecycleScope + WakeLockNode(wakeLock, TimeUnit.HOURS.toMillis(1)),
|
||||
)
|
||||
DownloadNotification.createChannel(this)
|
||||
val intentFilter = IntentFilter()
|
||||
@@ -75,17 +76,11 @@ class DownloadService : BaseService() {
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
super.onBind(intent)
|
||||
return binder ?: DownloadBinder(this).also { binder = it }
|
||||
}
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
binder = null
|
||||
return super.onUnbind(intent)
|
||||
return DownloadBinder(this)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
unregisterReceiver(controlReceiver)
|
||||
binder = null
|
||||
isRunning = false
|
||||
super.onDestroy()
|
||||
}
|
||||
@@ -126,7 +121,7 @@ class DownloadService : BaseService() {
|
||||
(job.progressValue as? DownloadState.Done)?.let {
|
||||
sendBroadcast(
|
||||
Intent(ACTION_DOWNLOAD_COMPLETE)
|
||||
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false))
|
||||
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false)),
|
||||
)
|
||||
}
|
||||
notificationSwitcher.detach(
|
||||
@@ -135,7 +130,7 @@ class DownloadService : BaseService() {
|
||||
null
|
||||
} else {
|
||||
notification.create(job.progressValue, -1L)
|
||||
}
|
||||
},
|
||||
)
|
||||
stopSelf(startId)
|
||||
}
|
||||
@@ -167,10 +162,25 @@ class DownloadService : BaseService() {
|
||||
}
|
||||
}
|
||||
|
||||
class DownloadBinder(private val service: DownloadService) : Binder() {
|
||||
class DownloadBinder(service: DownloadService) : Binder(), DefaultLifecycleObserver {
|
||||
|
||||
val downloads: Flow<Collection<ProgressJob<DownloadState>>>
|
||||
get() = service.jobCount.mapLatest { service.jobs.values }
|
||||
private var downloadsStateFlow = MutableStateFlow<Collection<PausingProgressJob<DownloadState>>>(emptyList())
|
||||
|
||||
init {
|
||||
service.lifecycle.addObserver(this)
|
||||
service.jobCount.onEach {
|
||||
downloadsStateFlow.value = service.jobs.values
|
||||
}.launchIn(service.lifecycleScope)
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
owner.lifecycle.removeObserver(this)
|
||||
downloadsStateFlow.value = emptyList()
|
||||
super.onDestroy(owner)
|
||||
}
|
||||
|
||||
val downloads
|
||||
get() = downloadsStateFlow.asStateFlow()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user