diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java index 5683cf83..1b615d24 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java @@ -55,17 +55,7 @@ public class K9Receiver extends BroadcastReceiver { * It should be the first one returned by the query in most cases, */ - Cursor c = null; - try { - c = context.getContentResolver().query(k9Uri, messagesProjection, null, null, null); - } catch (Exception e) { - e.printStackTrace(); - notificationSpec.sender = "Gadgetbridge"; - notificationSpec.subject = "Permission Error?"; - notificationSpec.body = "Please reinstall Gadgetbridge to enable K-9 Mail notifications"; - } - - try { + try (Cursor c = context.getContentResolver().query(k9Uri, messagesProjection, null, null, null)) { if (c != null) { while (c.moveToNext()) { String uri = c.getString(c.getColumnIndex("uri")); @@ -77,10 +67,11 @@ public class K9Receiver extends BroadcastReceiver { } } } - } finally { - if (c != null) { - c.close(); - } + } catch (Exception e) { + e.printStackTrace(); + notificationSpec.sender = "Gadgetbridge"; + notificationSpec.subject = "Permission Error?"; + notificationSpec.body = "Please reinstall Gadgetbridge to enable K-9 Mail notifications"; } GBApplication.deviceService().onNotification(notificationSpec); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CalendarEvents.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CalendarEvents.java index 1e814062..296ebda0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CalendarEvents.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CalendarEvents.java @@ -54,11 +54,11 @@ public class CalendarEvents { ContentUris.appendId(eventsUriBuilder, dtEnd); Uri eventsUri = eventsUriBuilder.build(); - Cursor evtCursor = null; - evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC"); - - if (evtCursor.moveToFirst()) { - do { + try (Cursor evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC")) { + if (evtCursor == null || evtCursor.getCount() == 0) { + return false; + } + while (evtCursor.moveToNext()) { CalendarEvent calEvent = new CalendarEvent( evtCursor.getLong(1), evtCursor.getLong(2), @@ -69,11 +69,9 @@ public class CalendarEvents { evtCursor.getString(7) ); calendarEventList.add(calEvent); - } while (evtCursor.moveToNext()); - + } return true; } - return false; } public class CalendarEvent { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 0eddd504..0d495422 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -474,21 +474,13 @@ public class DeviceCommunicationService extends Service { return name; } - ContentResolver contentResolver = getContentResolver(); - - Cursor contactLookup = null; - try { - contactLookup = contentResolver.query(uri, null, null, null, null); + try (Cursor contactLookup = getContentResolver().query(uri, null, null, null, null)) { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); } } catch (SecurityException e) { // ignore, just return name below - } finally { - if (contactLookup != null) { - contactLookup.close(); - } } return name;