Bug 1247636 - Close Cursors in GeckoSmsManager.getThreadFromCursorAndNotify. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 22 Feb 2016 15:11:28 -0800
changeset 333183 ac512df5428e2fce834aebc1cd988a1d30afa176
parent 332009 6008843fb7ba5de8a2c7e39bf6e2e4a2e7f1532b
child 333184 1aed33941186ad10fd129989fa6879dcb4bfaf5f
push id11290
push usermichael.l.comella@gmail.com
push dateMon, 22 Feb 2016 23:14:29 +0000
reviewerssebastian
bugs1247636
milestone47.0a1
Bug 1247636 - Close Cursors in GeckoSmsManager.getThreadFromCursorAndNotify. r=sebastian MozReview-Commit-ID: 3zCDyPhqlGJ
mobile/android/base/java/org/mozilla/gecko/GeckoSmsManager.java
--- 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) {