snaggs
snaggs

Reputation: 5713

How to delete Attendee from Calendar event, Android?

I want to update calendar event.

I know how to update title, location, add new attendee by using ContentResolver

But I have no idea how to remove some attendee, for example by email.

This is what I have so far, I parse JSONObject and fetch all new info:

public void updateEvent(Context context, long eventId, JSONObject updateObj) {
        ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        Uri updateUri = null;

        long startDate = updateObj.optLong("startDate");
        long endDate = updateObj.optLong("endDate");
        String title = updateObj.optString("title");
        String description = updateObj.optString("description");
        String location = updateObj.optString("location");
        int eventStatus = updateObj.optInt("eventStatus");
        JSONArray addAtt = updateObj.optJSONArray("add_attendee");
        JSONArray deleteAtt = updateObj.optJSONArray("delete_attendee");

        values.put(Events.EVENT_LOCATION, location);
        values.put(Events.DESCRIPTION, title);
        values.put(Events.TITLE, title);
        values.put(Events.DTSTART, startDate);
        values.put(Events.DTEND, endDate);
        values.put(Events.STATUS, eventStatus);

        updateUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
        int rows = cr.update(updateUri, values, null, null);
        Log.i(TAG, "Rows updated: " + rows);  

        // add new attendees
        for(int i=0; i<addAtt.length(); i++){           

            JSONObject attObj = addAtt.optJSONObject(i);

            String name = attObj.optString("name");
            String email = attObj.optString("email");           
            int relationship = attObj.optInt("relationship");
            int type = attObj.optInt("type");
            int status = attObj.optInt("status");

            values = new ContentValues();
            values.put(Attendees.ATTENDEE_NAME, name);
            values.put(Attendees.ATTENDEE_EMAIL, email);
            values.put(Attendees.ATTENDEE_RELATIONSHIP, relationship); // had 0
            values.put(Attendees.ATTENDEE_TYPE, type);// had 0
            values.put(Attendees.ATTENDEE_STATUS, status); // had 3 - invited
            values.put(Attendees.EVENT_ID, eventId);
            updateUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, eventId);
            cr.update(updateUri, values, null, null);
        }

        // remove attendees
        for(int i=0; i<removeAtt.length(); i++){            

            JSONObject attObj = addAtt.optJSONObject(i);

                    // HERE  iS A PROBLEM

//          Uri deleteUri = ContentUris.withAppendedId(Attendees.CONTENT_URI, eventId);
//          int rows = cr.delete(deleteUri, null, null);
        }
    }

[EDIT]

I tried also:

String selection = Attendees.ATTENDEE_EMAIL + " = ?";
String[] selectionArgs = new String[] {"[email protected]"};

Uri deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
rows = cr.delete(deleteUri, selection, selectionArgs);

get error:

java.lang.IllegalArgumentException: Selection not permitted for content://com.android.calendar/events/524

Please help,

Upvotes: 4

Views: 1033

Answers (2)

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10650

To delete specific attendee by email and event ID use:

    String selection = "(" + Attendees.EVENT_ID + " = ?) AND (" + Attendees.ATTENDEE_EMAIL + " = ?)";
    String[] selectionArgs = new String[] {eventId+"","[email protected]"};

    rows = cr.delete(Attendees.CONTENT_URI, selection, selectionArgs);
    Log.i(TAG, "Rows updated: " + rows);  

Upvotes: 2

ataulm
ataulm

Reputation: 15334

You could delete the calendar event and add a new one with insert (without the attendee you wanted deleted).


Could you not use the delete method, with the Attendees.CONTENT_URI to delete the attendee from the event you want?

http://developer.android.com/guide/topics/providers/calendar-provider.html#attendees

String emailAddress = ...;
cr.delete(Attendees.URI, Attendees.ATTENDEE_EMAIL + "=" + emailAddress, null);

Upvotes: -1

Related Questions