Created
September 19, 2016 16:58
-
-
Save DLKanth/18a4cda1befc4eb5970dbdad927a04bb to your computer and use it in GitHub Desktop.
Inserting recurring event and an event exception for a single instance from recurrence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Snippet for inserting a repeating event to meet jerry for 4 consecutive days at tower | |
* Snipper for adding an exception on repeating event (jerry will be at station on 3rd day, so adding an | |
* exception event on 3 day to meet jerry at station) | |
*/ | |
import android.accounts.Account; | |
import android.content.ContentResolver; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.provider.CalendarContract; | |
import android.util.Log; | |
import java.text.SimpleDateFormat; | |
import java.util.Locale; | |
public class CalendarEvents { | |
// hardcoded account type for syncAdapter's account authenticator | |
private static final String ACCOUNT_TYPE = "me.dlkanth.cal.test"; | |
// hardcoded Calendar Id | |
private static final long CAL_ID = 10; | |
private Context context; | |
private Account account; | |
public CalendarEvents (Context context, Account account) { | |
this.context = context; | |
this.account = account; | |
} | |
/* | |
* Inserting an event "Meet jerry at the tower" 10am - 11am, from 19th sept, everyday, until 22nd sept | |
*/ | |
private String insertRepeatEvent () throws Exception { | |
ContentResolver cr = this.context.getContentResolver(); | |
ContentValues values = new ContentValues(); | |
values.put(CalendarContract.Events.TITLE, "Meet jerry at the tower"); | |
values.put(CalendarContract.Events.ALL_DAY, false); | |
values.put(CalendarContract.Events.CALENDAR_ID, CAL_ID); | |
// dtstart and dtend for the event | |
String eventDate = "20150919"; | |
String startTime = "100000000"; | |
String endTime = "110000000"; | |
String eventStartTime = eventDate + startTime; | |
String eventEndTime = eventDate + endTime; | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.US); | |
values.put(CalendarContract.Events.DTSTART, dateFormat.parse(eventStartTime).getTime()); | |
values.put(CalendarContract.Events.DTEND, dateFormat.parse(eventEndTime).getTime()); | |
// rrule for repeating events for consecutive 4 days | |
values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;COUNT=4"); | |
String syncId = System.currentTimeMillis() + ""; | |
values.put(CalendarContract.Events._SYNC_ID, syncId); | |
try { | |
Uri uri = cr.insert(eventUriAsSyncAdapter(), values); | |
int eventId = Integer.parseInt(uri.getLastPathSegment()); | |
Log.i("CALENDAR", "Recurring Event Added"); | |
return syncId; | |
} catch (Exception e) { | |
Log.e("CALENDAR", "error while inserting", e); | |
} | |
return null; | |
} | |
/* | |
* Jerry will be at station on 21st sept, so change (exception) in event needed | |
* | |
* Exception for the original event will be considered with sync_id of the original event. | |
* Original Instance time of the event will also be considered for adding exception | |
* | |
* Original instance time of this exception event will be the 10am on 21st sept. | |
*/ | |
private void insertExceptionForRecurringEvent (String syncId) throws Exception { | |
ContentResolver cr = this.context.getContentResolver(); | |
ContentValues values = new ContentValues(); | |
values.put(CalendarContract.Events.TITLE, "Meet Jerry at the station"); | |
values.put(CalendarContract.Events.ALL_DAY, false); | |
values.put(CalendarContract.Events.CALENDAR_ID, CAL_ID); | |
// dtstart and dtend for the event | |
String eventDate = "20150919"; | |
String startTime = "100000000"; | |
String endTime = "110000000"; | |
String eventStartTime = eventDate + startTime; | |
String eventEndTime = eventDate + endTime; | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.US); | |
values.put(CalendarContract.Events.DTSTART, dateFormat.parse(eventStartTime).getTime()); | |
values.put(CalendarContract.Events.DTEND, dateFormat.parse(eventEndTime).getTime()); | |
// need to add an exception for recurring event on 21st sept 10 am event | |
// calculating original instance time | |
String date = "20150921"; | |
String time = "100000000"; | |
String originalInstanceTime = date + time; | |
// the following values are important for inserting an exception event | |
values.put(CalendarContract.Events.ORIGINAL_SYNC_ID, syncId); | |
// original instance time of the recurring event on 21st sept | |
values.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, dateFormat.parse(originalInstanceTime).getTime()); | |
try { | |
Uri uri = cr.insert(eventUriAsSyncAdapter(), values); | |
int eventId = Integer.parseInt(uri.getLastPathSegment()); | |
Log.i("CALENDAR", "Exception Event Added"); | |
} catch (Exception e) { | |
Log.e("CALENDAR", "error while inserting Exception eVent", e); | |
} | |
} | |
private Uri eventUriAsSyncAdapter () { | |
Uri uri = Uri.parse(CalendarContract.Events.CONTENT_URI.toString()); | |
uri = uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") | |
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, this.account.name) | |
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, ACCOUNT_TYPE).build(); | |
return uri; | |
} | |
} |
You also used 'Events.CONTENT_URI' for the exception date.
Why did you not use 'Events.CONTENT_EXCEPTION_URI' for the exception date instead?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excellent, it helped me a lot