Try with resources

pull/996/head
Alex Baker 4 years ago
parent 8f8650840d
commit 1e6ed2a2fa

@ -34,16 +34,13 @@ class CalendarEventAttendeeProvider {
}
List<AndroidCalendarEventAttendee> attendees = new ArrayList<>();
Cursor cursor = null;
try {
//noinspection ResourceType
cursor =
contentResolver.query(
CalendarContract.Attendees.CONTENT_URI,
COLUMNS,
CalendarContract.Attendees.EVENT_ID + " = ? ",
new String[] {Long.toString(id)},
null);
try (Cursor cursor =
contentResolver.query(
CalendarContract.Attendees.CONTENT_URI,
COLUMNS,
CalendarContract.Attendees.EVENT_ID + " = ? ",
new String[] {Long.toString(id)},
null)) {
if (cursor != null && cursor.getCount() > 0) {
int emailIndex = cursor.getColumnIndexOrThrow(CalendarContract.Attendees.ATTENDEE_EMAIL);
int nameIndex = cursor.getColumnIndexOrThrow(CalendarContract.Attendees.ATTENDEE_NAME);
@ -55,10 +52,6 @@ class CalendarEventAttendeeProvider {
}
} catch (Exception e) {
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return attendees;
}

@ -96,9 +96,7 @@ public class CalendarEventProvider {
}
List<AndroidCalendarEvent> events = new ArrayList<>();
Cursor cursor = null;
try {
cursor = contentResolver.query(uri, COLUMNS, selection, selectionArgs, null);
try (Cursor cursor = contentResolver.query(uri, COLUMNS, selection, selectionArgs, null)) {
if (cursor != null && cursor.getCount() > 0) {
int idIndex = cursor.getColumnIndex(_ID);
int startIndex = cursor.getColumnIndexOrThrow(CalendarContract.Events.DTSTART);
@ -119,10 +117,6 @@ public class CalendarEventProvider {
}
} catch (Exception e) {
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return events;
}

@ -58,9 +58,7 @@ public class CalendarProvider {
}
List<AndroidCalendar> calendars = new ArrayList<>();
Cursor cursor = null;
try {
cursor = contentResolver.query(uri, COLUMNS, selection, null, SORT);
try (Cursor cursor = contentResolver.query(uri, COLUMNS, selection, null, SORT)) {
if (cursor != null && cursor.getCount() > 0) {
int idColumn = cursor.getColumnIndex(_ID);
int nameColumn = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME);
@ -75,10 +73,6 @@ public class CalendarProvider {
}
} catch (Exception e) {
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return calendars;
}

@ -237,14 +237,13 @@ public class CalendarControlSet extends TaskEditControlFragment {
ContentResolver cr = getActivity().getContentResolver();
Uri uri = Uri.parse(eventUri);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Cursor cursor =
try (Cursor cursor =
cr.query(
uri,
new String[] {CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND},
null,
null,
null);
try {
null)) {
if (cursor.getCount() == 0) {
// event no longer exists
Toast.makeText(context, R.string.calendar_event_not_found, Toast.LENGTH_SHORT).show();
@ -259,8 +258,6 @@ public class CalendarControlSet extends TaskEditControlFragment {
} catch (Exception e) {
Timber.e(e);
Toast.makeText(getActivity(), R.string.gcal_TEA_error, Toast.LENGTH_LONG).show();
} finally {
cursor.close();
}
}
@ -321,15 +318,12 @@ public class CalendarControlSet extends TaskEditControlFragment {
try {
Uri uri = Uri.parse(eventUri);
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor =
try (Cursor cursor =
contentResolver.query(
uri, new String[] {CalendarContract.Events.DTSTART}, null, null, null);
try {
uri, new String[] {CalendarContract.Events.DTSTART}, null, null, null)) {
if (cursor.getCount() != 0) {
return true;
}
} finally {
cursor.close();
}
} catch (Exception e) {
Timber.e(e, "%s: %s", eventUri, e.getMessage());

Loading…
Cancel
Save