From 94be8037f4ee6321b69b8b9e4355cfb637c25236 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 16 Nov 2026 14:32:19 +0100 Subject: [PATCH] Alarm: a swipe of a silent alarm notification mutes it --- .../AlarmNotificationManagerFullScreenTest.kt | 14 +++++++++++++ .../AlarmNotificationManager.kt | 21 +++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/implementation/src/androidHostTest/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManagerFullScreenTest.kt b/implementation/src/androidHostTest/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManagerFullScreenTest.kt index cbde78e08d6c..96b6f800bb3c 100644 --- a/implementation/src/androidHostTest/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManagerFullScreenTest.kt +++ b/implementation/src/androidHostTest/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManagerFullScreenTest.kt @@ -134,4 +134,18 @@ class AlarmNotificationManagerFullScreenTest { assertThat(sut.unansweredAlarmIntent()).isNull() } + + @Test + fun swipingASilentAlarmNotificationAwayMutesTheAlarm() { + sut().postSilentAlarmNotification(notificationKey = 42, title = "AAPS", body = "No BG readings", urgent = true) + + val notification = shadowOf(context.getSystemService(NotificationManager::class.java)) + .getNotification(AlarmNotificationManager.SOUND_ID_OFFSET + 42) + val swipe = shadowOf(checkNotNull(notification.deleteIntent)) + + assertThat(swipe.isBroadcast).isTrue() + assertThat(swipe.savedIntent.component?.className).isEqualTo(AlarmMuteReceiver::class.java.name) + assertThat(swipe.savedIntent.getBooleanExtra(AlarmMuteReceiver.EXTRA_SWIPED, false)).isTrue() + } + } diff --git a/implementation/src/androidMain/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManager.kt b/implementation/src/androidMain/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManager.kt index e73f94dc5f02..4ecd4f6733cf 100644 --- a/implementation/src/androidMain/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManager.kt +++ b/implementation/src/androidMain/kotlin/app/aaps/implementation/androidNotification/AlarmNotificationManager.kt @@ -29,8 +29,6 @@ import app.aaps.core.ui.CoreUiStrings import app.aaps.core.ui.rawRes import app.aaps.implementation.R import app.aaps.implementation.alerts.EatReminderReceiver -import app.aaps.implementation.androidNotification.AlarmNotificationManager.Companion.CHANNEL_SILENT -import app.aaps.implementation.androidNotification.AlarmNotificationManager.Companion.CHANNEL_SILENT_URGENT import dev.zacsweers.metro.AppScope import dev.zacsweers.metro.Inject import dev.zacsweers.metro.SingleIn @@ -325,14 +323,6 @@ class AlarmNotificationManager( Intent(context, AlarmMuteReceiver::class.java), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) - // Since Android 14 the user can swipe away even an ongoing notification (not on the lock - // screen). A swipe works like Mute. Before, the looping sound went on with nothing left on - // screen to stop it, and only a force stop of AAPS ended it (#5338). - val swipePendingIntent = PendingIntent.getBroadcast( - context, SWIPE_REQUEST_CODE, - Intent(context, AlarmMuteReceiver::class.java).putExtra(AlarmMuteReceiver.EXTRA_SWIPED, true), - PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - ) val notification = NotificationCompat.Builder(context, channelId) .setSmallIcon(iconsProvider.getNotificationIcon()) @@ -344,7 +334,7 @@ class AlarmNotificationManager( .setOngoing(true) .setAutoCancel(false) .setContentIntent(pendingIntent) - .setDeleteIntent(swipePendingIntent) + .setDeleteIntent(swipePendingIntent()) .addAction(0, rh.gs(app.aaps.core.ui.R.string.mute), mutePendingIntent) .apply { // +5 min next to Mute, so the timer can be moved from the lock screen. Mute ends the timer, like @@ -464,6 +454,7 @@ class AlarmNotificationManager( .setOngoing(true) .setAutoCancel(false) .setContentIntent(notificationHolderProvider().openAppIntent()) + .setDeleteIntent(swipePendingIntent()) val systemId = SOUND_ID_OFFSET + notificationKey try { @@ -532,4 +523,12 @@ class AlarmNotificationManager( alarmSoundPlayer.stop(AlarmSoundPlayer.OWNER_FULLSCREEN) aapsLogger.debug(LTag.NOTIFICATION, "Cancelled all AAPS alarm notifications") } + + // Since Android 14 the user can swipe away even an ongoing notification (not on the lock + // screen). A swipe works like Mute, on every alarm notification (#5338). + private fun swipePendingIntent(): PendingIntent = PendingIntent.getBroadcast( + context, SWIPE_REQUEST_CODE, + Intent(context, AlarmMuteReceiver::class.java).putExtra(AlarmMuteReceiver.EXTRA_SWIPED, true), + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) }