/
TimePickerPreference.java
174 lines (148 loc) · 4.64 KB
/
TimePickerPreference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Sourced from http://www.ebessette.com/d/TimePickerPreference
* on 2010-11-27 by jessev
*/
package com.fsck.k9.preferences;
import android.content.Context;
import android.preference.DialogPreference;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
/**
* A preference type that allows a user to choose a time
*/
public class TimePickerPreference extends DialogPreference implements
TimePicker.OnTimeChangedListener {
/**
* The validation expression for this preference
*/
public static final String VALIDATION_EXPRESSION = "[0-2]*[0-9]:[0-5]*[0-9]";
/**
* The default value for this preference
*/
private String defaultValue;
/**
* Store the original value, in case the user
* chooses to abort the {@link DialogPreference}
* after making a change.
*/
private int originalHour = 0;
/**
* Store the original value, in case the user
* chooses to abort the {@link DialogPreference}
* after making a change.
*/
private int originalMinute = 0;
/**
* @param context
* @param attrs
*/
public TimePickerPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
initialize();
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public TimePickerPreference(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
initialize();
}
/**
* Initialize this preference
*/
private void initialize() {
setPersistent(true);
}
/*
* (non-Javadoc)
*
* @see android.preference.DialogPreference#onCreateDialogView()
*/
@Override
protected View onCreateDialogView() {
TimePicker tp = new TimePicker(getContext());
tp.setIs24HourView(DateFormat.is24HourFormat(getContext()));
tp.setOnTimeChangedListener(this);
originalHour = getHour();
originalMinute = getMinute();
if (originalHour >= 0 && originalMinute >= 0) {
tp.setCurrentHour(originalHour);
tp.setCurrentMinute(originalMinute);
}
return tp;
}
/**
* @see
* android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android
* .widget.TimePicker, int, int)
*/
@Override
public void onTimeChanged(final TimePicker view, final int hour, final int minute) {
persistString(String.format("%02d:%02d", hour, minute));
callChangeListener(String.format("%02d:%02d", hour, minute));
}
/**
* If not a positive result, restore the original value
* before going to super.onDialogClosed(positiveResult).
*/
@Override
protected void onDialogClosed(boolean positiveResult) {
if (!positiveResult) {
persistString(String.format("%02d:%02d", originalHour, originalMinute));
callChangeListener(String.format("%02d:%02d", originalHour, originalMinute));
}
super.onDialogClosed(positiveResult);
}
/**
* @see android.preference.Preference#setDefaultValue(java.lang.Object)
*/
@Override
public void setDefaultValue(final Object defaultValue) {
// BUG this method is never called if you use the 'android:defaultValue' attribute in your XML preference file, not sure why it isn't
super.setDefaultValue(defaultValue);
if (!(defaultValue instanceof String)) {
return;
}
if (!((String) defaultValue).matches(VALIDATION_EXPRESSION)) {
return;
}
this.defaultValue = (String) defaultValue;
}
/**
* Get the hour value (in 24 hour time)
*
* @return The hour value, will be 0 to 23 (inclusive) or -1 if illegal
*/
private int getHour() {
String time = getTime();
if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
return -1;
}
return Integer.valueOf(time.split(":")[0]);
}
/**
* Get the minute value
*
* @return the minute value, will be 0 to 59 (inclusive) or -1 if illegal
*/
private int getMinute() {
String time = getTime();
if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
return -1;
}
return Integer.valueOf(time.split(":")[1]);
}
/**
* Get the time. It is only legal, if it matches
* {@link #VALIDATION_EXPRESSION}.
*
* @return the time as hh:mm
*/
public String getTime() {
return getPersistedString(this.defaultValue);
}
}