Merge from master (21282 and 21283)
diff --git a/src/com/android/providers/contacts/LegacyContactImporter.java b/src/com/android/providers/contacts/LegacyContactImporter.java
index 42c419b..7d06a7d 100644
--- a/src/com/android/providers/contacts/LegacyContactImporter.java
+++ b/src/com/android/providers/contacts/LegacyContactImporter.java
@@ -257,6 +257,42 @@
     private interface PeopleQuery {
         String TABLE = "people";
 
+        String NAME_SQL =
+                "(CASE WHEN (name IS NOT NULL AND name != '') "
+                    + "THEN name "
+                + "ELSE "
+                    + "(CASE WHEN primary_organization is NOT NULL THEN "
+                        + "(SELECT company FROM organizations WHERE "
+                            + "organizations._id = primary_organization) "
+                    + "ELSE "
+                        + "(CASE WHEN primary_phone IS NOT NULL THEN "
+                            +"(SELECT number FROM phones WHERE phones._id = primary_phone) "
+                        + "ELSE "
+                            + "(CASE WHEN primary_email IS NOT NULL THEN "
+                                + "(SELECT data FROM contact_methods WHERE "
+                                    + "contact_methods._id = primary_email) "
+                            + "ELSE "
+                                + "null "
+                            + "END) "
+                        + "END) "
+                    + "END) "
+                + "END) ";
+
+
+        String[] COLUMNS_WITH_DISPLAY_NAME_WITHOUT_PHONETIC_NAME = {
+                "_id", NAME_SQL, "notes", "times_contacted", "last_time_contacted", "starred",
+                "primary_phone", "primary_organization", "primary_email", "custom_ringtone",
+                "send_to_voicemail", "_sync_account", "_sync_id", "_sync_time", "_sync_local_id",
+                "_sync_dirty",
+        };
+
+        String[] COLUMNS_WITH_DISPLAY_NAME_WITH_PHONETIC_NAME = {
+                "_id", NAME_SQL, "notes", "times_contacted", "last_time_contacted", "starred",
+                "primary_phone", "primary_organization", "primary_email", "custom_ringtone",
+                "send_to_voicemail", "_sync_account", "_sync_id", "_sync_time", "_sync_local_id",
+                "_sync_dirty", "phonetic_name",
+        };
+
         String[] COLUMNS_WITHOUT_PHONETIC_NAME = {
                 "_id", "name", "notes", "times_contacted", "last_time_contacted", "starred",
                 "primary_phone", "primary_organization", "primary_email", "custom_ringtone",
@@ -364,10 +400,25 @@
         SQLiteStatement structuredNameInsert =
                 mTargetDb.compileStatement(StructuredNameInsert.INSERT_SQL);
         SQLiteStatement noteInsert = mTargetDb.compileStatement(NoteInsert.INSERT_SQL);
-        String[] columns = mPhoneticNameAvailable ? PeopleQuery.COLUMNS_WITH_PHONETIC_NAME :
-            PeopleQuery.COLUMNS_WITHOUT_PHONETIC_NAME;
-        Cursor c = mSourceDb.query(PeopleQuery.TABLE, columns, null, null, null, null,
-                null);
+
+        String[] columns = mPhoneticNameAvailable
+                ? PeopleQuery.COLUMNS_WITH_DISPLAY_NAME_WITH_PHONETIC_NAME
+                : PeopleQuery.COLUMNS_WITH_DISPLAY_NAME_WITHOUT_PHONETIC_NAME;
+        Cursor c =
+                mSourceDb.query(PeopleQuery.TABLE, columns, "name IS NULL", null, null, null, null);
+        try {
+            while (c.moveToNext()) {
+                insertRawContact(c, rawContactInsert);
+                insertNote(c, noteInsert);
+            }
+        } finally {
+            c.close();
+        }
+
+        columns = mPhoneticNameAvailable
+                ? PeopleQuery.COLUMNS_WITH_PHONETIC_NAME
+                : PeopleQuery.COLUMNS_WITHOUT_PHONETIC_NAME;
+        c = mSourceDb.query(PeopleQuery.TABLE, columns, "name IS NOT NULL", null, null, null, null);
         try {
             while (c.moveToNext()) {
                 insertRawContact(c, rawContactInsert);
diff --git a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
index 04eb949..e57f1ee 100644
--- a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
@@ -19,6 +19,7 @@
 
 import android.accounts.Account;
 import android.content.ContentProvider;
+import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
@@ -44,6 +45,7 @@
 import android.test.AndroidTestCase;
 import android.test.mock.MockContentResolver;
 import android.test.suitebuilder.annotation.LargeTest;
+import android.util.Log;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -627,4 +629,63 @@
         }
         return mTestPhoto;
     }
+
+    public static void dump(ContentResolver resolver, boolean aggregatedOnly) {
+        String[] projection = new String[] {
+                Contacts._ID,
+                Contacts.DISPLAY_NAME
+        };
+        String selection = null;
+        if (aggregatedOnly) {
+            selection = Contacts._ID
+                    + " IN (SELECT contact_id" +
+                    		" FROM raw_contacts GROUP BY contact_id HAVING count(*) > 1)";
+        }
+
+        Cursor c = resolver.query(Contacts.CONTENT_URI, projection, selection, null,
+                Contacts.DISPLAY_NAME);
+        while(c.moveToNext()) {
+            long contactId = c.getLong(0);
+            Log.i("Contact   ", String.format("%5d %s", contactId, c.getString(1)));
+            dumpRawContacts(resolver, contactId);
+            Log.i("          ", ".");
+        }
+        c.close();
+    }
+
+    private static void dumpRawContacts(ContentResolver resolver, long contactId) {
+        String[] projection = new String[] {
+                RawContacts._ID,
+        };
+        Cursor c = resolver.query(RawContacts.CONTENT_URI, projection, RawContacts.CONTACT_ID + "="
+                + contactId, null, null);
+        while(c.moveToNext()) {
+            long rawContactId = c.getLong(0);
+            Log.i("RawContact", String.format("      %-5d", rawContactId));
+            dumpData(resolver, rawContactId);
+        }
+        c.close();
+    }
+
+    private static void dumpData(ContentResolver resolver, long rawContactId) {
+        String[] projection = new String[] {
+                Data.MIMETYPE,
+                Data.DATA1,
+                Data.DATA2,
+                Data.DATA3,
+        };
+        Cursor c = resolver.query(Data.CONTENT_URI, projection, Data.RAW_CONTACT_ID + "="
+                + rawContactId, null, Data.MIMETYPE);
+        while(c.moveToNext()) {
+            String mimetype = c.getString(0);
+            if (Photo.CONTENT_ITEM_TYPE.equals(mimetype)) {
+                Log.i("Photo     ", "");
+            } else {
+                mimetype = mimetype.substring(mimetype.indexOf('/') + 1);
+                Log.i("Data      ", String.format("            %-10s %s,%s,%s", mimetype,
+                        c.getString(1), c.getString(2), c.getString(3)));
+            }
+        }
+        c.close();
+    }
 }