-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathCalendar.java
563 lines (500 loc) · 22.3 KB
/
Calendar.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package nl.xservices.plugins;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import nl.xservices.plugins.accessor.AbstractCalendarAccessor;
import nl.xservices.plugins.accessor.CalendarProviderAccessor;
import nl.xservices.plugins.accessor.LegacyCalendarAccessor;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import java.util.TimeZone;
public class Calendar extends CordovaPlugin {
private static final String HAS_READ_PERMISSION = "hasReadPermission";
private static final String REQUEST_READ_PERMISSION = "requestReadPermission";
private static final String HAS_WRITE_PERMISSION = "hasWritePermission";
private static final String REQUEST_WRITE_PERMISSION = "requestWritePermission";
private static final String HAS_READWRITE_PERMISSION = "hasReadWritePermission";
private static final String REQUEST_READWRITE_PERMISSION = "requestReadWritePermission";
private static final String ACTION_OPEN_CALENDAR = "openCalendar";
private static final String ACTION_CREATE_EVENT_WITH_OPTIONS = "createEventWithOptions";
private static final String ACTION_CREATE_EVENT_INTERACTIVELY = "createEventInteractively";
private static final String ACTION_DELETE_EVENT = "deleteEvent";
private static final String ACTION_FIND_EVENT_WITH_OPTIONS = "findEventWithOptions";
private static final String ACTION_LIST_EVENTS_IN_RANGE = "listEventsInRange";
private static final String ACTION_LIST_CALENDARS = "listCalendars";
private static final String ACTION_CREATE_CALENDAR = "createCalendar";
private static final Integer RESULT_CODE_CREATE = 0;
private static final Integer RESULT_CODE_OPENCAL = 1;
private CallbackContext callback;
private static final String LOG_TAG = AbstractCalendarAccessor.LOG_TAG;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
callback = callbackContext;
final boolean hasLimitedSupport = Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH;
if (ACTION_OPEN_CALENDAR.equals(action)) {
if (hasLimitedSupport) {
openCalendarLegacy(args);
} else {
openCalendar(args);
}
return true;
} else if (ACTION_CREATE_EVENT_WITH_OPTIONS.equals(action)) {
if (hasLimitedSupport) {
// TODO investigate this option some day: http://stackoverflow.com/questions/3721963/how-to-add-calendar-events-in-android
createEventInteractively(args);
} else {
createEvent(args);
}
return true;
} else if (ACTION_CREATE_EVENT_INTERACTIVELY.equals(action)) {
createEventInteractively(args);
return true;
} else if (ACTION_LIST_EVENTS_IN_RANGE.equals(action)) {
listEventsInRange(args);
return true;
} else if (!hasLimitedSupport && ACTION_FIND_EVENT_WITH_OPTIONS.equals(action)) {
findEvents(args);
return true;
} else if (!hasLimitedSupport && ACTION_DELETE_EVENT.equals(action)) {
deleteEvent(args);
return true;
} else if (ACTION_LIST_CALENDARS.equals(action)) {
listCalendars();
return true;
} else if (!hasLimitedSupport && ACTION_CREATE_CALENDAR.equals(action)) {
createCalendar(args);
return true;
} else if (HAS_READ_PERMISSION.equals(action)) {
hasReadPermission();
return true;
} else if (HAS_WRITE_PERMISSION.equals(action)) {
hasWritePermission();
return true;
} else if (HAS_READWRITE_PERMISSION.equals(action)) {
hasReadWritePermission();
return true;
} else if (REQUEST_READ_PERMISSION.equals(action)) {
requestReadPermission();
return true;
} else if (REQUEST_WRITE_PERMISSION.equals(action)) {
requestWritePermission();
return true;
} else if (REQUEST_READWRITE_PERMISSION.equals(action)) {
requestReadWritePermission();
return true;
}
return false;
}
private void hasReadPermission() {
this.callback.sendPluginResult(new PluginResult(PluginResult.Status.OK,
calendarPermissionGranted(Manifest.permission.READ_CALENDAR)));
}
private void hasWritePermission() {
this.callback.sendPluginResult(new PluginResult(PluginResult.Status.OK,
calendarPermissionGranted(Manifest.permission.WRITE_CALENDAR)));
}
private void hasReadWritePermission() {
this.callback.sendPluginResult(new PluginResult(PluginResult.Status.OK,
calendarPermissionGranted(Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR)));
}
private void requestReadPermission() {
requestPermission(Manifest.permission.READ_CALENDAR);
}
private void requestWritePermission() {
requestPermission(Manifest.permission.WRITE_CALENDAR);
}
private void requestReadWritePermission() {
requestPermission(Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR);
}
private boolean calendarPermissionGranted(String... types) {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
for (final String type : types) {
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this.cordova.getActivity(), type)) {
return false;
}
}
return true;
}
private void requestPermission(String... types) {
if (!calendarPermissionGranted(types)) {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
types,
555);
}
// this method executes async and we seem to have no known way to receive the result, so simply returning ok now
this.callback.success();
}
private void openCalendarLegacy(JSONArray args) {
try {
final Long millis = args.getJSONObject(0).optLong("date");
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
final Intent calendarIntent = new Intent();
calendarIntent.putExtra("beginTime", millis);
calendarIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
calendarIntent.setClassName("com.android.calendar", "com.android.calendar.AgendaActivity");
Calendar.this.cordova.startActivityForResult(Calendar.this, calendarIntent, RESULT_CODE_OPENCAL);
callback.success();
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
@TargetApi(14)
private void openCalendar(JSONArray args) {
try {
final Long millis = args.getJSONObject(0).optLong("date");
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon().appendPath("time");
ContentUris.appendId(builder, millis);
final Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
Calendar.this.cordova.startActivityForResult(Calendar.this, intent, RESULT_CODE_OPENCAL);
callback.success();
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
private void listCalendars() {
// note that if the dev didn't call requestReadPermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.READ_CALENDAR)) {
requestReadPermission();
this.callback.error("Please allow Read access to the Calendar and try again.");
return;
}
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
JSONArray jsonObject = new JSONArray();
try {
jsonObject = Calendar.this.getCalendarAccessor().getActiveCalendars();
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
PluginResult res = new PluginResult(PluginResult.Status.OK, jsonObject);
callback.sendPluginResult(res);
}
});
}
// note: not quite ready for primetime yet
private void createCalendar(JSONArray args) {
if (args.length() == 0) {
System.err.println("Exception: No Arguments passed");
return;
}
// note that if the dev didn't call requestWritePermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.WRITE_CALENDAR)) {
requestWritePermission();
this.callback.error("Please allow Write access to the Calendar and try again.");
return;
}
try {
final JSONObject jsonFilter = args.getJSONObject(0);
final String calendarName = getPossibleNullString("calendarName", jsonFilter);
if (calendarName == null) {
callback.error("calendarName is mandatory");
return;
}
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
getCalendarAccessor().createCalendar(calendarName);
PluginResult res = new PluginResult(PluginResult.Status.OK, "yes");
res.setKeepCallback(true);
callback.sendPluginResult(res);
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
private void createEventInteractively(JSONArray args) {
try {
final JSONObject jsonFilter = args.getJSONObject(0);
final JSONObject argOptionsObject = jsonFilter.getJSONObject("options");
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
final Intent calIntent = new Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
.putExtra("title", getPossibleNullString("title", jsonFilter))
.putExtra("beginTime", jsonFilter.optLong("startTime"))
.putExtra("endTime", jsonFilter.optLong("endTime"))
.putExtra("hasAlarm", 1)
.putExtra("allDay", AbstractCalendarAccessor.isAllDayEvent(new Date(jsonFilter.optLong("startTime")), new Date(jsonFilter.optLong("endTime"))));
// TODO can we pass a reminder here?
// optional fields
if (!jsonFilter.isNull("location")) {
calIntent.putExtra("eventLocation", jsonFilter.optString("location"));
}
String description = null;
if (!jsonFilter.isNull("notes")) {
description = jsonFilter.optString("notes");
}
// there's no separate url field, so adding it to the notes
if (!argOptionsObject.isNull("url")) {
if (description == null) {
description = argOptionsObject.optString("url");
} else {
description += " " + argOptionsObject.optString("url");
}
}
calIntent.putExtra("description", description);
calIntent.putExtra("calendar_id", argOptionsObject.optInt("calendarId", 1));
Calendar.this.cordova.startActivityForResult(Calendar.this, calIntent, RESULT_CODE_CREATE);
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
private AbstractCalendarAccessor calendarAccessor;
private AbstractCalendarAccessor getCalendarAccessor() {
if (this.calendarAccessor == null) {
// Note: currently LegacyCalendarAccessor is never used, see the TO-DO at the top of this class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Log.d(LOG_TAG, "Initializing calendar plugin");
this.calendarAccessor = new CalendarProviderAccessor(this.cordova);
} else {
Log.d(LOG_TAG, "Initializing legacy calendar plugin");
this.calendarAccessor = new LegacyCalendarAccessor(this.cordova);
}
}
return this.calendarAccessor;
}
private void deleteEvent(JSONArray args) {
if (args.length() == 0) {
System.err.println("Exception: No Arguments passed");
return;
}
// note that if the dev didn't call requestWritePermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.WRITE_CALENDAR)) {
requestWritePermission();
this.callback.error("Please allow Write access to the Calendar and try again.");
return;
}
try {
final JSONObject jsonFilter = args.getJSONObject(0);
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
boolean deleteResult = getCalendarAccessor().deleteEvent(
null,
jsonFilter.optLong("startTime"),
jsonFilter.optLong("endTime"),
getPossibleNullString("title", jsonFilter),
getPossibleNullString("location", jsonFilter));
PluginResult res = new PluginResult(PluginResult.Status.OK, deleteResult);
res.setKeepCallback(true);
callback.sendPluginResult(res);
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
private void findEvents(JSONArray args) {
if (args.length() == 0) {
System.err.println("Exception: No Arguments passed");
return;
}
// note that if the dev didn't call requestReadPermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.READ_CALENDAR)) {
requestReadPermission();
this.callback.error("Please allow Read access to the Calendar and try again.");
return;
}
try {
final JSONObject jsonFilter = args.getJSONObject(0);
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
JSONArray jsonEvents = getCalendarAccessor().findEvents(
getPossibleNullString("title", jsonFilter),
getPossibleNullString("location", jsonFilter),
jsonFilter.optLong("startTime"),
jsonFilter.optLong("endTime"));
PluginResult res = new PluginResult(PluginResult.Status.OK, jsonEvents);
res.setKeepCallback(true);
callback.sendPluginResult(res);
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
private void createEvent(JSONArray args) {
// note that if the dev didn't call requestWritePermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.WRITE_CALENDAR)) {
requestWritePermission();
this.callback.error("Please allow Write access to the Calendar and try again.");
return;
}
try {
final JSONObject argObject = args.getJSONObject(0);
final JSONObject argOptionsObject = argObject.getJSONObject("options");
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
final String createdEventID = getCalendarAccessor().createEvent(
null,
getPossibleNullString("title", argObject),
argObject.getLong("startTime"),
argObject.getLong("endTime"),
getPossibleNullString("notes", argObject),
getPossibleNullString("location", argObject),
argOptionsObject.optLong("firstReminderMinutes"),
argOptionsObject.optLong("secondReminderMinutes"),
getPossibleNullString("recurrence", argOptionsObject),
argOptionsObject.optInt("recurrenceInterval"),
argOptionsObject.optLong("recurrenceEndTime"),
argOptionsObject.optInt("calendarId", 1),
getPossibleNullString("url", argOptionsObject), argOptionsObject.optJSONObject("attendees"));
callback.success(createdEventID);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "Error creating event. Invoking error callback.", e);
callback.error(e.getMessage());
}
}
private static String getPossibleNullString(String param, JSONObject from) {
return from.isNull(param) || "null".equals(from.optString(param)) ? null : from.optString(param);
}
private void listEventsInRange(JSONArray args) {
// note that if the dev didn't call requestReadPermission before calling this method and calendarPermissionGranted returns false,
// the app will ask permission and this method needs to be invoked again (done for backward compat).
if (!calendarPermissionGranted(Manifest.permission.READ_CALENDAR)) {
requestReadPermission();
this.callback.error("Please allow Read access to the Calendar and try again.");
return;
}
try {
final JSONObject jsonFilter = args.getJSONObject(0);
JSONArray result = new JSONArray();
long input_start_date = jsonFilter.optLong("startTime");
long input_end_date = jsonFilter.optLong("endTime");
final Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/instances/when/" + String.valueOf(input_start_date) + "/" + String.valueOf(input_end_date));
} else {
l_eventUri = Uri.parse("content://calendar/instances/when/" + String.valueOf(input_start_date) + "/" + String.valueOf(input_end_date));
}
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
ContentResolver contentResolver = Calendar.this.cordova.getActivity().getContentResolver();
JSONArray result = new JSONArray();
long input_start_date = jsonFilter.optLong("startTime");
long input_end_date = jsonFilter.optLong("endTime");
//prepare start date
java.util.Calendar calendar_start = java.util.Calendar.getInstance();
Date date_start = new Date(input_start_date);
calendar_start.setTime(date_start);
//prepare end date
java.util.Calendar calendar_end = java.util.Calendar.getInstance();
Date date_end = new Date(input_end_date);
calendar_end.setTime(date_end);
//projection of DB columns
String[] l_projection = new String[]{"calendar_id", "title", "begin", "end", "eventLocation", "allDay"};
//actual query
Cursor cursor = contentResolver.query(
l_eventUri,
l_projection,
"(deleted = 0 AND" +
" (" +
// all day events are stored in UTC, others in the user's timezone
" (eventTimezone = 'UTC' AND begin >=" + (calendar_start.getTimeInMillis() + TimeZone.getDefault().getOffset(calendar_start.getTimeInMillis())) + " AND end <=" + (calendar_end.getTimeInMillis() + TimeZone.getDefault().getOffset(calendar_end.getTimeInMillis())) + ")" +
" OR " +
" (eventTimezone <> 'UTC' AND begin >=" + calendar_start.getTimeInMillis() + " AND end <=" + calendar_end.getTimeInMillis() + ")" +
" )" +
")",
null,
"begin ASC");
int i = 0;
while (cursor.moveToNext()) {
try {
result.put(
i++,
new JSONObject()
.put("calendar_id", cursor.getString(cursor.getColumnIndex("calendar_id")))
.put("title", cursor.getString(cursor.getColumnIndex("title")))
.put("dtstart", cursor.getLong(cursor.getColumnIndex("begin")))
.put("dtend", cursor.getLong(cursor.getColumnIndex("end")))
.put("eventLocation", cursor.getString(cursor.getColumnIndex("eventLocation")) != null ? cursor.getString(cursor.getColumnIndex("eventLocation")) : "")
.put("allDay", cursor.getInt(cursor.getColumnIndex("allDay")))
);
} catch (JSONException e) {
e.printStackTrace();
}
}
cursor.close();
PluginResult res = new PluginResult(PluginResult.Status.OK, result);
callback.sendPluginResult(res);
}
});
} catch (JSONException e) {
System.err.println("Exception: " + e.getMessage());
callback.error(e.getMessage());
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_CODE_CREATE) {
if (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_CANCELED) {
// resultCode may be 0 (RESULT_CANCELED) even when it was created, so passing nothing is the clearest option here
Log.d(LOG_TAG, "onActivityResult resultcode: " + resultCode);
callback.success();
} else {
// odd case
Log.d(LOG_TAG, "onActivityResult weird resultcode: " + resultCode);
callback.success();
}
} else if (requestCode == RESULT_CODE_OPENCAL) {
Log.d(LOG_TAG, "onActivityResult requestCode: " + RESULT_CODE_OPENCAL);
callback.success();
} else {
Log.d(LOG_TAG, "onActivityResult error, resultcode: " + resultCode);
callback.error("Unable to add event (" + resultCode + ").");
}
}
}