Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Out of pocket and Android Calendars

0.00/5 (No votes)
17 Sep 2015CPOL2 min read 6.1K  
So, I know I've been out of pocket the past two weeks, and that includes all social medias. Honestly I just started a new job, and my wife did as well. So in my family, we do life events in batches. But I didn't want to stay out of pocket

So, I know I've been out of pocket the past two weeks, and that includes all social medias. Honestly I just started a new job, and my wife did as well. So in my family, we do life events in batches.

But I didn't want to stay out of pocket for too long. First and foremost, I wanted to officially announce it here, I have a talk at the PhillydotNet Code Camp on 10/9. I will be giving my Xamarin and Android talk. Which is one I enjoy but also tends to get a lot of good discussion going. For those who have never attended, this is a pretty amazing event. They always throw a great code camp with a lot of great speakers, and its one that I highly recommend you attend if you can.

Here's the link for the code camp:
http://phillydotnet.org/camps/2015-2/

And here's the schedule for Saturday, when I'll be presenting (I am presenting at 11:30)
http://phillydotnet.org/campdetails/grid/

Also, I did want to take some time to just talk about different options and ideas regarding the .net framework. First and foremost, let's look at Android since that's what my talk is about. I'm currently working on releasing an android app (more on that later).

Now one of the common things I see in apps, specifically that I've worked on is interfacing with resources on the device. Things like Calendar, Contacts, etc. And most developers would be inclined here to just start coding how they are going to get the data they need.

I've talked on this blog at length about my opinions on dependency injection. And given that we as developers should take time to architect this solution correctly, and that means wrapping this logic into a classes that we can utilize an interface to implement different implementations for different mobile platforms. Taking this time now will save us huge headaches in the future.

`public interface ICalendarProvider { List<calendarmodel> GetCalendars (); List<calendareventmodel> GetCalendarEvents (int calendarID); List<calendareventattendeemodel> GetEventAttendees (int eventID); }

public class CalendarProvider : ICalendarProvider
{
    private Activity _activity;

    public CalendarProvider (Activity activity)
    {
        _activity = activity;
    }

    public List<CalendarModel> GetCalendars()
    {
        List<CalendarModel> calendars = new List<CalendarModel> ();

        var calendarsUri = CalendarContract.Calendars.ContentUri;

        string[] calendarsProjection = {
            CalendarContract.Calendars.InterfaceConsts.Id,
            CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,
            CalendarContract.Calendars.InterfaceConsts.AccountName
        };

        var cursor = _activity.ManagedQuery (calendarsUri, calendarsProjection, null, null, null);

        while (cursor.MoveToNext()) {
            int id = cursor.GetInt (0);
            string displayName = cursor.GetString (1);
            string accountName = cursor.GetString (2);

            var calendar = new CalendarModel () {
                CalendarID = id,
                CalendarDisplayName = displayName,
                CalendarName = accountName
            };
            calendars.Add (calendar);
        }

        return calendars;
    }

    public List<CalendarEventModel> GetCalendarEvents(int calendarID)
    {
        List<CalendarEventModel> events = new List<CalendarEventModel> ();

        var eventsUri = CalendarContract.Events.ContentUri;

        string[] eventsProjection = {
            CalendarContract.Events.InterfaceConsts.Id,
            CalendarContract.Events.InterfaceConsts.CalendarId,
            CalendarContract.Events.InterfaceConsts.Organizer,
            CalendarContract.Events.InterfaceConsts.Title,
            CalendarContract.Events.InterfaceConsts.EventLocation,
            CalendarContract.Events.InterfaceConsts.Description,
            CalendarContract.Events.InterfaceConsts.Dtstart,
            CalendarContract.Events.InterfaceConsts.Dtend,
            CalendarContract.Events.InterfaceConsts.EventTimezone,
            CalendarContract.Events.InterfaceConsts.Duration,
            CalendarContract.Events.InterfaceConsts.AllDay
        };

        var cursor = _activity.ManagedQuery (eventsUri, eventsProjection,
            String.Format ("calendar_id={0}", calendarID), null, "dtstart ASC");

        while (cursor.MoveToNext()) {
            int id = cursor.GetInt (0);
            int calID = cursor.GetInt(1);
            string organizer = cursor.GetString (2);
            string title = cursor.GetString (3);
            string eventLocation = cursor.GetString (4);
            string description = cursor.GetString (5);
            DateTime startDate = Convert.ToDateTime(cursor.GetString (6));
            DateTime endDate = Convert.ToDateTime (cursor.GetString (7));
            string timeZone = cursor.GetString (8);
            string duration = cursor.GetString (9);
            bool allDay = bool.Parse (cursor.GetString (10));

            var eventRecord = new CalendarEventModel () {
                EventID = id,
                CalendarID = calID,
                Organizer = organizer,
                Title = title,
                Location = eventLocation,
                Description = description,
                Start = startDate,
                End = endDate,
                TimeZone = timeZone,
                Duration = duration,
                AllDay = allDay
            };
            events.Add (eventRecord);
        }

        return events;
    }

    public List<CalendarEventAttendeeModel> GetEventAttendees(int eventID)
    {
        List<CalendarEventAttendeeModel> attendees = new List<CalendarEventAttendeeModel> ();

        var eventsUri = CalendarContract.Attendees.ContentUri;

        string[] eventsProjection = {
            CalendarContract.Attendees.InterfaceConsts.Id,
            CalendarContract.Attendees.InterfaceConsts.EventId,
            CalendarContract.Attendees.InterfaceConsts.AttendeeName,
            CalendarContract.Attendees.InterfaceConsts.AttendeeEmail,
            CalendarContract.Attendees.InterfaceConsts.AttendeeType,
            CalendarContract.Attendees.InterfaceConsts.AttendeeStatus
        };

        var cursor = _activity.ManagedQuery (eventsUri, eventsProjection,
            String.Format ("event_id={0}", eventID), null, "dtstart ASC");

        while (cursor.MoveToNext ()) {
            CalendarEventAttendeeModel model = new CalendarEventAttendeeModel () {
                ID = cursor.GetInt(0),
                EventID = cursor.GetInt(1),
                AttendeeName = cursor.GetString(2),
                AttendeeEmail = cursor.GetString(3),
                AttendeeType = cursor.GetString(4),
                AttendeeStatus = cursor.GetString(5)
            };

            attendees.Add (model);
        }

        return attendees;
    }
}`

This class will provide basic interaction with the underlying calendar objects on android. This allows for the support of further growth later.

That's all for now. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)