Bug 1247636 - Close Cursors in GeckoSmsManager.getThreadFromCursorAndNotify. r=sebastian
MozReview-Commit-ID: 3zCDyPhqlGJ
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoSmsManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoSmsManager.java
@@ -1034,56 +1034,70 @@ public class GeckoSmsManager
Log.e("GeckoSmsManager", "Failed to add GetNextMessageRunnable to the SmsIOThread");
notifyCursorError(kUnknownError, aRequestId);
}
}
private void getThreadFromCursorAndNotify(Cursor aCursor, int aRequestId) throws Exception {
ContentResolver cr = GeckoAppShell.getContext().getContentResolver();
+ final String lastMessageSubject;
+ final String body;
+ final long timestamp;
+ final HashSet<String> participants;
long id = aCursor.getLong(aCursor.getColumnIndex("thread_id"));
- Cursor msgCursor = cr.query(kSmsContentUri,
- kRequiredMessageRowsForThread,
- "thread_id = " + id,
- null,
- "date DESC");
-
- if (msgCursor == null || msgCursor.getCount() == 0) {
- throw new Exception("Empty thread " + id);
- }
+ final Cursor msgCursor = cr.query(kSmsContentUri,
+ kRequiredMessageRowsForThread,
+ "thread_id = " + id,
+ null,
+ "date DESC");
+ try {
+ if (msgCursor == null || msgCursor.getCount() == 0) {
+ throw new Exception("Empty thread " + id);
+ }
- msgCursor.moveToFirst();
+ msgCursor.moveToFirst();
- String lastMessageSubject = msgCursor.getString(msgCursor.getColumnIndex("subject"));
- String body = msgCursor.getString(msgCursor.getColumnIndex("body"));
- long timestamp = msgCursor.getLong(msgCursor.getColumnIndex("date"));
+ lastMessageSubject = msgCursor.getString(msgCursor.getColumnIndex("subject"));
+ body = msgCursor.getString(msgCursor.getColumnIndex("body"));
+ timestamp = msgCursor.getLong(msgCursor.getColumnIndex("date"));
- HashSet<String> participants = new HashSet<>();
- do {
- String p = msgCursor.getString(msgCursor.getColumnIndex("address"));
- participants.add(p);
- } while (msgCursor.moveToNext());
+ participants = new HashSet<>();
+ do {
+ String p = msgCursor.getString(msgCursor.getColumnIndex("address"));
+ participants.add(p);
+ } while (msgCursor.moveToNext());
+ } finally {
+ if (msgCursor != null) {
+ msgCursor.close();
+ }
+ }
//TODO: handle MMS
String lastMessageType = "sms";
- msgCursor = cr.query(kSmsContentUri,
- kRequiredMessageRowsForThread,
- "thread_id = " + id + " AND read = 0",
- null,
- null);
+ final Cursor msgCursor2 = cr.query(kSmsContentUri,
+ kRequiredMessageRowsForThread,
+ "thread_id = " + id + " AND read = 0",
+ null,
+ null);
+ try {
+ if (msgCursor2 == null) {
+ Log.e("GeckoSmsManager", "We should never get here, should have errored before");
+ throw new Exception("Empty thread " + id);
+ }
- if (msgCursor == null) {
- Log.e("GeckoSmsManager", "We should never get here, should have errored before");
- throw new Exception("Empty thread " + id);
+ long unreadCount = msgCursor2.getCount();
+
+ notifyThreadCursorResult(id, lastMessageSubject, body, unreadCount, participants.toArray(), timestamp, lastMessageType, aRequestId);
+ } finally {
+ if (msgCursor2 != null) {
+ msgCursor2.close();
+ }
}
-
- long unreadCount = msgCursor.getCount();
-
- notifyThreadCursorResult(id, lastMessageSubject, body, unreadCount, participants.toArray(), timestamp, lastMessageType, aRequestId);
}
@Override
public void createThreadCursor(int aRequestId) {
class CreateThreadCursorRunnable implements Runnable {
private final int mRequestId;
CreateThreadCursorRunnable(int aRequestId) {