refactor quite hours to make them time zone indepentent. fixes #2565

This commit is contained in:
Daniel Gultsch 2018-11-21 18:14:02 +01:00
parent 63f88af743
commit 23cc305720
2 changed files with 22 additions and 25 deletions

View file

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

View file

@ -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,17 +57,19 @@ public class TimePreference extends DialogPreference implements Preference.OnPre
super.onDialogClosed(positiveResult);
if (positiveResult) {
setTime(picker.getCurrentHour() * 60 + picker.getCurrentMinute());
}
}
private static Calendar minutesToCalender(long time) {
final Calendar c = Calendar.getInstance();
c.set(Calendar.MINUTE, picker.getCurrentMinute());
c.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
if (!callChangeListener(c.getTimeInMillis())) {
return;
c.set(Calendar.HOUR_OF_DAY, (int) ((time % (24 * 60)) / 60));
c.set(Calendar.MINUTE, (int) ((time % (24 * 60)) % 60));
return c;
}
setTime(c.getTimeInMillis());
}
public static long minutesToTimestamp(long time) {
return minutesToCalender(time).getTimeInMillis();
}
@Override