Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokenlist with mutableList 3.x #683

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@file:Suppress("unused")

package info.mqtt.android.service

import android.app.Notification
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
Expand Down Expand Up @@ -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<IMqttToken?>()
private val tokenList = mutableListOf<IMqttToken>()

//The acknowledgment that a message has been processed by the application
private val messageAck = ackType
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -626,8 +628,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun subscribe(topic: Array<String>, 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
}

Expand Down Expand Up @@ -705,8 +707,8 @@ class MqttAndroidClient @JvmOverloads constructor(
Array<IMqttMessageListener>
): 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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -774,8 +776,8 @@ class MqttAndroidClient @JvmOverloads constructor(
*/
override fun unsubscribe(topic: Array<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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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}")
}
}
}

/**
Expand All @@ -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
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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
}

}
Loading
Loading