Sanitize date shortcuts

pull/324/merge
Alex Baker 9 years ago
parent 666afa056b
commit 8bf3d03043

@ -65,19 +65,25 @@ public class Preferences {
}
public int getDateShortcutMorning() {
return getInt(R.string.p_date_shortcut_morning, context.getResources().getInteger(R.integer.default_morning));
return getMillisPerDayPref(R.string.p_date_shortcut_morning, R.integer.default_morning);
}
public int getDateShortcutAfternoon() {
return getInt(R.string.p_date_shortcut_afternoon, context.getResources().getInteger(R.integer.default_afternoon));
return getMillisPerDayPref(R.string.p_date_shortcut_afternoon, R.integer.default_afternoon);
}
public int getDateShortcutEvening() {
return getInt(R.string.p_date_shortcut_evening, context.getResources().getInteger(R.integer.default_evening));
return getMillisPerDayPref(R.string.p_date_shortcut_evening, R.integer.default_evening);
}
public int getDateShortcutNight() {
return getInt(R.string.p_date_shortcut_night, context.getResources().getInteger(R.integer.default_night));
return getMillisPerDayPref(R.string.p_date_shortcut_night, R.integer.default_night);
}
private int getMillisPerDayPref(int resId, int defResId) {
int defaultValue = context.getResources().getInteger(defResId);
int setting = getInt(resId, defaultValue);
return setting < 0 || setting > DateTime.MAX_MILLIS_PER_DAY ? defaultValue : setting;
}
public boolean useDarkWidgetTheme(int widgetId) {

@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
public class DateTime {
private static final int MAX_MILLIS_PER_DAY = (int) TimeUnit.DAYS.toMillis(1);
public static final int MAX_MILLIS_PER_DAY = (int) TimeUnit.DAYS.toMillis(1) - 1;
private static final TimeZone UTC = TimeZone.getTimeZone("GMT");
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
private static final int MILLIS_PER_MINUTE = (int) TimeUnit.MINUTES.toMillis(1);
@ -69,7 +69,7 @@ public class DateTime {
}
public DateTime withMillisOfDay(int millisOfDay) {
if (millisOfDay >= MAX_MILLIS_PER_DAY || millisOfDay < 0) {
if (millisOfDay > MAX_MILLIS_PER_DAY || millisOfDay < 0) {
throw new RuntimeException("Illegal millis of day: " + millisOfDay);
}
int hours = millisOfDay / MILLIS_PER_HOUR;

Loading…
Cancel
Save