Skip to content

Commit

Permalink
Fix service leak IntentReceiver.
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Nov 16, 2024
1 parent a6b7758 commit 78d98b2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class AniTorrentService : LifecycleService(), CoroutineScope {

override fun onDestroy() {
logger.info { "AniTorrentService is stopping." }
meteredNetworkDetector.dispose()
val engine = kotlin.runCatching { anitorrent.getCompleted() }.getOrNull() ?: return
runBlocking(Dispatchers.IO_) {
val downloader = engine.getDownloader()
Expand All @@ -205,7 +206,6 @@ class AniTorrentService : LifecycleService(), CoroutineScope {
}
// cancel lifecycle scope
this.cancel()
meteredNetworkDetector.dispose()
// release wake lock if held
if (wakeLock.isHeld) {
wakeLock.release()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import me.him188.ani.utils.logging.logger

@SuppressLint("MissingPermission")
private class AndroidMeteredNetworkDetector(
context: Context
private val context: Context
) : MeteredNetworkDetector, BroadcastReceiver() {
private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val logger by lazy { logger<AndroidMeteredNetworkDetector>() }

private val flow = MutableStateFlow(getCurrentIsMetered())
override val isMeteredNetworkFlow: Flow<Boolean> get() = flow

private val unregisterReceiver = { context.unregisterReceiver(this) }

// Create a NetworkCallback to detect network changes
// private val networkCallback = object : ConnectivityManager.NetworkCallback() {
Expand Down Expand Up @@ -82,7 +80,7 @@ private class AndroidMeteredNetworkDetector(
override fun dispose() {
// Unregister the network callback when no longer needed
// connectivityManager.unregisterNetworkCallback(networkCallback)
unregisterReceiver()
context.unregisterReceiver(this)
}

private inline fun log(message: () -> String) {
Expand Down

0 comments on commit 78d98b2

Please sign in to comment.