diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt index 11960e76dd..f947a08647 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt @@ -60,8 +60,7 @@ class EditNotificationProfileScheduleViewModel( } fun setEndTime(hour: Int, minute: Int) { - val adjustedEndHour = if (hour == 0) 24 else hour - scheduleSubject.onNext(schedule.copy(end = adjustedEndHour * 100 + minute)) + scheduleSubject.onNext(schedule.copy(end = hour * 100 + minute)) } fun setEnabled(enabled: Boolean) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt index 61ecaaca96..c25d60a88c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt @@ -115,6 +115,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V255_AddCallTableLo import org.thoughtcrime.securesms.database.helpers.migration.V256_FixIncrementalDigestColumns import org.thoughtcrime.securesms.database.helpers.migration.V257_CreateBackupMediaSyncTable import org.thoughtcrime.securesms.database.helpers.migration.V258_FixGroupRevokedInviteeUpdate +import org.thoughtcrime.securesms.database.helpers.migration.V259_AdjustNotificationProfileMidnightEndTimes /** * Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness. @@ -232,10 +233,11 @@ object SignalDatabaseMigrations { 255 to V255_AddCallTableLogIndex, 256 to V256_FixIncrementalDigestColumns, 257 to V257_CreateBackupMediaSyncTable, - 258 to V258_FixGroupRevokedInviteeUpdate + 258 to V258_FixGroupRevokedInviteeUpdate, + 259 to V259_AdjustNotificationProfileMidnightEndTimes ) - const val DATABASE_VERSION = 258 + const val DATABASE_VERSION = 259 @JvmStatic fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V259_AdjustNotificationProfileMidnightEndTimes.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V259_AdjustNotificationProfileMidnightEndTimes.kt new file mode 100644 index 0000000000..3e18bb9afb --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V259_AdjustNotificationProfileMidnightEndTimes.kt @@ -0,0 +1,23 @@ +package org.thoughtcrime.securesms.database.helpers.migration + +import android.app.Application +import net.zetetic.database.sqlcipher.SQLiteDatabase + +/** + * Adjust notification profile schedules with end times between midnight at 1am. These were originally + * stored as 24xx and will now use the same as start with 00xx. + */ +@Suppress("ClassName") +object V259_AdjustNotificationProfileMidnightEndTimes : SignalDatabaseMigration { + override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL( + """ + UPDATE + notification_profile_schedule + SET 'end' = end - 2400 + WHERE + end >= 2400 + """ + ) + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt index 1bdf9efc60..8ad3487dfe 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt @@ -39,9 +39,8 @@ data class NotificationProfileSchedule( val localStart: LocalDateTime = start.toLocalDateTime(localNow) val localEnd: LocalDateTime = end.toLocalDateTime(localNow) - return if (end < start) { - (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd)) || - (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1))) + return if (end <= start) { + (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd)) || (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1))) } else { daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd) } @@ -55,7 +54,7 @@ data class NotificationProfileSchedule( val localStart: LocalDateTime = start.toLocalDateTime(localNow) val localEnd: LocalDateTime = end.toLocalDateTime(localNow) - return if (end < start && (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd))) { + return if (end <= start && (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd))) { localStart.minusDays(1) } else { localStart @@ -63,15 +62,14 @@ data class NotificationProfileSchedule( } fun endTime(): LocalTime { - val adjustedEnd = if (end == 2400) 0 else end - return LocalTime.of(adjustedEnd / 100, adjustedEnd % 100) + return LocalTime.of(end / 100, end % 100) } fun endDateTime(localNow: LocalDateTime): LocalDateTime { val localStart: LocalDateTime = start.toLocalDateTime(localNow) val localEnd: LocalDateTime = end.toLocalDateTime(localNow) - return if (end < start && (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))) { + return if (end <= start && (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))) { localEnd.plusDays(1) } else { localEnd @@ -80,9 +78,5 @@ data class NotificationProfileSchedule( } fun Int.toLocalDateTime(now: LocalDateTime): LocalDateTime { - if (this == 2400) { - return now.plusDays(1).withHour(0).withMinute(0).withSecond(0) - } - return now.withHour(this / 100).withMinute(this % 100).withSecond(0) } diff --git a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt index a9f8af416c..b7459dae06 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt @@ -78,7 +78,7 @@ class NotificationProfileScheduleTest { } @Test - fun `when time is inside enabled schedule 12am to 10am then return false`() { + fun `when time is inside enabled schedule 12am to 10am then return true`() { val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 1000, daysEnabled = setOf(DayOfWeek.SUNDAY)) assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC))) @@ -87,8 +87,8 @@ class NotificationProfileScheduleTest { } @Test - fun `when time is inside enabled schedule 12am to 12am then return false`() { - val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = setOf(DayOfWeek.SUNDAY)) + fun `when time is inside enabled schedule 12am to 12am then return true`() { + val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = setOf(DayOfWeek.SUNDAY)) assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC))) assertTrue(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC))) @@ -98,9 +98,9 @@ class NotificationProfileScheduleTest { @Test fun `when time is outside enabled schedule 12am to 12am then return false`() { - val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = setOf(DayOfWeek.SUNDAY)) + val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = setOf(DayOfWeek.SUNDAY)) - assertFalse(schedule.isCurrentlyActive(monday0am.toMillis(ZoneOffset.UTC))) + assertFalse(schedule.isCurrentlyActive(monday0am.plusMinutes(1).toMillis(ZoneOffset.UTC))) assertFalse(schedule.isCurrentlyActive(monday1am.toMillis(ZoneOffset.UTC))) assertFalse(schedule.isCurrentlyActive(monday9am.toMillis(ZoneOffset.UTC))) assertFalse(schedule.isCurrentlyActive(monday10pm.toMillis(ZoneOffset.UTC))) @@ -111,7 +111,7 @@ class NotificationProfileScheduleTest { @Test fun `when enabled schedule 12am to 12am for all days then return true`() { - val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = DayOfWeek.entries.toSet()) + val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet()) assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC))) assertTrue(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC))) @@ -128,7 +128,7 @@ class NotificationProfileScheduleTest { @Test fun `when disabled schedule 12am to 12am for all days then return false`() { - val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 2400, daysEnabled = DayOfWeek.entries.toSet()) + val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet()) assertFalse(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC))) assertFalse(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC))) @@ -145,7 +145,7 @@ class NotificationProfileScheduleTest { @Test fun `when end time is midnight return midnight of next day from now`() { - val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 2400, daysEnabled = DayOfWeek.entries.toSet()) + val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet()) assertThat(schedule.endDateTime(sunday930am), `is`(monday0am)) } }