0

I am currently working on creating a custom calendar application and have been struggling with saving a recurring event. If for example I set the event to occur daily for 100 days I am only seeing about 38 instances saved to the instances database. Not sure why this is happening.

Here is the code that adds/updates an event

public Event addUpdateCalendarEvent(Context ctx, Event event) throws InterruptedException {

    ContentResolver contentResolver = ctx.getContentResolver();

    ContentValues calEvent = new ContentValues();
    calEvent.put(Events.CALENDAR_ID, event.getCalendarId());

    calEvent.put(Events.TITLE, event.getEventName());
    calEvent.put(Events.DTSTART, event.getDateFrom().getTimeInMillis()); 
    calEvent.put(Events.EVENT_TIMEZONE,event.getDateFrom().getTimeZone().getID());
    if(event.isAllDay()) {
        calEvent.put(Events.ALL_DAY, 1);
    }
    if(null != event.getRrule()) {
        String rrule = RecurrenceRuleConverter.toString(event.getRrule());
        Log.d("rrule: ", rrule);
        calEvent.put(Events.RRULE, rrule);
    }
    if(null != event.getDuration()) {
        calEvent.put(Events.DURATION, event.getDuration());
    }
    if(null != event.getDateTo()) {
        calEvent.put(Events.DTEND, event.getDateTo().getTimeInMillis());
    }
    Uri uri = null;
    try {
         if(null != event.getEventId()) {
            uri = ContentUris.withAppendedId(Events.CONTENT_URI, event.getEventId());
            contentResolver.update(uri, calEvent, null, null);
        }
        else {
            uri = contentResolver.insert(Events.CONTENT_URI, calEvent);
        }
    }
    finally {
        event.setEventId(Integer.parseInt(uri.getLastPathSegment()));
    }
    return event;
}

Directly after I call the addUpdate method I call another method to list the instances

public List<Event> getEventInstances(Context context, Integer calendarId, Integer eventId) {
    // Projection array. Creating indices for this array instead of doing
    // dynamic lookups improves performance.
    final String[] EVENT_PROJECTION = new String[] {
        CalendarContract.Instances.CALENDAR_ID,
        CalendarContract.Instances._ID, 
        CalendarContract.Instances.BEGIN, 
        CalendarContract.Instances.END,
        CalendarContract.Instances.TITLE,
        CalendarContract.Instances.RRULE,
        CalendarContract.Instances.EVENT_ID,
        CalendarContract.Instances.EVENT_TIMEZONE,
        CalendarContract.Instances.ALL_DAY
    };

    // Run query
    Cursor cur = null;
    ContentResolver cr = context.getContentResolver();


    Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); 
    ContentUris.appendId(builder, Long.MIN_VALUE);
    ContentUris.appendId(builder, Long.MAX_VALUE); 
    Uri uri = builder.build();
    List<Event> events = new ArrayList<Event>();
    String selection = "((" + Instances.CALENDAR_ID + " = ?) AND (" + Instances.EVENT_ID + " = ?))";
    String[] selectionArgs = new String[] {String.valueOf(calendarId),String.valueOf(eventId)}; 
    try {
        cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);

        while (cur.moveToNext()) {
            Event event = new Event();
            event.setCalendarId(cur.getInt(0));
            java.util.Calendar calFrom = java.util.Calendar.getInstance();
            calFrom.setTimeInMillis(Long.valueOf(cur.getLong(2)));
            java.util.Calendar calTo = java.util.Calendar.getInstance();
            calTo.setTimeInMillis(Long.valueOf(Long.valueOf(cur.getLong(3))));
            event.setDateFrom(calFrom);
            event.setDateTo(calTo);
            event.setTitle(cur.getString(4));
            event.setEventId(cur.getInt(6));
            RecurrenceRule rule = new RecurrenceRule();
            rule.setRuleAsText(cur.getString(5));
            event.setRrule(rule);  
            event.setAllDay(cur.getInt(8) == 1);
            Log.d("Event:", event.getTitle() + " CalendarID: "  + event.getCalendarId() + " EventId: " + event.getEventId() + " " + DateUtil.dateTimeFormat.format(calFrom.getTime()) + " to " + DateUtil.dateTimeFormat.format(calTo.getTime()) + " TimeZone: " + cur.getString(7));

            events.add(event);
        }
    }
    finally {
        cur.close();
        sortListEventsByDate(events);
        Log.d("GetEventInstances Size", String.valueOf(events.size()));

    }
    return events;
}

Here is an example of the properties of an event I tried to save that occurred daily for 100 days:

event   Event  (id=830026469328)    
calendarId  Integer  (id=830015046320)  
dateFrom    GregorianCalendar  (id=830027217712)    
dateTo  null    
duration    "P1H" (id=830029516016) 
eventId Integer  (id=830029520232)  
eventName   "ginos pizza" (id=830044532096) 
eventTimezone   "Central Standard Time" (id=830024050504)   
isAllDay    false   
rdate   null    
rrule   ruleAsText  "FREQ=DAILY;COUNT=100" (id=830029519256)    

Here is the event information from the vcd file:

BEGIN:VCALENDAR
VERSION:1.0
PRODID:vCal ID default
TZ:-05:00
BEGIN:VEVENT
UID:content://com.android.calendar/events/1187
DTEND:20130810T040000Z
DTSTART:20130810T030000Z
DUE:P3600S
COMPLETED:20131117T050000Z
RRULE:FREQ=DAILY;COUNT=100
SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:=67=69=6E=6F=73=20=70=69=7A=7A=61
STATUS:TENTATIVE
END:VEVENT
END:VCALENDAR

Can't figure out why it is only saving a fraction of the instances. Please Help!!! Please let me know if you need to see any other information.

Thank You, Aaron

pleasantstranga
  • 231
  • 2
  • 14
  • UPDATE: I noticed that if I opened the native calendar app that came with my phone I could see all 100 events. I also notice that if I scrolled through my calendar week by week until I reach the last event instance that when I went back into my app and listed all the event instances I saw all 100 instances. I am not sure what that means for how I need to get a list of all instances. – pleasantstranga Aug 14 '13 at 15:28
  • I figured it out a while ago...sorry I didn't add it earlier. The issue was with passing in Long.min and Long.max when searching through the events. I solved the issues by loading events one month at a time – pleasantstranga Nov 06 '13 at 01:34

1 Answers1

0

I figured it out a while ago...sorry I didn't add it earlier. The issue was with passing in Long.min and Long.max when searching through the events. I solved the issues by loading events one month at a time.

pleasantstranga
  • 231
  • 2
  • 14