Refactor QuietTimeChecker

This commit is contained in:
cketti 2018-01-05 20:31:03 +01:00
parent e9d90b1e75
commit bb14309dd0

View file

@ -2,53 +2,45 @@ package com.fsck.k9;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar;
class QuietTimeChecker { class QuietTimeChecker {
private final Clock clock; private final Clock clock;
private final String quietTimeStart; private final int quietTimeStart;
private final String quietTimeEnd; private final int quietTimeEnd;
QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) { QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) {
this.clock = clock; this.clock = clock;
this.quietTimeStart = quietTimeStart; this.quietTimeStart = parseTime(quietTimeStart);
this.quietTimeEnd = quietTimeEnd; 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() { public boolean isQuietTime() {
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTimeInMillis(clock.getTime());
Integer startHour = Integer.parseInt(quietTimeStart.split(":")[0]);
Integer startMinute = Integer.parseInt(quietTimeStart.split(":")[1]);
Integer endHour = Integer.parseInt(quietTimeEnd.split(":")[0]);
Integer endMinute = Integer.parseInt(quietTimeEnd.split(":")[1]);
Integer now = (gregorianCalendar.get(Calendar.HOUR_OF_DAY) * 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 start and end times are the same, we're never quiet
if (quietStarts.equals(quietEnds)) { if (quietTimeStart == quietTimeEnd) {
return false; return false;
} }
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(clock.getTime());
// 21:00 - 05:00 means we want to be quiet if it's after 9 or before 5 int minutesSinceMidnight = (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE);
if (quietStarts > quietEnds) {
// if it's 22:00 or 03:00 but not 8:00 if (quietTimeStart > quietTimeEnd) {
if (now >= quietStarts || now <= quietEnds) { if (minutesSinceMidnight >= quietTimeStart || minutesSinceMidnight <= quietTimeEnd) {
return true; return true;
} }
} } else {
if (minutesSinceMidnight >= quietTimeStart && minutesSinceMidnight <= quietTimeEnd) {
// 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 true;
} }
} }