From 4be4eab18df3387b79a637da3199279a19c53c89 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 25 Jan 2017 20:58:52 +0100 Subject: [PATCH] add a basic .ics file parser implementation --- .../calendar/helpers/IcsParser.kt | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt new file mode 100644 index 000000000..085eba779 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt @@ -0,0 +1,56 @@ +package com.simplemobiletools.calendar.helpers + +import android.content.Context +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.models.Event + +object IcsParser { + private val BEGIN_EVENT = "BEGIN:VEVENT" + private val BEGIN_ALARM = "BEGIN:VALARM" + private val END = "END:VEVENT" + private val DTSTART = "DTSTART:" + private val DTEND = "DTEND:" + private val SUMMARY = "SUMMARY:" + private val DESCRIPTION = "DESCRIPTION:" + + var curStart = -1 + var curEnd = -1 + var curTitle = "" + var curDescription = "" + + fun parseIcs(context: Context) { + val inputStream = context.resources.openRawResource(R.raw.sample) + + inputStream.bufferedReader().use { + while (true) { + val line = it.readLine()?.trim() ?: break + if (line == BEGIN_EVENT) { + resetValues() + } else if (line.startsWith(DTSTART)) { + val datetime = line.substring(DTSTART.length) + curStart = 0 + } else if (line.startsWith(DTEND)) { + val datetime = line.substring(DTEND.length) + curEnd = 0 + } else if (line.startsWith(SUMMARY)) { + curTitle = line.substring(SUMMARY.length) + } else if (line.startsWith(DESCRIPTION)) { + curDescription = line.substring(DESCRIPTION.length) + } else if (line == END || line == BEGIN_ALARM) { + if (curTitle.isEmpty() || curStart == -1 || curEnd == -1) + continue + + val event = Event(0, 0, 0, curTitle, curDescription) + resetValues() + } + } + } + } + + fun resetValues() { + curStart = -1 + curEnd = -1 + curTitle = "" + curDescription = "" + } +}