add some checks if cursors arent empty

This commit is contained in:
tibbi 2017-08-14 19:24:02 +02:00
parent e65b97e647
commit ad9ad142ed
1 changed files with 37 additions and 34 deletions

View File

@ -250,7 +250,7 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = contentResolver.query(uri, projection, selection, null, null) cursor = contentResolver.query(uri, projection, selection, null, null)
cursor.moveToFirst() if (cursor != null && cursor.moveToFirst()) {
do { do {
val id = cursor.getLongValue(CalendarContract.Calendars._ID) val id = cursor.getLongValue(CalendarContract.Calendars._ID)
val displayName = cursor.getStringValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME) val displayName = cursor.getStringValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
@ -260,6 +260,7 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
val calendar = CalDAVCalendar(id, displayName, accountName, ownerName, color) val calendar = CalDAVCalendar(id, displayName, accountName, ownerName, color)
calendars.add(calendar) calendars.add(calendar)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
}
} finally { } finally {
cursor?.close() cursor?.close()
} }
@ -282,7 +283,7 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = contentResolver.query(uri, projection, selection, null, null) cursor = contentResolver.query(uri, projection, selection, null, null)
cursor.moveToFirst() if (cursor != null && cursor.moveToFirst()) {
do { do {
val id = cursor.getLongValue(CalendarContract.Events._ID) val id = cursor.getLongValue(CalendarContract.Events._ID)
val title = cursor.getStringValue(CalendarContract.Events.TITLE) val title = cursor.getStringValue(CalendarContract.Events.TITLE)
@ -299,6 +300,7 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval, reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval,
"", 0 or allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, lastUpdated = System.currentTimeMillis()) "", 0 or allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, lastUpdated = System.currentTimeMillis())
} while (cursor.moveToNext()) } while (cursor.moveToNext())
}
} finally { } finally {
cursor?.close() cursor?.close()
} }
@ -314,7 +316,7 @@ fun Context.getCalDAVEventReminders(eventId: Long): List<Int> {
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = contentResolver.query(uri, projection, selection, null, null) cursor = contentResolver.query(uri, projection, selection, null, null)
cursor.moveToFirst() if (cursor != null && cursor.moveToFirst()) {
do { do {
val minutes = cursor.getIntValue(CalendarContract.Reminders.MINUTES) val minutes = cursor.getIntValue(CalendarContract.Reminders.MINUTES)
val method = cursor.getIntValue(CalendarContract.Reminders.METHOD) val method = cursor.getIntValue(CalendarContract.Reminders.METHOD)
@ -322,6 +324,7 @@ fun Context.getCalDAVEventReminders(eventId: Long): List<Int> {
reminders.add(minutes) reminders.add(minutes)
} }
} while (cursor.moveToNext()) } while (cursor.moveToNext())
}
} finally { } finally {
cursor?.close() cursor?.close()
} }