diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt index 35f26819..3cf376ab 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package info.mqtt.android.service import android.app.Notification @@ -5,7 +7,8 @@ import android.content.* import android.os.Build import android.os.Bundle import android.os.IBinder -import android.util.SparseArray +import info.mqtt.android.service.extension.parcelable +import info.mqtt.android.service.extension.serializable import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -47,7 +50,7 @@ class MqttAndroidClient @JvmOverloads constructor( private val serviceConnection = MyServiceConnection() // We hold the various tokens in a collection and pass identifiers for them to the service - private val tokenMap = SparseArray() + private val tokenList = mutableListOf() //The acknowledgment that a message has been processed by the application private val messageAck = ackType @@ -57,7 +60,6 @@ class MqttAndroidClient @JvmOverloads constructor( // An identifier for the underlying client connection, which we can pass to the service private var clientHandle: String? = null - private var tokenNumber = 0 private var clientConnectOptions: MqttConnectOptions? = null private var connectToken: IMqttToken? = null @@ -254,9 +256,9 @@ class MqttAndroidClient @JvmOverloads constructor( } mqttService!!.isTraceEnabled = traceEnabled mqttService!!.setTraceCallbackId(clientHandle) - val activityToken = storeToken(connectToken) + storeToken(connectToken) try { - mqttService!!.connect(clientHandle!!, clientConnectOptions, activityToken) + mqttService!!.connect(clientHandle!!, clientConnectOptions, connectToken) } catch (e: Exception) { val listener = connectToken!!.actionCallback listener?.onFailure(connectToken, e) @@ -275,8 +277,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun disconnect(): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, null, null) - val activityToken = storeToken(token) - mqttService!!.disconnect(clientHandle!!, null, activityToken) + storeToken(token) + mqttService!!.disconnect(clientHandle!!, null, token) return token } @@ -295,8 +297,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun disconnect(quiesceTimeout: Long): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, null, null) - val activityToken = storeToken(token) - mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, activityToken) + storeToken(token) + mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, token) return token } @@ -313,8 +315,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun disconnect(userContext: Any?, callback: IMqttActionListener?): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback) - val activityToken = storeToken(token) - mqttService!!.disconnect(clientHandle!!, null, activityToken) + storeToken(token) + mqttService!!.disconnect(clientHandle!!, null, token) return token } @@ -347,8 +349,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun disconnect(quiesceTimeout: Long, userContext: Any?, callback: IMqttActionListener): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback) - val activityToken = storeToken(token) - mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, activityToken) + storeToken(token) + mqttService!!.disconnect(clientHandle!!, quiesceTimeout, null, token) return token } @@ -408,8 +410,8 @@ class MqttAndroidClient @JvmOverloads constructor( message.qos = qos message.isRetained = retained val token = MqttDeliveryTokenAndroid(this, userContext, callback, message) - val activityToken = storeToken(token) - val internalToken = mqttService!!.publish(clientHandle!!, topic, payload, QoS.valueOf(qos), retained, null, activityToken) + storeToken(token) + val internalToken = mqttService!!.publish(clientHandle!!, topic, payload, QoS.valueOf(qos), retained, null, token) token.setDelegate(internalToken) return token } @@ -470,8 +472,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun publish(topic: String, message: MqttMessage, userContext: Any?, callback: IMqttActionListener?): IMqttDeliveryToken { val token = MqttDeliveryTokenAndroid(this, userContext, callback, message) - val activityToken = storeToken(token) - val internalToken = mqttService!!.publish(clientHandle!!, topic, message, null, activityToken) + storeToken(token) + val internalToken = mqttService!!.publish(clientHandle!!, topic, message, null, token) token.setDelegate(internalToken) return token } @@ -522,8 +524,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun subscribe(topic: String, qos: Int, userContext: Any?, callback: IMqttActionListener?): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, arrayOf(topic)) - val activityToken = storeToken(token) - mqttService!!.subscribe(clientHandle!!, topic, QoS.valueOf(qos), null, activityToken) + storeToken(token) + mqttService!!.subscribe(clientHandle!!, topic, QoS.valueOf(qos), null, token) return token } @@ -626,8 +628,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun subscribe(topic: Array, qos: IntArray, userContext: Any?, callback: IMqttActionListener?): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, topic) - val activityToken = storeToken(token) - mqttService!!.subscribe(clientHandle!!, topic, qos, null, activityToken) + storeToken(token) + mqttService!!.subscribe(clientHandle!!, topic, qos, null, token) return token } @@ -705,8 +707,8 @@ class MqttAndroidClient @JvmOverloads constructor( Array ): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback, topicFilters) - val activityToken = storeToken(token) - mqttService!!.subscribe(clientHandle!!, topicFilters, qos.map { QoS.valueOf(it) }.toTypedArray(), null, activityToken, messageListeners) + storeToken(token) + mqttService!!.subscribe(clientHandle!!, topicFilters, qos.map { QoS.valueOf(it) }.toTypedArray(), null, token, messageListeners) return token } @@ -743,8 +745,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun unsubscribe(topic: String, userContext: Any?, callback: IMqttActionListener?): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback) - val activityToken = storeToken(token) - mqttService!!.unsubscribe(clientHandle!!, topic, null, activityToken) + storeToken(token) + mqttService!!.unsubscribe(clientHandle!!, topic, null, token) return token } @@ -774,8 +776,8 @@ class MqttAndroidClient @JvmOverloads constructor( */ override fun unsubscribe(topic: Array, userContext: Any?, callback: IMqttActionListener?): IMqttToken { val token: IMqttToken = MqttTokenAndroid(this, userContext, callback) - val activityToken = storeToken(token) - mqttService!!.unsubscribe(clientHandle!!, topic, null, activityToken) + storeToken(token) + mqttService!!.unsubscribe(clientHandle!!, topic, null, token) return token } @@ -953,7 +955,7 @@ class MqttAndroidClient @JvmOverloads constructor( /** * Process a notification that we have disconnected */ - private fun disconnected(data: Bundle?) { + private fun disconnected(data: Bundle) { clientHandle = null // avoid reuse! val token = removeMqttToken(data) token?.let { @@ -968,7 +970,7 @@ class MqttAndroidClient @JvmOverloads constructor( * Process a Connection Lost notification */ private fun connectionLostAction(data: Bundle?) { - val reason = data!!.getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION) as Exception? + val reason = data!!.parcelable(MqttServiceConstants.CALLBACK_EXCEPTION) as Exception? callbacksList.forEach { it.connectionLost(reason) } @@ -993,18 +995,18 @@ class MqttAndroidClient @JvmOverloads constructor( */ private fun simpleAction(token: IMqttToken?, data: Bundle) { if (token != null) { - val status = data.getSerializable(MqttServiceConstants.CALLBACK_STATUS) as Status? + val status = data.serializable(MqttServiceConstants.CALLBACK_STATUS) as Status? if (status == Status.OK) { (token as MqttTokenAndroid).notifyComplete() } else { - val errorMessage = data.getSerializable(MqttServiceConstants.CALLBACK_ERROR_MESSAGE) as String? - var exceptionThrown = data.getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION) as Throwable? + val errorMessage = data.serializable(MqttServiceConstants.CALLBACK_ERROR_MESSAGE) as String? + var exceptionThrown = data.serializable(MqttServiceConstants.CALLBACK_EXCEPTION) as Throwable? if (exceptionThrown == null && errorMessage != null) { exceptionThrown = Throwable(errorMessage) } else if (exceptionThrown == null) { val bundleToString = data.keySet() .joinToString(", ", "{", "}") { key -> - "$key=${data[key]}" + "$key=${data.getString(key)}" } exceptionThrown = Throwable("No Throwable given\n$bundleToString") } @@ -1047,7 +1049,7 @@ class MqttAndroidClient @JvmOverloads constructor( */ private fun messageDeliveredAction(data: Bundle) { val token = removeMqttToken(data) - val status = data.getSerializable(MqttServiceConstants.CALLBACK_STATUS) as Status? + val status = data.serializable(MqttServiceConstants.CALLBACK_STATUS) as Status? if (token != null) { if (status == Status.OK && token is IMqttDeliveryToken) { callbacksList.forEach { callback -> @@ -1063,7 +1065,7 @@ class MqttAndroidClient @JvmOverloads constructor( private fun messageArrivedAction(data: Bundle?) { val messageId = data!!.getString(MqttServiceConstants.CALLBACK_MESSAGE_ID)!! val destinationName = data.getString(MqttServiceConstants.CALLBACK_DESTINATION_NAME) - val message: ParcelableMqttMessage = data.getParcelable(MqttServiceConstants.CALLBACK_MESSAGE_PARCEL)!! + val message: ParcelableMqttMessage = data.parcelable(MqttServiceConstants.CALLBACK_MESSAGE_PARCEL)!! try { if (messageAck == Ack.AUTO_ACK) { callbacksList.forEach { callback -> @@ -1093,7 +1095,7 @@ class MqttAndroidClient @JvmOverloads constructor( MqttServiceConstants.TRACE_DEBUG -> it.traceDebug(message) MqttServiceConstants.TRACE_ERROR -> it.traceError(message) else -> { - val e = data.getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION) as Exception? + val e = data.serializable(MqttServiceConstants.CALLBACK_EXCEPTION) as Exception? it.traceException(message, e) } } @@ -1102,13 +1104,16 @@ class MqttAndroidClient @JvmOverloads constructor( /** * @param token identifying an operation - * @return an identifier for the token which can be passed to the Android - * Service */ @Synchronized - private fun storeToken(token: IMqttToken?): String { - tokenMap.put(tokenNumber, token) - return (tokenNumber++).toString() + private fun storeToken(token: IMqttToken?) { + token?.let { + val tokenFound = tokenList.find { it == token } + if (tokenFound == null) { + tokenList.add(token) + Timber.d("$token size=${tokenList.size}") + } + } } /** @@ -1117,12 +1122,12 @@ class MqttAndroidClient @JvmOverloads constructor( * @return the token */ @Synchronized - private fun removeMqttToken(data: Bundle?): IMqttToken? { - val activityToken = data!!.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN) - if (activityToken != null) { - val tokenNumber = activityToken.toInt() - val token = tokenMap[tokenNumber] - tokenMap.delete(tokenNumber) + private fun removeMqttToken(data: Bundle): IMqttToken? { + val activityToken = data.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN) + if (activityToken != null && activityToken != "null") { + val token = tokenList.find { it.toString() == activityToken } + tokenList.remove(token) + Timber.d("search=$activityToken ${tokenList.size}") return token } return null @@ -1134,9 +1139,10 @@ class MqttAndroidClient @JvmOverloads constructor( * @return the token */ @Synchronized - private fun getMqttToken(data: Bundle?): IMqttToken? { - val activityToken = data!!.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN) - return tokenMap[activityToken!!.toInt()] + private fun getMqttToken(data: Bundle): IMqttToken? { + val activityToken = data.getString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN) + val token = tokenList.find { it.toString() == activityToken } + return token } /** @@ -1281,7 +1287,7 @@ class MqttAndroidClient @JvmOverloads constructor( companion object { private val SERVICE_NAME = MqttService::class.java.name - private const val FOREGROUND_ID = 77 + private const val FOREGROUND_ID = 77 shl 16 } } diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttConnection.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttConnection.kt index 3e00d644..18124847 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttConnection.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttConnection.kt @@ -48,7 +48,9 @@ import java.util.* */ internal class MqttConnection( private val service: MqttService, // fields for the connection definition - var serverURI: String, var clientId: String, private var persistence: MqttClientPersistence?, // Client handle, used for callbacks... + var serverURI: String, + var clientId: String, + private var persistence: MqttClientPersistence?, // Client handle, used for callbacks... var clientHandle: String ) : MqttCallbackExtended { // Saved sent messages and their corresponding Topics, activityTokens and @@ -61,7 +63,7 @@ internal class MqttConnection( private var connectOptions: MqttConnectOptions? = null //store connect ActivityToken for reconnect - private var reconnectActivityToken: String? = null + private var reconnectActivityToken: IMqttToken? = null // our client object - instantiated on connect private var myClient: MqttAsyncClient? = null @@ -85,7 +87,7 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun connect(options: MqttConnectOptions?, invocationContext: String?, activityToken: String?) { + fun connect(options: MqttConnectOptions?, invocationContext: String?, activityToken: IMqttToken?) { connectOptions = options reconnectActivityToken = activityToken options?.let { @@ -102,7 +104,7 @@ internal class MqttConnection( } service.traceDebug("Connecting {$serverURI} as {$clientId}") val resultBundle = Bundle() - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_ACTION) try { @@ -262,11 +264,11 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary string to be passed back to the activity */ - fun disconnect(quiesceTimeout: Long, invocationContext: String?, activityToken: String?) { + fun disconnect(quiesceTimeout: Long, invocationContext: String?, activityToken: IMqttToken) { service.traceDebug("disconnect()") disconnected = true val resultBundle = Bundle() - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.DISCONNECT_ACTION) if (myClient != null && myClient!!.isConnected) { @@ -296,11 +298,11 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary string to be passed back to the activity */ - fun disconnect(invocationContext: String?, activityToken: String?) { + fun disconnect(invocationContext: String?, activityToken: IMqttToken?) { service.traceDebug("disconnect()") disconnected = true val resultBundle = Bundle() - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.DISCONNECT_ACTION) if (myClient != null && myClient!!.isConnected) { @@ -348,11 +350,11 @@ internal class MqttConnection( qos: QoS, retained: Boolean, invocationContext: String?, - activityToken: String + activityToken: IMqttToken ): IMqttDeliveryToken? { val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SEND_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) var sendToken: IMqttDeliveryToken? = null if (myClient != null && myClient!!.isConnected) { @@ -384,10 +386,10 @@ internal class MqttConnection( * @param activityToken arbitrary string to be passed back to the activity * @return token for tracking the operation */ - fun publish(topic: String, message: MqttMessage, invocationContext: String?, activityToken: String): IMqttDeliveryToken? { + fun publish(topic: String, message: MqttMessage, invocationContext: String?, activityToken: IMqttToken): IMqttDeliveryToken? { val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SEND_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) var sendToken: IMqttDeliveryToken? = null if (myClient != null && myClient!!.isConnected) { @@ -424,11 +426,11 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun subscribe(topic: String, qos: QoS, invocationContext: String?, activityToken: String) { + fun subscribe(topic: String, qos: QoS, invocationContext: String?, activityToken: IMqttToken) { service.traceDebug("subscribe({$topic},$qos,{$invocationContext}, {$activityToken}") val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) if (myClient != null && myClient!!.isConnected) { val listener: IMqttActionListener = MqttConnectionListener(resultBundle) @@ -452,14 +454,13 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun subscribe(topic: Array, qos: IntArray?, invocationContext: String?, activityToken: String) { + fun subscribe(topic: Array, qos: IntArray?, invocationContext: String?, activityToken: IMqttToken) { service.traceDebug( - "subscribe({" + topic.contentToString() + "}," + Arrays - .toString(qos) + ",{" + invocationContext + "}, {" + activityToken + "}" + "subscribe({" + topic.contentToString() + "}," + qos.contentToString() + ",{" + invocationContext + "}, {" + activityToken + "}" ) val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) if (myClient != null && myClient!!.isConnected) { val listener: IMqttActionListener = MqttConnectionListener(resultBundle) @@ -479,7 +480,7 @@ internal class MqttConnection( topicFilters: Array, qos: Array, invocationContext: String?, - activityToken: String, + activityToken: IMqttToken, messageListeners: Array? ) { service.traceDebug( @@ -488,7 +489,7 @@ internal class MqttConnection( ) val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) if (myClient != null && myClient!!.isConnected) { val listener: IMqttActionListener = MqttConnectionListener(resultBundle) @@ -511,11 +512,11 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun unsubscribe(topic: String, invocationContext: String?, activityToken: String) { + fun unsubscribe(topic: String, invocationContext: String?, activityToken: IMqttToken) { service.traceDebug("unsubscribe({$topic},{$invocationContext}, {$activityToken})") val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.UNSUBSCRIBE_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) if (myClient != null && myClient!!.isConnected) { val listener: IMqttActionListener = MqttConnectionListener(resultBundle) @@ -538,11 +539,11 @@ internal class MqttConnection( * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun unsubscribe(topic: Array, invocationContext: String?, activityToken: String) { + fun unsubscribe(topic: Array, invocationContext: String?, activityToken: IMqttToken) { service.traceDebug("unsubscribe({" + topic.contentToString() + "},{" + invocationContext + "}, {" + activityToken + "})") val resultBundle = Bundle() resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.UNSUBSCRIBE_ACTION) - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext) if (myClient != null && myClient!!.isConnected) { val listener: IMqttActionListener = MqttConnectionListener(resultBundle) @@ -672,11 +673,11 @@ internal class MqttConnection( @Synchronized private fun storeSendDetails( topic: String, msg: MqttMessage, messageToken: IMqttDeliveryToken?, - invocationContext: String?, activityToken: String + invocationContext: String?, activityToken: IMqttToken ) { savedTopics[messageToken] = topic savedSentMessages[messageToken] = msg - savedActivityTokens[messageToken] = activityToken + savedActivityTokens[messageToken] = activityToken.toString() invocationContext?.let { savedInvocationContexts[messageToken] = it } @@ -738,7 +739,7 @@ internal class MqttConnection( //The Automatic reconnect functionality is enabled here Timber.i("Requesting Automatic reconnect using New Java AC") val resultBundle = Bundle() - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null) resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_ACTION) CoroutineScope(Dispatchers.IO).launch { @@ -756,7 +757,7 @@ internal class MqttConnection( // use the activityToke the same with action connect service.traceDebug("Do Real Reconnect!") val resultBundle = Bundle() - resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken) + resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, reconnectActivityToken.toString()) resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null) resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_ACTION) try { diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttDeliveryTokenAndroid.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttDeliveryTokenAndroid.kt index 946379b4..c5b1adf7 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttDeliveryTokenAndroid.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttDeliveryTokenAndroid.kt @@ -8,7 +8,9 @@ import org.eclipse.paho.client.mqttv3.MqttMessage * Implementation of the IMqttDeliveryToken interface for use from within the MqttAndroidClient implementation */ internal class MqttDeliveryTokenAndroid( - client: MqttAndroidClient, userContext: Any?, listener: IMqttActionListener?, // The message which is being tracked by this token + client: MqttAndroidClient, + userContext: Any?, + listener: IMqttActionListener?, // The message which is being tracked by this token private var message: MqttMessage ) : MqttTokenAndroid(client, userContext, listener), IMqttDeliveryToken { diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt index 5c6f83c1..4119d005 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt @@ -287,7 +287,7 @@ class MqttService : Service(), MqttTraceHandler { * @param activityToken arbitrary identifier to be passed back to the Activity */ @Throws(MqttException::class) - fun connect(clientHandle: String, connectOptions: MqttConnectOptions?, activityToken: String?) { + fun connect(clientHandle: String, connectOptions: MqttConnectOptions?, activityToken: IMqttToken?) { val client = getConnection(clientHandle) CoroutineScope(Dispatchers.IO).launch { client.connect(connectOptions, null, activityToken) @@ -334,12 +334,11 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun disconnect(clientHandle: String, invocationContext: String?, activityToken: String?) { + fun disconnect(clientHandle: String, invocationContext: String?, activityToken: IMqttToken?) { val client = getConnection(clientHandle) client.disconnect(invocationContext, activityToken) connections.remove(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 // remain around until the last activity disconnects @@ -354,7 +353,7 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun disconnect(clientHandle: String, quiesceTimeout: Long, invocationContext: String?, activityToken: String?) { + fun disconnect(clientHandle: String, quiesceTimeout: Long, invocationContext: String?, activityToken: IMqttToken) { val client = getConnection(clientHandle) client.disconnect(quiesceTimeout, invocationContext, activityToken) connections.remove(clientHandle) @@ -386,7 +385,7 @@ class MqttService : Service(), MqttTraceHandler { * @return token for tracking the operation */ fun publish( - clientHandle: String, topic: String, payload: ByteArray, qos: QoS, retained: Boolean, invocationContext: String?, activityToken: String? + clientHandle: String, topic: String, payload: ByteArray, qos: QoS, retained: Boolean, invocationContext: String?, activityToken: IMqttToken? ): IMqttDeliveryToken? { return getConnection(clientHandle).publish(topic, payload, qos, retained, invocationContext, activityToken!!) } @@ -406,7 +405,7 @@ class MqttService : Service(), MqttTraceHandler { topic: String, message: MqttMessage, invocationContext: String?, - activityToken: String + activityToken: IMqttToken ): IMqttDeliveryToken? { return getConnection(clientHandle).publish(topic, message, invocationContext, activityToken) } @@ -420,7 +419,7 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun subscribe(clientHandle: String, topic: String, qos: QoS, invocationContext: String?, activityToken: String) { + fun subscribe(clientHandle: String, topic: String, qos: QoS, invocationContext: String?, activityToken: IMqttToken) { getConnection(clientHandle).subscribe(topic, qos, invocationContext, activityToken) } @@ -433,7 +432,7 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun subscribe(clientHandle: String, topic: Array, qos: IntArray?, invocationContext: String?, activityToken: String) { + fun subscribe(clientHandle: String, topic: Array, qos: IntArray?, invocationContext: String?, activityToken: IMqttToken) { getConnection(clientHandle).subscribe(topic, qos, invocationContext, activityToken) } @@ -448,7 +447,7 @@ class MqttService : Service(), MqttTraceHandler { * @param messageListeners a callback to handle incoming messages */ fun subscribe( - clientHandle: String, topicFilters: Array, qos: Array, invocationContext: String?, activityToken: String?, + clientHandle: String, topicFilters: Array, qos: Array, invocationContext: String?, activityToken: IMqttToken?, messageListeners: Array? ) { getConnection(clientHandle).subscribe(topicFilters, qos, invocationContext, activityToken!!, messageListeners) @@ -462,7 +461,7 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun unsubscribe(clientHandle: String, topic: String, invocationContext: String?, activityToken: String) { + fun unsubscribe(clientHandle: String, topic: String, invocationContext: String?, activityToken: IMqttToken) { getConnection(clientHandle).unsubscribe(topic, invocationContext, activityToken) } @@ -474,7 +473,7 @@ class MqttService : Service(), MqttTraceHandler { * @param invocationContext arbitrary data to be passed back to the application * @param activityToken arbitrary identifier to be passed back to the Activity */ - fun unsubscribe(clientHandle: String, topic: Array, invocationContext: String?, activityToken: String?) { + fun unsubscribe(clientHandle: String, topic: Array, invocationContext: String?, activityToken: IMqttToken?) { getConnection(clientHandle).unsubscribe(topic, invocationContext, activityToken!!) } diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt new file mode 100644 index 00000000..75ae9db0 --- /dev/null +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt @@ -0,0 +1,23 @@ +package info.mqtt.android.service.extension + +import android.os.Bundle +import java.io.Serializable +import android.os.Build +import android.os.Build.VERSION.SDK_INT +import android.os.Parcelable + +inline fun Bundle.parcelable(key: String): T? = when { + SDK_INT >= 33 -> getParcelable(key, T::class.java) + else -> { + @Suppress("DEPRECATION") + getParcelable(key) as? T + } +} + +inline fun Bundle.serializable(key: String): T? = when { + SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java) + else -> { + @Suppress("DEPRECATION") + getSerializable(key) as? T + } +} \ No newline at end of file