Bug 1247636 - close cursor before returning from createThreadCursor::run and getThreadFromCursorAndNotify. r?mcomella draft
authorBogdan Postelnicu <bogdan.postelnicu@softvision.ro>
Wed, 17 Feb 2016 12:01:10 +0200
changeset 331442 87f7483ba44872a642962978f887abfed90f4155
parent 331237 6ea654cad929c9bedd8a4161a182b6189fbeae6a
child 514391 f2799e0b071bfdd1dd3fe117b019997f084f0e88
push id10994
push userBogdan.Postelnicu@softvision.ro
push dateWed, 17 Feb 2016 10:01:43 +0000
reviewersmcomella
bugs1247636
milestone47.0a1
Bug 1247636 - close cursor before returning from createThreadCursor::run and getThreadFromCursorAndNotify. r?mcomella MozReview-Commit-ID: IMRDxKyQEG3
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
@@ -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) {