Fix NPE for missing data types

Fix NPEs when missing postal, phone, or relation types.

Bug:14436779
Change-Id: I9bf4aafc41a504cb370be583880c8ae4d5e0f080
diff --git a/src/com/android/exchange/eas/EasSyncContacts.java b/src/com/android/exchange/eas/EasSyncContacts.java
index 3836588..ed2b07e 100644
--- a/src/com/android/exchange/eas/EasSyncContacts.java
+++ b/src/com/android/exchange/eas/EasSyncContacts.java
@@ -499,7 +499,9 @@
     private static void sendPhone(final Serializer s, final ContentValues cv, final int workCount,
             final int homeCount) throws IOException {
         final String value = cv.getAsString(Phone.NUMBER);
-        if (value == null) return;
+        if (value == null || !cv.containsKey(Phone.TYPE)) {
+            return;
+        }
         switch (cv.getAsInteger(Phone.TYPE)) {
             case Phone.TYPE_WORK:
                 if (workCount < MAX_PHONE_ROWS) {
@@ -552,7 +554,9 @@
     private static void sendRelation(final Serializer s, final ContentValues cv)
             throws IOException {
         final String value = cv.getAsString(Relation.DATA);
-        if (value == null) return;
+        if (value == null || !cv.containsKey(Relation.TYPE)) {
+            return;
+        }
         switch (cv.getAsInteger(Relation.TYPE)) {
             case Relation.TYPE_ASSISTANT:
                 s.data(Tags.CONTACTS_ASSISTANT_NAME, value);
@@ -610,7 +614,10 @@
      * @throws IOException
      */
     private static void sendStructuredPostal(final Serializer s, final ContentValues cv)
-            throws IOException {
+        throws IOException {
+        if (!cv.containsKey(StructuredPostal.TYPE)) {
+            return;
+        }
         switch (cv.getAsInteger(StructuredPostal.TYPE)) {
             case StructuredPostal.TYPE_HOME:
                 sendOnePostal(s, cv, HOME_ADDRESS_TAGS);
@@ -890,9 +897,14 @@
                         hasSetFileAs = trySendFileAs(s, cv);
                     } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
                         sendPhone(s, cv, workPhoneCount, homePhoneCount);
-                        int type = cv.getAsInteger(Phone.TYPE);
-                        if (type == Phone.TYPE_HOME) homePhoneCount++;
-                        if (type == Phone.TYPE_WORK) workPhoneCount++;
+                        if (cv.containsKey(Phone.TYPE)) {
+                            final int type = cv.getAsInteger(Phone.TYPE);
+                            if (type == Phone.TYPE_HOME) {
+                                homePhoneCount++;
+                            } else if (type == Phone.TYPE_WORK) {
+                                workPhoneCount++;
+                            }
+                        }
                     } else if (mimeType.equals(Relation.CONTENT_ITEM_TYPE)) {
                         sendRelation(s, cv);
                     } else if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
@@ -906,9 +918,12 @@
                     } else if (mimeType.equals(Im.CONTENT_ITEM_TYPE)) {
                         sendIm(s, cv, imCount++);
                     } else if (mimeType.equals(Event.CONTENT_ITEM_TYPE)) {
-                        Integer eventType = cv.getAsInteger(Event.TYPE);
-                        if (eventType != null && eventType.equals(Event.TYPE_BIRTHDAY)) {
-                            sendBirthday(s, cv);
+                        if (cv.containsKey(Event.TYPE)) {
+                            final Integer eventType = cv.getAsInteger(Event.TYPE);
+                            if (eventType != null &&
+                                    eventType.equals(Event.TYPE_BIRTHDAY)) {
+                                sendBirthday(s, cv);
+                            }
                         }
                     } else if (mimeType.equals(GroupMembership.CONTENT_ITEM_TYPE)) {
                         // We must gather these, and send them together (below)