From 8026ff48223080e4d836c2272d21cfc8d4b48436 Mon Sep 17 00:00:00 2001 From: sim Date: Wed, 11 Dec 2024 17:38:03 +0000 Subject: [PATCH] Use CovesaServiceClient to connect to PushService --- .../sdk/api/client/CovesaServiceClient.kt | 2 + .../internal/InternalPushServiceClient.kt | 59 +++++++++++ .../sdk/api/client/internal/PushReceiver.kt | 38 ++++--- .../covesa/sdk/api/client/push/PushService.kt | 13 +-- .../sdk/api/client/push/ServiceConnetion.kt | 99 ------------------- .../client/push/{ => data}/FailedReason.kt | 2 +- .../client/push/{ => data}/PublicKeySet.kt | 2 +- .../client/push/{ => data}/PushEndpoint.kt | 2 +- .../api/client/push/{ => data}/PushMessage.kt | 2 +- .../covesa/sdk/client/push/PushServiceImpl.kt | 6 +- 10 files changed, 91 insertions(+), 134 deletions(-) create mode 100644 api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/InternalPushServiceClient.kt delete mode 100644 api/client/src/main/kotlin/global/covesa/sdk/api/client/push/ServiceConnetion.kt rename api/client/src/main/kotlin/global/covesa/sdk/api/client/push/{ => data}/FailedReason.kt (96%) rename api/client/src/main/kotlin/global/covesa/sdk/api/client/push/{ => data}/PublicKeySet.kt (96%) rename api/client/src/main/kotlin/global/covesa/sdk/api/client/push/{ => data}/PushEndpoint.kt (96%) rename api/client/src/main/kotlin/global/covesa/sdk/api/client/push/{ => data}/PushMessage.kt (97%) diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/CovesaServiceClient.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/CovesaServiceClient.kt index c6f0fd1..0d535a1 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/CovesaServiceClient.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/CovesaServiceClient.kt @@ -67,6 +67,8 @@ abstract class CovesaServiceClient( } catch (e: IllegalStateException) { cancel("connection to remote service failed", e) Log.w(TAG, e.message, e) + } catch (e: Exception) { + Log.w(TAG, e.message, e) } awaitClose { diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/InternalPushServiceClient.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/InternalPushServiceClient.kt new file mode 100644 index 0000000..8280fb1 --- /dev/null +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/InternalPushServiceClient.kt @@ -0,0 +1,59 @@ +package global.covesa.sdk.api.client.internal + +import android.content.Context +import android.content.Intent +import android.os.IBinder +import global.covesa.sdk.api.client.CovesaServiceClient +import global.covesa.sdk.api.client.push.PushService +import global.covesa.sdk.api.client.push.data.FailedReason +import global.covesa.sdk.api.client.push.data.PushEndpoint +import global.covesa.sdk.api.client.push.data.PushMessage +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.first + +/** + * This is used to connect to the service implementing [PushService] + */ +internal class InternalPushServiceClient( + context: Context, + coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO) +) : CovesaServiceClient(context, coroutineScope, TAG) { + + private val packageName = context.packageName + + override fun interfaceFromBinder(service: IBinder): PushService.PushBinder { + return service as PushService.PushBinder + } + + override fun serviceIntent(): Intent { + return Intent().apply { + action = PushService.ACTION_PUSH_EVENT + `package` = packageName + } + } + + suspend fun newEndpoint(endpoint: PushEndpoint, instance: String) { + getService()?.onNewEndpoint(endpoint, instance) + } + + suspend fun message(message: PushMessage, instance: String) { + getService()?.onMessage(message, instance) + } + + suspend fun registrationFailed(reason: FailedReason, instance: String) { + getService()?.onRegistrationFailed(reason, instance) + } + + suspend fun unregistered(instance: String) { + getService()?.onUnregistered(instance) + } + + private suspend fun getService(): PushService? { + return remoteService.first()?.getService() + } + + companion object { + private const val TAG = "InternalPushServiceClient" + } +} \ No newline at end of file diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/PushReceiver.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/PushReceiver.kt index f74695c..019bb68 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/PushReceiver.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/internal/PushReceiver.kt @@ -1,13 +1,15 @@ package global.covesa.sdk.api.client.internal import android.content.Context -import global.covesa.sdk.api.client.push.FailedReason -import global.covesa.sdk.api.client.push.PushEndpoint +import global.covesa.sdk.api.client.push.data.FailedReason +import global.covesa.sdk.api.client.push.data.PushEndpoint import global.covesa.sdk.api.client.push.PushService import org.unifiedpush.android.connector.FailedReason as UFailedReason import org.unifiedpush.android.connector.MessagingReceiver -import global.covesa.sdk.api.client.push.PushMessage -import global.covesa.sdk.api.client.push.ServiceConnection +import global.covesa.sdk.api.client.push.data.PushMessage +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.unifiedpush.android.connector.data.PushEndpoint as UPushEndpoint import org.unifiedpush.android.connector.data.PushMessage as UPushMessage @@ -19,30 +21,26 @@ import org.unifiedpush.android.connector.data.PushMessage as UPushMessage class PushReceiver: MessagingReceiver() { override fun onUnregistered(context: Context, instance: String) { - ServiceConnection.sendEvent( - context, - ServiceConnection.Event.Unregistered(instance) - ) + CoroutineScope(Dispatchers.IO).launch { + InternalPushServiceClient(context).onUnregistered(instance) + } } override fun onMessage(context: Context, message: UPushMessage, instance: String) { - ServiceConnection.sendEvent( - context, - ServiceConnection.Event.Message(PushMessage(message), instance) - ) + CoroutineScope(Dispatchers.IO).launch { + InternalPushServiceClient(context).onMessage(PushMessage(message), instance) + } } override fun onNewEndpoint(context: Context, endpoint: UPushEndpoint, instance: String) { - ServiceConnection.sendEvent( - context, - ServiceConnection.Event.NewEndpoint(PushEndpoint(endpoint), instance) - ) + CoroutineScope(Dispatchers.IO).launch { + InternalPushServiceClient(context).onNewEndpoint(PushEndpoint(endpoint), instance) + } } override fun onRegistrationFailed(context: Context, reason: UFailedReason, instance: String) { - ServiceConnection.sendEvent( - context, - ServiceConnection.Event.RegistrationFailed(FailedReason.fromUp(reason), instance) - ) + CoroutineScope(Dispatchers.IO).launch { + InternalPushServiceClient(context).onRegistrationFailed(FailedReason.fromUp(reason), instance) + } } } diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushService.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushService.kt index b486cf0..1d6e867 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushService.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushService.kt @@ -4,6 +4,9 @@ import android.app.Service import android.content.Intent import android.os.Binder import android.os.IBinder +import global.covesa.sdk.api.client.push.data.FailedReason +import global.covesa.sdk.api.client.push.data.PushEndpoint +import global.covesa.sdk.api.client.push.data.PushMessage /** * Service to receive UnifiedPush messages (new endpoints, unregistrations, push messages, errors) from the distributors @@ -48,14 +51,10 @@ abstract class PushService: Service() { /** * @hide - * Return [binder] onBind + * Return [PushBinder] onBind */ override fun onBind(intent: Intent?): IBinder? { - return synchronized(lock) { - binder ?: PushBinder().also { - binder = it - } - } + return PushBinder() } /** @@ -80,7 +79,5 @@ abstract class PushService: Service() { internal companion object { const val ACTION_PUSH_EVENT = "global.covesa.sdk.PUSH_EVENT" - private var binder: PushBinder? = null - private val lock = Object() } } \ No newline at end of file diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/ServiceConnetion.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/ServiceConnetion.kt deleted file mode 100644 index 99122db..0000000 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/ServiceConnetion.kt +++ /dev/null @@ -1,99 +0,0 @@ -package global.covesa.sdk.api.client.push - -import android.content.ComponentName -import android.content.Context -import android.content.Intent -import android.content.ServiceConnection -import android.os.IBinder -import android.util.Log -import java.util.LinkedList - -internal object ServiceConnection { - const val TAG = "ServiceConnection" - private lateinit var mService: PushService - private var binding: Boolean = false - private var mBound: Boolean = false - private var eventsQueue = LinkedList() - - private val connection = object : ServiceConnection { - - override fun onServiceConnected(className: ComponentName, service: IBinder) { - Log.d(TAG, "Service is connected") - val binder = service as PushService.PushBinder - mService = binder.getService() - synchronized(this@ServiceConnection) { - mBound = true - binding = false - } - handlePendingEvents() - } - - override fun onServiceDisconnected(arg0: ComponentName) { - Log.d(TAG, "Service is disconnected") - synchronized(this@ServiceConnection) { - mBound = false - binding = false - } - } - } - - private fun bind(context: Context) { - val context = context.applicationContext - Intent().apply { - action = PushService.ACTION_PUSH_EVENT - `package` = context.packageName - }.also { intent -> - context.bindService(intent, connection, Context.BIND_AUTO_CREATE) - } - } - - fun stop(context: Context) { - val context = context.applicationContext - context.unbindService(connection) - mBound = false - } - - sealed class Event { - class NewEndpoint(val endpoint: PushEndpoint, val instance: String): Event() - class Message(val message: PushMessage, val instance: String): Event() - class RegistrationFailed(val reason: FailedReason, val instance: String): Event() - class Unregistered(val instance: String): Event() - - fun handle() { - when (this) { - is Message -> mService.onMessage(message, instance) - is NewEndpoint -> mService.onNewEndpoint(endpoint, instance) - is RegistrationFailed -> mService.onRegistrationFailed(reason, instance) - is Unregistered -> mService.onUnregistered(instance) - } - } - } - - fun sendEvent(context: Context, event: Event) { - var shouldBind = false - var bound: Boolean - synchronized(this) { - bound = mBound - if (!mBound) { - eventsQueue.add(event) - if (!binding) { - binding = true - shouldBind = true - } - } - } - if (shouldBind) { - bind(context) - } else if (bound) { - event.handle() - } else { - Log.d(TAG, "Even has been added to the queue") - } - } - - private fun handlePendingEvents() { - while (eventsQueue.isNotEmpty()) { - eventsQueue.pop().handle() - } - } -} \ No newline at end of file diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/FailedReason.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/FailedReason.kt similarity index 96% rename from api/client/src/main/kotlin/global/covesa/sdk/api/client/push/FailedReason.kt rename to api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/FailedReason.kt index 2512fc1..a832de8 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/FailedReason.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/FailedReason.kt @@ -1,4 +1,4 @@ -package global.covesa.sdk.api.client.push +package global.covesa.sdk.api.client.push.data import org.unifiedpush.android.connector.FailedReason as UFailedReason diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PublicKeySet.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PublicKeySet.kt similarity index 96% rename from api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PublicKeySet.kt rename to api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PublicKeySet.kt index f2b691e..091fa28 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PublicKeySet.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PublicKeySet.kt @@ -1,4 +1,4 @@ -package global.covesa.sdk.api.client.push +package global.covesa.sdk.api.client.push.data import android.os.Parcel import android.os.Parcelable diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushEndpoint.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushEndpoint.kt similarity index 96% rename from api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushEndpoint.kt rename to api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushEndpoint.kt index 822bfe3..c95c740 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushEndpoint.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushEndpoint.kt @@ -1,4 +1,4 @@ -package global.covesa.sdk.api.client.push +package global.covesa.sdk.api.client.push.data import android.os.Parcel import android.os.Parcelable diff --git a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushMessage.kt b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushMessage.kt similarity index 97% rename from api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushMessage.kt rename to api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushMessage.kt index 36f523c..557b0e2 100644 --- a/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/PushMessage.kt +++ b/api/client/src/main/kotlin/global/covesa/sdk/api/client/push/data/PushMessage.kt @@ -1,4 +1,4 @@ -package global.covesa.sdk.api.client.push +package global.covesa.sdk.api.client.push.data import android.os.Parcel import android.os.Parcelable diff --git a/samples/client/src/main/java/global/covesa/sdk/client/push/PushServiceImpl.kt b/samples/client/src/main/java/global/covesa/sdk/client/push/PushServiceImpl.kt index 3e7394a..9253b26 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/push/PushServiceImpl.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/push/PushServiceImpl.kt @@ -1,9 +1,9 @@ package global.covesa.sdk.client.push import android.util.Log -import global.covesa.sdk.api.client.push.FailedReason -import global.covesa.sdk.api.client.push.PushEndpoint -import global.covesa.sdk.api.client.push.PushMessage +import global.covesa.sdk.api.client.push.data.FailedReason +import global.covesa.sdk.api.client.push.data.PushEndpoint +import global.covesa.sdk.api.client.push.data.PushMessage import global.covesa.sdk.api.client.push.PushService import global.covesa.sdk.client.ui.Notification import kotlinx.coroutines.CoroutineScope