Skip to content

Commit

Permalink
Use CovesaServiceClient to connect to PushService
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Dec 12, 2024
1 parent 1c10979 commit 8026ff4
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ abstract class CovesaServiceClient<T>(
} 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PushService.PushBinder>(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"
}
}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8026ff4

Please sign in to comment.