Only remove manager when first start failed

This commit is contained in:
Mygod
2019-02-05 17:48:23 +08:00
parent 89a4e9f071
commit 6920843a63

View File

@@ -25,7 +25,7 @@ class TetheringService : IpNeighbourMonitoringService() {
fun isActive(iface: String): Boolean = synchronized(downstreams) { downstreams.containsKey(iface) } fun isActive(iface: String): Boolean = synchronized(downstreams) { downstreams.containsKey(iface) }
} }
inner class Downstream(caller: Any, downstream: String) : private inner class Downstream(caller: Any, downstream: String) :
RoutingManager(caller, downstream, TetherType.ofInterface(downstream).isWifi) { RoutingManager(caller, downstream, TetherType.ofInterface(downstream).isWifi) {
override fun Routing.configure() { override fun Routing.configure() {
forward() forward()
@@ -43,12 +43,12 @@ class TetheringService : IpNeighbourMonitoringService() {
synchronized(downstreams) { synchronized(downstreams) {
for (iface in downstreams.keys - TetheringManager.getTetheredIfaces(extras)) for (iface in downstreams.keys - TetheringManager.getTetheredIfaces(extras))
downstreams.remove(iface)?.stop() downstreams.remove(iface)?.stop()
updateRoutingsLocked() onDownstreamsChangedLocked()
} }
} }
override val activeIfaces get() = synchronized(downstreams) { downstreams.keys.toList() } override val activeIfaces get() = synchronized(downstreams) { downstreams.keys.toList() }
private fun updateRoutingsLocked() { private fun onDownstreamsChangedLocked() {
if (downstreams.isEmpty()) { if (downstreams.isEmpty()) {
unregisterReceiver() unregisterReceiver()
ServiceNotification.stopForeground(this) ServiceNotification.stopForeground(this)
@@ -59,15 +59,6 @@ class TetheringService : IpNeighbourMonitoringService() {
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
IpNeighbourMonitor.registerCallback(this) IpNeighbourMonitor.registerCallback(this)
} }
val iterator = downstreams.iterator()
while (iterator.hasNext()) {
val downstream = iterator.next().value
if (downstream.routing == null && !downstream.initRouting()) iterator.remove()
}
if (downstreams.isEmpty()) {
updateRoutingsLocked()
return
}
updateNotification() updateNotification()
} }
GlobalScope.launch(Dispatchers.Main) { binder.routingsChanged() } GlobalScope.launch(Dispatchers.Main) { binder.routingsChanged() }
@@ -79,22 +70,21 @@ class TetheringService : IpNeighbourMonitoringService() {
if (intent != null) { if (intent != null) {
val ifaces = intent.getStringArrayExtra(EXTRA_ADD_INTERFACES) ?: emptyArray() val ifaces = intent.getStringArrayExtra(EXTRA_ADD_INTERFACES) ?: emptyArray()
synchronized(downstreams) { synchronized(downstreams) {
for (iface in ifaces) { for (iface in ifaces) Downstream(this, iface).let { downstream ->
Downstream(this, iface).run { if (downstream.initRouting()) downstreams[iface] = downstream else downstream.stop()
downstreams[iface] = this
initRouting()
}
} }
downstreams.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.stop() downstreams.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.stop()
updateRoutingsLocked() onDownstreamsChangedLocked()
} }
} else if (downstreams.isEmpty()) stopSelf(startId) } else if (downstreams.isEmpty()) stopSelf(startId)
return START_NOT_STICKY return START_NOT_STICKY
} }
override fun onDestroy() { override fun onDestroy() {
downstreams.values.forEach { it.stop() } // force clean to prevent leakage synchronized(downstreams) {
unregisterReceiver() downstreams.values.forEach { it.stop() } // force clean to prevent leakage
unregisterReceiver()
}
super.onDestroy() super.onDestroy()
} }