@@ -9,43 +9,35 @@ import androidx.wear.watchface.complications.datasource.ComplicationDataSourceSe
99import androidx.wear.watchface.complications.datasource.ComplicationDataSourceUpdateRequester
1010import dagger.hilt.android.AndroidEntryPoint
1111import io.homeassistant.companion.android.common.data.servers.ServerManager
12+ import io.homeassistant.companion.android.common.util.launchAsync
1213import io.homeassistant.companion.android.conversation.ConversationActivity
1314import javax.inject.Inject
1415import kotlinx.coroutines.CoroutineScope
1516import kotlinx.coroutines.Dispatchers
1617import kotlinx.coroutines.SupervisorJob
17- import kotlinx.coroutines.launch
1818
1919@AndroidEntryPoint
2020class ComplicationReceiver : BroadcastReceiver () {
2121 @Inject
2222 lateinit var serverManager: ServerManager
2323
24- private val scope = CoroutineScope (SupervisorJob () + Dispatchers .Main .immediate )
24+ private val scope = CoroutineScope (SupervisorJob () + Dispatchers .Default )
2525
2626 override fun onReceive (context : Context , intent : Intent ) {
27- val result = goAsync()
28-
29- try {
30- when (intent.action) {
31- UPDATE_COMPLICATION -> updateComplication(context, intent.getIntExtra(EXTRA_ID , - 1 ))
32- Intent .ACTION_SCREEN_ON -> onScreenOn(context)
33- }
34- } finally {
35- result.finish()
27+ when (intent.action) {
28+ UPDATE_COMPLICATION -> updateComplication(context, intent.getIntExtra(EXTRA_ID , - 1 ))
29+ Intent .ACTION_SCREEN_ON -> launchAsync(scope) { onScreenOn(context) }
3630 }
3731 }
3832
3933 private fun updateComplication (context : Context , id : Int ) {
40- scope.launch {
41- // Request an update for the complication that has just been toggled.
42- ComplicationDataSourceUpdateRequester
43- .create(
44- context = context,
45- complicationDataSourceComponent = ComponentName (context, EntityStateDataSourceService ::class .java),
46- )
47- .requestUpdate(id)
48- }
34+ // Request an update for the complication that has just been toggled.
35+ ComplicationDataSourceUpdateRequester
36+ .create(
37+ context = context,
38+ complicationDataSourceComponent = ComponentName (context, EntityStateDataSourceService ::class .java),
39+ )
40+ .requestUpdate(id)
4941 }
5042
5143 private fun updateAllComplications (context : Context ) {
@@ -57,11 +49,9 @@ class ComplicationReceiver : BroadcastReceiver() {
5749 .requestUpdateAll()
5850 }
5951
60- private fun onScreenOn (context : Context ) {
61- scope.launch {
62- if (! serverManager.isRegistered()) return @launch
63- updateAllComplications(context)
64- }
52+ private suspend fun onScreenOn (context : Context ) {
53+ if (! serverManager.isRegistered()) return
54+ updateAllComplications(context)
6555 }
6656
6757 companion object {
0 commit comments