bug #2438230: add more logging for catching the bug.

- added some optimization for the single recipient case.
- more logging for creating new recipient id and thread id.

Change-Id: Idb46c550179afab5e00b62c53bd04ac77763797b
diff --git a/src/com/android/providers/telephony/MmsSmsProvider.java b/src/com/android/providers/telephony/MmsSmsProvider.java
index bfe41ab..93668c1 100644
--- a/src/com/android/providers/telephony/MmsSmsProvider.java
+++ b/src/com/android/providers/telephony/MmsSmsProvider.java
@@ -456,6 +456,7 @@
         String refinedAddress = isEmail ? address.toLowerCase() : address;
         String selection = "address=?";
         String[] selectionArgs;
+        long retVal = -1L;
 
         if (isEmail) {
             selectionArgs = new String[] { refinedAddress };
@@ -478,12 +479,17 @@
                 contentValues.put(CanonicalAddressesColumns.ADDRESS, refinedAddress);
 
                 db = mOpenHelper.getWritableDatabase();
-                return db.insert("canonical_addresses",
+                retVal = db.insert("canonical_addresses",
                         CanonicalAddressesColumns.ADDRESS, contentValues);
+
+                Log.d(LOG_TAG, "getSingleAddressId: insert new canonical_address for " + address +
+                        ", _id=" + retVal);
+
+                return retVal;
             }
 
             if (cursor.moveToFirst()) {
-                return cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
+                retVal = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
             }
         } finally {
             if (cursor != null) {
@@ -491,7 +497,7 @@
             }
         }
 
-        return -1L;
+        return retVal;
     }
 
     /**
@@ -506,7 +512,7 @@
                 if (id != -1L) {
                     result.add(id);
                 } else {
-                    Log.e(LOG_TAG, "Address ID not found for: " + address);
+                    Log.e(LOG_TAG, "getAddressIds: address ID not found for " + address);
                 }
             }
         }
@@ -524,7 +530,11 @@
         for (Long number : numbers) {
             result[i++] = number;
         }
-        Arrays.sort(result);
+
+        if (size > 1) {
+            Arrays.sort(result);
+        }
+
         return result;
     }
 
@@ -559,10 +569,16 @@
         }
         values.put(ThreadsColumns.MESSAGE_COUNT, 0);
 
-        mOpenHelper.getWritableDatabase().insert("threads", null, values);
+        long result = mOpenHelper.getWritableDatabase().insert("threads", null, values);
+        Log.d(LOG_TAG, "insertThread: created new thread_id " + result +
+                " for recipientIds " + recipientIds);
+
         getContext().getContentResolver().notifyChange(MmsSms.CONTENT_URI, null);
     }
 
+    private static final String THREAD_QUERY =
+            "SELECT _id FROM threads " + "WHERE recipient_ids=?";
+
     /**
      * Return the thread ID for this list of
      * recipients IDs.  If no thread exists with this ID, create
@@ -570,30 +586,38 @@
      * Threads.getThreadId to access this information.
      */
     private synchronized Cursor getThreadId(List<String> recipients) {
-        String recipientIds =
-                getSpaceSeparatedNumbers(
-                        getSortedSet(getAddressIds(recipients)));
+        Set<Long> addressIds = getAddressIds(recipients);
+        String recipientIds = "";
 
-        String THREAD_QUERY = "SELECT _id FROM threads " + "WHERE recipient_ids = ?";
-
-        if (DEBUG) {
-            Log.v(LOG_TAG, "getThreadId THREAD_QUERY: " + THREAD_QUERY +
-                    ", recipientIds=" + recipientIds);
+        // optimize for size==1, which should be most of the cases
+        if (addressIds.size() == 1) {
+            for (Long addressId : addressIds) {
+                recipientIds = Long.toString(addressId);
+            }
+        } else {
+            recipientIds = getSpaceSeparatedNumbers(getSortedSet(addressIds));
         }
+
+        if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
+            Log.d(LOG_TAG, "getThreadId: recipientIds (selectionArgs) =" + recipientIds);
+        }
+
+        String[] selectionArgs = new String[] { recipientIds };
         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
-        Cursor cursor = db.rawQuery(THREAD_QUERY, new String[] { recipientIds });
+        Cursor cursor = db.rawQuery(THREAD_QUERY, selectionArgs);
 
         if (cursor.getCount() == 0) {
             cursor.close();
-            if (DEBUG) {
-                Log.v(LOG_TAG, "getThreadId cursor zero, creating new threadid");
-            }
+
+            Log.d(LOG_TAG, "getThreadId: create new thread_id for recipients " + recipients);
             insertThread(recipientIds, recipients.size());
+
             db = mOpenHelper.getReadableDatabase();  // In case insertThread closed it
-            cursor = db.rawQuery(THREAD_QUERY, new String[] { recipientIds });
+            cursor = db.rawQuery(THREAD_QUERY, selectionArgs);
         }
-        if (DEBUG) {
-            Log.v(LOG_TAG, "getThreadId cursor count: " + cursor.getCount());
+        
+        if (cursor.getCount() > 1) {
+            Log.w(LOG_TAG, "getThreadId: why is cursorCount=" + cursor.getCount());
         }
 
         return cursor;