parse repeat limit from ics files

This commit is contained in:
tibbi
2017-02-19 22:08:15 +01:00
parent ca1ca279fb
commit 5f7623deeb

View File

@ -27,6 +27,7 @@ class IcsParser {
private val DISPLAY = "DISPLAY" private val DISPLAY = "DISPLAY"
private val FREQ = "FREQ" private val FREQ = "FREQ"
private val UNTIL = "UNTIL"
private val INTERVAL = "INTERVAL" private val INTERVAL = "INTERVAL"
private val DAILY = "DAILY" private val DAILY = "DAILY"
@ -42,6 +43,7 @@ class IcsParser {
var curFlags = 0 var curFlags = 0
var curReminderMinutes = -1 var curReminderMinutes = -1
var curRepeatInterval = 0 var curRepeatInterval = 0
var curRepeatLimit = 0
var curShouldHaveNotification = false var curShouldHaveNotification = false
var isNotificationDescription = false var isNotificationDescription = false
@ -82,7 +84,7 @@ class IcsParser {
} else if (line.startsWith(UID)) { } else if (line.startsWith(UID)) {
curImportId = line.substring(UID.length) curImportId = line.substring(UID.length)
} else if (line.startsWith(RRULE)) { } else if (line.startsWith(RRULE)) {
curRepeatInterval = parseRepeatSeconds(line.substring(RRULE.length)) curRepeatInterval = parseRepeatInterval(line.substring(RRULE.length))
} else if (line.startsWith(ACTION)) { } else if (line.startsWith(ACTION)) {
isNotificationDescription = true isNotificationDescription = true
if (line.substring(ACTION.length) == DISPLAY) if (line.substring(ACTION.length) == DISPLAY)
@ -96,7 +98,8 @@ class IcsParser {
continue continue
importIDs.add(curImportId) importIDs.add(curImportId)
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes, -1, -1, curRepeatInterval, curImportId, curFlags) val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes, -1, -1, curRepeatInterval,
curImportId, curFlags, curRepeatLimit)
dbHelper.insert(event) { } dbHelper.insert(event) { }
eventsImported++ eventsImported++
resetValues() resetValues()
@ -121,16 +124,10 @@ class IcsParser {
try { try {
return if (fullString.startsWith(';')) { return if (fullString.startsWith(';')) {
curFlags = curFlags or FLAG_ALL_DAY curFlags = curFlags or FLAG_ALL_DAY
val value = fullString.substring(fullString.lastIndexOf(':') + 1).replace("T", "").replace("Z", "") val value = fullString.substring(fullString.lastIndexOf(':') + 1)
if (value.length == 14) { parseDateTimeValue(value)
parseLongFormat(value)
} else {
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd")
dateTimeFormat.parseDateTime(value).withHourOfDay(1).seconds()
}
} else { } else {
val digitString = fullString.substring(1).replace("T", "").replace("Z", "") parseDateTimeValue(fullString.substring(1))
parseLongFormat(digitString)
} }
} catch (e: Exception) { } catch (e: Exception) {
eventsFailed++ eventsFailed++
@ -138,6 +135,16 @@ class IcsParser {
} }
} }
private fun parseDateTimeValue(value: String): Int {
val edited = value.replace("T", "").replace("Z", "")
return if (edited.length == 14) {
parseLongFormat(edited)
} else {
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd")
dateTimeFormat.parseDateTime(edited).withHourOfDay(1).seconds()
}
}
// P0DT1H0M0S // P0DT1H0M0S
private fun decodeTime(duration: String): Int { private fun decodeTime(duration: String): Int {
val weeks = getDurationValue(duration, "W") val weeks = getDurationValue(duration, "W")
@ -161,7 +168,7 @@ class IcsParser {
return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds() return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds()
} }
private fun parseRepeatSeconds(fullString: String): Int { private fun parseRepeatInterval(fullString: String): Int {
val parts = fullString.split(";") val parts = fullString.split(";")
var frequencySeconds = 0 var frequencySeconds = 0
for (part in parts) { for (part in parts) {
@ -170,6 +177,8 @@ class IcsParser {
frequencySeconds = getFrequencySeconds(keyValue[1]) frequencySeconds = getFrequencySeconds(keyValue[1])
} else if (keyValue[0] == INTERVAL) { } else if (keyValue[0] == INTERVAL) {
return frequencySeconds * keyValue[1].toInt() return frequencySeconds * keyValue[1].toInt()
} else if (keyValue[0] == UNTIL) {
curRepeatLimit = parseDateTimeValue(keyValue[1])
} }
} }
return frequencySeconds return frequencySeconds
@ -194,6 +203,7 @@ class IcsParser {
curFlags = 0 curFlags = 0
curReminderMinutes = -1 curReminderMinutes = -1
curRepeatInterval = 0 curRepeatInterval = 0
curRepeatLimit = 0
curShouldHaveNotification = false curShouldHaveNotification = false
isNotificationDescription = false isNotificationDescription = false
} }