Add support for multiple items in a single mime type.

Bug: 10513085
Change-Id: I226fc4994f6e56a2da304749e770e94ef053d6e0
diff --git a/src/com/android/contacts/model/ContactLoader.java b/src/com/android/contacts/model/ContactLoader.java
index 8583f14..28b1e5a 100644
--- a/src/com/android/contacts/model/ContactLoader.java
+++ b/src/com/android/contacts/model/ContactLoader.java
@@ -55,6 +55,7 @@
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -405,23 +406,18 @@
         final Iterator keys = items.keys();
         while (keys.hasNext()) {
             final String mimetype = (String) keys.next();
-            final JSONObject item = items.getJSONObject(mimetype);
 
-            final ContentValues itemValues = new ContentValues();
-            itemValues.put(Data.MIMETYPE, mimetype);
-            itemValues.put(Data._ID, -1);
-
-            final Iterator iterator = item.keys();
-            while (iterator.hasNext()) {
-                String name = (String) iterator.next();
-                final Object o = item.get(name);
-                if (o instanceof String) {
-                    itemValues.put(name, (String) o);
-                } else if (o instanceof Integer) {
-                    itemValues.put(name, (Integer) o);
+            // Could be single object or array.
+            final JSONObject obj = items.optJSONObject(mimetype);
+            if (obj == null) {
+                final JSONArray array = items.getJSONArray(mimetype);
+                for (int i = 0; i < array.length(); i++) {
+                    final JSONObject item = array.getJSONObject(i);
+                    processOneRecord(rawContact, item, mimetype);
                 }
+            } else {
+                processOneRecord(rawContact, obj, mimetype);
             }
-            rawContact.addDataItemValues(itemValues);
         }
 
         contact.setRawContacts(new ImmutableList.Builder<RawContact>()
@@ -430,6 +426,25 @@
         return contact;
     }
 
+    private void processOneRecord(RawContact rawContact, JSONObject item, String mimetype)
+            throws JSONException {
+        final ContentValues itemValues = new ContentValues();
+        itemValues.put(Data.MIMETYPE, mimetype);
+        itemValues.put(Data._ID, -1);
+
+        final Iterator iterator = item.keys();
+        while (iterator.hasNext()) {
+            String name = (String) iterator.next();
+            final Object o = item.get(name);
+            if (o instanceof String) {
+                itemValues.put(name, (String) o);
+            } else if (o instanceof Integer) {
+                itemValues.put(name, (Integer) o);
+            }
+        }
+        rawContact.addDataItemValues(itemValues);
+    }
+
     private Contact loadContactEntity(ContentResolver resolver, Uri contactUri) {
         Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY);
         Cursor cursor = resolver.query(entityUri, ContactQuery.COLUMNS, null, null,