Include Google Talk presence, even when missing Im rows.

When inserting Google Talk presence updates, we match on
both Im and Email rows.  This change adds presence "dots"
to individual Im rows, and creates in-memory Im rows when
Email entries have presence.

This loads status details through a second query and holds
back building UI until both queries finish.  This change
also generalizes logic for building Im intents borrowed
from FastTrack code.

This change also fixes a regression that was dropping third-
party data rows.  The second-query approach above allows us
to remove a large chunk of code that was using old API.

Fixes http://b/2161796
diff --git a/src/com/android/contacts/ContactsUtils.java b/src/com/android/contacts/ContactsUtils.java
index 2a3c22d..25da482 100644
--- a/src/com/android/contacts/ContactsUtils.java
+++ b/src/com/android/contacts/ContactsUtils.java
@@ -19,6 +19,7 @@
 
 import android.content.ContentResolver;
 import android.content.ContentUris;
+import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
@@ -222,6 +223,31 @@
         return null;
     }
 
+    /**
+     * Build {@link Intent} to launch an action for the given {@link Im} or
+     * {@link Email} row. Returns null when missing protocol or data.
+     */
+    public static Intent buildImIntent(ContentValues values) {
+        final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE));
+        final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL);
+
+        String host = values.getAsString(Im.CUSTOM_PROTOCOL);
+        String data = values.getAsString(isEmail ? Email.DATA : Im.DATA);
+        if (protocol != Im.PROTOCOL_CUSTOM) {
+            // Try bringing in a well-known host for specific protocols
+            host = ContactsUtils.lookupProviderNameFromId(protocol);
+        }
+
+        if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(data)) {
+            final String authority = host.toLowerCase();
+            final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
+                    authority).appendPath(data).build();
+            return new Intent(Intent.ACTION_SENDTO, imUri);
+        } else {
+            return null;
+        }
+    }
+
     public static Intent getPhotoPickIntent() {
         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
         intent.setType("image/*");