Add meetingInfo column to Message; use for meeting invites

* Added a meetingInfo column to the Message database
* When a meeting invite is received, the start time is stored here
  in ms from start of epoch.  Note that this field is defined to be
  a String, for extensibility
* Update ProviderTests

Change-Id: If44892d27ccc5ebdc1f8667befafb8b8a27a2cf4
diff --git a/src/com/android/exchange/adapter/EmailSyncAdapter.java b/src/com/android/exchange/adapter/EmailSyncAdapter.java
index c835f85..84e845e 100644
--- a/src/com/android/exchange/adapter/EmailSyncAdapter.java
+++ b/src/com/android/exchange/adapter/EmailSyncAdapter.java
@@ -32,6 +32,7 @@
 import com.android.email.service.MailService;
 import com.android.exchange.Eas;
 import com.android.exchange.EasSyncService;
+import com.android.exchange.utility.CalendarUtilities;
 
 import android.content.ContentProviderOperation;
 import android.content.ContentResolver;
@@ -139,16 +140,7 @@
                         msg.mReplyTo = Address.pack(Address.parse(getValue()));
                         break;
                     case Tags.EMAIL_DATE_RECEIVED:
-                        String date = getValue();
-                        // 2009-02-11T18:03:03.627Z
-                        GregorianCalendar cal = new GregorianCalendar();
-                        cal.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date
-                                .substring(5, 7)) - 1, Integer.parseInt(date.substring(8, 10)),
-                                Integer.parseInt(date.substring(11, 13)), Integer.parseInt(date
-                                        .substring(14, 16)), Integer.parseInt(date
-                                                .substring(17, 19)));
-                        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
-                        msg.mTimeStamp = cal.getTimeInMillis();
+                        msg.mTimeStamp = CalendarUtilities.parseEmailDateTimeToMillis(getValue());
                         break;
                     case Tags.EMAIL_SUBJECT:
                         msg.mSubject = getValue();
@@ -174,6 +166,9 @@
                             msg.mFlags |= Message.FLAG_MEETING_CANCEL_NOTICE;
                         }
                         break;
+                    case Tags.EMAIL_MEETING_REQUEST:
+                        meetingRequestParser(msg);
+                        break;
                     default:
                         skipTag();
                 }
@@ -184,6 +179,43 @@
             }
         }
 
+        private void meetingRequestParser(Message msg) throws IOException {
+            while (nextTag(Tags.EMAIL_MEETING_REQUEST) != END) {
+                switch (tag) {
+                    case Tags.EMAIL_START_TIME:
+                        // For now, we'll just save the time in millis as a String
+                        msg.mMeetingInfo =
+                            Long.toString(CalendarUtilities.parseEmailDateTimeToMillis(getValue()));
+                        break;
+                    case Tags.EMAIL_CATEGORIES:
+                        nullParser();
+                        break;
+                    case Tags.EMAIL_RECURRENCES:
+                        recurrencesParser();
+                        break;
+                    default:
+                        skipTag();
+                }
+            }
+        }
+
+        private void nullParser() throws IOException {
+            while (nextTag(Tags.EMAIL_CATEGORIES) != END) {
+                skipTag();
+            }
+        }
+
+        private void recurrencesParser() throws IOException {
+            while (nextTag(Tags.EMAIL_RECURRENCES) != END) {
+                switch (tag) {
+                    case Tags.EMAIL_RECURRENCE:
+                        nullParser();
+                    default:
+                        skipTag();
+                }
+            }
+        }
+
         private void addParser(ArrayList<Message> emails) throws IOException {
             Message msg = new Message();
             msg.mAccountKey = mAccount.mId;
diff --git a/src/com/android/exchange/utility/CalendarUtilities.java b/src/com/android/exchange/utility/CalendarUtilities.java
index 7360c50..655b588 100644
--- a/src/com/android/exchange/utility/CalendarUtilities.java
+++ b/src/com/android/exchange/utility/CalendarUtilities.java
@@ -451,6 +451,21 @@
     }
 
     /**
+     * Generate a time in milliseconds from an email date string that represents a date/time in GMT
+     * @param Email style DateTime string from Exchange server
+     * @return the time in milliseconds (since Jan 1, 1970)
+     */
+    static public long parseEmailDateTimeToMillis(String date) {
+        // Format for email date strings is 2010-02-23T16:00:00.000Z
+        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)),
+                Integer.parseInt(date.substring(5, 7)) - 1, Integer.parseInt(date.substring(8, 10)),
+                Integer.parseInt(date.substring(11, 13)), Integer.parseInt(date.substring(14, 16)),
+                Integer.parseInt(date.substring(17, 19)));
+        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
+        return cal.getTimeInMillis();
+    }
+
+    /**
      * Generate a time in milliseconds from a date string that represents a date/time in GMT
      * @param DateTime string from Exchange server
      * @return the time in milliseconds (since Jan 1, 1970)
diff --git a/tests/src/com/android/exchange/utility/CalendarUtilitiesTests.java b/tests/src/com/android/exchange/utility/CalendarUtilitiesTests.java
index 6437309..e8155a8 100644
--- a/tests/src/com/android/exchange/utility/CalendarUtilitiesTests.java
+++ b/tests/src/com/android/exchange/utility/CalendarUtilitiesTests.java
@@ -18,6 +18,8 @@
 
 import android.test.AndroidTestCase;
 
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 import java.util.TimeZone;
 
 /**
@@ -101,6 +103,35 @@
                 CalendarUtilities.recurrenceUntilToEasUntil("YYYYMMDD"));
     }
 
+    public void testParseEmailDateTimeToMillis(String date) {
+        // Format for email date strings is 2010-02-23T16:00:00.000Z
+        String dateString = "2010-02-23T15:16:17.000Z";
+        long dateTime = CalendarUtilities.parseEmailDateTimeToMillis(dateString);
+        GregorianCalendar cal = new GregorianCalendar();
+        cal.setTimeInMillis(dateTime);
+        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
+        assertEquals(cal.get(Calendar.YEAR), 2010);
+        assertEquals(cal.get(Calendar.MONTH), 1);  // 0 based
+        assertEquals(cal.get(Calendar.DAY_OF_MONTH), 23);
+        assertEquals(cal.get(Calendar.HOUR_OF_DAY), 16);
+        assertEquals(cal.get(Calendar.MINUTE), 16);
+        assertEquals(cal.get(Calendar.SECOND), 17);
+    }
+
+    public void testParseDateTimeToMillis(String date) {
+        // Format for calendar date strings is 20100223T160000000Z
+        String dateString = "20100223T151617000Z";
+        long dateTime = CalendarUtilities.parseDateTimeToMillis(dateString);
+        GregorianCalendar cal = new GregorianCalendar();
+        cal.setTimeInMillis(dateTime);
+        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
+        assertEquals(cal.get(Calendar.YEAR), 2010);
+        assertEquals(cal.get(Calendar.MONTH), 1);  // 0 based
+        assertEquals(cal.get(Calendar.DAY_OF_MONTH), 23);
+        assertEquals(cal.get(Calendar.HOUR_OF_DAY), 16);
+        assertEquals(cal.get(Calendar.MINUTE), 16);
+        assertEquals(cal.get(Calendar.SECOND), 17);
+    }
 
     // Tests in progress...