diff --git a/k9mail/src/main/java/com/fsck/k9/K9.java b/k9mail/src/main/java/com/fsck/k9/K9.java index 586919825..a9ae78d25 100644 --- a/k9mail/src/main/java/com/fsck/k9/K9.java +++ b/k9mail/src/main/java/com/fsck/k9/K9.java @@ -4,8 +4,6 @@ package com.fsck.k9; import java.io.File; import java.util.ArrayList; -import java.util.Calendar; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -998,41 +996,8 @@ public class K9 extends Application { return false; } - GregorianCalendar gregorianCalendar = new GregorianCalendar(); - - Integer startHour = Integer.parseInt(mQuietTimeStarts.split(":")[0]); - Integer startMinute = Integer.parseInt(mQuietTimeStarts.split(":")[1]); - Integer endHour = Integer.parseInt(mQuietTimeEnds.split(":")[0]); - Integer endMinute = Integer.parseInt(mQuietTimeEnds.split(":")[1]); - - Integer now = (gregorianCalendar.get(Calendar.HOUR) * 60) + gregorianCalendar.get(Calendar.MINUTE); - Integer quietStarts = startHour * 60 + startMinute; - Integer quietEnds = endHour * 60 + endMinute; - - // If start and end times are the same, we're never quiet - if (quietStarts.equals(quietEnds)) { - return false; - } - - - // 21:00 - 05:00 means we want to be quiet if it's after 9 or before 5 - if (quietStarts > quietEnds) { - // if it's 22:00 or 03:00 but not 8:00 - if (now >= quietStarts || now <= quietEnds) { - return true; - } - } - - // 01:00 - 05:00 - else { - - // if it' 2:00 or 4:00 but not 8:00 or 0:00 - if (now >= quietStarts && now <= quietEnds) { - return true; - } - } - - return false; + QuietTimeChecker quietTimeChecker = new QuietTimeChecker(Clock.INSTANCE, mQuietTimeStarts, mQuietTimeEnds); + return quietTimeChecker.isQuietTime(); } public static void setDebug(boolean debug) { diff --git a/k9mail/src/main/java/com/fsck/k9/QuietTimeChecker.java b/k9mail/src/main/java/com/fsck/k9/QuietTimeChecker.java new file mode 100644 index 000000000..23e036019 --- /dev/null +++ b/k9mail/src/main/java/com/fsck/k9/QuietTimeChecker.java @@ -0,0 +1,50 @@ +package com.fsck.k9; + + +import java.util.Calendar; + + +class QuietTimeChecker { + private final Clock clock; + private final int quietTimeStart; + private final int quietTimeEnd; + + + QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) { + this.clock = clock; + this.quietTimeStart = parseTime(quietTimeStart); + this.quietTimeEnd = parseTime(quietTimeEnd); + } + + private static int parseTime(String time) { + String[] parts = time.split(":"); + int hour = Integer.parseInt(parts[0]); + int minute = Integer.parseInt(parts[1]); + + return hour * 60 + minute; + } + + public boolean isQuietTime() { + // If start and end times are the same, we're never quiet + if (quietTimeStart == quietTimeEnd) { + return false; + } + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(clock.getTime()); + + int minutesSinceMidnight = (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE); + + if (quietTimeStart > quietTimeEnd) { + if (minutesSinceMidnight >= quietTimeStart || minutesSinceMidnight <= quietTimeEnd) { + return true; + } + } else { + if (minutesSinceMidnight >= quietTimeStart && minutesSinceMidnight <= quietTimeEnd) { + return true; + } + } + + return false; + } +} diff --git a/k9mail/src/test/java/com/fsck/k9/QuietTimeCheckerTest.kt b/k9mail/src/test/java/com/fsck/k9/QuietTimeCheckerTest.kt new file mode 100644 index 000000000..bbb306ae5 --- /dev/null +++ b/k9mail/src/test/java/com/fsck/k9/QuietTimeCheckerTest.kt @@ -0,0 +1,121 @@ +package com.fsck.k9 + + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.mockito.Mockito.mock +import java.util.* + + +class QuietTimeCheckerTest { + private val clock = mock(Clock::class.java) + + @Test + fun endTimeBeforeStartTime_timeIsBeforeEndOfQuietTime() { + setClockTo("02:00") + val quietTimeChecker = QuietTimeChecker(clock, "22:30", "06:45") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun endTimeBeforeStartTime_timeIsAfterEndOfQuietTime() { + setClockTo("10:00") + val quietTimeChecker = QuietTimeChecker(clock, "22:30", "06:45") + + assertFalse(quietTimeChecker.isQuietTime) + } + + @Test + fun endTimeBeforeStartTime_timeIsAfterStartOfQuietTime() { + setClockTo("23:00") + val quietTimeChecker = QuietTimeChecker(clock, "22:30", "06:45") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun endTimeBeforeStartTime_timeIsStartOfQuietTime() { + setClockTo("22:30") + val quietTimeChecker = QuietTimeChecker(clock, "22:30", "06:45") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun endTimeBeforeStartTime_timeIsEndOfQuietTime() { + setClockTo("06:45") + val quietTimeChecker = QuietTimeChecker(clock, "22:30", "06:45") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeBeforeEndTime_timeIsBeforeStartOfQuietTime() { + setClockTo("02:00") + val quietTimeChecker = QuietTimeChecker(clock, "09:00", "17:00") + + assertFalse(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeBeforeEndTime_timeIsAfterStartOfQuietTime() { + setClockTo("10:00") + val quietTimeChecker = QuietTimeChecker(clock, "09:00", "17:00") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeBeforeEndTime_timeIsAfterEndOfQuietTime() { + setClockTo("20:00") + val quietTimeChecker = QuietTimeChecker(clock, "09:00", "17:00") + + assertFalse(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeBeforeEndTime_timeIsStartOfQuietTime() { + setClockTo("09:00") + val quietTimeChecker = QuietTimeChecker(clock, "09:00", "17:00") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeBeforeEndTime_timeIsEndOfQuietTime() { + setClockTo("17:00") + val quietTimeChecker = QuietTimeChecker(clock, "09:00", "17:00") + + assertTrue(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeEqualsEndTime_timeIsDifferentFromStartAndEndOfQuietTime_shouldReturnFalse() { + setClockTo("10:00") + val quietTimeChecker = QuietTimeChecker(clock, "06:00", "06:00") + + assertFalse(quietTimeChecker.isQuietTime) + } + + @Test + fun startTimeEqualsEndTime_timeIsEqualToStartAndEndOfQuietTime_shouldReturnFalse() { + setClockTo("06:00") + val quietTimeChecker = QuietTimeChecker(clock, "06:00", "06:00") + + assertFalse(quietTimeChecker.isQuietTime) + } + + + private fun setClockTo(time: String) { + val (hourOfDay, minute) = time.split(':').map { it.toInt() } + + val calendar = Calendar.getInstance() + calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) + calendar.set(Calendar.MINUTE, minute) + + val timeInMillis = calendar.timeInMillis + whenever(clock.time).thenReturn(timeInMillis) + } +}