From 6920843a632f634b71665bccb25f8b3d33d49dfc Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 5 Feb 2019 17:48:23 +0800 Subject: [PATCH] Only remove manager when first start failed --- .../be/mygod/vpnhotspot/TetheringService.kt | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index f79c7a8d..3037369d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -25,7 +25,7 @@ class TetheringService : IpNeighbourMonitoringService() { 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) { override fun Routing.configure() { forward() @@ -43,12 +43,12 @@ class TetheringService : IpNeighbourMonitoringService() { synchronized(downstreams) { for (iface in downstreams.keys - TetheringManager.getTetheredIfaces(extras)) downstreams.remove(iface)?.stop() - updateRoutingsLocked() + onDownstreamsChangedLocked() } } override val activeIfaces get() = synchronized(downstreams) { downstreams.keys.toList() } - private fun updateRoutingsLocked() { + private fun onDownstreamsChangedLocked() { if (downstreams.isEmpty()) { unregisterReceiver() ServiceNotification.stopForeground(this) @@ -59,15 +59,6 @@ class TetheringService : IpNeighbourMonitoringService() { registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) 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() } GlobalScope.launch(Dispatchers.Main) { binder.routingsChanged() } @@ -79,22 +70,21 @@ class TetheringService : IpNeighbourMonitoringService() { if (intent != null) { val ifaces = intent.getStringArrayExtra(EXTRA_ADD_INTERFACES) ?: emptyArray() synchronized(downstreams) { - for (iface in ifaces) { - Downstream(this, iface).run { - downstreams[iface] = this - initRouting() - } + for (iface in ifaces) Downstream(this, iface).let { downstream -> + if (downstream.initRouting()) downstreams[iface] = downstream else downstream.stop() } downstreams.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.stop() - updateRoutingsLocked() + onDownstreamsChangedLocked() } } else if (downstreams.isEmpty()) stopSelf(startId) return START_NOT_STICKY } override fun onDestroy() { - downstreams.values.forEach { it.stop() } // force clean to prevent leakage - unregisterReceiver() + synchronized(downstreams) { + downstreams.values.forEach { it.stop() } // force clean to prevent leakage + unregisterReceiver() + } super.onDestroy() }