From a7613f203c936a71696f72935086085873c7309a Mon Sep 17 00:00:00 2001 From: "Jack Boswell (boswelja)" Date: Mon, 26 Apr 2021 15:22:09 +1200 Subject: [PATCH 1/2] Pass context through MessageReceiver.kt --- .../watchconnection/core/MessageReceiver.kt | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/core/src/main/kotlin/com/boswelja/watchconnection/core/MessageReceiver.kt b/core/src/main/kotlin/com/boswelja/watchconnection/core/MessageReceiver.kt index 491b06bd..384466a2 100644 --- a/core/src/main/kotlin/com/boswelja/watchconnection/core/MessageReceiver.kt +++ b/core/src/main/kotlin/com/boswelja/watchconnection/core/MessageReceiver.kt @@ -23,6 +23,7 @@ abstract class MessageReceiver : BroadcastReceiver() { * @param data The data included with the message, or null if there was no data. */ abstract suspend fun onMessageReceived( + context: Context, sourceWatchId: UUID, message: String, data: ByteArray? @@ -32,19 +33,21 @@ abstract class MessageReceiver : BroadcastReceiver() { // Don't handle intent if it's not ACTION_MESSAGE_RECEIVED if (intent?.action != Messages.ACTION_MESSAGE_RECEIVED) return - // Going async - val pendingResult = goAsync() - coroutineScope.launch { - // Collect data from intent - val watchId = UUID.fromString(intent.getStringExtra(EXTRA_WATCH_ID)) - val message = intent.getStringExtra(EXTRA_MESSAGE)!! - val data = intent.getByteArrayExtra(EXTRA_DATA) - - // Pass it on to user code - onMessageReceived(watchId, message, data) - - // Let the BroadcastReceiver know we're done - pendingResult.finish() + context?.let { + // Going async + val pendingResult = goAsync() + coroutineScope.launch { + // Collect data from intent + val watchId = UUID.fromString(intent.getStringExtra(EXTRA_WATCH_ID)) + val message = intent.getStringExtra(EXTRA_MESSAGE)!! + val data = intent.getByteArrayExtra(EXTRA_DATA) + + // Pass it on to user code + onMessageReceived(context, watchId, message, data) + + // Let the BroadcastReceiver know we're done + pendingResult.finish() + } } } From 0a63b7dd54ed12b478ab35fb0abd3a1c45e8a4f8 Mon Sep 17 00:00:00 2001 From: "Jack Boswell (boswelja)" Date: Mon, 26 Apr 2021 15:38:09 +1200 Subject: [PATCH 2/2] Update tests --- .../watchconnection/core/ConcreteMessageReceiver.kt | 2 ++ .../boswelja/watchconnection/core/MessageReceiverTest.kt | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/src/test/kotlin/com/boswelja/watchconnection/core/ConcreteMessageReceiver.kt b/test/src/test/kotlin/com/boswelja/watchconnection/core/ConcreteMessageReceiver.kt index f27dc1cf..93b0fcef 100644 --- a/test/src/test/kotlin/com/boswelja/watchconnection/core/ConcreteMessageReceiver.kt +++ b/test/src/test/kotlin/com/boswelja/watchconnection/core/ConcreteMessageReceiver.kt @@ -1,10 +1,12 @@ package com.boswelja.watchconnection.core +import android.content.Context import java.util.UUID class ConcreteMessageReceiver : MessageReceiver() { override suspend fun onMessageReceived( + context: Context, sourceWatchId: UUID, message: String, data: ByteArray? diff --git a/test/src/test/kotlin/com/boswelja/watchconnection/core/MessageReceiverTest.kt b/test/src/test/kotlin/com/boswelja/watchconnection/core/MessageReceiverTest.kt index a6639862..431ddce9 100644 --- a/test/src/test/kotlin/com/boswelja/watchconnection/core/MessageReceiverTest.kt +++ b/test/src/test/kotlin/com/boswelja/watchconnection/core/MessageReceiverTest.kt @@ -42,7 +42,7 @@ class MessageReceiverTest { val intent = Intent("action") messageReceiver.onReceive(context, intent) verify(inverse = true) { messageReceiver.goAsync() } - coVerify(inverse = true) { messageReceiver.onMessageReceived(any(), any(), any()) } + coVerify(inverse = true) { messageReceiver.onMessageReceived(any(), any(), any(), any()) } } @Test @@ -57,7 +57,7 @@ class MessageReceiverTest { messageReceiver.onReceive(context, intent) } - coVerify { messageReceiver.onMessageReceived(id, message, null) } + coVerify { messageReceiver.onMessageReceived(context, id, message, null) } } @Test @@ -74,6 +74,6 @@ class MessageReceiverTest { messageReceiver.onReceive(context, intent) } - coVerify { messageReceiver.onMessageReceived(id, message, data) } + coVerify { messageReceiver.onMessageReceived(context, id, message, data) } } }