Support for data_with_presence URI
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 4924192..3af2f7a 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -159,7 +159,7 @@
     private static final int SEARCH_SUGGESTIONS = 12001;
     private static final int SEARCH_SHORTCUT = 12002;
 
-    private static final int PHONES_WITH_PRESENCE = 13000;
+    private static final int DATA_WITH_PRESENCE = 13000;
 
     private interface ContactsQuery {
         public static final String TABLE = Tables.RAW_CONTACTS;
@@ -302,7 +302,7 @@
     /** Contains Presence columns */
     private static final HashMap<String, String> sPresenceProjectionMap;
     /** Contains Presence columns */
-    private static final HashMap<String, String> sPhonesWithPresenceProjectionMap;
+    private static final HashMap<String, String> sDataWithPresenceProjectionMap;
 
     /** Sql select statement that returns the contact id associated with a data record. */
     private static final String sNestedRawContactIdSelect;
@@ -387,7 +387,7 @@
                 SEARCH_SHORTCUT);
 
         // Private API
-        matcher.addURI(ContactsContract.AUTHORITY, "phones_with_presence", PHONES_WITH_PRESENCE);
+        matcher.addURI(ContactsContract.AUTHORITY, "data_with_presence", DATA_WITH_PRESENCE);
     }
 
     static {
@@ -573,21 +573,13 @@
         columns.put(Presence.PRESENCE_CUSTOM_STATUS, Presence.PRESENCE_CUSTOM_STATUS);
         sPresenceProjectionMap = columns;
 
-        sPhonesWithPresenceProjectionMap = new HashMap<String, String>();
-        sPhonesWithPresenceProjectionMap.put(Phone.NUMBER, Phone.NUMBER);
-        sPhonesWithPresenceProjectionMap.put(Phone.TYPE, Phone.TYPE);
-        sPhonesWithPresenceProjectionMap.put(Phone.LABEL, Phone.LABEL);
-        sPhonesWithPresenceProjectionMap.put(Contacts.DISPLAY_NAME,
-                ContactsColumns.CONCRETE_DISPLAY_NAME + " AS " + Contacts.DISPLAY_NAME);
-        sPhonesWithPresenceProjectionMap.put(Contacts._ID,
-                ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID);
-        sPhonesWithPresenceProjectionMap.put(RawContacts.CONTACT_ID,
-                ContactsColumns.CONCRETE_ID + " AS " + RawContacts.CONTACT_ID);
-        sPhonesWithPresenceProjectionMap.put(Presence.PRESENCE_STATUS,
+        sDataWithPresenceProjectionMap = new HashMap<String, String>();
+        sDataWithPresenceProjectionMap.putAll(sDataProjectionMap);
+        sDataWithPresenceProjectionMap.put(Presence.PRESENCE_STATUS,
                 "MAX(" + Presence.PRESENCE_STATUS + ") AS " + Presence.PRESENCE_STATUS);
 
         // TODO implement support for latest custom status
-        sPhonesWithPresenceProjectionMap.put(Presence.PRESENCE_CUSTOM_STATUS,
+        sDataWithPresenceProjectionMap.put(Presence.PRESENCE_CUSTOM_STATUS,
                 "NULL AS " + Presence.PRESENCE_CUSTOM_STATUS);
 
         sNestedRawContactIdSelect = "SELECT " + Data.RAW_CONTACT_ID + " FROM " + Tables.DATA + " WHERE "
@@ -2283,6 +2275,16 @@
                 break;
             }
 
+            case DATA_WITH_PRESENCE: {
+                qb.setTables(mOpenHelper.getDataView() + " data"
+                        + " LEFT OUTER JOIN " + Tables.PRESENCE
+                        + " ON (" + Presence.RAW_CONTACT_ID + "="
+                                + DataColumns.CONCRETE_RAW_CONTACT_ID + ")");
+                qb.setProjectionMap(sDataWithPresenceProjectionMap);
+                groupBy = DataColumns.CONCRETE_ID;
+                break;
+            }
+
             case PHONE_LOOKUP: {
 
                 if (TextUtils.isEmpty(sortOrder)) {
@@ -2369,25 +2371,6 @@
                 break;
             }
 
-            case PHONES_WITH_PRESENCE: {
-                long phoneMimeType = mOpenHelper.getMimeTypeId(Phone.CONTENT_ITEM_TYPE);
-                qb.setTables(Tables.PRESENCE
-                        + " JOIN " + mOpenHelper.getRawContactView() + " raw_contacts"
-                                + " ON (" + Presence.RAW_CONTACT_ID
-                                        + "=" + RawContactsColumns.CONCRETE_ID + ")"
-                        + " JOIN " + Tables.CONTACTS
-                                + " ON (" + ContactsColumns.CONCRETE_ID
-                                        + "=" + RawContacts.CONTACT_ID + ")"
-                        + " JOIN " + Tables.DATA
-                                + " ON (" + DataColumns.CONCRETE_RAW_CONTACT_ID
-                                        + "=" + Presence.RAW_CONTACT_ID
-                                        + " AND " + DataColumns.MIMETYPE_ID + "=" + phoneMimeType
-                                        + " AND " + Phone.DATA + " NOT NULL" + ")");
-                qb.setProjectionMap(sPhonesWithPresenceProjectionMap);
-                groupBy = RawContacts.CONTACT_ID + "," + DataColumns.CONCRETE_ID;
-                break;
-            }
-
             default:
                 return mLegacyApiSupport.query(uri, projection, selection, selectionArgs,
                         sortOrder, limit);
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index fea02b0..46b7a2f 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -22,6 +22,7 @@
 import android.content.Entity;
 import android.content.EntityIterator;
 import android.database.Cursor;
+import android.database.DatabaseUtils;
 import android.net.Uri;
 import android.os.RemoteException;
 import android.provider.ContactsContract;
@@ -364,10 +365,10 @@
         insertPresence(Im.PROTOCOL_GOOGLE_TALK, "goog412@acme.com", Presence.AVAILABLE);
         long contactId = queryContactId(rawContactId);
 
-        Uri uri = Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "phones_with_presence");
+        Uri uri = Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "data_with_presence");
 
-        Cursor c = mResolver.query(uri, null,
-                RawContacts.CONTACT_ID + "=" + contactId, null, Phone.NUMBER);
+        Cursor c = mResolver.query(uri, null, RawContacts.CONTACT_ID + "=" + contactId + " AND "
+                + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null, Phone.NUMBER);
         assertEquals(2, c.getCount());
 
         c.moveToFirst();
@@ -377,7 +378,7 @@
         values.put(Contacts.DISPLAY_NAME, "John Doe");
         values.put(Phone.NUMBER, "18004664411");
         values.putNull(Phone.LABEL);
-        values.put(Contacts._ID, contactId);
+        values.put(RawContacts.CONTACT_ID, contactId);
         assertCursorValues(c, values);
 
         c.moveToNext();
@@ -387,7 +388,7 @@
         values.put(Contacts.DISPLAY_NAME, "John Doe");
         values.put(Phone.NUMBER, "18004664412");
         values.putNull(Phone.LABEL);
-        values.put(Contacts._ID, contactId);
+        values.put(RawContacts.CONTACT_ID, contactId);
         assertCursorValues(c, values);
 
         c.close();