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

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

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

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

Loading…
Cancel
Save