This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
/
DatePickerPlugin.java
273 lines (235 loc) · 11.8 KB
/
DatePickerPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @author Bikas Vaibhav (http://bikasv.com) 2013
* Rewrote the plug-in at https://github.com/phonegap/phonegap-plugins/tree/master/Android/DatePicker
* It can now accept `min` and `max` dates for DatePicker.
* added clear button for time input dialogs
*/
package com.bikasv.plugins.datepicker;
import java.util.Calendar;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
import android.os.Build;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public class DatePickerPlugin extends CordovaPlugin {
private static final String ACTION_DATE = "date";
private static final String ACTION_TIME = "time";
private final String pluginName = "DatePickerPlugin";
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
Log.d(pluginName, "DatePicker called with options: " + data);
PluginResult result = null;
this.show(data, callbackContext);
result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
return true;
}
public synchronized void show(final JSONArray data, final CallbackContext callbackContext) {
final DatePickerPlugin datePickerPlugin = this;
final Context currentCtx = cordova.getActivity();
final Calendar c = Calendar.getInstance();
final Runnable runnable;
String action = "date";
String clearText = "";
long minDateLong = 0, maxDateLong = 0;
int month = -1, day = -1, year = -1, hour = -1, min = -1;
try {
JSONObject obj = data.getJSONObject(0);
action = obj.getString("mode");
clearText = obj.getString("clearText");
String optionDate = obj.getString("date");
String[] datePart = optionDate.split("/");
month = Integer.parseInt(datePart[0]);
day = Integer.parseInt(datePart[1]);
year = Integer.parseInt(datePart[2]);
hour = Integer.parseInt(datePart[3]);
min = Integer.parseInt(datePart[4]);
minDateLong = obj.getLong("minDate");
maxDateLong = obj.getLong("maxDate");
} catch (JSONException e) {
e.printStackTrace();
}
// By default initalize these fields to 'now'
final int mYear = year == -1 ? c.get(Calendar.YEAR) : year;
final int mMonth = month == -1 ? c.get(Calendar.MONTH) : month - 1;
final int mDay = day == -1 ? c.get(Calendar.DAY_OF_MONTH) : day;
final int mHour = hour == -1 ? c.get(Calendar.HOUR_OF_DAY) : hour;
final int mMinutes = min == -1 ? c.get(Calendar.MINUTE) : min;
final String clearButtonText = clearText;
final long minDate = minDateLong;
final long maxDate = maxDateLong;
if (ACTION_TIME.equalsIgnoreCase(action)) {
runnable = new Runnable() {
@Override
public void run() {
final TimeSetListener timeSetListener = new TimeSetListener(datePickerPlugin, callbackContext);
final TimePickerDialog timeDialog = new TimePickerDialog(currentCtx, timeSetListener, mHour,
mMinutes, DateFormat.is24HourFormat(currentCtx));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
timeDialog.setCancelable(true);
timeDialog.setCanceledOnTouchOutside(false);
if (!clearButtonText.isEmpty()){
timeDialog.setButton(TimePickerDialog.BUTTON_NEUTRAL, clearButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
callbackContext.success("-1");
}
});
}
timeDialog.setButton(DialogInterface.BUTTON_NEGATIVE, currentCtx.getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
callbackContext.success("");
}
});
timeDialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
callbackContext.success("");
return false;
}
});
}
timeDialog.show();
}
};
} else if (ACTION_DATE.equalsIgnoreCase(action)) {
runnable = new Runnable() {
@Override
public void run() {
final DateSetListener dateSetListener = new DateSetListener(datePickerPlugin, callbackContext);
final DatePickerDialog dateDialog = new DatePickerDialog(currentCtx, dateSetListener, mYear,
mMonth, mDay);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
DatePicker dp = dateDialog.getDatePicker();
if (minDate > 0) {
dp.setMinDate(minDate);
}
if (maxDate > 0 && maxDate > minDate) {
dp.setMaxDate(maxDate);
}
dateDialog.setCancelable(true);
dateDialog.setCanceledOnTouchOutside(false);
dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
callbackContext.success("");
}
});
dateDialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
callbackContext.success("");
return false;
}
});
} else {
java.lang.reflect.Field mDatePickerField = null;
try {
mDatePickerField = dateDialog.getClass().getDeclaredField("mDatePicker");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mDatePickerField.setAccessible(true);
DatePicker pickerView = null;
try {
pickerView = (DatePicker) mDatePickerField.get(dateDialog);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
final Calendar startDate = Calendar.getInstance();
startDate.setTimeInMillis(minDate);
final Calendar endDate = Calendar.getInstance();
endDate.setTimeInMillis(maxDate);
final int minYear = startDate.get(Calendar.YEAR);
final int minMonth = startDate.get(Calendar.MONTH);
final int minDay = startDate.get(Calendar.DAY_OF_MONTH);
final int maxYear = endDate.get(Calendar.YEAR);
final int maxMonth = endDate.get(Calendar.MONTH);
final int maxDay = endDate.get(Calendar.DAY_OF_MONTH);
if (startDate != null || endDate != null) {
pickerView.init(mYear, mMonth, mDay, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
if (maxDate > 0 && maxDate > minDate) {
if (year > maxYear || month > maxMonth && year == maxYear || day > maxDay && year == maxYear && month == maxMonth) {
view.updateDate(maxYear, maxMonth, maxDay);
}
}
if (minDate > 0) {
if (year < minYear || month < minMonth && year == minYear || day < minDay && year == minYear && month == minMonth) {
view.updateDate(minYear, minMonth, minDay);
}
}
}
});
}
}
dateDialog.show();
}
};
} else {
Log.d(pluginName, "Unknown action. Only 'date' or 'time' are valid actions");
return;
}
cordova.getActivity().runOnUiThread(runnable);
}
private final class DateSetListener implements OnDateSetListener {
private final CallbackContext callbackContext;
private DateSetListener(DatePickerPlugin datePickerPlugin, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
/**
* Return a string containing the date in the format YYYY/MM/DD
*/
@Override
public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
String returnDate = year + "/" + (monthOfYear + 1) + "/" + dayOfMonth;
callbackContext.success(returnDate);
}
}
private final class TimeSetListener implements OnTimeSetListener {
private final CallbackContext callbackContext;
private TimeSetListener(DatePickerPlugin datePickerPlugin, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
/**
* Return the current date with the time modified as it was set in the
* time picker.
*/
@Override
public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute) {
Date date = new Date();
date.setHours(hourOfDay);
date.setMinutes(minute);
callbackContext.success(date.toGMTString());
}
}
}