Revert "Ensure receiver unregistered synchronously in onDestroy"

This reverts commit e55aa17399.
This commit is contained in:
Mygod
2019-08-19 23:49:09 +08:00
parent 5e87244041
commit 4aece2204a
3 changed files with 37 additions and 31 deletions

View File

@@ -10,7 +10,6 @@ import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.util.Event0
import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.ensureReceiverUnregistered
import kotlinx.coroutines.*
import java.util.concurrent.ConcurrentHashMap
@@ -47,6 +46,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
override val coroutineContext = dispatcher + Job()
private val binder = Binder()
private val downstreams = ConcurrentHashMap<String, Downstream>()
private var receiverRegistered = false
private val receiver = broadcastReceiver { _, intent ->
launch {
val toRemove = downstreams.toMutableMap() // make a copy
@@ -69,8 +69,11 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
ServiceNotification.stopForeground(this)
stopSelf()
} else {
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
IpNeighbourMonitor.registerCallback(this)
if (!receiverRegistered) {
receiverRegistered = true
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
IpNeighbourMonitor.registerCallback(this)
}
updateNotification()
}
launch(Dispatchers.Main) { binder.routingsChanged() }
@@ -103,9 +106,9 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
}
override fun onDestroy() {
unregisterReceiver()
launch {
downstreams.values.forEach { it.destroy() } // force clean to prevent leakage
unregisterReceiver()
cancel()
dispatcher.close()
}
@@ -113,7 +116,10 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
}
private fun unregisterReceiver() {
ensureReceiverUnregistered(receiver)
IpNeighbourMonitor.unregisterCallback(this)
if (receiverRegistered) {
unregisterReceiver(receiver)
IpNeighbourMonitor.unregisterCallback(this)
receiverRegistered = false
}
}
}