Merge pull request #3050 from k9mail/GH-2999_fix_quiet_time

Fix quiet time
This commit is contained in:
cketti 2018-01-06 09:00:45 +01:00 committed by GitHub
commit 9e181e3180
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 173 additions and 37 deletions

View file

@ -4,8 +4,6 @@ package com.fsck.k9;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -998,41 +996,8 @@ public class K9 extends Application {
return false; return false;
} }
GregorianCalendar gregorianCalendar = new GregorianCalendar(); QuietTimeChecker quietTimeChecker = new QuietTimeChecker(Clock.INSTANCE, mQuietTimeStarts, mQuietTimeEnds);
return quietTimeChecker.isQuietTime();
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;
} }
public static void setDebug(boolean debug) { public static void setDebug(boolean debug) {

View file

@ -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;
}
}

View file

@ -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)
}
}