diff options
| author | Kalle Lindström <karl.lindstrom@mullvad.net> | 2025-08-04 11:19:50 +0200 |
|---|---|---|
| committer | Kalle Lindström <karl.lindstrom@mullvad.net> | 2025-08-04 12:44:04 +0200 |
| commit | 0a12ecffef67b5a3422dcd6ae53eeca114d407d4 (patch) | |
| tree | 44d63b0b26b4c01cba6ab3bcb97dd00d5b7676eb /android/app/src | |
| parent | 1b2e05d8c9353da4caf97d71001e1f4f9311079a (diff) | |
| download | mullvadvpn-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.
Diffstat (limited to 'android/app/src')
| -rw-r--r-- | android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/NotificationAlarmReceiver.kt | 7 | ||||
| -rw-r--r-- | android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/BroadcastReceiverExtensions.kt | 23 |
2 files changed, 27 insertions, 3 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() + } + } +} |
