2014-12-14 07:02:17 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.preference.DialogPreference;
|
2014-12-15 22:46:32 +00:00
|
|
|
import android.preference.Preference;
|
2014-12-14 07:02:17 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TimePicker;
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
|
2014-12-15 22:46:32 +00:00
|
|
|
public class TimePreference extends DialogPreference implements Preference.OnPreferenceChangeListener {
|
2014-12-14 07:02:17 +00:00
|
|
|
private TimePicker picker = null;
|
|
|
|
public final static long DEFAULT_VALUE = 0;
|
|
|
|
|
|
|
|
public TimePreference(final Context context, final AttributeSet attrs) {
|
|
|
|
super(context, attrs, 0);
|
2014-12-15 22:46:32 +00:00
|
|
|
this.setOnPreferenceChangeListener(this);
|
2014-12-14 07:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void setTime(final long time) {
|
|
|
|
persistLong(time);
|
|
|
|
notifyDependencyChange(shouldDisableDependents());
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
|
2014-12-15 22:46:32 +00:00
|
|
|
protected void updateSummary(final long time) {
|
2014-12-14 07:02:17 +00:00
|
|
|
final DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
|
|
|
|
final Date date = new Date(time);
|
|
|
|
setSummary(dateFormat.format(date.getTime()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected View onCreateDialogView() {
|
|
|
|
picker = new TimePicker(getContext());
|
|
|
|
picker.setIs24HourView(android.text.format.DateFormat.is24HourFormat(getContext()));
|
|
|
|
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();
|
|
|
|
|
|
|
|
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
|
|
|
|
picker.setCurrentMinute(c.get(Calendar.MINUTE));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDialogClosed(final boolean positiveResult) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Object onGetDefaultValue(final TypedArray a, final int index) {
|
|
|
|
return a.getInteger(index, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSetInitialValue(final boolean restorePersistedValue, final Object defaultValue) {
|
|
|
|
long time;
|
|
|
|
if (defaultValue == null) {
|
|
|
|
time = restorePersistedValue ? getPersistedLong(DEFAULT_VALUE) : DEFAULT_VALUE;
|
|
|
|
} else if (defaultValue instanceof Long) {
|
|
|
|
time = restorePersistedValue ? getPersistedLong((Long) defaultValue) : (Long) defaultValue;
|
|
|
|
} else if (defaultValue instanceof Calendar) {
|
|
|
|
time = restorePersistedValue ? getPersistedLong(((Calendar)defaultValue).getTimeInMillis()) : ((Calendar)defaultValue).getTimeInMillis();
|
|
|
|
} else {
|
|
|
|
time = restorePersistedValue ? getPersistedLong(DEFAULT_VALUE) : DEFAULT_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTime(time);
|
2014-12-15 22:46:32 +00:00
|
|
|
updateSummary(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
|
|
|
|
((TimePreference) preference).updateSummary((Long)newValue);
|
|
|
|
return true;
|
2014-12-14 07:02:17 +00:00
|
|
|
}
|
|
|
|
}
|