diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt index df915e0f..089d7c2f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt @@ -171,6 +171,7 @@ class SettingsPrefs @Inject constructor( val remindersEnabled: Flow = prefs.flow(SettingsKeys.remindersEnabled) val mutedReminderCalendarIds: Flow> = prefs.flow(SettingsKeys.mutedReminderCalendarIds) val reminderAlertStyles: Flow> = prefs.flow(SettingsKeys.reminderAlertStyles) + val notificationCategoryHintDismissed: Flow = prefs.flow(SettingsKeys.notificationCategoryHintDismissed) val allowColorOnUnsupportedCalendars: Flow = prefs.flow(SettingsKeys.allowColorOnUnsupportedCalendars) val defaultReminderMinutes: Flow> = prefs.flow(SettingsKeys.defaultReminderMinutes) @@ -278,6 +279,8 @@ class SettingsPrefs @Inject constructor( suspend fun setRemindersEnabled(enabled: Boolean) = prefs.set(SettingsKeys.remindersEnabled, enabled) + suspend fun dismissNotificationCategoryHint() = prefs.set(SettingsKeys.notificationCategoryHintDismissed, true) + suspend fun setCalendarRemindersEnabled(calendarId: Long, enabled: Boolean) { prefs.update(SettingsKeys.mutedReminderCalendarIds) { if (enabled) it - calendarId else it + calendarId } } @@ -617,6 +620,12 @@ object SettingsKeys { val reminderAlertStyles: Pref> = nullableStringPref("reminder_alert_styles").map(::parseAlertStyles) { it.toStoredAlertStyles() } + /** + * Whether the one-time hint that some skins (Samsung) start new notification + * categories switched off has been dismissed (#145). + */ + val notificationCategoryHintDismissed: Pref = booleanPref("notification_category_hint_dismissed", false) + /** * Whether to offer a custom event colour on calendars that publish no colour * palette; synced ones without a palette (some CalDAV) may drop or overwrite diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt index da13a7cf..aa3a6b68 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt @@ -131,3 +131,41 @@ fun planReminderChannels( } private fun accountGroupId(type: String, name: String): String = "$GROUP_PREFIX$type/$name" + +/** A channel as far as its blocked state goes: switched off itself, and the group it sits in. */ +data class ReminderChannelState(val blocked: Boolean, val groupId: String?) + +/** Reminder notifications the system blocks, while Calendula would post them. */ +data class ReminderChannelStatus( + val appBlocked: Boolean = false, + val blockedCalendarIds: Set = emptySet(), + /** The blocked ones whose whole account group is off, which only the app's own screen can undo. */ + val groupBlockedCalendarIds: Set = emptySet(), + /** The shared ring channel (#171) is switched off, so ringing calendars only sound once. */ + val ringBlocked: Boolean = false, +) + +/** + * What the system blocks of [calendarIds]' reminders: all of them while the + * app's notifications are off, otherwise those whose channel, or the group it + * sits in, is switched off. A calendar without a channel yet isn't blocked. + */ +fun reminderChannelStatus( + calendarIds: Collection, + appEnabled: Boolean, + channels: Map, + blockedGroupIds: Set, +): ReminderChannelStatus { + val groupBlocked = calendarIds.filterTo(HashSet()) { channels[reminderChannelId(it)]?.groupId in blockedGroupIds } + val blocked = if (!appEnabled) { + calendarIds.toSet() + } else { + calendarIds.filterTo(HashSet()) { channels[reminderChannelId(it)]?.blocked == true } + groupBlocked + } + return ReminderChannelStatus( + appBlocked = !appEnabled, + blockedCalendarIds = blocked, + groupBlockedCalendarIds = groupBlocked, + ringBlocked = appEnabled && channels[RING_CHANNEL_ID]?.blocked == true, + ) +} 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 c6686ed0..368f228c 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 @@ -137,22 +137,38 @@ class ReminderChannels @Inject constructor( Log.w(TAG, "Could not look up calendar $calendarId for its channel", e) emptyList() } + val fallback = ReminderChannelSpec(id, calendarId, context.getString(R.string.reminder_channel_name), null) if (calendars.isEmpty()) { - val fallback = ReminderChannelSpec(id, calendarId, context.getString(R.string.reminder_channel_name), null) write(emptyList(), listOf(fallback), existing) return id } val plan = plan(calendars, existing, emptyList()) val spec = plan.channels.firstOrNull { it.calendarId == calendarId } ?: return null - val knownGroups = manager.notificationChannelGroups.mapTo(HashSet()) { it.id } - write( - groups = plan.groups.filter { it.id == spec.groupId && it.id !in knownGroups }, - channels = listOf(spec), - existing = existing, - ) + try { + val knownGroups = manager.notificationChannelGroups.mapTo(HashSet()) { it.id } + write( + groups = plan.groups.filter { it.id == spec.groupId && it.id !in knownGroups }, + channels = listOf(spec), + existing = existing, + ) + } catch (e: RuntimeException) { + Log.w(TAG, "Could not file the channel of calendar $calendarId", e) + write(emptyList(), listOf(fallback), existing) + } return id } + /** What the system currently lets through for [calendarIds]' reminders (#145). */ + fun status(calendarIds: Collection): ReminderChannelStatus { + val manager = manager + val appEnabled = manager.areNotificationsEnabled() + val channels = manager.notificationChannels.associate { + it.id to ReminderChannelState(it.importance == NotificationManager.IMPORTANCE_NONE, it.group) + } + val blockedGroups = manager.notificationChannelGroups.filter { it.isBlocked }.mapTo(HashSet()) { it.id } + return reminderChannelStatus(calendarIds, appEnabled, channels, blockedGroups) + } + private fun plan( calendars: List, existing: List, @@ -176,9 +192,9 @@ class ReminderChannels @Inject constructor( } if (channels.isEmpty()) return val byId = existing.associateBy { it.id } + val description = context.getString(R.string.reminder_channel_description) val template = byId[LEGACY_REMINDER_CHANNEL_ID] - // Only a channel's first creation sets its behaviour; later calls just - // rename it, so an existing one passes its own importance back. + // Importance only applies on creation; an existing channel keeps its own. manager.createNotificationChannels( channels.map { spec -> val current = byId[spec.id] @@ -187,7 +203,7 @@ class ReminderChannels @Inject constructor( spec.name, current?.importance ?: template?.importance ?: NotificationManager.IMPORTANCE_HIGH, ).apply { - description = context.getString(R.string.reminder_channel_description) + this.description = description spec.groupId?.let { group = it } if (current == null && template != null) inheritFrom(template) } @@ -196,8 +212,8 @@ class ReminderChannels @Inject constructor( } /** - * Carry the old single channel's sound, vibration and lock-screen choices - * over to a new per-calendar one, so an upgrade keeps how reminders sounded. + * Carry the old single channel's sound, vibration, lights and badge over to + * a new per-calendar one (the system resets lock-screen visibility itself), so an upgrade keeps how reminders sounded. */ private fun NotificationChannel.inheritFrom(template: NotificationChannel) { setSound(template.sound, template.audioAttributes) @@ -206,7 +222,6 @@ class ReminderChannels @Inject constructor( enableVibration(template.shouldVibrate()) enableLights(template.shouldShowLights()) lightColor = template.lightColor - lockscreenVisibility = template.lockscreenVisibility setShowBadge(template.canShowBadge()) // Only honoured with Do Not Disturb access, so usually left to the user. setBypassDnd(template.canBypassDnd()) @@ -241,3 +256,4 @@ class ReminderChannels @Inject constructor( const val TAG = "ReminderChannels" } } + 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 bdea773d..f4a24547 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 @@ -127,7 +127,9 @@ class ReminderNotifier @Inject constructor( } private fun channelFor(alert: ReminderAlert, style: ReminderAlertStyle): String? = try { - channels.channelFor(alert.calendarId, style) + channels.channelFor(alert.calendarId, style).also { + if (it == null) Log.i(TAG, "Dropped the reminder of event ${alert.eventId}: calendar ${alert.calendarId} is gone") + } } catch (e: Exception) { Log.w(TAG, "No channel for the reminder of event ${alert.eventId}", e) null 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 cef7545b..63f8281c 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 @@ -9,27 +9,36 @@ import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.animation.AnimatedVisibility +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight import androidx.compose.material.icons.automirrored.filled.OpenInNew +import androidx.compose.material.icons.outlined.Info import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.core.net.toUri import androidx.lifecycle.compose.LifecycleResumeEffect +import androidx.lifecycle.compose.collectAsStateWithLifecycle import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.data.reminders.RING_CHANNEL_ID import de.jeanlucmakiola.calendula.domain.CalendarSource import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle import de.jeanlucmakiola.calendula.domain.reminders.alertStyleFor @@ -46,6 +55,7 @@ import de.jeanlucmakiola.floret.components.CollapsingScaffold import de.jeanlucmakiola.floret.components.ExpandIcon import de.jeanlucmakiola.floret.components.GroupedRow import de.jeanlucmakiola.floret.components.GroupedSwitchRow +import de.jeanlucmakiola.floret.components.IconInfoCard import de.jeanlucmakiola.floret.components.OptionPicker import de.jeanlucmakiola.floret.components.Position import de.jeanlucmakiola.floret.components.SelectedCheck @@ -97,8 +107,14 @@ internal fun NotificationsScreen( var calendarSectionExpanded by remember { mutableStateOf(false) } var expandedCalendars by remember { mutableStateOf(emptySet()) } val scope = rememberCoroutineScope() + val channelStatus by viewModel.channelStatus.collectAsStateWithLifecycle() + val calendarIds = state.reminderCalendars.map { it.id } + LifecycleResumeEffect(calendarIds) { + viewModel.refreshChannelStatus(calendarIds) + onPauseOrDispose { } + } val openCalendarSound: (Long) -> Unit = { calendarId -> - scope.launch { context.openNotificationSettings(viewModel.reminderChannelFor(calendarId)) } + scope.launch { context.openNotificationSettings(viewModel.soundSettingsChannel(calendarId)) } } CollapsingScaffold( @@ -107,6 +123,19 @@ internal fun NotificationsScreen( predictiveBack = true, visible = visible, ) { + if (IS_SAMSUNG && !state.notificationCategoryHintDismissed) { + NotificationCategoryHint( + onOpen = { + viewModel.dismissNotificationCategoryHint() + scope.launch { + viewModel.syncReminderChannels() + context.openNotificationSettings() + } + }, + onDismiss = viewModel::dismissNotificationCategoryHint, + ) + Spacer(Modifier.height(16.dp)) + } GroupedSwitchRow( title = stringResource(R.string.settings_reminders), checked = state.remindersEnabled, @@ -116,7 +145,11 @@ internal fun NotificationsScreen( ) GroupedRow( title = stringResource(R.string.settings_notification_sound), - summary = stringResource(R.string.settings_notification_sound_hint), + summary = if (channelStatus.appBlocked) { + stringResource(R.string.settings_notifications_blocked_hint) + } else { + stringResource(R.string.settings_notification_sound_hint) + }, position = Position.Middle, trailing = { ExternalLinkIcon() }, onClick = { @@ -175,9 +208,7 @@ internal fun NotificationsScreen( onClick = { showSnooze = true }, ) - // Per-calendar settings, folded behind one header: every switched-on - // calendar gets its notification switch and sound (#145, #272); the ones - // you can edit also keep, drop or replace the default reminders. + // Per-calendar notifications and reminder overrides (#145, #272). if (state.reminderCalendars.isNotEmpty()) { Spacer(Modifier.height(24.dp)) GroupedRow( @@ -197,9 +228,14 @@ internal fun NotificationsScreen( Spacer(Modifier.height(16.dp)) val expanded = calendar.id in expandedCalendars val notifying = calendar.id !in state.mutedReminderCalendarIds + val blocked = notifying && calendar.id in channelStatus.blockedCalendarIds GroupedRow( title = calendar.displayName.orUnnamed(), - summary = if (notifying) null else stringResource(R.string.settings_calendar_notifications_off), + summary = when { + !notifying -> stringResource(R.string.settings_calendar_notifications_off) + blocked -> stringResource(R.string.settings_notifications_blocked) + else -> null + }, position = if (expanded) Position.Top else Position.Alone, leading = { CalendarColorChip(calendar.color) }, trailing = { ExpandIcon(expanded) }, @@ -220,6 +256,9 @@ internal fun NotificationsScreen( calendar = calendar, state = state, notifying = notifying, + blocked = blocked, + ringBlocked = channelStatus.ringBlocked, + onOpenRingSettings = { context.openNotificationSettings(RING_CHANNEL_ID) }, onSetNotifying = { viewModel.setCalendarRemindersEnabled(calendar.id, it) }, onOpenSound = { openCalendarSound(calendar.id) }, canFullScreen = canFullScreen, @@ -370,6 +409,9 @@ private fun CalendarNotificationRows( calendar: CalendarSource, state: SettingsUiState, notifying: Boolean, + blocked: Boolean, + ringBlocked: Boolean, + onOpenRingSettings: () -> Unit, onSetNotifying: (Boolean) -> Unit, onOpenSound: () -> Unit, canFullScreen: Boolean, @@ -383,6 +425,7 @@ private fun CalendarNotificationRows( val takesOverrides = calendar.canModifyContents && !managed val alertStyle = state.reminderAlertStyles.alertStyleFor(calendar.id) val needsPermission = alertStyle.isFullScreen && !canFullScreen + val ringHeldBack = alertStyle == ReminderAlertStyle.FullScreenRing && ringBlocked && !blocked Column { GroupedSwitchRow( title = stringResource(R.string.settings_calendar_notifications), @@ -393,13 +436,12 @@ private fun CalendarNotificationRows( ) GroupedRow( title = stringResource(R.string.settings_notification_sound), - summary = stringResource( - if (alertStyle == ReminderAlertStyle.FullScreenRing) { - R.string.settings_calendar_notification_sound_ring_hint - } else { - R.string.settings_calendar_notification_sound_hint - }, - ), + summary = when { + blocked -> stringResource(R.string.settings_notifications_blocked_hint) + alertStyle == ReminderAlertStyle.FullScreenRing -> + stringResource(R.string.settings_calendar_notification_sound_ring_hint) + else -> stringResource(R.string.settings_calendar_notification_sound_hint) + }, position = Position.Middle, trailing = { ExternalLinkIcon() }, onClick = onOpenSound, @@ -415,10 +457,19 @@ private fun CalendarNotificationRows( GroupedRow( title = stringResource(R.string.settings_alert_sound), summary = alertSoundLabel(alertStyle), - position = if (lastAlertRow && !needsPermission) Position.Bottom else Position.Middle, + position = if (lastAlertRow && !needsPermission && !ringHeldBack) Position.Bottom else Position.Middle, onClick = onEditAlertSound, ) } + if (ringHeldBack) { + GroupedRow( + title = stringResource(R.string.reminder_ring_channel_name), + summary = stringResource(R.string.settings_ring_blocked_hint), + position = if (lastAlertRow && !needsPermission) Position.Bottom else Position.Middle, + trailing = { ExternalLinkIcon() }, + onClick = onOpenRingSettings, + ) + } if (needsPermission) { GroupedRow( title = stringResource(R.string.settings_full_screen_permission), @@ -461,6 +512,28 @@ private fun CalendarNotificationRows( } } +/** Samsung's One UI can start a new notification category switched off (#145). */ +private val IS_SAMSUNG = Build.MANUFACTURER.equals("samsung", ignoreCase = true) + +/** The one-time Samsung note, with a way into the app's notification settings. */ +@Composable +private fun NotificationCategoryHint(onOpen: () -> Unit, onDismiss: () -> Unit) { + IconInfoCard( + icon = Icons.Outlined.Info, + iconContentDescription = null, + iconAlignment = Alignment.Top, + ) { + Text( + text = stringResource(R.string.settings_notification_category_hint), + style = MaterialTheme.typography.bodyMedium, + ) + Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + TextButton(onClick = onDismiss) { Text(stringResource(R.string.reminder_action_dismiss)) } + TextButton(onClick = onOpen) { Text(stringResource(R.string.settings_notification_category_hint_open)) } + } + } +} + @Composable private fun ExternalLinkIcon() { Icon( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt index af27bcf5..12edff70 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt @@ -97,7 +97,7 @@ data class SettingsUiState( val perCalendarReminderOverride: Map> = emptyMap(), /** Per-calendar overrides of [defaultAllDayReminderMinutes] for all-day events. */ val perCalendarAllDayReminderOverride: Map> = emptyMap(), - /** Switched-on calendars you can edit: the per-calendar duration and reminder-override rows. */ + /** Switched-on calendars you can edit: the per-calendar event-duration rows. */ val writableCalendars: List = emptyList(), /** Switched-on calendars, each with its own notification switch and channel (#145, #272). */ val reminderCalendars: List = emptyList(), @@ -105,6 +105,8 @@ data class SettingsUiState( val mutedReminderCalendarIds: Set = emptySet(), /** Per-calendar alert styles (#171); absent means a plain notification. */ val reminderAlertStyles: Map = emptyMap(), + /** Whether the Samsung notification-category hint has been dismissed (#145); hidden until prefs load. */ + val notificationCategoryHintDismissed: Boolean = true, /** * Whether the event-colour picker is offered on calendars that publish no * colour palette (the colour may then not survive their next sync). 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 265a22df..80b553af 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 @@ -21,6 +21,7 @@ import de.jeanlucmakiola.floret.android.stateWhileSubscribed import de.jeanlucmakiola.floret.di.IoDispatcher import de.jeanlucmakiola.floret.reminders.ReminderOverride import de.jeanlucmakiola.floret.reminders.reminderOverrideFor +import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefs import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay import de.jeanlucmakiola.calendula.data.prefs.SettingsKeys import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs @@ -34,6 +35,7 @@ import de.jeanlucmakiola.calendula.data.prefs.SpecialDatesStalledReason import de.jeanlucmakiola.floret.prefs.ThemeMode import de.jeanlucmakiola.calendula.data.prefs.TimeFormatPref import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref +import de.jeanlucmakiola.calendula.data.reminders.ReminderChannelStatus import de.jeanlucmakiola.calendula.data.reminders.ReminderChannels import de.jeanlucmakiola.calendula.data.reminders.ReminderNotifier import de.jeanlucmakiola.calendula.data.reminders.RingMatch @@ -52,6 +54,7 @@ import de.jeanlucmakiola.calendula.ui.theme.AppFontSettings import de.jeanlucmakiola.calendula.ui.theme.FONT_CUSTOM_TOKEN import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -68,6 +71,7 @@ import javax.inject.Inject @HiltViewModel class SettingsViewModel @Inject constructor( private val prefs: SettingsPrefs, + calendarPrefs: CalendarPrefs, repository: CalendarRepository, private val specialDatesEngine: SpecialDatesSyncEngine, specialDatesSpec: SpecialDatesCalendarSpec, @@ -85,9 +89,10 @@ class SettingsViewModel @Inject constructor( * Calendars that are switched on — the only ones that plan reminders at * all (#75), so the only ones with notification settings. */ - private val visibleCalendars: Flow> = repository.calendars() - .map { calendars -> calendars.filter { it.isVisibleInSystem } } - .catch { emit(emptyList()) } + private val visibleCalendars: Flow> = + combine(repository.calendars(), calendarPrefs.pendingDisabledCalendarIds) { calendars, pending -> + calendars.filter { it.isVisibleInSystem && it.id !in pending } + }.catch { emit(emptyList()) } val state: StateFlow = combine(prefs.data, visibleCalendars) { stored, calendars -> stored.toUiState(calendars) } @@ -136,6 +141,7 @@ class SettingsViewModel @Inject constructor( reminderCalendars = calendars, mutedReminderCalendarIds = this[SettingsKeys.mutedReminderCalendarIds], reminderAlertStyles = this[SettingsKeys.reminderAlertStyles], + notificationCategoryHintDismissed = this[SettingsKeys.notificationCategoryHintDismissed], allowColorOnUnsupportedCalendars = this[SettingsKeys.allowColorOnUnsupportedCalendars], managedCalendarIds = specialDatesCalendars.values.toSet(), ) @@ -368,9 +374,14 @@ class SettingsViewModel @Inject constructor( } } - /** Re-plans, since a muted calendar armed no wake-up for its next reminder. */ + /** + * Re-plans, since a muted calendar armed no wake-up for its next reminder. + * Unmuting scans once more before the write, so the watermark starts at the + * unmute and nothing that came due while muted is replayed. + */ fun setCalendarRemindersEnabled(calendarId: Long, enabled: Boolean) { viewModelScope.launch { + if (enabled) reminderScanner.scan() prefs.setCalendarRemindersEnabled(calendarId, enabled) if (!enabled) reminderNotifier.stopRing(RingMatch.Calendar(calendarId)) reminderScanner.scan() @@ -387,8 +398,32 @@ class SettingsViewModel @Inject constructor( } } - /** [calendarId]'s reminder channel, created if needed so its system settings can open (#340). */ - suspend fun reminderChannelFor(calendarId: Long): String? = onChannels { channelFor(calendarId) } + private val _channelStatus = MutableStateFlow(ReminderChannelStatus()) + + /** What the system blocks of the reminders; re-read with [refreshChannelStatus] (#145). */ + val channelStatus: StateFlow = _channelStatus.asStateFlow() + + private var channelStatusJob: Job? = null + + fun refreshChannelStatus(calendarIds: Collection) { + channelStatusJob?.cancel() + channelStatusJob = viewModelScope.launch { + onChannels { status(calendarIds) }?.let { _channelStatus.value = it } + } + } + + fun dismissNotificationCategoryHint() = save { dismissNotificationCategoryHint() } + + /** + * The channel whose system screen to open for [calendarId] (#340), created + * if needed; null when only the app's own screen can help — the app or the + * calendar's account group is blocked there, or the channel couldn't be made. + */ + suspend fun soundSettingsChannel(calendarId: Long): String? = onChannels { + val channelId = channelFor(calendarId) ?: return@onChannels null + val status = status(listOf(calendarId)) + channelId.takeUnless { status.appBlocked || calendarId in status.groupBlockedCalendarIds } + } /** Create the per-calendar channels before showing the app's channel list, even with reminders off. */ suspend fun syncReminderChannels() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e9252d1c..4e433d57 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -739,6 +739,10 @@ Notifications off Notification sound Sound, vibration and pop-ups for each calendar, in system settings + Blocked in system settings + Blocked in system settings — tap to allow + On Samsung phones, new notification categories can start switched off. If a calendar\'s reminders don\'t arrive, check that its category is on. + Open settings Sound, vibration and pop-ups for this calendar, in system settings Alert style Notification @@ -751,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. + 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. Default (%1$s) diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt index e32fa3a4..af4bb15e 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt @@ -127,6 +127,32 @@ class ReminderChannelPlanTest { assertThat(posted.groupsInUse).hasSize(1) } + @Test + fun `blocked calendars follow app, channel and group`() { + val channels = mapOf( + reminderChannelId(1) to ReminderChannelState(blocked = true, groupId = "g1"), + reminderChannelId(2) to ReminderChannelState(blocked = false, groupId = "g2"), + reminderChannelId(3) to ReminderChannelState(blocked = false, groupId = "g1"), + ) + val ids = listOf(1L, 2L, 3L, 4L) + val groupOff = reminderChannelStatus(ids, appEnabled = true, channels, blockedGroupIds = setOf("g2")) + assertThat(groupOff.blockedCalendarIds).containsExactly(1L, 2L) + assertThat(groupOff.groupBlockedCalendarIds).containsExactly(2L) + assertThat(reminderChannelStatus(ids, appEnabled = true, channels, blockedGroupIds = emptySet()).blockedCalendarIds) + .containsExactly(1L) + val appOff = reminderChannelStatus(ids, appEnabled = false, channels, blockedGroupIds = emptySet()) + assertThat(appOff.appBlocked).isTrue() + assertThat(appOff.blockedCalendarIds).containsExactly(1L, 2L, 3L, 4L) + } + + @Test + fun `a switched-off ring channel is reported while the app may notify`() { + val ringOff = mapOf(RING_CHANNEL_ID to ReminderChannelState(blocked = true, groupId = null)) + assertThat(reminderChannelStatus(listOf(1L), appEnabled = true, ringOff, emptySet()).ringBlocked).isTrue() + assertThat(reminderChannelStatus(listOf(1L), appEnabled = false, ringOff, emptySet()).ringBlocked).isFalse() + assertThat(reminderChannelStatus(listOf(1L), appEnabled = true, emptyMap(), emptySet()).ringBlocked).isFalse() + } + private companion object { const val DESC = "Reminders" } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index ceb3ae50..869d7d71 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -407,7 +407,9 @@ the system's channel screens (#340); the app never sets a sound itself. Separate from both, `SettingsPrefs.mutedReminderCalendarIds` is the in-app per-calendar off switch (#272): the scan plans nothing for those calendars (so they arm no wake-ups; toggling it re-scans), and `ReminderNotifier.post` checks it again -for snoozes. +for snoozes. The settings screen re-reads `ReminderChannels.status` on every +resume and marks a calendar "Blocked in system settings" when the app, its +channel or its account group is switched off there. **Full-screen alerts (#171).** Settings → Notifications gives each calendar an alert style (`SettingsPrefs.reminderAlertStyles`): a plain notification, a @@ -427,7 +429,8 @@ from the screen, the notification or a swipe, all go through the service as commands, so a dismiss sent while a newer ring is on its way applies in order rather than killing it, and the service re-reads the settings once a ring arrives, so a style change or mute that raced a scan still wins. A -blocked calendar, account group or ring channel never rings, the tone pauses +blocked calendar, account group or ring channel never rings (Settings flags a +blocked ring channel next to each ringing calendar, beside #145's blocked status), the tone pauses for a phone call, and a ring restarted after a process death only rings out what was left of its ten minutes. When Android 14+ withholds `USE_FULL_SCREEN_INTENT`, the intent is left off and the alert is a diff --git a/floret-kit b/floret-kit index 1c6c96fe..2fc730dd 160000 --- a/floret-kit +++ b/floret-kit @@ -1 +1 @@ -Subproject commit 1c6c96fe33ef32692948aa8c603091737e99390f +Subproject commit 2fc730dd44d1c8a97384ceb0146d3111aa7969d5