diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt index 43886e1e28..eddd472772 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt @@ -1,9 +1,13 @@ package org.thoughtcrime.securesms.notifications.profiles import android.content.Context +import org.signal.core.util.concurrent.SignalExecutors import org.thoughtcrime.securesms.R +import org.thoughtcrime.securesms.database.SignalDatabase import org.thoughtcrime.securesms.keyvalue.NotificationProfileValues import org.thoughtcrime.securesms.keyvalue.SignalStore +import org.thoughtcrime.securesms.recipients.Recipient +import org.thoughtcrime.securesms.storage.StorageSyncHelper import org.thoughtcrime.securesms.util.formatHours import org.thoughtcrime.securesms.util.toLocalDateTime import org.thoughtcrime.securesms.util.toLocalTime @@ -34,6 +38,13 @@ object NotificationProfiles { profile.schedule.startDateTime(localNow).toMillis(zoneId.toOffset()) > storeValues.manuallyDisabledAt } + if (shouldClearManualOverride(manualProfile, scheduledProfile)) { + SignalExecutors.UNBOUNDED.execute { + SignalDatabase.recipients.markNeedsSync(Recipient.self().id) + StorageSyncHelper.scheduleSyncForDataChange() + } + } + if (manualProfile == null || scheduledProfile == null) { return manualProfile ?: scheduledProfile } @@ -45,6 +56,24 @@ object NotificationProfiles { } } + private fun shouldClearManualOverride(manualProfile: NotificationProfile?, scheduledProfile: NotificationProfile?): Boolean { + val storeValues: NotificationProfileValues = SignalStore.notificationProfile + var shouldScheduleSync = false + + if (manualProfile == null && storeValues.manuallyEnabledProfile != 0L) { + storeValues.manuallyEnabledProfile = 0 + storeValues.manuallyEnabledUntil = 0 + shouldScheduleSync = true + } + + if (scheduledProfile != null && storeValues.manuallyDisabledAt != 0L) { + storeValues.manuallyDisabledAt = 0 + shouldScheduleSync = true + } + + return shouldScheduleSync + } + fun getActiveProfileDescription(context: Context, profile: NotificationProfile, now: Long = System.currentTimeMillis()): String { val storeValues: NotificationProfileValues = SignalStore.notificationProfile diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.kt b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.kt index 609d94b256..5d9e5849af 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.kt @@ -218,10 +218,12 @@ object StorageSyncHelper { endAtTimestampMs = endTimestamp ) ) - } else { + } else if (SignalStore.notificationProfile.manuallyDisabledAt != 0L) { AccountRecord.NotificationProfileManualOverride( disabledAtTimestampMs = SignalStore.notificationProfile.manuallyDisabledAt ) + } else { + AccountRecord.NotificationProfileManualOverride() } } diff --git a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt index 6c8d80f23d..7d510e5cd7 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt @@ -58,7 +58,7 @@ class NotificationProfilesTest { @Before fun setUp() { - notificationProfileValues = mockk() + notificationProfileValues = mockk(relaxed = true) every { notificationProfileValues.manuallyEnabledUntil } returns 0 every { notificationProfileValues.manuallyDisabledAt } returns 0