Fixed end date in multiday events where it be off by a day

Bug: 5819365
Change-Id: I0b7ee7a5c7c73261b2cf38bd1b6d80a74647c8d6
diff --git a/src/com/android/calendar/EventInfoFragment.java b/src/com/android/calendar/EventInfoFragment.java
index 89f8988..a67be9d 100644
--- a/src/com/android/calendar/EventInfoFragment.java
+++ b/src/com/android/calendar/EventInfoFragment.java
@@ -18,6 +18,7 @@
 
 import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
 import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
+import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
 import static com.android.calendar.CalendarController.EVENT_EDIT_ON_LAUNCH;
 
 import com.android.calendar.CalendarController.EventInfo;
@@ -245,6 +246,7 @@
 
     private long mStartMillis;
     private long mEndMillis;
+    private boolean mAllDay;
 
     private boolean mHasAttendeeData;
     private boolean mIsOrganizer;
@@ -814,6 +816,7 @@
                 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
                 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, mStartMillis);
                 intent.putExtra(EXTRA_EVENT_END_TIME, mEndMillis);
+                intent.putExtra(EXTRA_EVENT_ALL_DAY, mAllDay);
                 intent.setClass(mActivity, EditEventActivity.class);
                 intent.putExtra(EVENT_EDIT_ON_LAUNCH, true);
                 startActivity(intent);
@@ -1005,7 +1008,7 @@
             eventName = getActivity().getString(R.string.no_title_label);
         }
 
-        boolean allDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
+        mAllDay = mEventCursor.getInt(EVENT_INDEX_ALL_DAY) != 0;
         String location = mEventCursor.getString(EVENT_INDEX_EVENT_LOCATION);
         String description = mEventCursor.getString(EVENT_INDEX_DESCRIPTION);
         String rRule = mEventCursor.getString(EVENT_INDEX_RRULE);
@@ -1036,7 +1039,7 @@
             EventRecurrence eventRecurrence = new EventRecurrence();
             eventRecurrence.parse(rRule);
             Time date = new Time(Utils.getTimeZone(getActivity(), mTZUpdater));
-            if (allDay) {
+            if (mAllDay) {
                 date.timezone = Time.TIMEZONE_UTC;
             }
             date.set(mStartMillis);
@@ -1045,7 +1048,7 @@
                     getActivity().getResources(), eventRecurrence);
         }
         // If an all day event , show the date without the time
-        if (allDay) {
+        if (mAllDay) {
             Formatter f = new Formatter(new StringBuilder(50), Locale.getDefault());
             whenDate = DateUtils.formatDateRange(getActivity(), f, mStartMillis, mEndMillis,
                     flagsDate, Time.TIMEZONE_UTC).toString();
@@ -1073,7 +1076,7 @@
                 String displayName;
                 // Figure out if this is in DST
                 Time date = new Time(Utils.getTimeZone(getActivity(), mTZUpdater));
-                if (allDay) {
+                if (mAllDay) {
                     date.timezone = Time.TIMEZONE_UTC;
                 }
                 date.set(mStartMillis);
diff --git a/src/com/android/calendar/agenda/AgendaListView.java b/src/com/android/calendar/agenda/AgendaListView.java
index 1cecb95..532fdad 100644
--- a/src/com/android/calendar/agenda/AgendaListView.java
+++ b/src/com/android/calendar/agenda/AgendaListView.java
@@ -202,9 +202,15 @@
 
             if (event != null && (oldInstanceId != mWindowAdapter.getSelectedInstanceId() ||
                     !mShowEventDetailsWithAgenda)) {
+                long startTime = event.begin;
+                long endTime = event.end;
+                if (event.allDay) {
+                    startTime = Utils.convertAlldayLocalToUTC(mTime, startTime, mTimeZone);
+                    endTime = Utils.convertAlldayLocalToUTC(mTime, endTime, mTimeZone);
+                }
                 CalendarController controller = CalendarController.getInstance(mContext);
-                controller.sendEventRelatedEvent(this, EventType.VIEW_EVENT, event.id, event.begin,
-                        event.end, 0, 0, controller.getTime());
+                controller.sendEventRelatedEvent(this, EventType.VIEW_EVENT, event.id, startTime,
+                        endTime, 0, 0, controller.getTime());
             }
         }
     }
diff --git a/src/com/android/calendar/agenda/AgendaWindowAdapter.java b/src/com/android/calendar/agenda/AgendaWindowAdapter.java
index 701db29..23d640f 100644
--- a/src/com/android/calendar/agenda/AgendaWindowAdapter.java
+++ b/src/com/android/calendar/agenda/AgendaWindowAdapter.java
@@ -42,7 +42,6 @@
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
-import android.widget.AbsListView;
 import android.widget.AbsListView.OnScrollListener;
 import android.widget.BaseAdapter;
 import android.widget.TextView;
@@ -279,6 +278,7 @@
         long end;
         long id;
         int startDay;
+        boolean allDay;
     }
 
     static class DayAdapterInfo {
@@ -598,8 +598,8 @@
         event.end = cursor.getLong(AgendaWindowAdapter.INDEX_END);
         event.startDay = cursor.getInt(AgendaWindowAdapter.INDEX_START_DAY);
 
-        boolean allDay = cursor.getInt(AgendaWindowAdapter.INDEX_ALL_DAY) != 0;
-        if (allDay) { // UTC
+        event.allDay = cursor.getInt(AgendaWindowAdapter.INDEX_ALL_DAY) != 0;
+        if (event.allDay) { // UTC
             Time time = new Time(mTimeZone);
             time.setJulianDay(Time.getJulianDay(event.begin, 0));
             event.begin = time.toMillis(false /* use isDst */);
@@ -613,7 +613,7 @@
         }
 
         if (!isDayHeader) {
-            if (allDay) {
+            if (event.allDay) {
                 Time time = new Time(mTimeZone);
                 time.setJulianDay(Time.getJulianDay(event.end, 0));
                 event.end = time.toMillis(false /* use isDst */);
diff --git a/src/com/android/calendar/event/EditEventActivity.java b/src/com/android/calendar/event/EditEventActivity.java
index 1fdbbc5..01a71b7 100644
--- a/src/com/android/calendar/event/EditEventActivity.java
+++ b/src/com/android/calendar/event/EditEventActivity.java
@@ -108,19 +108,27 @@
             eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
         }
 
+        boolean allDay = intent.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false);
+
         long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
         long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
         if (end != -1) {
             info.endTime = new Time();
+            if (allDay) {
+                info.endTime.timezone = Time.TIMEZONE_UTC;
+            }
             info.endTime.set(end);
         }
         if (begin != -1) {
             info.startTime = new Time();
+            if (allDay) {
+                info.startTime.timezone = Time.TIMEZONE_UTC;
+            }
             info.startTime.set(begin);
         }
         info.id = eventId;
 
-        if (intent.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false)) {
+        if (allDay) {
             info.extraLong = CalendarController.EXTRA_CREATE_ALL_DAY;
         } else {
             info.extraLong = 0;
diff --git a/src/com/android/calendar/event/EditEventView.java b/src/com/android/calendar/event/EditEventView.java
index 71c992c..d07e1e4 100644
--- a/src/com/android/calendar/event/EditEventView.java
+++ b/src/com/android/calendar/event/EditEventView.java
@@ -190,6 +190,7 @@
     private Time mStartTime;
     private Time mEndTime;
     private String mTimezone;
+    private boolean mAllDay = false;
     private int mModification = EditEventHelper.MODIFY_UNINITIALIZED;
 
     private EventRecurrence mEventRecurrence = new EventRecurrence();
@@ -1051,6 +1052,7 @@
         });
 
         boolean prevAllDay = mAllDayCheckBox.isChecked();
+        mAllDay = false; // default to false. Let setAllDayViewsVisibility update it as needed
         if (model.mAllDay) {
             mAllDayCheckBox.setChecked(true);
             // put things back in local time for all day events
@@ -1563,7 +1565,10 @@
     protected void setAllDayViewsVisibility(boolean isChecked) {
         if (isChecked) {
             if (mEndTime.hour == 0 && mEndTime.minute == 0) {
-                mEndTime.monthDay--;
+                if (mAllDay != isChecked) {
+                    mEndTime.monthDay--;
+                }
+
                 long endMillis = mEndTime.normalize(true);
 
                 // Do not allow an event to have an end time
@@ -1582,7 +1587,10 @@
             mTimezoneRow.setVisibility(View.GONE);
         } else {
             if (mEndTime.hour == 0 && mEndTime.minute == 0) {
-                mEndTime.monthDay++;
+                if (mAllDay != isChecked) {
+                    mEndTime.monthDay++;
+                }
+
                 long endMillis = mEndTime.normalize(true);
                 setDate(mEndDateButton, endMillis);
                 setTime(mEndTimeButton, endMillis);
@@ -1591,6 +1599,7 @@
             mEndTimeButton.setVisibility(View.VISIBLE);
             mTimezoneRow.setVisibility(View.VISIBLE);
         }
+        mAllDay = isChecked;
         updateHomeTime();
     }