Skip to content

Commit

Permalink
Allow multiple disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Feb 15, 2024
1 parent f7560d7 commit ed08d5a
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,12 @@ class MqttService : Service(), MqttTraceHandler {
* @param activityToken arbitrary identifier to be passed back to the Activity
*/
fun disconnect(clientHandle: String, invocationContext: String?, activityToken: String?) {
val client = getConnection(clientHandle)
client.disconnect(invocationContext, activityToken)
connections.remove(clientHandle)

if (isConnectionAvailable(clientHandle)) {
val client = getConnection(clientHandle)
client.disconnect(invocationContext, activityToken)
connections.remove(clientHandle)
} else
Timber.w("Connection is not available $clientHandle")

// the activity has finished using us, so we can stop the service
// the activities are bound with BIND_AUTO_CREATE, so the service will
Expand Down Expand Up @@ -507,6 +509,8 @@ class MqttService : Service(), MqttTraceHandler {
return connections[clientHandle] ?: throw IllegalArgumentException("Invalid ClientHandle >$clientHandle<")
}

private fun isConnectionAvailable(clientHandle: String) = connections.contains(clientHandle)

/**
* Called by the Activity when a message has been passed back to the application
*
Expand Down

0 comments on commit ed08d5a

Please sign in to comment.