--- a/mobile/android/base/java/org/mozilla/gecko/GeckoSmsManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoSmsManager.java
@@ -1032,58 +1032,70 @@ public class GeckoSmsManager
if (!SmsIOThread.getInstance().execute(new GetNextMessageRunnable(aRequestId))) {
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();
-
- long id = aCursor.getLong(aCursor.getColumnIndex("thread_id"));
- Cursor msgCursor = cr.query(kSmsContentUri,
- kRequiredMessageRowsForThread,
- "thread_id = " + id,
- null,
- "date DESC");
+ Cursor msgCursorFirst = null;
+ Cursor msgCursorSecond = null;
+ try {
+ ContentResolver cr = GeckoAppShell.getContext().getContentResolver();
- if (msgCursor == null || msgCursor.getCount() == 0) {
- throw new Exception("Empty thread " + id);
- }
+ long id = aCursor.getLong(aCursor.getColumnIndex("thread_id"));
+ msgCursorFirst = cr.query(kSmsContentUri,
+ kRequiredMessageRowsForThread,
+ "thread_id = " + id,
+ null,
+ "date DESC");
- msgCursor.moveToFirst();
+ if (msgCursorFirst == null || msgCursorFirst.getCount() == 0) {
+ throw new Exception("Empty thread " + id);
+ }
- String lastMessageSubject = msgCursor.getString(msgCursor.getColumnIndex("subject"));
- String body = msgCursor.getString(msgCursor.getColumnIndex("body"));
- long timestamp = msgCursor.getLong(msgCursor.getColumnIndex("date"));
+ msgCursorFirst.moveToFirst();
+
+ String lastMessageSubject = msgCursorFirst.getString(msgCursorFirst.getColumnIndex("subject"));
+ String body = msgCursorFirst.getString(msgCursorFirst.getColumnIndex("body"));
+ long timestamp = msgCursorFirst.getLong(msgCursorFirst.getColumnIndex("date"));
- HashSet<String> participants = new HashSet<>();
- do {
- String p = msgCursor.getString(msgCursor.getColumnIndex("address"));
- participants.add(p);
- } while (msgCursor.moveToNext());
+ HashSet<String> participants = new HashSet<>();
+ do {
+ String p = msgCursorFirst.getString(msgCursorFirst.getColumnIndex("address"));
+ participants.add(p);
+ } while (msgCursorFirst.moveToNext());
- //TODO: handle MMS
- String lastMessageType = "sms";
+ //TODO: handle MMS
+ String lastMessageType = "sms";
+
+ msgCursorSecond = cr.query(kSmsContentUri,
+ kRequiredMessageRowsForThread,
+ "thread_id = " + id + " AND read = 0",
+ null,
+ null);
- msgCursor = cr.query(kSmsContentUri,
- kRequiredMessageRowsForThread,
- "thread_id = " + id + " AND read = 0",
- null,
- null);
+ if (msgCursorSecond == null) {
+ Log.e("GeckoSmsManager", "We should never get here, should have errored before");
+ throw new Exception("Empty thread " + id);
+ }
+
+ long unreadCount = msgCursorSecond.getCount();
- if (msgCursor == null) {
- Log.e("GeckoSmsManager", "We should never get here, should have errored before");
- throw new Exception("Empty thread " + id);
+ notifyThreadCursorResult(id, lastMessageSubject, body, unreadCount, participants.toArray(), timestamp, lastMessageType, aRequestId);
}
-
- long unreadCount = msgCursor.getCount();
-
- notifyThreadCursorResult(id, lastMessageSubject, body, unreadCount, participants.toArray(), timestamp, lastMessageType, aRequestId);
+ finally {
+ if (msgCursorFirst != null) {
+ msgCursorFirst.close();
+ }
+ if (msgCursorSecond != null) {
+ msgCursorSecond.close();
+ }
+ }
}
@Override
public void createThreadCursor(int aRequestId) {
class CreateThreadCursorRunnable implements Runnable {
private final int mRequestId;
CreateThreadCursorRunnable(int aRequestId) {
@@ -1095,16 +1107,18 @@ public class GeckoSmsManager
try {
ContentResolver cr = GeckoAppShell.getContext().getContentResolver();
Cursor cursor = cr.query(kSmsThreadsContentUri,
kThreadProjection,
null,
null,
"date DESC");
if (cursor == null || !cursor.moveToFirst()) {
+ if (cursor != null )
+ cursor.close();
notifyCursorDone(mRequestId);
return;
}
MessagesListManager.getInstance().add(mRequestId, cursor);
getThreadFromCursorAndNotify(cursor, mRequestId);
} catch (Exception e) {