Android Calendar API

This article focuses on the use of undocumented API to work with standard calendar Android. 
For what?! 

I ran into this problem when he wrote an application to form an university schedule. It is convenient to have their schedules separately, so even a standard calendar does not support repeating events in one week, which is necessary for a two-week (Thu / odd.) Schedule. Idée fixe was a function of the application, which will "fill" introduced by the schedule Android calendar. Pluses are obvious: Sync with Google Calendar from google (pardon the tautology), built-in calendar widget (very much good this widget from HTC Sense) and a mountain of widgets from third-party manufacturers, which show at least the next event, though utilization of the week, etc.

Use an undocumented API - this is BAD! Got it?

Does that solve the problem of the obvious need to use an undocumented API? The answer - no. The proper method is to use the API Google Calendar, I advise you andmake my own applications.

But the "right" method imposes several limitations:
• Do not use the lack of connection to the Internet;
 the need to synchronize the calendar after filling;
• Data (and there are not enough in filling the whole year) go to the server and then goback in syncwhich obviously increases the traffic in half.

In my opinion, is much more convenient to use the offline version of the calendar, whichat any opportunity, you can synchronize with guglokalendaremUnfortunately google did not care about developers for Android and has not published an official API, but to our delight Android source code open and intelligent people have long found the magic URIcontent providers.

We ask the user for permission to work with calendar


ParadoxAPI does not have, and resolution of the documentation is. You just have toadd them to vashAndroidManifest.xml.



<uses-permission android:name="android.permission.READ_CALENDAR"> </uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"> </uses-permission>

URI

To use the calendar, Android, we must turn to its content provider for a particular URI.Much to our chagrin that the URI is different in different versions of operating systems andis likely to change in the next (the costs of undocumented API). But the problem is solved.
For Froyo [2.2] and is likely for the following versions-content: / / com.android.calendar / calendars.
For versions <2.2 - content: / / calendar / calendars.
As you can see, it's not so bad. You can determine the version of the system and use the appropriate URI, but I went the other way. I watch if there's a URI for <2.2 and if there is no one, then use the remainder.

// Calendar URI before 2.2
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = this.managedQuery(calendars, new String[] { "_id", "name" }, null, null, null);
if(managedCursor == null || managedCursor.getCount() == 0)
{
 // 2.2 (Froyo)
}
else
{
 // < 2.2
}
We assume that the URI received.

Get the list of calendars


For users of Google Calendar do not have to explain, and explain to the restPublic Access consists of one or several small calendars, which can vary in color markers. This is usually something like "personal", "work", "training", etc. Establish such calendars canonly visit guglokalendarya, in the android that standard means you can do.
To add an event, we need to know the ID of the calendar. To perform this task, use the construction of the preceding paragraph.

String[] projection = new String[] { "_id", "name" };
Cursor managedCursor = this.managedQuery(URI, projection, null, null, null);
if (managedCursor != null && managedCursor.moveToFirst()) 
        {
         String calName;
         String calID;         
         int nameColumn = managedCursor.getColumnIndex("name");
         int idColumn = managedCursor.getColumnIndex("_id");
         do 
         {
          calName = managedCursor.getString(nameColumn);
          calID = managedCursor.getString(idColumn);
          if (calName != null) // … UI         
            } while (managedCursor.moveToNext());
            managedCursor.close();
        }
Now the resulting set of names can be displayed in the interface and allow the user to choose.

Add event

Then work with the calendar isas with any other content providerThis can be read in the documentation.

ContentValues event = new ContentValues();
event.put("calendar_id", calID);  // ID календаря мы получили ранее
event.put("title", title); // Название события
event.put("description", desc); // Описание события
event.put("eventLocation", loc); // Место проведения
event.put("dtstart", start); // время начала
event.put("dtend", end); // время окончания
eventsURI=Uri.parse(URI + “/events"); 
this.getContentResolver().insert(eventsURI, event);
Let's talk about the fields of the event. The description field, I advise you to use to indicateevents. Use this field can then be removed en masse all the events generated by your program. Field names, descriptions and venue are string. We should also mention thetime field. These fields have type long, but these fields can be generated, for example,the following function.

public static long pickDate(int year, int month, int day, int hour, int minute) 
{
     Calendar rightNow = Calendar.getInstance();
     int timeShift = rightNow.get(Calendar.ZONE_OFFSET);     
     return Date.UTC(year - 1900, month, day, hour - (timeShift / 3600000), minute, 0);
}
With her, I thinkwill understand themselves, let me note only that it responds to thecurrent time zone.

Remove Event
To delete a message by ID to add it to the URI.
 

//eventsURI = URI + events/
uri = ContentUris.withAppendedId(eventsURI, EVENT_ID);
this.getContentResolver().delete(uri, null, null); 
To remove the other fields you can get all mouse events, like we did with the calendars, and then selectively remove the ID.

Finally

In conclusion, I remind you that you're using it at your own risk. During development, Ionce wrote a URI is not and my phone every time you sync started to issue an error (corrected erasure of information from the calendar altogether.)
Another trouble - the emulator is no calendar, for it to repeat all the above fails.
And it concluded. I can not take the opportunity popiarit the application, which was discussed in the article. You can find it in the market under the name uTimetable.