Thread can't be deleted

Bug 6593778

Delete all the messages in a thread and the thread comes back. If you
get a row in the pdu or sms table with an empty thread_id, the query to
delete threads that aren't referenced by any message fails. The query
has to weed out messages with no thread_ids.

Change-Id: I2a2121a27a4d4ce9ed02b8415dc50b0a59623d94
diff --git a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
index 3ba6e78..d3aaa51 100644
--- a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
+++ b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
@@ -396,8 +396,8 @@
         // transaction to make it faster.
         // remove orphaned threads
         db.delete("threads",
-                "_id NOT IN (SELECT DISTINCT thread_id FROM sms " +
-                "UNION SELECT DISTINCT thread_id FROM pdu)", null);
+                "_id NOT IN (SELECT DISTINCT thread_id FROM sms where thread_id NOT NULL" +
+                "UNION SELECT DISTINCT thread_id FROM pdu where thread_id NOT NULL)", null);
 
         // remove orphaned canonical_addresses
         removeUnferencedCanonicalAddresses(db);
diff --git a/src/com/android/providers/telephony/MmsSmsProvider.java b/src/com/android/providers/telephony/MmsSmsProvider.java
index 839e9ff..fb082b9 100644
--- a/src/com/android/providers/telephony/MmsSmsProvider.java
+++ b/src/com/android/providers/telephony/MmsSmsProvider.java
@@ -1178,8 +1178,8 @@
                 break;
             case URI_OBSOLETE_THREADS:
                 affectedRows = db.delete("threads",
-                        "_id NOT IN (SELECT DISTINCT thread_id FROM sms " +
-                        "UNION SELECT DISTINCT thread_id FROM pdu)", null);
+                        "_id NOT IN (SELECT DISTINCT thread_id FROM sms where thread_id NOT NULL " +
+                        "UNION SELECT DISTINCT thread_id FROM pdu where thread_id NOT NULL)", null);
                 break;
             default:
                 throw new UnsupportedOperationException(NO_DELETES_INSERTS_OR_UPDATES + uri);