if caldav event endTS is missing, use start + duration

This commit is contained in:
tibbi 2017-08-14 22:08:17 +02:00
parent e71c8b699e
commit 297db04f46
3 changed files with 26 additions and 22 deletions

View File

@ -289,12 +289,16 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
val title = cursor.getStringValue(CalendarContract.Events.TITLE) val title = cursor.getStringValue(CalendarContract.Events.TITLE)
val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION) val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION)
val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt() val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt()
val endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt() var endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt()
val duration = cursor.getStringValue(CalendarContract.Events.DURATION)
val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY) val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY)
val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: "" val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: ""
val reminders = getCalDAVEventReminders(id) val reminders = getCalDAVEventReminders(id)
if (endTS == 0) {
val duration = cursor.getStringValue(CalendarContract.Events.DURATION)
endTS = startTS + Parser().parseDuration(duration)
}
val importId = getCalDAVEventImportId(calendarID, id) val importId = getCalDAVEventImportId(calendarID, id)
val repeatRule = Parser().parseRepeatInterval(rrule, startTS) val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }), val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }),

View File

@ -63,7 +63,7 @@ class IcsImporter {
curEnd = getTimestamp(line.substring(DTEND.length)) curEnd = getTimestamp(line.substring(DTEND.length))
} else if (line.startsWith(DURATION)) { } else if (line.startsWith(DURATION)) {
val duration = line.substring(DURATION.length) val duration = line.substring(DURATION.length)
curEnd = curStart + decodeTime(duration) curEnd = curStart + Parser().parseDuration(duration)
} else if (line.startsWith(SUMMARY) && !isNotificationDescription) { } else if (line.startsWith(SUMMARY) && !isNotificationDescription) {
curTitle = line.substring(SUMMARY.length) curTitle = line.substring(SUMMARY.length)
curTitle = getTitle(curTitle).replace("\\n", "\n") curTitle = getTitle(curTitle).replace("\\n", "\n")
@ -81,7 +81,7 @@ class IcsImporter {
lastReminderAction = line.substring(ACTION.length) lastReminderAction = line.substring(ACTION.length)
} else if (line.startsWith(TRIGGER)) { } else if (line.startsWith(TRIGGER)) {
if (lastReminderAction == DISPLAY) if (lastReminderAction == DISPLAY)
curReminderMinutes.add(decodeTime(line.substring(TRIGGER.length)) / 60) curReminderMinutes.add(Parser().parseDuration(line.substring(TRIGGER.length)) / 60)
} else if (line.startsWith(CATEGORIES)) { } else if (line.startsWith(CATEGORIES)) {
val categories = line.substring(CATEGORIES.length) val categories = line.substring(CATEGORIES.length)
tryAddCategories(categories, context) tryAddCategories(categories, context)
@ -170,24 +170,6 @@ class IcsImporter {
} }
} }
// P0DT1H0M0S
private fun decodeTime(duration: String): Int {
val weeks = getDurationValue(duration, "W")
val days = getDurationValue(duration, "DT")
val hours = getDurationValue(duration, "H")
val minutes = getDurationValue(duration, "M")
val seconds = getDurationValue(duration, "S")
val minSecs = 60
val hourSecs = minSecs * 60
val daySecs = hourSecs * 24
val weekSecs = daySecs * 7
return seconds + (minutes * minSecs) + (hours * hourSecs) + (days * daySecs) + (weeks * weekSecs)
}
private fun getDurationValue(duration: String, char: String): Int = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0
private fun resetValues() { private fun resetValues() {
curStart = -1 curStart = -1
curEnd = -1 curEnd = -1

View File

@ -190,4 +190,22 @@ class Parser {
else -> SU else -> SU
} }
} }
// from P0DT1H5M0S to 3900 (seconds)
fun parseDuration(duration: String): Int {
val weeks = getDurationValue(duration, "W")
val days = getDurationValue(duration, "DT")
val hours = getDurationValue(duration, "H")
val minutes = getDurationValue(duration, "M")
val seconds = getDurationValue(duration, "S")
val minSecs = 60
val hourSecs = minSecs * 60
val daySecs = hourSecs * 24
val weekSecs = daySecs * 7
return seconds + (minutes * minSecs) + (hours * hourSecs) + (days * daySecs) + (weeks * weekSecs)
}
private fun getDurationValue(duration: String, char: String) = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0
} }