Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Calendar: how to write sync adapter for calendar INSERT

I would basically like to reopen the following unanswered post:

  • Insert new calendar with SyncAdapter- Calendar API Android

I would like to make use of the Android Calendar Provider API to create a calendar and insert events into it. The calendar that is created must not be a local copy. It must synchronize with the Google (online) Calendar. The documentation says in order to do this I must use a sync adapter. But how do I write such a sync adapter?

So far I've found the two following posts but no solution.

  • Create new synced calendar with android api
  • How can I add a new phone calendar to Android?

I want to use the Android Calendar API for this and not the Google API so that users can work offline.

like image 417
Jason Posit Avatar asked May 03 '13 13:05

Jason Posit


People also ask

What is sync adapter in Android?

The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component.

How do I add a calendar to sync?

On your computer, open Google Calendar. Settings. On the left panel, under “Settings for my calendars,” click the name of the calendar you want to use. Click Integrate calendar.

How do I import a calendar into my Android calendar?

Add a calendar someone shared with youIn your email, tap the link that says Add this calendar. Your Google Calendar app opens. In the pop-up that appears, tap Yes. Your calendar will appear on the left, under “My calendars.”


2 Answers

It looks like Google messed up CalendarContract in this regard. The documentation is not completely clear about it:

...inserting new calendars should be done as a sync adapter.

The way this is phrased appears to me like there is the slim chance it might work. But practically speaking (judging your linked other SO questions) an app using CalendarContract cannot create synchronized calendars.

You really do not want to go through all that trouble of creating your own SyncAdapter. I did it once for one of my apps because I wanted a reliable solution for having synchronized calendar access on pre Ice Cream Sandwich devices (where CalendarContract doesn't exist, yet).

That's a lot of work. It involves interfacing with the Google Calendar web service API (while respecting their access limitations), keeping a local copy of the data and synchronizing just the differences. Ultimately your own SyncAdapter would also duplicate the data of the already existing Google Calendar SyncAdapter.

What you could do as a workaround is just implementing the necessary "create calendar" function directly on the Google Calendar online API. It requires that your app asks the user for authorization via android.permission.USE_CREDENTIALS.

AccountManager can be used for getting the correct authorization token for accessing the Google Calendar API. The method getAuthToken requires an auth token type parameter. The correct one for calendar read / write access is the following string literal: "Manage your calendars".

There are two big problems with that workaround though:

  1. You cannot know for sure if the calendar synchronization backend makes use of Google Calendar. It's unfortunately a black box.

  2. You cannot know for sure when the calendar sync adapter is going to catch up with these changes. I guess it is possible to force a re-sync somehow. But even then, your app will be stalled. You cannot just operate based on that new calendar and use it right away. Your app must wait until the synchronization happened.

You could poll CalendarContract for that. Let your app wait and look for changes in a background thread until the new calendar shows up. Then you have its local id and move on.

like image 53
tiguchi Avatar answered Nov 16 '22 01:11

tiguchi


It's not possible to create syncable calendar with Google (Gmail) account programmatically using Clanedar API for now. SyncAdapter can be used to create new non-Gmail account and create new calendars there but these aren't syncable with Google Calendar.

More details here: https://stackoverflow.com/a/19734278/237232

like image 25
Dragan Marjanović Avatar answered Nov 16 '22 00:11

Dragan Marjanović