fix(#171): merge review - ring channel up front, held-back status in the calendar header, silenced calendars stop a live ring

This commit is contained in:
makiolaj committed 2026-11-14 03:10:20 +01:00
1 parent 04f3b7ec8f
commit f181ae7ac9
6 files changed
+85 -26

No files matched your search

@@ -59,7 +59,7 @@ class ReminderChannels @Inject constructor(
channels = plan.channelsToWrite,
existing = existing,
)
if (RING_CHANNEL_ID in existingIds) writeRingChannel()
if (RING_CHANNEL_ID in existingIds) ensureRingChannel()
val ids = existingIds - plan.staleChannelIds.toSet() + plan.channelsToWrite.map { it.id }
lastSync = syncKey(calendars, if (retireLegacyChannel()) ids - LEGACY_REMINDER_CHANNEL_ID else ids)
}
@@ -91,20 +91,30 @@ class ReminderChannels @Inject constructor(
* full-screen intent degrades to.
*/
private fun ringChannel(): String {
val manager = manager
if (manager.getNotificationChannel(RING_CHANNEL_ID) == null) writeRingChannel()
ensureRingChannel()
return RING_CHANNEL_ID
}
/** Creates the ring channel, or renames it after a language change. */
private fun writeRingChannel() {
/**
* Creates the ring channel, or renames it after a language change. Called
* as soon as a calendar is set to ring, so the channel can be checked in
* system settings (and switched on, where a skin starts it off) before the
* first ring.
*/
@Synchronized
fun ensureRingChannel() {
val manager = manager
val name = context.getString(R.string.reminder_ring_channel_name)
val description = context.getString(R.string.reminder_ring_channel_description)
val current = manager.getNotificationChannel(RING_CHANNEL_ID)
if (current != null && current.name?.toString() == name && current.description == description) return
manager.createNotificationChannel(
NotificationChannel(
RING_CHANNEL_ID,
context.getString(R.string.reminder_ring_channel_name),
name,
NotificationManager.IMPORTANCE_HIGH,
).apply {
description = context.getString(R.string.reminder_ring_channel_description)
this.description = description
setSound(null, null)
enableVibration(false)
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
@@ -118,12 +128,8 @@ class ReminderChannels @Inject constructor(
* the sound itself.
*/
fun canRing(calendarId: Long): Boolean {
val manager = manager
return listOf(reminderChannelId(calendarId), RING_CHANNEL_ID).none { id ->
val channel = manager.getNotificationChannel(id) ?: return@none false
channel.importance == NotificationManager.IMPORTANCE_NONE ||
channel.group?.let { manager.getNotificationChannelGroup(it)?.isBlocked } == true
}
val status = status(listOf(calendarId))
return !status.appBlocked && calendarId !in status.blockedCalendarIds && !status.ringBlocked
}
private fun calendarChannelFor(calendarId: Long): String? {
@@ -77,7 +77,7 @@ class ReminderNotifier @Inject constructor(
*/
suspend fun post(alert: ReminderAlert, quiet: Boolean = false, ring: Boolean = true) {
if (isSilenced(alert.calendarId)) return
val style = when {
var style = when {
quiet -> ReminderAlertStyle.Notification
else -> effectiveStyle(alert).let {
if (it == ReminderAlertStyle.FullScreenRing && !ring) ReminderAlertStyle.FullScreen else it
@@ -85,7 +85,14 @@ class ReminderNotifier @Inject constructor(
}
// Rung already and handed back to the shade: a catch-up post leaves it there.
if (style == ReminderAlertStyle.FullScreenRing && (isShowing(alert) || alerts.isPending(alert))) return
val channelId = channelFor(alert, style) ?: return
val channelId = channelFor(alert, style)
// The ring channel could not be made: still full-screen, sounding once.
?: if (style == ReminderAlertStyle.FullScreenRing) {
style = ReminderAlertStyle.FullScreen
channelFor(alert, style) ?: return
} else {
return
}
val title = displayTitle(alert)
val text = contentText(alert)
if (style == ReminderAlertStyle.FullScreenRing) {
@@ -108,13 +115,15 @@ class ReminderNotifier @Inject constructor(
/** Whether a ring for [alert] should go on, now that settings may have changed since it started. */
suspend fun ringVerdict(alert: ReminderAlert): RingVerdict = when {
!settingsPrefs.remindersEnabled.first() || !canPost() || isSilenced(alert.calendarId) -> RingVerdict.Drop
!settingsPrefs.remindersEnabled.first() || !canPost() || isSilenced(alert.calendarId) ||
calendarDataSource.isCalendarVisible(alert.calendarId) == null -> RingVerdict.Drop
effectiveStyle(alert) != ReminderAlertStyle.FullScreenRing -> RingVerdict.HandBack
else -> RingVerdict.Ring
}
/** Whether a live ring for [alert] may go on: its notification can still be seen. */
fun canStillRing(alert: ReminderAlert): Boolean = canPost() && channels.canRing(alert.calendarId)
/** Whether a live ring for [alert] may go on: its notification can still be seen, and its calendar still notifies. */
suspend fun canStillRing(alert: ReminderAlert): Boolean =
canPost() && channels.canRing(alert.calendarId) && !isSilenced(alert.calendarId)
/** The calendar's style, short of ringing where the user blocked its calendar, group or the ring channel. */
private suspend fun effectiveStyle(alert: ReminderAlert): ReminderAlertStyle {
@@ -172,7 +172,7 @@ class ReminderRingService : Service() {
var sounding = false
var tick = 0
while (true) {
if (tick++ % VISIBILITY_CHECK_TICKS == 0 && !notifier.canStillRing(alert)) {
if (tick++ % VISIBILITY_CHECK_TICKS == 0 && !stillRings(alert)) {
handBack(alert)
end()
return
@@ -191,6 +191,15 @@ class ReminderRingService : Service() {
}
}
private suspend fun stillRings(alert: ReminderAlert): Boolean = try {
withContext(Dispatchers.IO) { notifier.canStillRing(alert) }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w(TAG, "Could not re-check the ring for event ${alert.eventId}", e)
true
}
private fun stop(intent: Intent) {
val current = alerts.ringing
if (current != null && matchFrom(intent).matches(current)) {
@@ -229,11 +229,19 @@ internal fun NotificationsScreen(
val expanded = calendar.id in expandedCalendars
val notifying = calendar.id !in state.mutedReminderCalendarIds
val blocked = notifying && calendar.id in channelStatus.blockedCalendarIds
val heldBack = fullScreenHeldBack(
style = state.reminderAlertStyles.alertStyleFor(calendar.id),
notifying = notifying,
blocked = blocked,
ringBlocked = channelStatus.ringBlocked,
canFullScreen = canFullScreen,
)
GroupedRow(
title = calendar.displayName.orUnnamed(),
summary = when {
!notifying -> stringResource(R.string.settings_calendar_notifications_off)
blocked -> stringResource(R.string.settings_notifications_blocked)
heldBack.any -> stringResource(R.string.settings_full_screen_held_back)
else -> null
},
position = if (expanded) Position.Top else Position.Alone,
@@ -257,11 +265,10 @@ internal fun NotificationsScreen(
state = state,
notifying = notifying,
blocked = blocked,
ringBlocked = channelStatus.ringBlocked,
heldBack = heldBack,
onOpenRingSettings = { context.openNotificationSettings(RING_CHANNEL_ID) },
onSetNotifying = { viewModel.setCalendarRemindersEnabled(calendar.id, it) },
onOpenSound = { openCalendarSound(calendar.id) },
canFullScreen = canFullScreen,
onEditAlertStyle = { alertStylePicker = calendar.id },
onEditAlertSound = { alertSoundPicker = calendar.id },
onAllowFullScreen = { context.openFullScreenIntentSettings() },
@@ -410,11 +417,10 @@ private fun CalendarNotificationRows(
state: SettingsUiState,
notifying: Boolean,
blocked: Boolean,
ringBlocked: Boolean,
heldBack: FullScreenHeldBack,
onOpenRingSettings: () -> Unit,
onSetNotifying: (Boolean) -> Unit,
onOpenSound: () -> Unit,
canFullScreen: Boolean,
onEditAlertStyle: () -> Unit,
onEditAlertSound: () -> Unit,
onAllowFullScreen: () -> Unit,
@@ -424,8 +430,8 @@ private fun CalendarNotificationRows(
val managed = calendar.id in state.managedCalendarIds
val takesOverrides = calendar.canModifyContents && !managed
val alertStyle = state.reminderAlertStyles.alertStyleFor(calendar.id)
val needsPermission = alertStyle.isFullScreen && !canFullScreen
val ringHeldBack = alertStyle == ReminderAlertStyle.FullScreenRing && ringBlocked && !blocked
val needsPermission = heldBack.permission
val ringHeldBack = heldBack.ring
Column {
GroupedSwitchRow(
title = stringResource(R.string.settings_calendar_notifications),
@@ -562,6 +568,25 @@ private fun calendarOverrideSummary(
is ReminderOverride.Minutes -> reminderChoiceLabel(choice.minutes)
}
/** What keeps a full-screen calendar from alerting as chosen: the missing permission, or the blocked ring channel. */
private data class FullScreenHeldBack(val permission: Boolean, val ring: Boolean) {
val any: Boolean get() = permission || ring
}
private fun fullScreenHeldBack(
style: ReminderAlertStyle,
notifying: Boolean,
blocked: Boolean,
ringBlocked: Boolean,
canFullScreen: Boolean,
): FullScreenHeldBack {
val live = notifying && !blocked
return FullScreenHeldBack(
permission = live && style.isFullScreen && !canFullScreen,
ring = live && style == ReminderAlertStyle.FullScreenRing && ringBlocked,
)
}
@Composable
private fun alertStyleLabel(style: ReminderAlertStyle): String = stringResource(
if (style.isFullScreen) R.string.settings_alert_style_full_screen else R.string.settings_alert_style_notification,
@@ -391,6 +391,11 @@ class SettingsViewModel @Inject constructor(
fun setCalendarAlertStyle(calendarId: Long, style: ReminderAlertStyle) {
viewModelScope.launch {
prefs.setCalendarAlertStyle(calendarId, style)
// Up front, so it can be checked in system settings before the first ring.
if (style == ReminderAlertStyle.FullScreenRing) {
onChannels { ensureRingChannel() }
refreshChannelStatus(state.value.reminderCalendars.map { it.id })
}
// A ring for it goes quiet but stays in the shade.
if (style != ReminderAlertStyle.FullScreenRing) {
reminderNotifier.stopRing(RingMatch.Calendar(calendarId), handBack = true)
@@ -427,7 +432,11 @@ class SettingsViewModel @Inject constructor(
/** Create the per-calendar channels before showing the app's channel list, even with reminders off. */
suspend fun syncReminderChannels() {
onChannels { sync() }
val ringing = state.value.reminderAlertStyles.containsValue(ReminderAlertStyle.FullScreenRing)
onChannels {
sync()
if (ringing) ensureRingChannel()
}
}
private suspend fun <T> onChannels(block: ReminderChannels.() -> T): T? = withContext(io) {
+1
View File
@@ -755,6 +755,7 @@
<string name="settings_alert_sound_ring">Ring until dismissed</string>
<string name="settings_alert_sound_ring_summary">Your alarm sound and vibration, like an alarm, also in Do Not Disturb when alarms may sound</string>
<string name="settings_calendar_notification_sound_ring_hint">Ringing reminders use your alarm sound. This sets the sound of the ones handed back to the shade.</string>
<string name="settings_full_screen_held_back">Full-screen alerts held back</string>
<string name="settings_ring_blocked_hint">Blocked in system settings, so this calendar\'s reminders only sound once — tap to allow</string>
<string name="settings_full_screen_permission">Allow full-screen alerts</string>
<string name="settings_full_screen_permission_hint">Android blocks Calendula\'s full-screen alerts, so they only pop up as notifications. Tap to allow them.</string>