Add safety to unbindService (#266)

This commit is contained in:
Ian VanSchooten 2025-02-25 10:11:47 -05:00 committed by GitHub
parent f6016f5da8
commit 69b0a4dafa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -36,6 +36,10 @@ class MainActivity: FlutterActivity() {
private var apiClient: APIClient? = null private var apiClient: APIClient? = null
private var sites: Sites? = null private var sites: Sites? = null
// Don't attempt to unbind from the service unless the client has received some
// information about the service's state.
private var isServiceBound = false
// When starting a site we may need to request VPN permissions. These variables help us // When starting a site we may need to request VPN permissions. These variables help us
// maintain state while waiting for a permission result. // maintain state while waiting for a permission result.
private var startResult: MethodChannel.Result? = null private var startResult: MethodChannel.Result? = null
@ -440,6 +444,7 @@ class MainActivity: FlutterActivity() {
private val connection = object : ServiceConnection { private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) { override fun onServiceConnected(className: ComponentName, service: IBinder) {
outMessenger = Messenger(service) outMessenger = Messenger(service)
isServiceBound = true
// We want to monitor the service for as long as we are connected to it. // We want to monitor the service for as long as we are connected to it.
try { try {
@ -461,6 +466,7 @@ class MainActivity: FlutterActivity() {
override fun onServiceDisconnected(arg0: ComponentName) { override fun onServiceDisconnected(arg0: ComponentName) {
outMessenger = null outMessenger = null
isServiceBound = false
if (activeSiteId != null) { if (activeSiteId != null) {
//TODO: this indicates the service died, notify that it is disconnected //TODO: this indicates the service died, notify that it is disconnected
} }
@ -510,7 +516,14 @@ class MainActivity: FlutterActivity() {
msg.replyTo = inMessenger msg.replyTo = inMessenger
outMessenger!!.send(msg) outMessenger!!.send(msg)
// Unbind // Unbind
if (isServiceBound) {
try {
unbindService(connection) unbindService(connection)
isServiceBound = false
} catch (e: IllegalArgumentException) {
Log.e(TAG, e.toString())
}
}
} }
outMessenger = null outMessenger = null
} }