@@ -3,11 +3,19 @@ package dev.johnoreilly.confetti.notifications
33import android.content.BroadcastReceiver
44import android.content.Context
55import android.content.Intent
6+ import androidx.concurrent.futures.await
67import androidx.core.app.NotificationManagerCompat
8+ import androidx.core.net.toUri
9+ import androidx.wear.remote.interactions.RemoteActivityHelper
10+ import com.google.android.gms.wearable.CapabilityClient
11+ import com.google.android.gms.wearable.Wearable
712import dev.johnoreilly.confetti.ConfettiRepository
813import dev.johnoreilly.confetti.auth.Authentication
914import kotlinx.coroutines.CoroutineScope
15+ import kotlinx.coroutines.Dispatchers
16+ import kotlinx.coroutines.asExecutor
1017import kotlinx.coroutines.launch
18+ import kotlinx.coroutines.tasks.await
1119import org.koin.core.component.KoinComponent
1220import org.koin.core.component.inject
1321import kotlin.coroutines.CoroutineContext
@@ -20,19 +28,23 @@ class NotificationReceiver: BroadcastReceiver(), KoinComponent {
2028 private val notificationManager: NotificationManagerCompat by inject()
2129
2230 override fun onReceive (context : Context , intent : Intent ) {
23- if (intent.action == " REMOVE_BOOKMARK " ) {
31+ if (intent.action == ActionRemoveBookmark ) {
2432 doAsync {
2533 removeBookmark(intent)
2634 val notificationId = intent.getIntExtra(" notificationId" , - 1 )
2735 if (notificationId != - 1 ) {
2836 notificationManager.cancel(notificationId)
2937 }
3038 }
39+ } else if (intent.action == ActionOpenOnWear ) {
40+ doAsync {
41+ openOnWear(intent, context)
42+ }
3143 }
3244 }
3345
34- private suspend fun removeBookmark (intent : Intent ? ) {
35- val conference = intent? .getStringExtra(" conferenceId" ) ? : return
46+ private suspend fun removeBookmark (intent : Intent ) {
47+ val conference = intent.getStringExtra(" conferenceId" ) ? : return
3648 val sessionId = intent.getStringExtra(" sessionId" ) ? : return
3749 val user = authentication.currentUser.value ? : return
3850
@@ -46,4 +58,35 @@ class NotificationReceiver: BroadcastReceiver(), KoinComponent {
4658 val pendingResult = goAsync()
4759 appScope.launch(coroutineContext) { block() }.invokeOnCompletion { pendingResult.finish() }
4860 }
61+
62+ private suspend fun openOnWear (intent : Intent , context : Context ) {
63+ val conference = intent.getStringExtra(" conferenceId" )
64+ val sessionId = intent.getStringExtra(" sessionId" )
65+
66+ val remoteActivityHelper = RemoteActivityHelper (context, Dispatchers .Default .asExecutor())
67+
68+ val capabilityClient = Wearable .getCapabilityClient(context)
69+ val installedNodes = capabilityClient.getCapability(" confetti_wear_app" , CapabilityClient .FILTER_ALL ).await().nodes.map { it.id }
70+
71+ if (installedNodes.isNotEmpty()) {
72+ remoteActivityHelper.startRemoteActivity(
73+ Intent (Intent .ACTION_VIEW )
74+ .setData(" https://confetti-app.dev/conference/$conference /session/$sessionId " .toUri())
75+ .addCategory(Intent .CATEGORY_BROWSABLE ),
76+ null // all devices
77+ ).await()
78+ } else {
79+ remoteActivityHelper.startRemoteActivity(
80+ Intent (Intent .ACTION_VIEW )
81+ .setData(" http://play.google.com/store/apps/details?id=dev.johnoreilly.confetti" .toUri())
82+ .addCategory(Intent .CATEGORY_BROWSABLE ),
83+ null // all devices
84+ ).await()
85+ }
86+ }
87+
88+ companion object {
89+ val ActionRemoveBookmark = " REMOVE_BOOKMARK"
90+ val ActionOpenOnWear = " OPEN_ON_WEAR"
91+ }
4992}
0 commit comments