Skip to content

Commit

Permalink
Add safety to unbindService (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS authored Feb 25, 2025
1 parent f6016f5 commit 69b0a4d
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class MainActivity: FlutterActivity() {
private var apiClient: APIClient? = 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
// maintain state while waiting for a permission result.
private var startResult: MethodChannel.Result? = null
Expand Down Expand Up @@ -440,6 +444,7 @@ class MainActivity: FlutterActivity() {
private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
outMessenger = Messenger(service)
isServiceBound = true

// We want to monitor the service for as long as we are connected to it.
try {
Expand All @@ -461,6 +466,7 @@ class MainActivity: FlutterActivity() {

override fun onServiceDisconnected(arg0: ComponentName) {
outMessenger = null
isServiceBound = false
if (activeSiteId != null) {
//TODO: this indicates the service died, notify that it is disconnected
}
Expand Down Expand Up @@ -510,7 +516,14 @@ class MainActivity: FlutterActivity() {
msg.replyTo = inMessenger
outMessenger!!.send(msg)
// Unbind
unbindService(connection)
if (isServiceBound) {
try {
unbindService(connection)
isServiceBound = false
} catch (e: IllegalArgumentException) {
Log.e(TAG, e.toString())
}
}
}
outMessenger = null
}
Expand Down

0 comments on commit 69b0a4d

Please sign in to comment.