Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dschuermann authored and jspricke committed Jul 15, 2022
1 parent b34c97f commit 4e0d8b2
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 154 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ dependencies {
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.gitlab.bitfireAT:ical4android:640fc41119'
implementation 'com.gitlab.bitfireAT:ical4android:1.0'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.13.2'

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/*
* Copyright (c) Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
* Copyright (C) 2020 Dominik Schürmann <[email protected]>
* Copyright (c) Ricki Hirner (bitfire web engineering)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.android.calendar.ical4android
Expand All @@ -20,13 +30,13 @@ import at.bitfire.ical4android.AndroidCalendarFactory
import at.bitfire.ical4android.CalendarStorageException

/**
* Based on ICSx5
* Based on LocalCalendar in ICSx5
*/
class LocalCalendar private constructor(
class AndroidStorageCalendar private constructor(
account: Account,
provider: ContentProviderClient,
id: Long
): AndroidCalendar<LocalEvent>(account, provider, LocalEvent.Factory, id) {
): AndroidCalendar<AndroidStorageEvent>(account, provider, AndroidStorageEvent.Factory, id) {

companion object {

Expand Down Expand Up @@ -134,10 +144,10 @@ class LocalCalendar private constructor(
}


object Factory: AndroidCalendarFactory<LocalCalendar> {
object Factory: AndroidCalendarFactory<AndroidStorageCalendar> {

override fun newInstance(account: Account, provider: ContentProviderClient, id: Long) =
LocalCalendar(account, provider, id)
AndroidStorageCalendar(account, provider, id)

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2020 Dominik Schürmann <[email protected]>
* Copyright (c) Ricki Hirner (bitfire web engineering)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.android.calendar.ical4android

import android.content.ContentProviderOperation.Builder
import android.content.ContentValues
import android.provider.CalendarContract
import at.bitfire.ical4android.AndroidCalendar
import at.bitfire.ical4android.AndroidEvent
import at.bitfire.ical4android.AndroidEventFactory
import at.bitfire.ical4android.Event

/**
* Based on LocalEvent in ICSx5
*/
class AndroidStorageEvent: AndroidEvent {

// companion object {
// const val COLUMN_LAST_MODIFIED = CalendarContract.Events.SYNC_DATA2
// }

var uid: String? = null
// var lastModified = 0L

private constructor(calendar: AndroidCalendar<AndroidEvent>, values: ContentValues): super(calendar, values) {
uid = values.getAsString(CalendarContract.Events._SYNC_ID)
// lastModified = values.getAsLong(COLUMN_LAST_MODIFIED) ?: 0
}

constructor(calendar: AndroidCalendar<AndroidEvent>, event: Event): super(calendar, event) {
uid = event.uid
// lastModified = event.lastModified?.dateTime?.time ?: 0
}

override fun populateEvent(row: ContentValues) {
super.populateEvent(row)

val event = requireNotNull(event)
event.uid = row.getAsString(CalendarContract.Events._SYNC_ID)

// row.getAsLong(COLUMN_LAST_MODIFIED).let {
// lastModified = it
// event.lastModified = LastModified(DateTime(it))
// }
}

override fun buildEvent(recurrence: Event?, builder: Builder) {
super.buildEvent(recurrence, builder)

if (recurrence == null) {
// master event
builder .withValue(CalendarContract.Events._SYNC_ID, uid)
// .withValue(COLUMN_LAST_MODIFIED, lastModified)
} else {
// exception
builder.withValue(CalendarContract.Events.ORIGINAL_SYNC_ID, uid)
}
}


object Factory: AndroidEventFactory<AndroidStorageEvent> {

override fun fromProvider(calendar: AndroidCalendar<AndroidEvent>, values: ContentValues) =
AndroidStorageEvent(calendar, values)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2020 Dominik Schürmann <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.android.calendar.ical4android

class IcalExporter
100 changes: 100 additions & 0 deletions app/src/main/java/com/android/calendar/ical4android/IcalImporter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2020 Dominik Schürmann <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.android.calendar.ical4android

import android.accounts.Account
import android.content.ContentProviderClient
import android.content.Context
import android.provider.CalendarContract
import at.bitfire.ical4android.Event
import java.io.InputStream
import java.io.InputStreamReader
import java.util.HashSet

/**
* TODO:
* - don't use _SYNC_ID, https://developer.android.com/reference/android/provider/CalendarContract.SyncColumns
* - use UID_2445? https://github.com/SufficientlySecure/calendar-import-export/search?q=generateUid&unscoped_q=generateUid
* - still detect duplicates and replace them, but without modified time
*
*
* - exporter: work on EventInfoFragment.shareEvent
*/
class IcalImporter(val context: Context) {

fun import(inputStream: InputStream, account: Account, calendarId: Long) {
InputStreamReader(inputStream, Charsets.UTF_8).use { reader ->
try {
val events = Event.eventsFromReader(reader)
processEvents(events, account, calendarId)

// Log.i(Constants.TAG, "Calendar sync successful, ETag=$eTag, lastModified=$lastModified")
// calendar.updateStatusSuccess(eTag, lastModified ?: 0L)
} catch (e: Exception) {
// Log.e(Constants.TAG, "Couldn't process events", e)
// errorMessage = e.localizedMessage
}
}
}

private fun processEvents(events: List<Event>, account: Account, calendarId: Long) {
// Log.i(Constants.TAG, "Processing ${events.size} events")
val uids = HashSet<String>(events.size)

for (event in events) {
val uid = event.uid!!
// Log.d(Constants.TAG, "Found VEVENT: $uid")
uids += uid

// val localEvents = calendar.queryByUID(uid)
// if (localEvents.isEmpty()) {
// Log.d(Constants.TAG, "$uid not in local calendar, adding")

val client: ContentProviderClient? = context.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)
val calendar = AndroidStorageCalendar.findById(account, client!!, calendarId)
AndroidStorageEvent(calendar, event).add()

// } else {
// val localEvent = localEvents.first()
// var lastModified = event.lastModified
//
// if (lastModified != null) {
// // process LAST-MODIFIED of exceptions
// for (exception in event.exceptions) {
// val exLastModified = exception.lastModified
// if (exLastModified == null) {
// lastModified = null
// break
// } else if (lastModified != null && exLastModified.dateTime.after(lastModified.date))
// lastModified = exLastModified
// }
// }
//
// if (lastModified == null || lastModified.dateTime.time > localEvent.lastModified)
// // either there is no LAST-MODIFIED, or LAST-MODIFIED has been increased
// localEvent.update(event)
// else
// Log.d(Constants.TAG, "$uid has not been modified since last sync")
// }
}

// Log.i(Constants.TAG, "Deleting old events (retaining ${uids.size} events by UID) …")
// val deleted = calendar.retainByUID(uids)
// Log.i(Constants.TAG, "… $deleted events deleted")
}
}
Loading

0 comments on commit 4e0d8b2

Please sign in to comment.