From b804417dddc5ba204b228b06b69871ca77d42d42 Mon Sep 17 00:00:00 2001 From: hoangchungk53qx1 Date: Wed, 15 Nov 2023 11:21:27 +0700 Subject: [PATCH 1/4] alternative parcelable and serializable are deprecated. --- .../mqtt/android/service/MqttAndroidClient.kt | 20 ++++++++++--------- .../mqtt/android/service/MqttConnection.kt | 4 +++- .../service/MqttDeliveryTokenAndroid.kt | 4 +++- .../android/service/extension/BundleExt.kt | 17 ++++++++++++++++ 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt 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 f2c9f43ef..8efed987c 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt @@ -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 @@ -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? callbacksList.forEach { it.connectionLost(reason) } @@ -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") } @@ -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 -> @@ -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 -> @@ -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) } } @@ -1289,7 +1291,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 3e00d6444..1ea17df01 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 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 946379b45..c5b1adf71 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/extension/BundleExt.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt new file mode 100644 index 000000000..651c876c1 --- /dev/null +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt @@ -0,0 +1,17 @@ +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 From 3335497351b830c585b229b321fbaaa7e409d120 Mon Sep 17 00:00:00 2001 From: hoangchungk53qx1 Date: Wed, 15 Nov 2023 11:21:55 +0700 Subject: [PATCH 2/4] optimize MqMessageDatabase --- .../mqtt/android/service/room/MqMessageDao.kt | 6 ++--- .../android/service/room/MqMessageDatabase.kt | 24 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDao.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDao.kt index 037a067dc..1574f077a 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDao.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDao.kt @@ -13,13 +13,13 @@ interface MqMessageDao { fun allArrived(clientHandle: String): List @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 diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDatabase.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDatabase.kt index 3fb1dbeb3..dab8e9c27 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDatabase.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/room/MqMessageDatabase.kt @@ -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() + } } } From 37cf17c6a197f41311326637adfe90f20821a8d6 Mon Sep 17 00:00:00 2001 From: hoangchungk53qx1 Date: Wed, 15 Nov 2023 11:34:14 +0700 Subject: [PATCH 3/4] ktlint check --- .../java/info/mqtt/android/service/extension/BundleExt.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 index 651c876c1..a6f39187a 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/extension/BundleExt.kt @@ -6,12 +6,14 @@ import android.os.Build import android.os.Build.VERSION.SDK_INT import android.os.Parcelable +@Suppress("DEPRECATION") inline fun Bundle.parcelable(key: String): T? = when { SDK_INT >= 33 -> getParcelable(key, T::class.java) - else -> @Suppress("DEPRECATION") getParcelable(key) as? T + else -> getParcelable(key) as? T } +@Suppress("DEPRECATION") 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 + else -> getSerializable(key) as? T } \ No newline at end of file From 6669d43ae6a03e18f7494f3ebb3e6d88ab8e73ec Mon Sep 17 00:00:00 2001 From: hoangchungk53qx1 Date: Wed, 15 Nov 2023 13:20:05 +0700 Subject: [PATCH 4/4] revert FOREGROUND_ID --- .../main/java/info/mqtt/android/service/MqttAndroidClient.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8efed987c..1fd331b3a 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt @@ -1291,7 +1291,7 @@ class MqttAndroidClient @JvmOverloads constructor( companion object { private val SERVICE_NAME = MqttService::class.java.name - private const val FOREGROUND_ID = 77 shl 16 + private const val FOREGROUND_ID = 77 } }