Modern Apps git

← Modern-Apps.git

clock: a silent alarm no longer rings at full volume

Commit
208eb544aadbb1cab120d8de41b3fdf6a6485114
Author
Vayun Mathur <[email protected]>
When
2026-12-01 03:13 UTC (5 days ago)
Parents
0f8b690
A silent alarm (ringtoneUri="silent"; null means system
default) fired through the ring-notification backstop: the
channel choice treated silent as a reason to use
ALARM_RING_CHANNEL_ID, whose sound is fixed to the default
alarm sound, and FLAG_INSISTENT looped it — so the picked
'No sound' alarm audibly rang until the 10-minute timeout.
AlarmSoundService deliberately plays nothing for silent and
only cancels the notification when it took over sounding, so
nothing ever silenced the backstop.

AlarmReceiver now places silent alarms on the silent
channel (no sound, no channel vibration; the ringing UI,
full-screen intent and service vibration are preserved) and
never marks them insistent. Sounding and gradual-service
alarms keep their exact previous backstop behaviour, pinned
by a control test that passes both before and after. DST
stepping, timezone/boot re-arm, timer lifecycle and world
clock were audited and cleared. Verified:
:clock:testDevUnitTest 268 tests pass; lint errors are
pre-existing on untouched files.

Diff

clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.ktindex 087f3ca748..912cc4c740 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt@@ -71,6 +71,12 @@ class AlarmReceiver : BroadcastReceiver() {         // opposite of the setting. That trade is only worth it when the service is likely to         // take over: when its start was denied (or the fire carries no ringtone), an insistent         // notification is the only sound there will ever be, so ring anyway.+        //+        // A silent alarm is the exception that must never take that trade. The user picked+        // "Silent" (vibrate-only / visual), and AlarmSoundService honours it by playing+        // nothing and leaving this notification up. Posting it on the sounding ring channel+        // with FLAG_INSISTENT would therefore play the channel's default alarm sound on a+        // loop for up to the ring timeout - the exact sound the user turned off.         val gradual = intent.getIntExtra(AlarmScheduler.EXTRA_GRADUAL_SECONDS, 0) > 0         val silent = intent.getStringExtra(AlarmScheduler.EXTRA_RINGTONE_URI) == RINGTONE_SILENT         val canStartService = canStartForegroundService(context)@@ -79,7 +85,7 @@ class AlarmReceiver : BroadcastReceiver() {         val label = intent.getStringExtra(AlarmScheduler.EXTRA_LABEL).orEmpty()         val builder = NotificationCompat.Builder(             context,-            if (gradual && canStartService && !silent) ALARM_CHANNEL_ID else ALARM_RING_CHANNEL_ID,+            if (silent || (gradual && canStartService)) ALARM_CHANNEL_ID else ALARM_RING_CHANNEL_ID,         )             .setSmallIcon(R.drawable.baseline_access_alarm_24)             .setContentTitle(context.getString(R.string.label_alarm))@@ -113,7 +119,7 @@ class AlarmReceiver : BroadcastReceiver() {         notificationManager.notify(             ALARM_RING_NOTIFICATION_ID,             builder.build().apply {-                if (!gradual || !canStartService || silent) {+                if (!silent && (!gradual || !canStartService)) {                     flags = flags or Notification.FLAG_INSISTENT                 }             },
clock/src/test/java/com/vayunmathur/clock/platform/AlarmReceiverSilentTest.kt
diff --git a/clock/src/test/java/com/vayunmathur/clock/platform/AlarmReceiverSilentTest.kt b/clock/src/test/java/com/vayunmathur/clock/platform/AlarmReceiverSilentTest.ktnew file mode 100644index 0000000000..0070089e61--- /dev/null+++ b/clock/src/test/java/com/vayunmathur/clock/platform/AlarmReceiverSilentTest.kt@@ -0,0 +1,107 @@+package com.vayunmathur.clock.platform++import android.app.Notification+import android.app.NotificationManager+import android.content.Context+import android.content.Intent+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.clock.FakeAndroidKeyStore+import com.vayunmathur.library.ui.RINGTONE_SILENT+import org.junit.Before+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf+import com.vayunmathur.testsupport.awaitWithMainIdle+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++/**+ * Regression tests for the silent-alarm ring bug: an alarm whose ringtone is+ * [RINGTONE_SILENT] ("Silent" in the picker, stored as `"silent"`) fired on+ * the sounding ring channel with FLAG_INSISTENT, so the channel's default+ * alarm sound looped for up to the ten-minute ring timeout.+ * AlarmSoundService honours silent by playing nothing and leaving the ring+ * notification up, so that notification is the sound - it must live on the+ * silent channel with no insistent flag.+ */+@RunWith(RobolectricTestRunner::class)+class AlarmReceiverSilentTest {++    @Before fun setUp() = FakeAndroidKeyStore.install()++    companion object {+        /** See ReceiversAndNotificationsTest: drain the stored SQLCipher error. */+        @org.junit.AfterClass @JvmStatic fun drainStoredDbErrors() {+            repeat(8) {+                run { var pumped = 0; awaitWithMainIdle("pump background work", maxSteps = 25_000) { ++pumped >= 25_000 } }+                try {+                    kotlinx.coroutines.test.runTest { }+                } catch (e: IllegalStateException) {+                    if (e.javaClass.simpleName != "UncaughtExceptionsBeforeTest") throw e+                }+            }+        }+    }++    private val ctx: Context get() = ApplicationProvider.getApplicationContext()+    private val nm: NotificationManager get() = ctx.getSystemService(NotificationManager::class.java)++    private fun fire(alarmId: Long, gradualSeconds: Int, ringtoneUri: String?) {+        createNotificationChannels(ctx)+        val intent = Intent().apply {+            putExtra("ALARM_ID", alarmId)+            putExtra(AlarmScheduler.EXTRA_LABEL, "wake")+            putExtra(AlarmScheduler.EXTRA_GRADUAL_SECONDS, gradualSeconds)+            if (ringtoneUri != null) putExtra(AlarmScheduler.EXTRA_RINGTONE_URI, ringtoneUri)+            putExtra(AlarmScheduler.EXTRA_VIBRATE, false)+        }+        AlarmReceiver().onReceive(ctx, intent)+        // The receiver's async reschedule reaches the host-unopenable+        // SQLCipher repository; pump so its failure settles here.+        run { var pumped = 0; awaitWithMainIdle("pump background work", maxSteps = 5_000) { ++pumped >= 400 } }+    }++    private fun posted(): Notification {+        val notification = shadowOf(nm).getNotification(ALARM_RING_NOTIFICATION_ID)+        assertNotNull(notification, "ring notification should be posted")+        return notification+    }++    @Test+    fun silentAlarmUsesSilentChannelWithoutInsistentFlag() {+        fire(alarmId = 9_101L, gradualSeconds = 0, ringtoneUri = RINGTONE_SILENT)+        val notification = posted()+        assertEquals(+            ALARM_CHANNEL_ID, notification.channelId,+            "a silent alarm must not be posted on the sounding ring channel",+        )+        assertTrue(+            notification.flags and Notification.FLAG_INSISTENT == 0,+            "a silent alarm must not loop a sound with FLAG_INSISTENT",+        )+    }++    @Test+    fun silentGradualAlarmUsesSilentChannelWithoutInsistentFlag() {+        fire(alarmId = 9_102L, gradualSeconds = 30, ringtoneUri = RINGTONE_SILENT)+        val notification = posted()+        assertEquals(ALARM_CHANNEL_ID, notification.channelId)+        assertTrue(notification.flags and Notification.FLAG_INSISTENT == 0)+    }++    @Test+    fun soundingAlarmStillUsesRingChannelWithInsistentFlag() {+        fire(alarmId = 9_103L, gradualSeconds = 0, ringtoneUri = null)+        val notification = posted()+        assertEquals(+            ALARM_RING_CHANNEL_ID, notification.channelId,+            "the sounding backstop for a normal alarm must be preserved",+        )+        assertTrue(+            notification.flags and Notification.FLAG_INSISTENT != 0,+            "a normal alarm keeps its insistent backstop ring",+        )+    }+}