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,
*/
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);

View File

@ -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 {

View File

@ -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;