Fix onServiceDisconnected not called

This commit is contained in:
Mygod
2018-05-09 18:10:22 -07:00
parent dad9bc19e3
commit efa387fd7a
7 changed files with 19 additions and 23 deletions

View File

@@ -137,11 +137,10 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
} }
override fun onServiceDisconnected(name: ComponentName?) { override fun onServiceDisconnected(name: ComponentName?) {
if (name == ComponentName(requireContext(), ClientMonitorService::class.java)) { val clients = clients
val clients = clients ?: return if (clients != null) {
this.clients = null this.clients = null
clients.clientsChanged -= this clients.clientsChanged -= this
return
} }
val binder = binder ?: return val binder = binder ?: return
this.binder = null this.binder = null

View File

@@ -11,6 +11,7 @@ import android.service.quicksettings.Tile
import android.service.quicksettings.TileService import android.service.quicksettings.TileService
import android.support.annotation.RequiresApi import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
import be.mygod.vpnhotspot.util.stopAndUnbind
@RequiresApi(24) @RequiresApi(24)
class RepeaterTileService : TileService(), ServiceConnection { class RepeaterTileService : TileService(), ServiceConnection {
@@ -26,7 +27,7 @@ class RepeaterTileService : TileService(), ServiceConnection {
override fun onStopListening() { override fun onStopListening() {
super.onStopListening() super.onStopListening()
unbindService(this) stopAndUnbind(this)
} }
override fun onClick() { override fun onClick() {

View File

@@ -370,17 +370,10 @@ class TetheringFragment : Fragment(), ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) { override fun onServiceDisconnected(name: ComponentName?) {
val context = requireContext() val context = requireContext()
when (name) { tetheringBinder?.fragment = null
ComponentName(context, TetheringService::class.java) -> { tetheringBinder = null
tetheringBinder?.fragment = null context.unregisterReceiver(receiver)
tetheringBinder = null hotspotBinder?.fragment = null
context.unregisterReceiver(receiver) hotspotBinder = null
}
ComponentName(context, LocalOnlyHotspotService::class.java) -> {
hotspotBinder?.fragment = null
hotspotBinder = null
}
else -> throw IllegalArgumentException("name")
}
} }
} }

View File

@@ -42,4 +42,4 @@ abstract class Client {
return true return true
} }
override fun hashCode() = Objects.hash(iface, mac, ip) override fun hashCode() = Objects.hash(iface, mac, ip)
} }

View File

@@ -10,6 +10,7 @@ import be.mygod.vpnhotspot.net.IpNeighbourMonitor
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
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.util.stopAndUnbind
class ClientMonitorService : Service(), ServiceConnection, IpNeighbourMonitor.Callback { class ClientMonitorService : Service(), ServiceConnection, IpNeighbourMonitor.Callback {
inner class Binder : android.os.Binder() { inner class Binder : android.os.Binder() {
@@ -70,7 +71,7 @@ class ClientMonitorService : Service(), ServiceConnection, IpNeighbourMonitor.Ca
override fun onDestroy() { override fun onDestroy() {
unregisterReceiver(receiver) unregisterReceiver(receiver)
IpNeighbourMonitor.unregisterCallback(this) IpNeighbourMonitor.unregisterCallback(this)
unbindService(this) stopAndUnbind(this)
super.onDestroy() super.onDestroy()
} }

View File

@@ -30,5 +30,5 @@ class ServiceForegroundConnector(private val host: ServiceConnection, private va
} }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() = context.unbindService(host) fun onStop() = context.stopAndUnbind(host)
} }

View File

@@ -1,9 +1,6 @@
package be.mygod.vpnhotspot.util package be.mygod.vpnhotspot.util
import android.content.BroadcastReceiver import android.content.*
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.databinding.BindingAdapter import android.databinding.BindingAdapter
import android.support.annotation.DrawableRes import android.support.annotation.DrawableRes
import android.util.Log import android.util.Log
@@ -53,3 +50,8 @@ fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = fals
if (start) thread.start() if (start) thread.start()
return thread return thread
} }
fun Context.stopAndUnbind(connection: ServiceConnection) {
connection.onServiceDisconnected(null)
unbindService(connection)
}