Fix wifi lock not released when monitoring

This commit is contained in:
Mygod
2019-11-06 08:35:41 +08:00
parent ef48fe5ec4
commit 4689c9a975
4 changed files with 12 additions and 15 deletions

View File

@@ -158,7 +158,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
receiverRegistered = false receiverRegistered = false
} }
launch { launch {
routingManager?.destroy() routingManager?.stop()
routingManager = null routingManager = null
if (exit) { if (exit) {
cancel() cancel()

View File

@@ -410,7 +410,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
timeoutMonitor?.close() timeoutMonitor?.close()
timeoutMonitor = null timeoutMonitor = null
} }
routingManager?.destroy() routingManager?.stop()
routingManager = null routingManager = null
status = Status.IDLE status = Status.IDLE
ServiceNotification.stopForeground(this) ServiceNotification.stopForeground(this)

View File

@@ -55,9 +55,6 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
val started get() = active[downstream] === this val started get() = active[downstream] === this
private var routing: Routing? = null private var routing: Routing? = null
init {
if (isWifi) WifiDoubleLock.acquire(this)
}
fun start() = when (val other = active.putIfAbsentCompat(downstream, this)) { fun start() = when (val other = active.putIfAbsentCompat(downstream, this)) {
null -> initRouting() null -> initRouting()
@@ -66,6 +63,7 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
} }
private fun initRouting() = try { private fun initRouting() = try {
if (isWifi) WifiDoubleLock.acquire(this)
routing = Routing(caller, downstream).apply { routing = Routing(caller, downstream).apply {
try { try {
configure() configure()
@@ -85,11 +83,9 @@ abstract class RoutingManager(private val caller: Any, val downstream: String, p
protected abstract fun Routing.configure() protected abstract fun Routing.configure()
fun stop() { fun stop() {
if (active.removeCompat(downstream, this)) routing?.revert() if (active.removeCompat(downstream, this)) {
} if (isWifi) WifiDoubleLock.release(this)
routing?.revert()
fun destroy() { }
if (isWifi) WifiDoubleLock.release(this)
stop()
} }
} }

View File

@@ -56,7 +56,8 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
if (downstream.monitor) downstream.start() if (downstream.monitor) downstream.start()
} }
for ((iface, downstream) in toRemove) { for ((iface, downstream) in toRemove) {
if (downstream.monitor) downstream.stop() else downstreams.remove(iface)?.destroy() downstream.stop()
if (!downstream.monitor) check(downstreams.remove(iface, downstream))
} }
onDownstreamsChangedLocked() onDownstreamsChangedLocked()
} }
@@ -87,7 +88,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
if (intent != null) { if (intent != null) {
for (iface in intent.getStringArrayExtra(EXTRA_ADD_INTERFACES) ?: emptyArray()) { for (iface in intent.getStringArrayExtra(EXTRA_ADD_INTERFACES) ?: emptyArray()) {
if (downstreams[iface] == null) Downstream(this@TetheringService, iface).apply { if (downstreams[iface] == null) Downstream(this@TetheringService, iface).apply {
if (start()) check(downstreams.put(iface, this) == null) else destroy() if (start()) check(downstreams.put(iface, this) == null) else stop()
} }
} }
intent.getStringExtra(EXTRA_ADD_INTERFACE_MONITOR)?.also { iface -> intent.getStringExtra(EXTRA_ADD_INTERFACE_MONITOR)?.also { iface ->
@@ -98,7 +99,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
downstreams[iface] = this downstreams[iface] = this
} else downstream.monitor = true } else downstream.monitor = true
} }
intent.getStringExtra(EXTRA_REMOVE_INTERFACE)?.also { downstreams.remove(it)?.destroy() } intent.getStringExtra(EXTRA_REMOVE_INTERFACE)?.also { downstreams.remove(it)?.stop() }
updateNotification() // call this first just in case we are shutting down immediately updateNotification() // call this first just in case we are shutting down immediately
onDownstreamsChangedLocked() onDownstreamsChangedLocked()
} else if (downstreams.isEmpty()) stopSelf(startId) } else if (downstreams.isEmpty()) stopSelf(startId)
@@ -109,7 +110,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
override fun onDestroy() { override fun onDestroy() {
launch { launch {
unregisterReceiver() unregisterReceiver()
downstreams.values.forEach { it.destroy() } // force clean to prevent leakage downstreams.values.forEach { it.stop() } // force clean to prevent leakage
cancel() cancel()
dispatcher.close() dispatcher.close()
} }