Created
July 26, 2012 20:40
-
-
Save chrishuan9/3184373 to your computer and use it in GitHub Desktop.
TimePicker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2011 Morphoss Ltd | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* The original (Licence Free) code for this class was downloaded from http://www.ebessette.com/d/TimePickerPreference | |
* | |
*/ | |
// Please note this must be the package if you want to use XML-based preferences | |
package com.morphoss.acal; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.DialogPreference; | |
import android.preference.PreferenceManager; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
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 | |
*/ | |
private static final String VALIDATION_EXPRESSION = "[0-2]*[0-9]:[0-5]*[0-9]"; | |
/** | |
* The default value for this preference | |
*/ | |
private String defaultValue = "12:00"; | |
/** | |
* @param context | |
* @param attrs | |
*/ | |
public TimePickerPreference(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initialize(context); | |
} | |
/** | |
* @param context | |
* @param attrs | |
* @param defStyle | |
*/ | |
public TimePickerPreference(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
initialize(context); | |
} | |
/** | |
* Initialize this preference | |
*/ | |
private void initialize( Context context) { | |
setPersistent(true); | |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | |
defaultValue = prefs.getString(getKey(), defaultValue); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see android.preference.DialogPreference#onCreateDialogView() | |
*/ | |
@Override | |
protected View onCreateDialogView() { | |
TimePicker tp = new TimePicker(getContext()); | |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); | |
boolean is24Hour = prefs.getBoolean(getContext().getString(R.string.prefTwelveTwentyfour), false); | |
tp.setIs24HourView(is24Hour); | |
tp.setOnTimeChangedListener(this); | |
Log.d("TimePicker","Current default ="+getPersistedString(this.defaultValue)); | |
int h = getHour(); | |
int m = getMinute(); | |
if (h >= 0 && h < 24) tp.setCurrentHour(h); | |
if ( m >= 0 && m < 60) 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) { | |
persistString(String.format("%02d:%02d",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; | |
} | |
/** | |
* 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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment