Create meeting invite text when none exists

Bug: 6497393
Change-Id: I47e639da81dea352cd8d37b78e3f8f9e3555feea
diff --git a/exchange2/src/com/android/exchange/adapter/EmailSyncAdapter.java b/exchange2/src/com/android/exchange/adapter/EmailSyncAdapter.java
index 5a6523c..b419f23 100644
--- a/exchange2/src/com/android/exchange/adapter/EmailSyncAdapter.java
+++ b/exchange2/src/com/android/exchange/adapter/EmailSyncAdapter.java
@@ -25,6 +25,9 @@
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.RemoteException;
+import android.provider.CalendarContract.Events;
+import android.text.Html;
+import android.text.SpannedString;
 import android.text.TextUtils;
 import android.util.Base64;
 import android.util.Log;
@@ -52,6 +55,7 @@
 import com.android.emailcommon.service.SyncWindow;
 import com.android.emailcommon.utility.AttachmentUtilities;
 import com.android.emailcommon.utility.ConversionUtilities;
+import com.android.emailcommon.utility.TextUtilities;
 import com.android.emailcommon.utility.Utility;
 import com.android.exchange.CommandStatusException;
 import com.android.exchange.Eas;
@@ -607,6 +611,39 @@
             if (atts.size() > 0) {
                 msg.mAttachments = atts;
             }
+
+            if ((msg.mFlags & Message.FLAG_INCOMING_MEETING_MASK) != 0) {
+                String text = TextUtilities.makeSnippetFromHtmlText(
+                        msg.mText != null ? msg.mText : msg.mHtml);
+                if (TextUtils.isEmpty(text)) {
+                    // Create text for this invitation
+                    String meetingInfo = msg.mMeetingInfo;
+                    if (!TextUtils.isEmpty(meetingInfo)) {
+                        PackedString ps = new PackedString(meetingInfo);
+                        ContentValues values = new ContentValues();
+                        putFromMeeting(ps, MeetingInfo.MEETING_LOCATION, values,
+                                Events.EVENT_LOCATION);
+                        String dtstart = ps.get(MeetingInfo.MEETING_DTSTART);
+                        if (!TextUtils.isEmpty(dtstart)) {
+                            long startTime = Utility.parseEmailDateTimeToMillis(dtstart);
+                            values.put(Events.DTSTART, startTime);
+                        }
+                        putFromMeeting(ps, MeetingInfo.MEETING_ALL_DAY, values,
+                                Events.ALL_DAY);
+                        msg.mText = CalendarUtilities.buildMessageTextFromEntityValues(
+                                mContext, values, null);
+                        msg.mHtml = Html.toHtml(new SpannedString(msg.mText));
+                    }
+                }
+            }
+        }
+
+        private void putFromMeeting(PackedString ps, String field, ContentValues values,
+                String column) {
+            String val = ps.get(field);
+            if (!TextUtils.isEmpty(val)) {
+                values.put(column, val);
+            }
         }
 
         /**
@@ -648,6 +685,11 @@
                     case Tags.EMAIL_RESPONSE_REQUESTED:
                         packedString.put(MeetingInfo.MEETING_RESPONSE_REQUESTED, getValue());
                         break;
+                    case Tags.EMAIL_ALL_DAY_EVENT:
+                        if (getValueInt() == 1) {
+                            packedString.put(MeetingInfo.MEETING_ALL_DAY, getValue());
+                        }
+                        break;
                     default:
                         skipTag();
                 }