add a helper function for fetching caldav calendar events

This commit is contained in:
tibbi 2017-08-13 23:14:25 +02:00
parent cebc748a7f
commit d794d59166
3 changed files with 38 additions and 4 deletions

View File

@ -146,6 +146,10 @@ class SettingsActivity : SimpleActivity() {
dbHelper.insertEventType(eventType)
}
}
calendars.forEach {
fetchCalDAVCalendarEvents(it.id)
}
}
}).start()
}

View File

@ -249,7 +249,8 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, null, null)
while (cursor.moveToNext()) {
cursor.moveToFirst()
do {
val id = cursor.getLongValue(CalendarContract.Calendars._ID)
val displayName = cursor.getStringValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
val accountName = cursor.getStringValue(CalendarContract.Calendars.ACCOUNT_NAME)
@ -257,13 +258,44 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
val color = cursor.getIntValue(CalendarContract.Calendars.CALENDAR_COLOR)
val calendar = CalDAVCalendar(id, displayName, accountName, ownerName, color)
calendars.add(calendar)
}
} while (cursor.moveToNext())
} finally {
cursor?.close()
}
return calendars
}
fun Context.fetchCalDAVCalendarEvents(calendarID: Long) {
val eventsUri = CalendarContract.Events.CONTENT_URI
val projection = arrayOf(
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.DURATION,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.RRULE)
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarID"
var cursor: Cursor? = null
try {
cursor = contentResolver.query(eventsUri, projection, selection, null, null)
cursor.moveToFirst()
do {
val title = cursor.getStringValue(CalendarContract.Events.TITLE)
val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION)
val startTS = cursor.getLongValue(CalendarContract.Events.DTSTART)
val endTS = cursor.getLongValue(CalendarContract.Events.DTEND)
val duration = cursor.getStringValue(CalendarContract.Events.DURATION)
val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY)
val rrule = cursor.getStringValue(CalendarContract.Events.RRULE)
} while (cursor.moveToNext())
} finally {
cursor?.close()
}
}
fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDateTimeFromCode(dayCode).withTime(13, 0, 0, 0).seconds()
fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date())

View File

@ -326,8 +326,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun deleteEvents(ids: Array<String>) {
val args = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)"
val cursor = getEventsCursor(selection)
val events = fillEvents(cursor).filter { it.importId.isNotEmpty() }
mDb.delete(MAIN_TABLE_NAME, selection, null)