Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Android 5+ title being set with max date #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/android/DatePickerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -138,7 +138,7 @@ public void onClick(DialogInterface dialog, int which) {
}
};
}

private Runnable runnableDatePicker(
final DatePickerPlugin datePickerPlugin,
final int theme, final Context currentCtx,
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -195,16 +192,18 @@ 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);
}
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;
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
Expand All @@ -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 = "";
Expand Down