From a848eb3982ddb680409e70fcaa7a2c1aa1628e4b Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 4 Aug 2016 12:28:41 -0500 Subject: [PATCH] Fixed Android 5+ title being set with max date --- src/android/DatePickerPlugin.java | 47 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/android/DatePickerPlugin.java b/src/android/DatePickerPlugin.java index b76edcc..b1e7d84 100644 --- a/src/android/DatePickerPlugin.java +++ b/src/android/DatePickerPlugin.java @@ -45,7 +45,7 @@ public class DatePickerPlugin extends CordovaPlugin { private static final String RESULT_ERROR = "error"; private static final String RESULT_CANCEL = "cancel"; private final String pluginName = "DatePickerPlugin"; - + // On some devices, onDateSet or onTimeSet are being called twice private boolean called = false; private boolean canceled = false; @@ -68,7 +68,7 @@ public synchronized void show(final JSONArray data, final CallbackContext callba Context currentCtx = cordova.getActivity(); Runnable runnable; JsonDate jsonDate = new JsonDate().fromJson(data); - + // Retrieve Android theme JSONObject options = data.optJSONObject(0); int theme = options.optInt("androidTheme", 1); @@ -83,11 +83,11 @@ public synchronized void show(final JSONArray data, final CallbackContext callba cordova.getActivity().runOnUiThread(runnable); } - + private TimePicker timePicker; private int timePickerHour = 0; private int timePickerMinute = 0; - + private Runnable runnableTimeDialog(final DatePickerPlugin datePickerPlugin, final int theme, final Context currentCtx, final CallbackContext callbackContext, final JsonDate jsonDate, final Calendar calendarDate) { @@ -106,7 +106,7 @@ public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { timeDialog.setCancelable(true); timeDialog.setCanceledOnTouchOutside(false); - + if (!jsonDate.titleText.isEmpty()){ timeDialog.setTitle(jsonDate.titleText); } @@ -121,7 +121,7 @@ public void onClick(DialogInterface dialog, int which) { } }); } - String labelCancel = jsonDate.cancelText.isEmpty() ? currentCtx.getString(android.R.string.cancel) : jsonDate.cancelText; + String labelCancel = jsonDate.cancelText.isEmpty() ? currentCtx.getString(android.R.string.cancel) : jsonDate.cancelText; timeDialog.setButton(DialogInterface.BUTTON_NEGATIVE, labelCancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -138,7 +138,7 @@ public void onClick(DialogInterface dialog, int which) { } }; } - + private Runnable runnableDatePicker( final DatePickerPlugin datePickerPlugin, final int theme, final Context currentCtx, @@ -155,19 +155,16 @@ public void run() { else { prepareDialogPreHoneycomb(dateDialog, callbackContext, currentCtx, jsonDate); } - + dateDialog.show(); } }; } - - private void prepareDialog(final DatePickerDialog dateDialog, final OnDateSetListener dateListener, + + private void prepareDialog(final DatePickerDialog dateDialog, final OnDateSetListener dateListener, final CallbackContext callbackContext, Context currentCtx, JsonDate jsonDate) { dateDialog.setCancelable(true); dateDialog.setCanceledOnTouchOutside(false); - if (!jsonDate.titleText.isEmpty()){ - dateDialog.setTitle(jsonDate.titleText); - } if (!jsonDate.todayText.isEmpty()){ dateDialog.setButton(DialogInterface.BUTTON_NEUTRAL, jsonDate.todayText, new DialogInterface.OnClickListener() { @Override @@ -178,7 +175,7 @@ public void onClick(DialogInterface dialog, int which) { } }); } - String labelCancel = jsonDate.cancelText.isEmpty() ? currentCtx.getString(android.R.string.cancel) : jsonDate.cancelText; + String labelCancel = jsonDate.cancelText.isEmpty() ? currentCtx.getString(android.R.string.cancel) : jsonDate.cancelText; dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, labelCancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -195,7 +192,7 @@ public void onClick(DialogInterface dialog, int which) { dateListener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()); } }); - + DatePicker dp = dateDialog.getDatePicker(); if(jsonDate.minDate > 0) { dp.setMinDate(jsonDate.minDate); @@ -203,8 +200,10 @@ public void onClick(DialogInterface dialog, int which) { if(jsonDate.maxDate > 0 && jsonDate.maxDate > jsonDate.minDate) { dp.setMaxDate(jsonDate.maxDate); } + + dateDialog.setTitle(jsonDate.titleText); } - + private void prepareDialogPreHoneycomb(DatePickerDialog dateDialog, final CallbackContext callbackContext, Context currentCtx, final JsonDate jsonDate){ java.lang.reflect.Field mDatePickerField = null; @@ -277,24 +276,24 @@ public void onDateSet(final DatePicker view, final int year, final int monthOfYe } called = true; canceled = false; - + Log.d("onDateSet", "called: " + called); Log.d("onDateSet", "canceled: " + canceled); Log.d("onDateSet", "mode: " + jsonDate.action); - + if (ACTION_DATE.equalsIgnoreCase(jsonDate.action)) { String returnDate = year + "/" + (monthOfYear + 1) + "/" + dayOfMonth; Log.d("onDateSet", "returnDate: " + returnDate); - + callbackContext.success(returnDate); - + } else { // Open time dialog Calendar selectedDate = Calendar.getInstance(); selectedDate.set(Calendar.YEAR, year); selectedDate.set(Calendar.MONTH, monthOfYear); selectedDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); - + cordova.getActivity().runOnUiThread(runnableTimeDialog(datePickerPlugin, theme, cordova.getActivity(), callbackContext, jsonDate, selectedDate)); } @@ -319,7 +318,7 @@ public void onTimeSet(final TimePicker view, final int hourOfDay, final int minu if (canceled) { return; } - + calendarDate.set(Calendar.HOUR_OF_DAY, hourOfDay); calendarDate.set(Calendar.MINUTE, minute); calendarDate.set(Calendar.SECOND, 0); @@ -331,9 +330,9 @@ public void onTimeSet(final TimePicker view, final int hourOfDay, final int minu callbackContext.success(toReturn); } } - + private final class JsonDate { - + private String action = ACTION_DATE; private String titleText = ""; private String okText = "";