Fix for Android.com issue 1421:
	"The insert method doesn't get a theradID if the thread ID is missing."
	http://code.google.com/p/android/issues/detail?id=1421

Change-Id: Iea19f22afd436b17a838314a51af67ec90e327fc
diff --git a/src/com/android/providers/telephony/MmsProvider.java b/src/com/android/providers/telephony/MmsProvider.java
index 74c69f1..f141cd4 100644
--- a/src/com/android/providers/telephony/MmsProvider.java
+++ b/src/com/android/providers/telephony/MmsProvider.java
@@ -30,6 +30,7 @@
 import android.net.Uri;
 import android.os.ParcelFileDescriptor;
 import android.provider.BaseColumns;
+import android.provider.Telephony.CanonicalAddressesColumns;
 import android.provider.Telephony.Mms;
 import android.provider.Telephony.MmsSms;
 import android.provider.Telephony.Mms.Addr;
@@ -42,6 +43,7 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import android.provider.Telephony.Threads;
 
 /**
  * The class to provide base facility to access MMS related content,
@@ -342,6 +344,14 @@
                 finalValues.put(Mms.READ, 1);
             }
 
+            // thread_id
+            Long threadId = values.getAsLong(Mms.THREAD_ID);
+            String address = values.getAsString(CanonicalAddressesColumns.ADDRESS);
+
+            if (((threadId == null) || (threadId == 0)) && (address != null)) {
+                finalValues.put(Mms.THREAD_ID, Threads.getOrCreateThreadId(getContext(), address));
+            }
+
             if ((rowId = db.insert(table, null, finalValues)) <= 0) {
                 Log.e(TAG, "MmsProvider.insert: failed! " + finalValues);
                 return null;