Fix recycler NPE

It's caused by getting back a null cursor from a query. There are other
places in the code where we could get null cursors. Fixed 'em. Also,
the cursor wasn't getting closed. Fixed that too. Bug 3315962

Change-Id: Ifff2b35cf25627847376131a16686d3420a21946
diff --git a/src/com/android/mms/util/Recycler.java b/src/com/android/mms/util/Recycler.java
index c0ae45d..23c304c 100644
--- a/src/com/android/mms/util/Recycler.java
+++ b/src/com/android/mms/util/Recycler.java
@@ -248,6 +248,9 @@
         @Override
         protected boolean anyThreadOverLimit(Context context) {
             Cursor cursor = getAllThreads(context);
+            if (cursor == null) {
+                return false;
+            }
             int limit = getMessageLimit(context);
             try {
                 while (cursor.moveToNext()) {
@@ -258,9 +261,15 @@
                             SMS_MESSAGE_PROJECTION,
                             "locked=0",
                             null, "date DESC");     // get in newest to oldest order
-
-                    if (msgs.getCount() >= limit) {
-                        return true;
+                    if (msgs == null) {
+                        return false;
+                    }
+                    try {
+                        if (msgs.getCount() >= limit) {
+                            return true;
+                        }
+                    } finally {
+                        msgs.close();
                     }
                 }
             } finally {
@@ -433,6 +442,9 @@
         @Override
         protected boolean anyThreadOverLimit(Context context) {
             Cursor cursor = getAllThreads(context);
+            if (cursor == null) {
+                return false;
+            }
             int limit = getMessageLimit(context);
             try {
                 while (cursor.moveToNext()) {
@@ -444,11 +456,16 @@
                             "thread_id=" + threadId + " AND locked=0",
                             null, "date DESC");     // get in newest to oldest order
 
-                    if (msgs.getCount() >= limit) {
-                        msgs.close();
-                        return true;
+                    if (msgs == null) {
+                        return false;
                     }
-                    msgs.close();
+                    try {
+                        if (msgs.getCount() >= limit) {
+                            return true;
+                        }
+                    } finally {
+                        cursor.close();
+                    }
                 }
             } finally {
                 cursor.close();