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,16 +250,17 @@ 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)
val accountName = cursor.getStringValue(CalendarContract.Calendars.ACCOUNT_NAME) val accountName = cursor.getStringValue(CalendarContract.Calendars.ACCOUNT_NAME)
val ownerName = cursor.getStringValue(CalendarContract.Calendars.OWNER_ACCOUNT) val ownerName = cursor.getStringValue(CalendarContract.Calendars.OWNER_ACCOUNT)
val color = cursor.getIntValue(CalendarContract.Calendars.CALENDAR_COLOR) val color = cursor.getIntValue(CalendarContract.Calendars.CALENDAR_COLOR)
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,23 +283,24 @@ 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)
val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION) val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION)
val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt() val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt()
val endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt() val endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt()
val duration = cursor.getStringValue(CalendarContract.Events.DURATION) val duration = cursor.getStringValue(CalendarContract.Events.DURATION)
val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY) val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY)
val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) val rrule = cursor.getStringValue(CalendarContract.Events.RRULE)
val reminders = getCalDAVEventReminders(id) val reminders = getCalDAVEventReminders(id)
val repeatRule = Parser().parseRepeatInterval(rrule, startTS) val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }), val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }),
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,14 +316,15 @@ 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)
if (method == CalendarContract.Reminders.METHOD_ALERT) { if (method == CalendarContract.Reminders.METHOD_ALERT) {
reminders.add(minutes) reminders.add(minutes)
} }
} while (cursor.moveToNext()) } while (cursor.moveToNext())
}
} finally { } finally {
cursor?.close() cursor?.close()
} }