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; return false;
} }
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final long startTime = preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final long startTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE));
final long endTime = preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final long endTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE));
final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY; final long nowTime = Calendar.getInstance().getTimeInMillis();
if (endTime < startTime) { if (endTime < startTime) {
return nowTime > startTime || nowTime < endTime; return nowTime > startTime || nowTime < endTime;

View file

@ -12,6 +12,7 @@ import java.text.DateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
public class TimePreference extends DialogPreference implements Preference.OnPreferenceChangeListener { public class TimePreference extends DialogPreference implements Preference.OnPreferenceChangeListener {
private TimePicker picker = null; private TimePicker picker = null;
public final static long DEFAULT_VALUE = 0; public final static long DEFAULT_VALUE = 0;
@ -25,11 +26,12 @@ public class TimePreference extends DialogPreference implements Preference.OnPre
persistLong(time); persistLong(time);
notifyDependencyChange(shouldDisableDependents()); notifyDependencyChange(shouldDisableDependents());
notifyChanged(); 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 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())); setSummary(dateFormat.format(date.getTime()));
} }
@ -40,21 +42,14 @@ public class TimePreference extends DialogPreference implements Preference.OnPre
return picker; return picker;
} }
protected Calendar getPersistedTime() {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(getPersistedLong(DEFAULT_VALUE));
return c;
}
@SuppressWarnings("NullableProblems") @SuppressWarnings("NullableProblems")
@Override @Override
protected void onBindDialogView(final View v) { protected void onBindDialogView(final View v) {
super.onBindDialogView(v); super.onBindDialogView(v);
final Calendar c = getPersistedTime(); long time = getPersistedLong(DEFAULT_VALUE);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY)); picker.setCurrentHour((int) (time / 60));
picker.setCurrentMinute(c.get(Calendar.MINUTE)); picker.setCurrentMinute((int) (time % 60));
} }
@Override @Override
@ -62,19 +57,21 @@ public class TimePreference extends DialogPreference implements Preference.OnPre
super.onDialogClosed(positiveResult); super.onDialogClosed(positiveResult);
if (positiveResult) { if (positiveResult) {
final Calendar c = Calendar.getInstance(); setTime(picker.getCurrentHour() * 60 + picker.getCurrentMinute());
c.set(Calendar.MINUTE, picker.getCurrentMinute());
c.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
if (!callChangeListener(c.getTimeInMillis())) {
return;
}
setTime(c.getTimeInMillis());
} }
} }
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 @Override
protected Object onGetDefaultValue(final TypedArray a, final int index) { protected Object onGetDefaultValue(final TypedArray a, final int index) {
return a.getInteger(index, 0); return a.getInteger(index, 0);