Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #26 from boswelja/messagereceiver-context
Browse files Browse the repository at this point in the history
Pass context through MessageReceiver.kt
  • Loading branch information
boswelja authored Apr 26, 2021
2 parents 5d8bbb8 + 0a63b7d commit d5f7727
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +57,7 @@ class MessageReceiverTest {
messageReceiver.onReceive(context, intent)
}

coVerify { messageReceiver.onMessageReceived(id, message, null) }
coVerify { messageReceiver.onMessageReceived(context, id, message, null) }
}

@Test
Expand All @@ -74,6 +74,6 @@ class MessageReceiverTest {
messageReceiver.onReceive(context, intent)
}

coVerify { messageReceiver.onMessageReceived(id, message, data) }
coVerify { messageReceiver.onMessageReceived(context, id, message, data) }
}
}

0 comments on commit d5f7727

Please sign in to comment.