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

Alternative Parcelable and Serializable are deprecated. #527

Merged
merged 4 commits into from
Nov 15, 2023
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
Expand Up @@ -6,6 +6,8 @@ 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.Job
Expand Down Expand Up @@ -973,7 +975,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?
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
callbacksList.forEach {
it.connectionLost(reason)
}
Expand All @@ -998,18 +1000,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 @@ -1052,7 +1054,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 @@ -1068,7 +1070,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 @@ -1098,7 +1100,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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

@Suppress("DEPRECATION")
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> getParcelable(key) as? T
}

@Suppress("DEPRECATION")
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? = when {
SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java)
else -> getSerializable(key) as? T
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ interface MqMessageDao {
fun allArrived(clientHandle: String): List<MqMessageEntity>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(MqMessageEntity: MqMessageEntity): Long
fun insert(mqMessageEntity: MqMessageEntity): Long

@Update
fun updateAll(vararg MqMessageEntities: MqMessageEntity)
fun updateAll(vararg mqMessageEntity: MqMessageEntity)

@Delete
fun delete(MqMessageEntity: MqMessageEntity)
fun delete(mqMessageEntity: MqMessageEntity)

@Query("DELETE FROM MqMessageEntity WHERE clientHandle = :clientHandle AND messageId = :id")
fun deleteId(clientHandle: String, id: String): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,26 @@ abstract class MqMessageDatabase : RoomDatabase() {
return result
}

@Suppress("SimpleRedundantLet")
companion object {

const val MQ_DB_VERSION = 1
private var db: MqMessageDatabase? = null

@Volatile
private var instance: MqMessageDatabase? = null

@Synchronized
fun getDatabase(context: Context, storageName: String = "messageMQ"): MqMessageDatabase {
return db?.let {
it
} ?: run {
db = Room.databaseBuilder(
context.applicationContext,
MqMessageDatabase::class.java,
storageName
).build()
db!!
return instance ?: synchronized(this) {
instance ?: buildDatabase(context.applicationContext, storageName).also { instance = it }
}
}

private fun buildDatabase(context: Context, storageName: String): MqMessageDatabase {
return Room.databaseBuilder(
context.applicationContext,
MqMessageDatabase::class.java,
storageName
).build()
}
}
}