Bug 1177510 - Add max/min mechanism to DateTimePicker.java in order to prevent inputting invalid Date values. r=wesj

This commit is contained in:
Mantaroh Yoshinaga 2015-07-22 03:26:00 -04:00
parent 86d3830447
commit ef2a0d50b5
4 changed files with 41 additions and 9 deletions

View File

@ -39,6 +39,8 @@ public class PromptInput {
protected final String mType;
protected final String mId;
protected final String mValue;
protected final String mMinValue;
protected final String mMaxValue;
protected OnChangeListener mListener;
protected View mView;
public static final String LOGTAG = "GeckoPromptInput";
@ -178,7 +180,7 @@ public class PromptInput {
if (mType.equals("date")) {
try {
DateTimePicker input = new DateTimePicker(context, "yyyy-MM-dd", mValue,
DateTimePicker.PickersState.DATE);
DateTimePicker.PickersState.DATE, mMinValue, mMaxValue);
input.toggleCalendar(true);
mView = (View)input;
} catch (UnsupportedOperationException ex) {
@ -200,7 +202,7 @@ public class PromptInput {
}
} else if (mType.equals("week")) {
DateTimePicker input = new DateTimePicker(context, "yyyy-'W'ww", mValue,
DateTimePicker.PickersState.WEEK);
DateTimePicker.PickersState.WEEK, mMinValue, mMaxValue);
mView = (View)input;
} else if (mType.equals("time")) {
TimePicker input = new TimePicker(context);
@ -217,12 +219,13 @@ public class PromptInput {
mView = (View)input;
} else if (mType.equals("datetime-local") || mType.equals("datetime")) {
DateTimePicker input = new DateTimePicker(context, "yyyy-MM-dd HH:mm", mValue.replace("T"," ").replace("Z", ""),
DateTimePicker.PickersState.DATETIME);
DateTimePicker.PickersState.DATETIME,
mMinValue.replace("T"," ").replace("Z",""), mMaxValue.replace("T"," ").replace("Z", ""));
input.toggleCalendar(true);
mView = (View)input;
} else if (mType.equals("month")) {
DateTimePicker input = new DateTimePicker(context, "yyyy-MM", mValue,
DateTimePicker.PickersState.MONTH);
DateTimePicker.PickersState.MONTH, mMinValue, mMaxValue);
mView = (View)input;
}
return mView;
@ -343,6 +346,8 @@ public class PromptInput {
String id = obj.optString("id");
mId = TextUtils.isEmpty(id) ? mType : id;
mValue = obj.optString("value");
mMaxValue = obj.optString("max");
mMinValue = obj.optString("min");
}
public static PromptInput getInput(JSONObject obj) {

View File

@ -236,18 +236,17 @@ public class DateTimePicker extends FrameLayout {
}
public DateTimePicker(Context context) {
this(context, "", "", PickersState.DATE);
this(context, "", "", PickersState.DATE, null, null);
}
public DateTimePicker(Context context, String dateFormat, String dateTimeValue, PickersState state) {
public DateTimePicker(Context context, String dateFormat, String dateTimeValue, PickersState state, String minDateValue, String maxDateValue) {
super(context);
if (Versions.preHC) {
throw new UnsupportedOperationException("Custom DateTimePicker is only available for SDK > 10");
}
setCurrentLocale(Locale.getDefault());
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
mState = state;
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.datetime_picker, this, true);
@ -290,6 +289,7 @@ public class DateTimePicker extends FrameLayout {
mCalendar.setFocusableInTouchMode(true);
mCalendar.setMaxDate(mMaxDate.getTimeInMillis());
mCalendar.setMinDate(mMinDate.getTimeInMillis());
mCalendar.setDate(mTempDate.getTimeInMillis(), false, false);
mCalendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
@ -324,6 +324,29 @@ public class DateTimePicker extends FrameLayout {
mTempDate.setTimeInMillis(System.currentTimeMillis());
}
// Set the min / max attribute.
try {
if (minDateValue != null && !minDateValue.equals("")) {
mMinDate.setTime(new SimpleDateFormat(dateFormat).parse(minDateValue));
} else {
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
}
} catch (Exception ex) {
Log.e(LOGTAG, "Error parsing format sting: " + ex);
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
}
try {
if (maxDateValue != null && !maxDateValue.equals("")) {
mMaxDate.setTime(new SimpleDateFormat(dateFormat).parse(maxDateValue));
} else {
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
}
} catch (Exception ex) {
Log.e(LOGTAG, "Error parsing format string: " + ex);
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
}
// Initialize all spinners.
mDaySpinner = setupSpinner(R.id.day, 1,
mTempDate.get(Calendar.DAY_OF_MONTH));

View File

@ -34,6 +34,8 @@ var InputWidgetHelper = {
}).addDatePicker({
value: aElement.value,
type: type,
min: aElement.min,
max: aElement.max,
}).show((function(data) {
let changed = false;
if (data.button == -1) {

View File

@ -120,7 +120,9 @@ Prompt.prototype = {
return this._addInput({
type: aOptions.type || "date",
value: aOptions.value,
id: aOptions.id
id: aOptions.id,
max: aOptions.max,
min: aOptions.min
});
},