Search code examples
androidandroid-calendar

Android Calendar Provider: How can I delete my own local calendars?


I am just learning how to work with the Android Calendars. So far, I am able to display the info about existing calendars. I can also create my own local calendars -- the test code like:

private void createCalendarTest()
{
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    builder.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
           .appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
           .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);

    Uri uri = builder.build();

    ContentValues values = new ContentValues();
    values.put(Calendars.NAME, "TEST");
    values.put(Calendars.CALENDAR_DISPLAY_NAME, "Calendar named TEST");
    values.put(Calendars.SYNC_EVENTS, false);
    values.put(Calendars.VISIBLE, true);

    getContentResolver().insert(uri, values);
}

Actually, I can create many calendars that differ only in _ID. I have read elsewhere that I can create a calendar only when using the sync adapter. Now, how can I delete the calendar? I expect the URI must also contain the sync adapter info, and the _ID of the deleted calendar. I tried the following code, but I was unsuccessful:

private void deleteCalendarTest()
{
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    builder.appendPath("6")   // here for testing; I know the calender has this ID
           .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
           .appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
           .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);

    Uri uri = builder.build();

    getContentResolver().delete(uri, null, null);
    Toast.makeText(this, "??? deleteCalendarTest() not working", Toast.LENGTH_SHORT).show();
}

How can I fix it?


Solution

  • After reading with more attention the documentation, i found out you should add to the content values the following fields too:

    values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
    values.put(CalendarContract.Calendars.ACCOUNT_NAME, "private");
    values.put(CalendarContract.Calendars.ACCOUNT_TYPE,CalendarContract.ACCOUNT_TYPE_LOCAL);
    

    Then everything else should be fine and you should be able to delete the inserted calendar! ;)