Add organizer to Attendees before handling exceptions

* Exceptions take a copy of Attendees when they are created, but
  we weren't adding the organizer to Attendees before exceptions
  were parsed
* Fix this by adding the organizer just before exception parsing
* If there aren't any exceptions, we add the organizer as before

Bug: 2585817
Change-Id: Ie894682977e38a55d975135c8fc2fd8f2d4b1365
diff --git a/src/com/android/exchange/adapter/CalendarSyncAdapter.java b/src/com/android/exchange/adapter/CalendarSyncAdapter.java
index eff9b51..2126b7b 100644
--- a/src/com/android/exchange/adapter/CalendarSyncAdapter.java
+++ b/src/com/android/exchange/adapter/CalendarSyncAdapter.java
@@ -244,6 +244,27 @@
                     new String[] {mAccount.mEmailAddress, Email.EXCHANGE_ACCOUNT_MANAGER_TYPE});
         }
 
+        private void addOrganizerToAttendees(CalendarOperations ops, long eventId,
+                String organizerName, String organizerEmail) {
+            // Handle the organizer (who IS an attendee on device, but NOT in EAS)
+            if (organizerName != null || organizerEmail != null) {
+                ContentValues attendeeCv = new ContentValues();
+                if (organizerName != null) {
+                    attendeeCv.put(Attendees.ATTENDEE_NAME, organizerName);
+                }
+                if (organizerEmail != null) {
+                    attendeeCv.put(Attendees.ATTENDEE_EMAIL, organizerEmail);
+                }
+                attendeeCv.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ORGANIZER);
+                attendeeCv.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_REQUIRED);
+                if (eventId < 0) {
+                    ops.newAttendee(attendeeCv);
+                } else {
+                    ops.updatedAttendee(attendeeCv, eventId);
+                }
+            }
+        }
+
         public void addEvent(CalendarOperations ops, String serverId, boolean update)
                 throws IOException {
             ContentValues cv = new ContentValues();
@@ -269,6 +290,7 @@
             ArrayList<ContentValues> attendeeValues = new ArrayList<ContentValues>();
             int reminderMins = -1;
             String dtStamp = null;
+            boolean organizerAdded = false;
 
             while (nextTag(Tags.SYNC_APPLICATION_DATA) != END) {
                 if (update && firstTag) {
@@ -345,6 +367,10 @@
                         endTime = Utility.parseDateTimeToMillis(getValue());
                         break;
                     case Tags.CALENDAR_EXCEPTIONS:
+                        // For exceptions to show the organizer, the organizer must be added before
+                        // we call exceptionsParser
+                        addOrganizerToAttendees(ops, eventId, organizerName, organizerEmail);
+                        organizerAdded = true;
                         exceptionsParser(ops, cv, attendeeValues, reminderMins);
                         break;
                     case Tags.CALENDAR_LOCATION:
@@ -399,22 +425,9 @@
                 }
             }
 
-            // Handle the organizer (who IS an attendee on device, but NOT in EAS)
-            if (organizerName != null || organizerEmail != null) {
-                ContentValues attendeeCv = new ContentValues();
-                if (organizerName != null) {
-                    attendeeCv.put(Attendees.ATTENDEE_NAME, organizerName);
-                }
-                if (organizerEmail != null) {
-                    attendeeCv.put(Attendees.ATTENDEE_EMAIL, organizerEmail);
-                }
-                attendeeCv.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ORGANIZER);
-                attendeeCv.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_REQUIRED);
-                if (eventId < 0) {
-                    ops.newAttendee(attendeeCv);
-                } else {
-                    ops.updatedAttendee(attendeeCv, eventId);
-                }
+            // If we haven't added the organizer to attendees, do it now
+            if (!organizerAdded) {
+                addOrganizerToAttendees(ops, eventId, organizerName, organizerEmail);
             }
 
             // Store email addresses of attendees (in a tokenizable string) in ExtendedProperties