Some more cursor-related improvements (closing)

This commit is contained in:
cpfeiffer 2016-03-08 23:48:31 +01:00
parent 10975feb49
commit 3f39928df5
3 changed files with 13 additions and 32 deletions

View File

@ -55,17 +55,7 @@ public class K9Receiver extends BroadcastReceiver {
* It should be the first one returned by the query in most cases, * It should be the first one returned by the query in most cases,
*/ */
Cursor c = null; try (Cursor c = context.getContentResolver().query(k9Uri, messagesProjection, null, null, 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 {
if (c != null) { if (c != null) {
while (c.moveToNext()) { while (c.moveToNext()) {
String uri = c.getString(c.getColumnIndex("uri")); String uri = c.getString(c.getColumnIndex("uri"));
@ -77,10 +67,11 @@ public class K9Receiver extends BroadcastReceiver {
} }
} }
} }
} finally { } catch (Exception e) {
if (c != null) { e.printStackTrace();
c.close(); notificationSpec.sender = "Gadgetbridge";
} notificationSpec.subject = "Permission Error?";
notificationSpec.body = "Please reinstall Gadgetbridge to enable K-9 Mail notifications";
} }
GBApplication.deviceService().onNotification(notificationSpec); GBApplication.deviceService().onNotification(notificationSpec);

View File

@ -54,11 +54,11 @@ public class CalendarEvents {
ContentUris.appendId(eventsUriBuilder, dtEnd); ContentUris.appendId(eventsUriBuilder, dtEnd);
Uri eventsUri = eventsUriBuilder.build(); Uri eventsUri = eventsUriBuilder.build();
Cursor evtCursor = null; try (Cursor evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC")) {
evtCursor = mContext.getContentResolver().query(eventsUri, EVENT_INSTANCE_PROJECTION, null, null, CalendarContract.Instances.BEGIN + " ASC"); if (evtCursor == null || evtCursor.getCount() == 0) {
return false;
if (evtCursor.moveToFirst()) { }
do { while (evtCursor.moveToNext()) {
CalendarEvent calEvent = new CalendarEvent( CalendarEvent calEvent = new CalendarEvent(
evtCursor.getLong(1), evtCursor.getLong(1),
evtCursor.getLong(2), evtCursor.getLong(2),
@ -69,11 +69,9 @@ public class CalendarEvents {
evtCursor.getString(7) evtCursor.getString(7)
); );
calendarEventList.add(calEvent); calendarEventList.add(calEvent);
} while (evtCursor.moveToNext()); }
return true; return true;
} }
return false;
} }
public class CalendarEvent { public class CalendarEvent {

View File

@ -474,21 +474,13 @@ public class DeviceCommunicationService extends Service {
return name; return name;
} }
ContentResolver contentResolver = getContentResolver(); try (Cursor contactLookup = getContentResolver().query(uri, null, null, null, null)) {
Cursor contactLookup = null;
try {
contactLookup = contentResolver.query(uri, null, null, null, null);
if (contactLookup != null && contactLookup.getCount() > 0) { if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext(); contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
} }
} catch (SecurityException e) { } catch (SecurityException e) {
// ignore, just return name below // ignore, just return name below
} finally {
if (contactLookup != null) {
contactLookup.close();
}
} }
return name; return name;