From 23cc305720543713ce3d4c67e3a7ee30189df03f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 21 Nov 2018 18:14:02 +0100 Subject: [PATCH] refactor quite hours to make them time zone indepentent. fixes #2565 --- .../services/NotificationService.java | 6 +-- .../conversations/ui/TimePreference.java | 41 +++++++++---------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index e9491d45d..beecad52d 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -195,9 +195,9 @@ public class NotificationService { return false; } final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); - final long startTime = preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; - final long endTime = preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; - final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY; + final long startTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE)); + final long endTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE)); + final long nowTime = Calendar.getInstance().getTimeInMillis(); if (endTime < startTime) { return nowTime > startTime || nowTime < endTime; diff --git a/src/main/java/eu/siacs/conversations/ui/TimePreference.java b/src/main/java/eu/siacs/conversations/ui/TimePreference.java index e32b068c7..c0d5489e9 100644 --- a/src/main/java/eu/siacs/conversations/ui/TimePreference.java +++ b/src/main/java/eu/siacs/conversations/ui/TimePreference.java @@ -12,6 +12,7 @@ import java.text.DateFormat; import java.util.Calendar; import java.util.Date; + public class TimePreference extends DialogPreference implements Preference.OnPreferenceChangeListener { private TimePicker picker = null; public final static long DEFAULT_VALUE = 0; @@ -25,11 +26,12 @@ public class TimePreference extends DialogPreference implements Preference.OnPre persistLong(time); notifyDependencyChange(shouldDisableDependents()); notifyChanged(); + updateSummary(time); } - protected void updateSummary(final long time) { + private void updateSummary(final long time) { final DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext()); - final Date date = new Date(time); + final Date date = minutesToCalender(time).getTime(); setSummary(dateFormat.format(date.getTime())); } @@ -40,21 +42,14 @@ public class TimePreference extends DialogPreference implements Preference.OnPre return picker; } - protected Calendar getPersistedTime() { - final Calendar c = Calendar.getInstance(); - c.setTimeInMillis(getPersistedLong(DEFAULT_VALUE)); - - return c; - } - @SuppressWarnings("NullableProblems") @Override protected void onBindDialogView(final View v) { super.onBindDialogView(v); - final Calendar c = getPersistedTime(); + long time = getPersistedLong(DEFAULT_VALUE); - picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY)); - picker.setCurrentMinute(c.get(Calendar.MINUTE)); + picker.setCurrentHour((int) (time / 60)); + picker.setCurrentMinute((int) (time % 60)); } @Override @@ -62,19 +57,21 @@ public class TimePreference extends DialogPreference implements Preference.OnPre super.onDialogClosed(positiveResult); if (positiveResult) { - final Calendar c = Calendar.getInstance(); - c.set(Calendar.MINUTE, picker.getCurrentMinute()); - c.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour()); - - - if (!callChangeListener(c.getTimeInMillis())) { - return; - } - - setTime(c.getTimeInMillis()); + setTime(picker.getCurrentHour() * 60 + picker.getCurrentMinute()); } } + private static Calendar minutesToCalender(long time) { + final Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, (int) ((time % (24 * 60)) / 60)); + c.set(Calendar.MINUTE, (int) ((time % (24 * 60)) % 60)); + return c; + } + + public static long minutesToTimestamp(long time) { + return minutesToCalender(time).getTimeInMillis(); + } + @Override protected Object onGetDefaultValue(final TypedArray a, final int index) { return a.getInteger(index, 0);