Sat, 14 Feb 2015 21:28:43 +0100
Merge manually from 842945e5b782, correct build configuration to assert.
1 /**
2 * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger
3 *
4 * This file is part of Andoid Caldav Sync Adapter Free.
5 *
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute
7 * it and/or modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, either version 3 of the
9 * License, or at your option any later version.
10 *
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Andoid Caldav Sync Adapter Free.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
22 package org.gege.caldavsyncadapter.android.entities;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.provider.CalendarContract.Attendees;
27 import android.provider.CalendarContract.Events;
28 import android.provider.CalendarContract.Reminders;
30 import net.fortuna.ical4j.model.Calendar;
31 import net.fortuna.ical4j.model.Component;
32 import net.fortuna.ical4j.model.ComponentList;
33 import net.fortuna.ical4j.model.Date;
34 import net.fortuna.ical4j.model.DateTime;
35 import net.fortuna.ical4j.model.Dur;
36 import net.fortuna.ical4j.model.ParameterList;
37 import net.fortuna.ical4j.model.Property;
38 import net.fortuna.ical4j.model.PropertyList;
39 import net.fortuna.ical4j.model.TimeZone;
40 import net.fortuna.ical4j.model.TimeZoneRegistry;
41 import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
42 import net.fortuna.ical4j.model.component.VAlarm;
43 import net.fortuna.ical4j.model.component.VEvent;
44 import net.fortuna.ical4j.model.parameter.Cn;
45 import net.fortuna.ical4j.model.parameter.PartStat;
46 import net.fortuna.ical4j.model.parameter.Role;
47 import net.fortuna.ical4j.model.parameter.Rsvp;
48 import net.fortuna.ical4j.model.parameter.Value;
49 import net.fortuna.ical4j.model.property.Action;
50 import net.fortuna.ical4j.model.property.Attendee;
51 import net.fortuna.ical4j.model.property.CalScale;
52 import net.fortuna.ical4j.model.property.Clazz;
53 import net.fortuna.ical4j.model.property.Description;
54 import net.fortuna.ical4j.model.property.DtEnd;
55 import net.fortuna.ical4j.model.property.DtStart;
56 import net.fortuna.ical4j.model.property.Duration;
57 import net.fortuna.ical4j.model.property.ExDate;
58 import net.fortuna.ical4j.model.property.ExRule;
59 import net.fortuna.ical4j.model.property.Location;
60 import net.fortuna.ical4j.model.property.Organizer;
61 import net.fortuna.ical4j.model.property.ProdId;
62 import net.fortuna.ical4j.model.property.RDate;
63 import net.fortuna.ical4j.model.property.RRule;
64 import net.fortuna.ical4j.model.property.Status;
65 import net.fortuna.ical4j.model.property.Summary;
66 import net.fortuna.ical4j.model.property.Trigger;
67 import net.fortuna.ical4j.model.property.Uid;
68 import net.fortuna.ical4j.model.property.Version;
70 import org.gege.caldavsyncadapter.caldav.entities.CalendarEvent;
72 import java.net.URISyntaxException;
73 import java.text.ParseException;
75 import static junit.framework.Assert.*;
77 import com.europalab.caldavsyn.Appctxpro;
79 //import android.accounts.Account;
80 //import android.content.ContentProviderClient;
81 //import android.content.ContentValues;
82 //import android.content.SyncStats;
83 //import android.os.RemoteException;
84 //import android.provider.CalendarContract.Calendars;
85 //import org.gege.caldavsyncadapter.Event;
86 //import org.gege.caldavsyncadapter.caldav.CaldavFacade;
87 //import org.gege.caldavsyncadapter.caldav.entities.DavCalendar;
88 //import org.gege.caldavsyncadapter.syncadapter.SyncAdapter;
90 public class AndroidEvent extends org.gege.caldavsyncadapter.Event {
92 private Uri muri;
94 private Uri mAndroidCalendarUri;
96 /**
97 * the list of attendees
98 */
99 private PropertyList mAttendees = new PropertyList();
101 /**
102 * the list of reminders
103 */
104 private ComponentList mReminders = new ComponentList();
106 private Calendar mCalendar = null;
108 /* private Account mAccount = null;
109 private ContentProviderClient mProvider = null;*/
111 //public AndroidEvent(Account account, ContentProviderClient provider, Uri uri, Uri calendarUri) {
112 public AndroidEvent(Uri uri, Uri calendarUri) {
113 super();
114 this.setUri(uri);
115 /* this.mAccount = account;
116 this.mProvider = provider;*/
117 //this.setCounterpartUri(calendarUri);
118 mAndroidCalendarUri = calendarUri;
119 }
121 public Calendar getIcsEvent() {
122 return mCalendar;
123 }
125 public String getETag() {
126 String Result = "";
127 if (this.ContentValues.containsKey(ETAG))
128 Result = this.ContentValues.getAsString(ETAG);
129 return Result;
130 }
132 public void setETag(String eTag) {
133 this.ContentValues.put(ETAG, eTag);
134 }
136 public Uri getUri() {
137 return muri;
138 }
140 public void setUri(Uri uri) {
141 this.muri = uri;
142 }
144 public Uri getAndroidCalendarUri() {
145 return mAndroidCalendarUri;
146 }
148 @Override
149 public String toString() {
150 return this.getUri().toString();
151 }
153 /**
154 * reads an android event from a given cursor into {@link AndroidEvent#ContentValues}
155 *
156 * @param cur the cursor with the event
157 * @return success of this funtion
158 * @see AndroidEvent#ContentValues
159 */
160 public boolean readContentValues(Cursor cur) {
161 this.setETag(cur.getString(cur.getColumnIndex(ETAG)));
163 this.ContentValues.put(Events.EVENT_TIMEZONE, cur.getString(cur.getColumnIndex(Events.EVENT_TIMEZONE)));
164 this.ContentValues.put(Events.EVENT_END_TIMEZONE, cur.getString(cur.getColumnIndex(Events.EVENT_END_TIMEZONE)));
165 this.ContentValues.put(Events.DTSTART, cur.getLong(cur.getColumnIndex(Events.DTSTART)));
166 this.ContentValues.put(Events.DTEND, cur.getLong(cur.getColumnIndex(Events.DTEND)));
167 this.ContentValues.put(Events.ALL_DAY, cur.getLong(cur.getColumnIndex(Events.ALL_DAY)));
168 this.ContentValues.put(Events.TITLE, cur.getString(cur.getColumnIndex(Events.TITLE)));
169 this.ContentValues.put(Events.CALENDAR_ID, cur.getString(cur.getColumnIndex(Events.CALENDAR_ID)));
170 this.ContentValues.put(Events._SYNC_ID, cur.getString(cur.getColumnIndex(Events._SYNC_ID)));
171 //this.ContentValues.put(Events.SYNC_DATA1, cur.getString(cur.getColumnIndex(Events.SYNC_DATA1))); //not needed here, eTag has already been read
172 this.ContentValues.put(Events.DESCRIPTION, cur.getString(cur.getColumnIndex(Events.DESCRIPTION)));
173 this.ContentValues.put(Events.EVENT_LOCATION, cur.getString(cur.getColumnIndex(Events.EVENT_LOCATION)));
174 this.ContentValues.put(Events.ACCESS_LEVEL, cur.getInt(cur.getColumnIndex(Events.ACCESS_LEVEL)));
176 this.ContentValues.put(Events.STATUS, cur.getInt(cur.getColumnIndex(Events.STATUS)));
178 this.ContentValues.put(Events.LAST_DATE, cur.getInt(cur.getColumnIndex(Events.LAST_DATE)));
179 this.ContentValues.put(Events.DURATION, cur.getString(cur.getColumnIndex(Events.DURATION)));
181 this.ContentValues.put(Events.RDATE, cur.getString(cur.getColumnIndex(Events.RDATE)));
182 this.ContentValues.put(Events.RRULE, cur.getString(cur.getColumnIndex(Events.RRULE)));
183 this.ContentValues.put(Events.EXRULE, cur.getString(cur.getColumnIndex(Events.EXRULE)));
184 this.ContentValues.put(Events.EXDATE, cur.getString(cur.getColumnIndex(Events.EXDATE)));
185 this.ContentValues.put(Events.DIRTY, cur.getInt(cur.getColumnIndex(Events.DIRTY)));
186 this.ContentValues.put(UID, cur.getString(cur.getColumnIndex(UID)));
187 this.ContentValues.put(RAWDATA, cur.getString(cur.getColumnIndex(RAWDATA)));
189 return true;
190 }
192 /**
193 * reads the attendees from a given cursor
194 *
195 * @param cur the cursor with the attendees
196 * @return success of this function
197 * @see AndroidEvent#mAttendees
198 */
199 public boolean readAttendees(Cursor cur) {
200 Attendee attendee = null;
201 Organizer organizer = null;
202 ParameterList paraList = null;
204 String Name = "";
205 Cn cn = null;
207 String Email = "";
209 int Relationship = 0;
212 int Status = 0;
213 PartStat partstat = null;
215 int Type = 0;
216 Role role = null;
218 try {
219 while (cur.moveToNext()) {
220 Name = cur.getString(cur.getColumnIndex(Attendees.ATTENDEE_NAME));
221 Email = cur.getString(cur.getColumnIndex(Attendees.ATTENDEE_EMAIL));
222 Relationship = cur.getInt(cur.getColumnIndex(Attendees.ATTENDEE_RELATIONSHIP));
223 Type = cur.getInt(cur.getColumnIndex(Attendees.ATTENDEE_TYPE));
224 Status = cur.getInt(cur.getColumnIndex(Attendees.ATTENDEE_STATUS));
226 if (Relationship == Attendees.RELATIONSHIP_ORGANIZER) {
227 organizer = new Organizer();
228 organizer.setValue("mailto:" + Email);
229 paraList = organizer.getParameters();
230 mAttendees.add(organizer);
231 } else {
232 attendee = new Attendee();
233 attendee.setValue("mailto:" + Email);
234 paraList = attendee.getParameters();
235 mAttendees.add(attendee);
236 }
238 Rsvp rsvp = new Rsvp(true);
239 paraList.add(rsvp);
241 cn = new Cn(Name);
242 paraList.add(cn);
244 if (Status == Attendees.ATTENDEE_STATUS_INVITED)
245 partstat = new PartStat(PartStat.NEEDS_ACTION.getValue());
246 else if (Status == Attendees.ATTENDEE_STATUS_ACCEPTED)
247 partstat = new PartStat(PartStat.ACCEPTED.getValue());
248 else if (Status == Attendees.ATTENDEE_STATUS_DECLINED)
249 partstat = new PartStat(PartStat.DECLINED.getValue());
250 else if (Status == Attendees.ATTENDEE_STATUS_NONE)
251 partstat = new PartStat(PartStat.COMPLETED.getValue());
252 else if (Status == Attendees.ATTENDEE_STATUS_TENTATIVE)
253 partstat = new PartStat(PartStat.TENTATIVE.getValue());
254 else
255 partstat = new PartStat(PartStat.NEEDS_ACTION.getValue());
256 paraList.add(partstat);
258 if (Type == Attendees.TYPE_OPTIONAL)
259 role = new Role(Role.OPT_PARTICIPANT.getValue());
260 else if (Type == Attendees.TYPE_NONE)
261 role = new Role(Role.NON_PARTICIPANT.getValue()); //regular participants in android are non required?
262 else if (Type == Attendees.TYPE_REQUIRED)
263 role = new Role(Role.REQ_PARTICIPANT.getValue());
264 else
265 role = new Role(Role.NON_PARTICIPANT.getValue());
266 paraList.add(role);
267 }
269 } catch (URISyntaxException e) {
270 e.printStackTrace();
271 }
272 return true;
273 }
275 /**
276 * reads the reminders from a given cursor
277 *
278 * @param cur the cursor with the reminders
279 * @return success of this function
280 */
281 public boolean readReminder(Cursor cur) {
282 int Method;
283 int Minutes;
284 VAlarm reminder;
285 while (cur.moveToNext()) {
286 reminder = new VAlarm();
287 Method = cur.getInt(cur.getColumnIndex(Reminders.METHOD));
288 Minutes = cur.getInt(cur.getColumnIndex(Reminders.MINUTES)) * -1;
291 Dur dur = new Dur(0, 0, Minutes, 0);
292 Trigger tri = new Trigger(dur);
293 Value val = new Value(Duration.DURATION);
294 tri.getParameters().add(val);
295 reminder.getProperties().add(tri);
297 Description desc = new Description();
298 desc.setValue("caldavsyncadapter standard description");
299 reminder.getProperties().add(desc);
302 if (Method == Reminders.METHOD_EMAIL)
303 reminder.getProperties().add(Action.EMAIL);
304 else
305 reminder.getProperties().add(Action.DISPLAY);
307 this.mReminders.add(reminder);
308 }
309 return true;
310 }
312 /**
313 * generates a new ics-file.
314 * uses {@link AndroidEvent#ContentValues} as source.
315 * this should only be used when a new event has been generated within android.
316 *
317 * @param strUid the UID for this event. example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter
318 * @return success of the function
319 * @see CalendarEvent#fetchBody()
320 */
321 public boolean createIcs(String strUid) {
322 boolean Result = false;
323 TimeZone timeZone = null;
324 // Hack to bootstrap a multithreaded class loader context
325 if (Thread.currentThread().getContextClassLoader() == null)
326 Thread.currentThread().setContextClassLoader(Appctxpro.getContext().getClassLoader());
327 TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
328 assertNotNull(registry);
329 //TODO: do not simply create the ics-file new. take into account the RAWDATA if available
330 /*
331 * dtstart=1365598800000
332 * dtend=1365602400000
333 * eventTimezone=Europe/Berlin
334 * eventEndTimezone=null
335 * duration=null
336 * allDay=0
337 * rrule=null
338 * rdate=null
339 * exrule=null
340 * exdate=null
341 * title=Einurlner Termin
342 * description=null
343 * eventLocation=null
344 * accessLevel=0
345 * eventStatus=0
346 *
347 * calendar_id=4
348 * lastDate=-197200128
349 * sync_data1=null
350 * _sync_id=null
351 * dirty=1
352 */
354 try {
355 mCalendar = new Calendar();
356 PropertyList propCalendar = mCalendar.getProperties();
357 propCalendar.add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
358 propCalendar.add(Version.VERSION_2_0);
359 propCalendar.add(CalScale.GREGORIAN);
361 VEvent event = new VEvent();
362 mCalendar.getComponents().add(event);
363 PropertyList propEvent = event.getProperties();
365 // DTSTAMP -> is created by new VEvent() automatical
366 //na
368 // CREATED
369 //na
371 // LAST-MODIFIED
372 //na
374 // SEQUENCE
375 //na
377 // DTSTART
378 long lngStart = this.ContentValues.getAsLong(Events.DTSTART);
379 String strTZStart = this.ContentValues.getAsString(Events.EVENT_TIMEZONE);
380 boolean allDay = this.ContentValues.getAsBoolean(Events.ALL_DAY);
381 if (lngStart > 0) {
382 DtStart dtStart = new DtStart();
383 if (allDay) {
384 Date dateStart = new Date();
385 dateStart.setTime(lngStart);
386 dtStart.setDate(dateStart);
387 } else {
388 DateTime datetimeStart = new DateTime();
389 datetimeStart.setTime(lngStart);
390 dtStart.setDate(datetimeStart);
392 timeZone = registry.getTimeZone(strTZStart);
393 if (timeZone == null) {
394 java.util.TimeZone systemTimeZone = TimeZone.getTimeZone(strTZStart);
395 if (systemTimeZone == null) {
396 systemTimeZone = TimeZone.getDefault();
397 }
398 timeZone = registry.getTimeZone(systemTimeZone.getID());
399 }
400 dtStart.setTimeZone(timeZone);
402 // no timezone information for allDay events
403 mCalendar.getComponents().add(timeZone.getVTimeZone());
404 }
405 propEvent.add(dtStart);
406 }
408 // DTEND
409 long lngEnd = this.ContentValues.getAsLong(Events.DTEND);
410 String strTZEnd = this.ContentValues.getAsString(Events.EVENT_END_TIMEZONE);
411 if (strTZEnd == null)
412 strTZEnd = strTZStart;
413 if (lngEnd > 0) {
414 DtEnd dtEnd = new DtEnd();
415 if (allDay) {
416 Date dateEnd = new Date();
417 dateEnd.setTime(lngEnd);
418 dtEnd.setDate(dateEnd);
419 } else {
420 DateTime datetimeEnd = new DateTime();
421 datetimeEnd.setTime(lngEnd);
422 dtEnd.setDate(datetimeEnd);
423 if (strTZEnd != null)
424 timeZone = registry.getTimeZone(strTZEnd);
425 dtEnd.setTimeZone(timeZone);
426 }
427 propEvent.add(dtEnd);
428 }
430 // DURATION
431 if (this.ContentValues.containsKey(Events.DURATION)) {
432 String strDuration = this.ContentValues.getAsString(Events.DURATION);
433 if (strDuration != null) {
434 Duration duration = new Duration();
435 duration.setValue(strDuration);
437 propEvent.add(duration);
438 }
439 }
441 //RRULE
442 if (this.ContentValues.containsKey(Events.RRULE)) {
443 String strRrule = this.ContentValues.getAsString(Events.RRULE);
444 if (strRrule != null) {
445 if (!strRrule.equals("")) {
446 RRule rrule = new RRule();
447 rrule.setValue(strRrule);
448 propEvent.add(rrule);
449 }
450 }
451 }
453 //RDATE
454 if (this.ContentValues.containsKey(Events.RDATE)) {
455 String strRdate = this.ContentValues.getAsString(Events.RDATE);
456 if (strRdate != null) {
457 if (!strRdate.equals("")) {
458 RDate rdate = new RDate();
459 rdate.setValue(strRdate);
460 propEvent.add(rdate);
461 }
462 }
463 }
465 //EXRULE
466 if (this.ContentValues.containsKey(Events.EXRULE)) {
467 String strExrule = this.ContentValues.getAsString(Events.EXRULE);
468 if (strExrule != null) {
469 if (!strExrule.equals("")) {
470 ExRule exrule = new ExRule();
471 exrule.setValue(strExrule);
472 propEvent.add(exrule);
473 }
474 }
475 }
477 //EXDATE
478 if (this.ContentValues.containsKey(Events.EXDATE)) {
479 String strExdate = this.ContentValues.getAsString(Events.EXDATE);
480 if (strExdate != null) {
481 if (!strExdate.equals("")) {
482 ExDate exdate = new ExDate();
483 exdate.setValue(strExdate);
484 propEvent.add(exdate);
485 }
486 }
487 }
489 //SUMMARY
490 if (this.ContentValues.containsKey(Events.TITLE)) {
491 String strTitle = this.ContentValues.getAsString(Events.TITLE);
492 if (strTitle != null) {
493 Summary summary = new Summary(strTitle);
494 propEvent.add(summary);
495 }
496 }
498 //DESCIPTION
499 if (this.ContentValues.containsKey(Events.DESCRIPTION)) {
500 String strDescription = this.ContentValues.getAsString(Events.DESCRIPTION);
501 if (strDescription != null) {
502 if (!strDescription.equals("")) {
503 Description description = new Description(strDescription);
504 propEvent.add(description);
505 }
506 }
507 }
509 //LOCATION
510 if (this.ContentValues.containsKey(Events.EVENT_LOCATION)) {
511 String strLocation = this.ContentValues.getAsString(Events.EVENT_LOCATION);
512 if (strLocation != null) {
513 if (!strLocation.equals("")) {
514 Location location = new Location(strLocation);
515 propEvent.add(location);
516 }
517 }
518 }
520 //CLASS / ACCESS_LEVEL
521 if (this.ContentValues.containsKey(Events.ACCESS_LEVEL)) {
522 int accessLevel = this.ContentValues.getAsInteger(Events.ACCESS_LEVEL);
523 Clazz clazz = new Clazz();
524 if (accessLevel == Events.ACCESS_PUBLIC)
525 clazz.setValue(Clazz.PUBLIC.getValue());
526 else if (accessLevel == Events.ACCESS_PRIVATE)
527 clazz.setValue(Clazz.PRIVATE.getValue());
528 else if (accessLevel == Events.ACCESS_CONFIDENTIAL)
529 clazz.setValue(Clazz.CONFIDENTIAL.getValue());
530 else
531 clazz.setValue(Clazz.PUBLIC.getValue());
533 propEvent.add(clazz);
534 }
536 //STATUS
537 if (this.ContentValues.containsKey(Events.STATUS)) {
538 int intStatus = this.ContentValues.getAsInteger(Events.STATUS);
539 if (intStatus > -1) {
540 Status status = new Status();
541 if (intStatus == Events.STATUS_CANCELED)
542 status.setValue(Status.VEVENT_CANCELLED.getValue());
543 else if (intStatus == Events.STATUS_CONFIRMED)
544 status.setValue(Status.VEVENT_CONFIRMED.getValue());
545 else if (intStatus == Events.STATUS_TENTATIVE)
546 status.setValue(Status.VEVENT_TENTATIVE.getValue());
548 propEvent.add(status);
549 }
550 }
552 //UID
553 Uid uid = new Uid(strUid);
554 propEvent.add(uid);
556 // Attendees
557 if (mAttendees.size() > 0) {
558 for (Object objProp : mAttendees) {
559 Property prop = (Property) objProp;
560 propEvent.add(prop);
561 }
562 }
564 // Reminders
565 if (mReminders.size() > 0) {
566 for (Object objComp : mReminders) {
567 VAlarm com = (VAlarm) objComp;
568 event.getAlarms().add(com);
569 }
570 }
572 } catch (ParseException e) {
573 e.printStackTrace();
574 }
576 return Result;
577 }
579 /**
580 * marks the android event as already handled
581 * @return
582 * @see AndroidEvent#cInternalTag
583 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
584 * @throws RemoteException
585 */
586 /* public boolean tagAndroidEvent() throws RemoteException {
588 ContentValues values = new ContentValues();
589 values.put(Event.INTERNALTAG, 1);
591 int RowCount = this.mProvider.update(asSyncAdapter(this.getUri(), this.mAccount.name, this.mAccount.type), values, null, null);
592 //Log.e(TAG,"Rows updated: " + RowCount.toString());
594 return (RowCount == 1);
595 }*/
597 /* private static Uri asSyncAdapter(Uri uri, String account, String accountType) {
598 return uri.buildUpon()
599 .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
600 .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
601 .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
602 }*/
603 }