Fix temp hotspot

This commit is contained in:
Mygod
2019-07-17 17:30:58 +08:00
parent 3bd81b942f
commit 96fbb7ddf9
2 changed files with 22 additions and 14 deletions

View File

@@ -12,10 +12,11 @@ import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.util.StickyEvent1 import be.mygod.vpnhotspot.util.StickyEvent1
import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.*
import timber.log.Timber import timber.log.Timber
@RequiresApi(26) @RequiresApi(26)
class LocalOnlyHotspotService : IpNeighbourMonitoringService() { class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
companion object { companion object {
private const val TAG = "LocalOnlyHotspotService" private const val TAG = "LocalOnlyHotspotService"
} }
@@ -38,6 +39,10 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService() {
private val binder = Binder() private val binder = Binder()
private var reservation: WifiManager.LocalOnlyHotspotReservation? = null private var reservation: WifiManager.LocalOnlyHotspotReservation? = null
/**
* Writes and critical reads to routingManager should be protected with this context.
*/
override val coroutineContext = newSingleThreadContext("LocalOnlyHotspotService") + Job()
private var routingManager: RoutingManager? = null private var routingManager: RoutingManager? = null
private var receiverRegistered = false private var receiverRegistered = false
private val receiver = broadcastReceiver { _, intent -> private val receiver = broadcastReceiver { _, intent ->
@@ -50,11 +55,11 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService() {
unregisterReceiver() unregisterReceiver()
ServiceNotification.stopForeground(this) ServiceNotification.stopForeground(this)
stopSelf() stopSelf()
} else { } else launch {
val routingManager = routingManager val routingManager = routingManager
if (routingManager == null) { if (routingManager == null) {
this.routingManager = RoutingManager.LocalOnly(this, iface).apply { start() } this@LocalOnlyHotspotService.routingManager = RoutingManager.LocalOnly(this, iface).apply { start() }
IpNeighbourMonitor.registerCallback(this) IpNeighbourMonitor.registerCallback(this@LocalOnlyHotspotService)
} else check(iface == routingManager.downstream) } else check(iface == routingManager.downstream)
} }
} }
@@ -120,13 +125,16 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService() {
override fun onDestroy() { override fun onDestroy() {
binder.stop() binder.stop()
unregisterReceiver() unregisterReceiver(true)
super.onDestroy() super.onDestroy()
} }
private fun unregisterReceiver() { private fun unregisterReceiver(exit: Boolean = false) {
routingManager?.destroy() launch {
routingManager = null routingManager?.destroy()
routingManager = null
if (exit) cancel()
}
if (receiverRegistered) { if (receiverRegistered) {
unregisterReceiver(receiver) unregisterReceiver(receiver)
IpNeighbourMonitor.unregisterCallback(this) IpNeighbourMonitor.unregisterCallback(this)

View File

@@ -23,10 +23,7 @@ import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps
import be.mygod.vpnhotspot.net.wifi.configuration.channelToFrequency import be.mygod.vpnhotspot.net.wifi.configuration.channelToFrequency
import be.mygod.vpnhotspot.util.* import be.mygod.vpnhotspot.util.*
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
@@ -144,7 +141,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
/** /**
* Writes and critical reads to routingManager should be protected with this context. * Writes and critical reads to routingManager should be protected with this context.
*/ */
override val coroutineContext = newSingleThreadContext("TetheringService") + Job() override val coroutineContext = newSingleThreadContext("RepeaterService") + Job()
private var routingManager: RoutingManager? = null private var routingManager: RoutingManager? = null
private var persistNextGroup = false private var persistNextGroup = false
@@ -402,7 +399,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
override fun onDestroy() { override fun onDestroy() {
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
if (status != Status.IDLE) binder.shutdown() if (status != Status.IDLE) binder.shutdown()
launch { cleanLocked() } // force clean to prevent leakage launch { // force clean to prevent leakage
cleanLocked()
cancel()
}
if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") { if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") {
app.pref.unregisterOnSharedPreferenceChangeListener(this) app.pref.unregisterOnSharedPreferenceChangeListener(this)
unregisterReceiver(deviceListener) unregisterReceiver(deviceListener)