Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9459f80d authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Apply ical4j rules to change Outlook timezone IDs to more Android-friendly values

parent ea01dcd5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line

buildscript {
    ext.versions = [
        kotlin: '1.3.50',
        kotlin: '1.3.60',
        dokka: '0.10.0',
        ical4j: '2.2.6'
    ]
+7 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import java.io.IOException
import java.io.OutputStream
import java.io.Reader
import java.util.*
import java.util.logging.Level

class Event: ICalendar() {

@@ -82,6 +83,12 @@ class Event: ICalendar() {
                throw InvalidCalendarException("iCalendar object contains invalid value", e)
            }

            try {
                ICalPreprocessor.preProcess(ical)
            } catch (e: Exception) {
                Constants.log.log(Level.WARNING, "Couldn't pre-process iCalendar", e)
            }

            // fill calendar properties
            properties?.let {
                ical.getProperty(CALENDAR_NAME)?.let { calName ->
+47 −0
Original line number Diff line number Diff line
package at.bitfire.ical4android

import net.fortuna.ical4j.model.Calendar
import net.fortuna.ical4j.model.Property
import net.fortuna.ical4j.transform.rfc5545.DateListPropertyRule
import net.fortuna.ical4j.transform.rfc5545.DatePropertyRule
import net.fortuna.ical4j.transform.rfc5545.Rfc5545PropertyRule
import java.util.logging.Level

/**
 * Applies some rules to increase compatibility or parsed iCalendars:
 *
 * - [DatePropertyRule], [DateListPropertyRule]: to rename Outlook-specific TZID parameters
 * (like "W. Europe Standard Time" to an Android-friendly name like "Europe/Vienna")
 *
 */
object ICalPreprocessor {

    private val propertyRules = arrayOf(
            DatePropertyRule(),
            DateListPropertyRule()
    )

    /**
     * Applies the set of rules (see class definition) to a given calendar object.
     *
     * @param calendar the calendar object that is going to be modified
     */
    fun preProcess(calendar: Calendar) {
        for (component in calendar.components) {
            for (property in component.properties)
                applyRules(property)
        }
    }

    @Suppress("UNCHECKED_CAST")
    private fun applyRules(property: Property) {
        propertyRules
                .filter { rule -> rule.supportedType.isAssignableFrom(property::class.java) }
                .forEach {
                    Constants.log.log(Level.INFO, "Applying rules to ${property.toString()}")
                    (it as Rfc5545PropertyRule<Property>).applyTo(property)
                    Constants.log.log(Level.INFO, "-> ${property.toString()}")
                }
    }

}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ object MiscUtils {
            val tzID = tz.id ?: return
            val deviceTzID = DateUtils.findAndroidTimezoneID(tzID)
            if (tzID != deviceTzID) {
                Constants.log.warning("Android doesn't know time zone \"$tzID\", storing event in time zone \"$deviceTzID\"")
                Constants.log.warning("Android doesn't know time zone \"$tzID\", assuming device time zone \"$deviceTzID\"")
                date.timeZone = DateUtils.tzRegistry.getTimeZone(deviceTzID)
            }
        }
+2 −2
Original line number Diff line number Diff line
@@ -102,9 +102,9 @@ class EventTest {
        // event with start+end date-time
        val eViennaEvolution = parseCalendar("vienna-evolution.ics").first()
        assertEquals(1381330800000L, eViennaEvolution.dtStart!!.date.time)
        assertEquals("/freeassociation.sourceforge.net/Tzfile/Europe/Vienna", eViennaEvolution.dtStart!!.timeZone.id)
        assertEquals("Europe/Vienna", eViennaEvolution.dtStart!!.timeZone.id)
        assertEquals(1381334400000L, eViennaEvolution.dtEnd!!.date.time)
        assertEquals("/freeassociation.sourceforge.net/Tzfile/Europe/Vienna", eViennaEvolution.dtEnd!!.timeZone.id)
        assertEquals("Europe/Vienna", eViennaEvolution.dtEnd!!.timeZone.id)
    }

    @Test
Loading