public class PrefAutoRefreshStartTime extends DialogPreference implements TimePicker.OnTimeChangedListener {
/** * The validation expression for this preference */
private static final String VALIDATION_EXPRESSION = "[0-2]*[0-9]:[0-5]*[0-9]";
/** * The default value for this preference */
private String defaultValue;
/** * @param context * @param attrs */
public PrefAutoRefreshStartTime(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
/** * @param context * @param attrs * @param defStyle */
public PrefAutoRefreshStartTime(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(context);
}
/** * Initialize this preference */
private void initialize(Context context) {
setPersistent(true);
SharedPreferences sp = getPreferenceManager().getDefaultSharedPreferences(getContext());
String timeTemp = sp.getString(IConfigsParameters.KEY_P_AUTOFRESH_START, "");
if(null != timeTemp){
setSummary(packageTimeInfo(timeTemp));
}else{
setSummary(packageTimeInfo(context.getText(R.string.time_default_auto_update_start).toString()));
}
}
/* * (non-Javadoc) * * @see android.preference.DialogPreference#onCreateDialogView() */
@Override protected View onCreateDialogView() {
TimePicker tp = new TimePicker(getContext());
tp.setOnTimeChangedListener(this);
/** */
SharedPreferences sp = getPreferenceManager().getDefaultSharedPreferences(getContext());
String timeTemp = sp.getString("p_autofresh_strart", "");
String[] time = timeTemp.split(":");
if((time != null) && (time.length == 2)){
Integer hour = Integer.decode(time[0]);
Integer min = Integer.decode(time[1]);
tp.setCurrentHour(hour);
tp.setCurrentMinute(min);
}else{
/** */
int h = getHour();
int m = getMinute();
if (h >= 0 && m >= 0) {
tp.setCurrentHour(h);
tp.setCurrentMinute(m);
}
} return tp;
}
/* * (non-Javadoc) * * @see * android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android * .widget.TimePicker, int, int) */
@Override public void onTimeChanged(TimePicker view, int hour, int minute) {
setSummary(packageTimeInfo((""+hour+":"+minute)));
persistString(hour + ":" + minute);
}
/* * (non-Javadoc) * * @see android.preference.Preference#setDefaultValue(java.lang.Object) */
@Override public void setDefaultValue(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;
user.qzone.qq.com/2984712312
user.qzone.qq.com/2967623657
user.qzone.qq.com/2351087502
user.qzone.qq.com/2310972975
user.qzone.qq.com/2089991873
user.qzone.qq.com/2056947511
user.qzone.qq.com/2931839848
user.qzone.qq.com/2772688133
user.qzone.qq.com/2778402622
user.qzone.qq.com/2083980836
user.qzone.qq.com/2939713978
user.qzone.qq.com/2070861889
user.qzone.qq.com/2533136852
user.qzone.qq.com/2772639602
user.qzone.qq.com/2984628057
user.qzone.qq.com/2926728310
user.qzone.qq.com/2076557889
user.qzone.qq.com/2990629021
user.qzone.qq.com/2927723568
user.qzone.qq.com/2938769568
user.qzone.qq.com/1275429625
user.qzone.qq.com/2951748089
user.qzone.qq.com/2951748089
user.qzone.qq.com/2694131258
user.qzone.qq.com/2105023931
user.qzone.qq.com/2645067143
user.qzone.qq.com/2244560733
user.qzone.qq.com/2794813027
user.qzone.qq.com/2108987967
user.qzone.qq.com/2401917273
user.qzone.qq.com/1773140321
user.qzone.qq.com/1562886240
user.qzone.qq.com/2401053736
user.qzone.qq.com/1845162072
user.qzone.qq.com/482921132
user.qzone.qq.com/2047332906
user.qzone.qq.com/2422052684
user.qzone.qq.com/2416781417
user.qzone.qq.com/2926844613
user.qzone.qq.com/1804270962
}
/** * Get the hour value (in 24 hour time) * * @return The hour value, will be 0 to 23 (inclusive) */
private int getHour() {
String time = getPersistedString(this.defaultValue);
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) */
private int getMinute() {
String time = getPersistedString(this.defaultValue);
if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
return -1;
} return Integer.valueOf(time.split(":")[1]);
} private String packageTimeInfo(String time){
String[] times = time.split(":");
StringBuffer sb = new StringBuffer();
sb.append(times[0]).append("时");
if(times[1].length() < 2){
sb.append("0");
} sb.append(times[1]).append("分");
return sb.toString();
}
}