diff options
Diffstat (limited to 'android')
3 files changed, 30 insertions, 5 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt index 7b8ab4e04a..49bd290230 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt @@ -6,9 +6,9 @@ import android.content.Intent import co.touchlab.kermit.Logger import java.time.Duration import java.time.ZonedDateTime -import kotlinx.coroutines.runBlocking import net.mullvad.mullvadvpn.service.notifications.accountexpiry.AccountExpiryNotificationProvider import net.mullvad.mullvadvpn.usecase.ScheduleNotificationAlarmUseCase +import net.mullvad.mullvadvpn.util.goAsync import net.mullvad.mullvadvpn.util.serializable import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -28,8 +28,9 @@ class NotificationAlarmReceiver : BroadcastReceiver(), KoinComponent { Logger.d("Account expiry alarm triggered") val untilExpiry = Duration.between(ZonedDateTime.now(), expiry) - runBlocking { - notificationProvider.showNotification(untilExpiry) + notificationProvider.showNotification(untilExpiry) + + goAsync { // Only schedule the next alarm if we still have time left on the account. if (context != null && expiry > ZonedDateTime.now()) { scheduleNotificationAlarmUseCase(context = context, accountExpiry = expiry) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/BroadcastReceiverExtensions.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/BroadcastReceiverExtensions.kt new file mode 100644 index 0000000000..9d835a96a7 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/BroadcastReceiverExtensions.kt @@ -0,0 +1,23 @@ +package net.mullvad.mullvadvpn.util + +import android.content.BroadcastReceiver +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch + +@OptIn(DelicateCoroutinesApi::class) +fun BroadcastReceiver.goAsync( + coroutineScope: CoroutineScope = GlobalScope, + block: suspend () -> Unit, +) { + val result = goAsync() + coroutineScope.launch { + try { + block() + } finally { + // Always call finish(), even if the coroutineScope was cancelled + result.finish() + } + } +} diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryNotificationProvider.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryNotificationProvider.kt index 776ce47960..0be99896c9 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryNotificationProvider.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryNotificationProvider.kt @@ -20,7 +20,7 @@ class AccountExpiryNotificationProvider(private val channelId: NotificationChann override val notifications: Flow<NotificationUpdate<Notification.AccountExpiry>> get() = notificationChannel.receiveAsFlow() - suspend fun showNotification(durationUntilExpiry: Duration) { + fun showNotification(durationUntilExpiry: Duration) { val notification = Notification.AccountExpiry( channelId = channelId, @@ -29,7 +29,8 @@ class AccountExpiryNotificationProvider(private val channelId: NotificationChann ) val notificationUpdate = NotificationUpdate.Notify(NOTIFICATION_ID, notification) - notificationChannel.send(notificationUpdate) + // Always succeeds because the channel is conflated. + notificationChannel.trySend(notificationUpdate) } suspend fun cancelNotification() { |
