Skip to content

Commit 91b2f38

Browse files
committed
AndroidCalendar: add methods
1 parent b9bd7f7 commit 91b2f38

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

lib/src/main/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendar.kt

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
package at.bitfire.synctools.storage.calendar
88

9+
import android.content.ContentUris
910
import android.content.ContentValues
11+
import android.os.RemoteException
1012
import android.provider.CalendarContract
1113
import android.provider.CalendarContract.Calendars
1214
import at.bitfire.ical4android.AndroidEvent
1315
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
16+
import at.bitfire.synctools.storage.LocalStorageException
1417
import toContentValues
1518
import java.util.LinkedList
1619

@@ -59,17 +62,63 @@ class AndroidCalendar(
5962
* @return events from this calendar which match the selection
6063
*/
6164
fun findEvents(where: String?, whereArgs: Array<String>?): List<AndroidEvent> {
62-
val whereWithId = "(${where ?: "1"}) AND " + CalendarContract.Events.CALENDAR_ID + "=?"
63-
val whereArgsWithId = (whereArgs ?: arrayOf()) + id.toString()
64-
6565
val events = LinkedList<AndroidEvent>()
66-
client.query(eventsUri, null, whereWithId, whereArgsWithId, null)?.use { cursor ->
67-
while (cursor.moveToNext())
68-
events += AndroidEvent(this, cursor.toContentValues())
66+
try {
67+
val (protectedWhere, protectedWhereArgs) = whereWithCalendarId(where, whereArgs)
68+
client.query(eventsUri, null, protectedWhere, protectedWhereArgs, null)?.use { cursor ->
69+
while (cursor.moveToNext())
70+
events += AndroidEvent(this, cursor.toContentValues())
71+
}
72+
} catch (e: RemoteException) {
73+
throw LocalStorageException("Couldn't query events", e)
6974
}
7075
return events
7176
}
7277

78+
fun getEventValues(id: Long, projection: Array<String>? = null, where: String? = null, whereArgs: Array<String>? = null): ContentValues? {
79+
try {
80+
client.query(eventUri(id), projection, where, whereArgs, null)?.use { cursor ->
81+
if (cursor.moveToNext())
82+
return cursor.toContentValues()
83+
}
84+
} catch (e: RemoteException) {
85+
throw LocalStorageException("Couldn't query event", e)
86+
}
87+
return null
88+
}
89+
90+
/**
91+
* Iterates events from this calendar.
92+
*
93+
* Adds a WHERE clause that restricts the query to [CalendarContract.EventsColumns.CALENDAR_ID] = [id].
94+
*
95+
* @param projection requested fields
96+
* @param where selection
97+
* @param whereArgs arguments for selection
98+
*
99+
* @return event IDs from this calendar which match the selection
100+
*/
101+
fun iterateEvents(projection: Array<String>, where: String?, whereArgs: Array<String>?, body: (ContentValues) -> Unit) {
102+
try {
103+
val (protectedWhere, protectedWhereArgs) = whereWithCalendarId(where, whereArgs)
104+
client.query(eventsUri, projection, protectedWhere, protectedWhereArgs, null)?.use { cursor ->
105+
while (cursor.moveToNext()) {
106+
body(cursor.toContentValues())
107+
}
108+
}
109+
} catch (e: RemoteException) {
110+
throw LocalStorageException("Couldn't iterate events", e)
111+
}
112+
}
113+
114+
fun updateEvents(values: ContentValues, where: String?, whereArgs: Array<String>?) {
115+
try {
116+
client.update(eventsUri, values, where, whereArgs)
117+
} catch (e: RemoteException) {
118+
throw LocalStorageException("Couldn't update events", e)
119+
}
120+
}
121+
73122

74123
// shortcuts to upper level
75124

@@ -87,4 +136,13 @@ class AndroidCalendar(
87136
val eventsUri
88137
get() = CalendarContract.Events.CONTENT_URI.asSyncAdapter(account)
89138

139+
fun eventUri(id: Long) =
140+
ContentUris.withAppendedId(eventsUri, id)
141+
142+
private fun whereWithCalendarId(where: String?, whereArgs: Array<String>?): Pair<String, Array<String>> {
143+
val protectedWhere = "(${where ?: "1"}) AND " + CalendarContract.Events.CALENDAR_ID + "=?"
144+
val protectedWhereArgs = (whereArgs ?: arrayOf()) + id.toString()
145+
return Pair(protectedWhere, protectedWhereArgs)
146+
}
147+
90148
}

lib/src/main/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendarProvider.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AndroidCalendarProvider(
5959
return getCalendar(id) ?: throw LocalStorageException("Couldn't query calendar that was just created")
6060
}
6161

62-
fun findCalendars(where: String?, whereArgs: Array<String>?, sortOrder: String? = null): List<AndroidCalendar> {
62+
fun findCalendars(where: String? = null, whereArgs: Array<String>? = null, sortOrder: String? = null): List<AndroidCalendar> {
6363
val result = LinkedList<AndroidCalendar>()
6464
try {
6565
client.query(calendarsUri, null, where, whereArgs, sortOrder)?.use { cursor ->
@@ -196,9 +196,7 @@ class AndroidCalendarProvider(
196196
get() = Calendars.CONTENT_URI.asSyncAdapter(account)
197197

198198
private fun calendarUri(id: Long) =
199-
ContentUris
200-
.withAppendedId(Calendars.CONTENT_URI, id)
201-
.asSyncAdapter(account)
199+
ContentUris.withAppendedId(calendarsUri, id)
202200

203201
private val colorsUri
204202
get() = Colors.CONTENT_URI.asSyncAdapter(account)

0 commit comments

Comments
 (0)