diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt index 368f228c..20ec0453 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt @@ -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? { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt index f4a24547..5d6582e6 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt @@ -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 { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderRingService.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderRingService.kt index 93aaaa91..47244dd3 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderRingService.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderRingService.kt @@ -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)) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt index 63f8281c..4b359dee 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt @@ -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, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt index 80b553af..4aaaa3a0 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt @@ -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 onChannels(block: ReminderChannels.() -> T): T? = withContext(io) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e433d57..44e4c6ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -755,6 +755,7 @@ Ring until dismissed Your alarm sound and vibration, like an alarm, also in Do Not Disturb when alarms may sound Ringing reminders use your alarm sound. This sets the sound of the ones handed back to the shade. + Full-screen alerts held back Blocked in system settings, so this calendar\'s reminders only sound once — tap to allow Allow full-screen alerts Android blocks Calendula\'s full-screen alerts, so they only pop up as notifications. Tap to allow them.