66
77package at.bitfire.synctools.storage.calendar
88
9+ import android.content.ContentUris
910import android.content.ContentValues
11+ import android.os.RemoteException
1012import android.provider.CalendarContract
1113import android.provider.CalendarContract.Calendars
1214import at.bitfire.ical4android.AndroidEvent
1315import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
16+ import at.bitfire.synctools.storage.LocalStorageException
1417import toContentValues
1518import 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}
0 commit comments