@@ -39,7 +39,7 @@ import java.util.LinkedList
3939 */
4040class AndroidCalendar (
4141 internal val provider : AndroidCalendarProvider ,
42- val values : ContentValues
42+ private val values : ContentValues
4343) {
4444
4545 /* * see [Calendars._ID] */
@@ -359,41 +359,36 @@ class AndroidCalendar(
359359 // query event to get first and last instance
360360 var first: Long? = null
361361 var last: Long? = null
362- client.query(
363- eventUri(eventId),
364- arrayOf(Events .DTSTART , Events .LAST_DATE ), null , null , null
365- )?.use { cursor ->
366- cursor.moveToNext()
367- if (! cursor.isNull(0 ))
368- first = cursor.getLong(0 )
369- if (! cursor.isNull(1 ))
370- last = cursor.getLong(1 )
362+ getEventRow(id, arrayOf(Events .DTSTART , Events .LAST_DATE ))?.let { values ->
363+ first = values.getAsLong(Events .DTSTART )
364+ last = values.getAsLong(Events .LAST_DATE )
371365 }
372366 // if this event doesn't have a last occurrence, it's endless and always has instances
373367 if (first == null || last == null )
374368 return null
375369
376370 /* We can't use Long.MIN_VALUE and Long.MAX_VALUE because Android generates the instances
377- on the fly and it doesn't accept those values. So we use the first/last actual occurence
378- of the event (calculated by Android). */
371+ on the fly and it doesn't accept those values. So we use the first/last actual occurrence
372+ of the event (as calculated by Android). */
379373 val instancesUri = CalendarContract .Instances .CONTENT_URI .asSyncAdapter(account)
380374 .buildUpon()
381375 .appendPath(first.toString()) // begin timestamp
382376 .appendPath(last.toString()) // end timestamp
383377 .build()
384378
379+ var numInstances: Int? = null
385380 try {
386381 client.query(
387382 instancesUri, null ,
388383 " ${CalendarContract .Instances .EVENT_ID } =?" , arrayOf(eventId.toString()),
389384 null
390385 )?.use { cursor ->
391- return cursor.count
386+ numInstances = cursor.count
392387 }
393388 } catch (e: RemoteException ) {
394389 throw LocalStorageException (" Couldn't query number of instances for event $eventId " , e)
395390 }
396- return null
391+ return numInstances
397392 }
398393
399394 /* *
@@ -406,30 +401,22 @@ class AndroidCalendar(
406401 */
407402 fun numInstances (eventId : Long ): Int? {
408403 // num instances of the main event
409- var numInstances = numDirectInstances(eventId) ? : return null
404+ val numDirectInstances = numDirectInstances(eventId) ? : return null
410405
411406 // add the number of instances of every main event's exception
412- try {
413- client.query(
414- Events .CONTENT_URI ,
415- arrayOf(Events ._ID ),
416- " ${Events .ORIGINAL_ID } =?" , // get exception events of the main event
417- arrayOf(eventId.toString()), null
418- )?.use { exceptionsEventCursor ->
419- while (exceptionsEventCursor.moveToNext()) {
420- val exceptionEventId = exceptionsEventCursor.getLong(0 )
421- val exceptionInstances = numDirectInstances(exceptionEventId)
422-
423- if (exceptionInstances == null )
424- return null // number of instances of exception can't be determined; so the total number of instances is also unclear
425-
426- numInstances + = exceptionInstances
427- }
428- }
429- } catch (e: RemoteException ) {
430- throw LocalStorageException (" Couldn't query number of exception instances for event $eventId " , e)
407+ var numExInstances = 0
408+ iterateEventRows(
409+ arrayOf(Events ._ID ),
410+ " ${Events .ORIGINAL_ID } =?" , // get exception events of the main event
411+ arrayOf(eventId.toString())
412+ ) { values ->
413+ val exceptionEventId = values.getAsLong(Events ._ID )
414+ val exceptionInstances = numDirectInstances(exceptionEventId)
415+
416+ if (exceptionInstances != null )
417+ numExInstances + = exceptionInstances
431418 }
432- return numInstances
419+ return numDirectInstances - numExInstances
433420 }
434421
435422
0 commit comments