summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKalle Lindström <karl.lindstrom@mullvad.net>2025-08-04 11:19:50 +0200
committerKalle Lindström <karl.lindstrom@mullvad.net>2025-08-04 12:44:04 +0200
commit0a12ecffef67b5a3422dcd6ae53eeca114d407d4 (patch)
tree44d63b0b26b4c01cba6ab3bcb97dd00d5b7676eb
parent1b2e05d8c9353da4caf97d71001e1f4f9311079a (diff)
downloadmullvadvpn-0a12ecffef67b5a3422dcd6ae53eeca114d407d4.tar.xz
mullvadvpn-0a12ecffef67b5a3422dcd6ae53eeca114d407d4.zip
Attempt to fix potential ANR
In onReceive in NotificationAlarmReceiver onBlocking() was used which could potentially take too much time and cause an ANR. This PR changes the onBlocking() call to instead use the goAsync() API so that the onReceive method can return immediately.
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt7
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/BroadcastReceiverExtensions.kt23
-rw-r--r--android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryNotificationProvider.kt5
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() {