Repository: thunderbird/thunderbird-android (language: kotlin)

Issue:
Quiet time always acts as enabled
~~**Note:** This issue is based only on code analysis and problems observed in tests. I did not check if it's a problem when using the app.~~ **EDIT:** Tested, see https://github.com/thunderbird/thunderbird-android/issues/9629#issuecomment-3258078905

When Quiet Time is not enabled, notifications are issued as silent between Quiet Time start and Quiet Time end. #9448 introduced new `isQuietTime` methods that do not check the `isQuietTimeEnabled` preference:

1. https://github.com/thunderbird/thunderbird-android/blob/39a6a6d559760d7052fc6c0cc871a8d5bdf6fb64/legacy/core/src/main/java/com/fsck/k9/notification/NotificationHelper.kt#L143-L153
2. https://github.com/thunderbird/thunderbird-android/blob/39a6a6d559760d7052fc6c0cc871a8d5bdf6fb64/legacy/common/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt#L85-L95

The earlier version introduced in #9405 - `getIsQuietTime` - wasn't correct either. It returned that Quiet Time was disabled when it was enabled:

- old and correct version:
  https://github.com/thunderbird/thunderbird-android/blob/616705c1f39446845c4ad491d9cd73c9b0d87c77/legacy/core/src/main/java/com/fsck/k9/K9.kt#L260-L269
- version that replaced it in #9405 (notice the missing `!` in line 179):
  https://github.com/thunderbird/thunderbird-android/blob/6a7ef80f09cffda73031e129d16756bcdb16deb7/legacy/core/src/main/java/com/fsck/k9/preferences/RealGeneralSettingsManager.kt#L164-L190


## Original issue description

4 tests related to notifications fail between 23:00 and 00:00 of machine's local time.

```
NewMailNotificationManagerTest > add first notification FAILED
    org.opentest4j.AssertionFailedError: expected to be false
        at app//com.fsck.k9.notification.NewMailNotificationManagerTest.add first notification(NewMailNotificationManagerTest.kt:117)

NewMailNotificationManagerTest > add second notification FAILED
    org.opentest4j.AssertionFailedError: expected to be false
        at app//com.fsck.k9.notification.NewMailNotificationManagerTest.add second notification(NewMailNotificationManagerTest.kt:163)

SummaryNotificationDataCreatorTest > single notification with quiet time disabled FAILED
    org.opentest4j.AssertionFailedError: expected to be false
        at app//com.fsck.k9.notification.SummaryNotificationDataCreatorTest.single notification with quiet time disabled(SummaryNotificationDataCreatorTest.kt:116)

SummaryNotificationDataCreatorTest > inbox-style notification with quiet time disabled FAILED
    org.opentest4j.AssertionFailedError: expected to be false
        at app//com.fsck.k9.notification.SummaryNotificationDataCreatorTest.inbox-style notification with quiet time disabled(SummaryNotificationDataCreatorTest.kt:152)
```

Example failed CI build: https://github.com/thunderbird/thunderbird-android/actions/runs/16762410368/job/47460999106



Discussion hints:
The failing tests:

1. use quiet time 23:00 - 00:00
2. create notifications with `silent` argument `false`
3. expect the `.isSilent` property to be `false`

However, notifications are silent if `silent` is `true` or it is quiet time. Since these tests do not change the clock, running them after 23:00 (local time) results in silent notifications and failures.

https://github.com/thunderbird/thunderbird-android/blob/f952209a8128ef0b186661cf4d8acba7d7dd79ce/legacy/core/src/main/java/com/fsck/k9/notification/SummaryNotificationDataCreator.kt#L13-L15

Pull request: fix(notifications): Respect isQuietTimeEnabled
This PR restores quiet time behaviour from before #9405/TB12. It resolves problems with tests failing between 23:00 and 00:00 and modifies one of the tests, so that such regression will be caught earlier in the future (not only after 23:00).

Fixes #9629

<!-- Please ensure that your pull request meets the following requirements - thanks!

- Does not contain merge commits. Rebase instead.
- Contains commits with descriptive titles.
- New code is written in Kotlin whenever possible.
- Follows our existing codestyle (`gradlew spotlessCheck` to check and `gradlew spotlessApply` to format your source code; will be checked by CI).
- Does not break any unit tests (`gradlew testDebugUnitTest`; will be checked by CI).
- Uses a descriptive title; don't put issue numbers in there.
- Contains a reference to the issue that it fixes (e.g. _Closes #XXX_ or _Fixes #XXX_) in the body text.
- For cosmetic changes add one or multiple images, if possible.

Finally, please replace this template text with a description of the change and additional context if necessary.
-->

Test command: ./gradlew :legacy:core:testDebugUnitTest --tests "com.fsck.k9.notification.SummaryNotificationDataCreatorTest"

Fix patch:
diff --git a/legacy/common/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt b/legacy/common/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt
index 3bf142cdd79..d84f4f677a6 100644
--- a/legacy/common/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt
+++ b/legacy/common/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt
@@ -85,6 +85,10 @@ class K9NotificationStrategy(
     @OptIn(ExperimentalTime::class)
     private val NotificationPreference.isQuietTime: Boolean
         get() {
+            if (!isQuietTimeEnabled) {
+                return false
+            }
+
             val clock = DI.get<Clock>()
             val quietTimeChecker = QuietTimeChecker(
                 clock = clock,
diff --git a/legacy/core/src/main/java/com/fsck/k9/notification/NotificationHelper.kt b/legacy/core/src/main/java/com/fsck/k9/notification/NotificationHelper.kt
index f6504faecb3..3f99f873da1 100644
--- a/legacy/core/src/main/java/com/fsck/k9/notification/NotificationHelper.kt
+++ b/legacy/core/src/main/java/com/fsck/k9/notification/NotificationHelper.kt
@@ -143,6 +143,10 @@ internal fun NotificationCompat.Builder.setAppearance(
 @OptIn(ExperimentalTime::class)
 internal val NotificationPreference.isQuietTime: Boolean
     get() {
+        if (!isQuietTimeEnabled) {
+            return false
+        }
+
         val clock = DI.get<Clock>()
         val quietTimeChecker = QuietTimeChecker(
             clock = clock,


Test patch:
diff --git a/legacy/core/src/test/java/com/fsck/k9/notification/SummaryNotificationDataCreatorTest.kt b/legacy/core/src/test/java/com/fsck/k9/notification/SummaryNotificationDataCreatorTest.kt
index 3a35507d448..4274f621877 100644
--- a/legacy/core/src/test/java/com/fsck/k9/notification/SummaryNotificationDataCreatorTest.kt
+++ b/legacy/core/src/test/java/com/fsck/k9/notification/SummaryNotificationDataCreatorTest.kt
@@ -104,6 +104,7 @@ class SummaryNotificationDataCreatorTest {
 
     @Test
     fun `single notification with quiet time disabled`() {
+        setClockTo("23:01")
         setQuietTime(false)
         val notificationData = createNotificationData()