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?) {
if (name == ComponentName(requireContext(), ClientMonitorService::class.java)) {
val clients = clients ?: return
val clients = clients
if (clients != null) {
this.clients = null
clients.clientsChanged -= this
return
}
val binder = binder ?: return
this.binder = null

View File

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

View File

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

View File

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

View File

@@ -30,5 +30,5 @@ class ServiceForegroundConnector(private val host: ServiceConnection, private va
}
@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
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.*
import android.databinding.BindingAdapter
import android.support.annotation.DrawableRes
import android.util.Log
@@ -53,3 +50,8 @@ fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = fals
if (start) thread.start()
return thread
}
fun Context.stopAndUnbind(connection: ServiceConnection) {
connection.onServiceDisconnected(null)
unbindService(connection)
}