Adds hometz display to EditEvent. Do not merge

If the event tz is different than the Calendar app's tz this will
display the start and end time's in the Calendar app's tz beneath
where they are set in EditEvent.

Change-Id: I32249920d30919e031d58ea9c9265a950f38456e
diff --git a/res/layout/edit_event.xml b/res/layout/edit_event.xml
index 26ceba8..aabd602 100644
--- a/res/layout/edit_event.xml
+++ b/res/layout/edit_event.xml
@@ -82,6 +82,31 @@
 
             </LinearLayout>
 
+            <LinearLayout
+                android:orientation="horizontal"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:paddingRight="3dip"
+                android:paddingLeft="3dip" >
+
+                <TextView android:id="@+id/start_date_home"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:gravity="left"
+                    android:visibility="gone"
+                    style="@style/TextAppearance.EditEvent_homeTime"/>
+
+                <TextView android:id="@+id/start_time_home"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:gravity="right"
+                    android:visibility="gone"
+                    style="@style/TextAppearance.EditEvent_homeTime"/>
+
+            </LinearLayout>
+
             <TextView android:id="@+id/to_label"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
@@ -110,6 +135,31 @@
                     style="?android:attr/textAppearanceMediumInverse"/>
             </LinearLayout>
 
+            <LinearLayout
+                android:orientation="horizontal"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:paddingRight="3dip"
+                android:paddingLeft="3dip" >
+
+                <TextView android:id="@+id/end_date_home"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:gravity="left"
+                    android:visibility="gone"
+                    style="@style/TextAppearance.EditEvent_homeTime"/>
+
+                <TextView android:id="@+id/end_time_home"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:gravity="right"
+                    android:visibility="gone"
+                    style="@style/TextAppearance.EditEvent_homeTime"/>
+
+            </LinearLayout>
+
             <TextView android:id="@+id/timezone_label"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 242c90a..62659f8 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -71,6 +71,12 @@
         <item name="android:paddingLeft">2dip</item>
     </style>
 
+    <style name="TextAppearance.EditEvent_homeTime">
+        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
+        <item name="android:textColor">@android:color/darker_gray</item>
+        <item name="android:paddingRight">2dip</item>
+    </style>
+
     <style name="EditEvent_Layout">
         <item name="android:paddingLeft">6dip</item>
         <item name="android:paddingRight">7dip</item>
diff --git a/src/com/android/calendar/EditEvent.java b/src/com/android/calendar/EditEvent.java
index 4410fe6..a212751 100644
--- a/src/com/android/calendar/EditEvent.java
+++ b/src/com/android/calendar/EditEvent.java
@@ -87,9 +87,11 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
+import java.util.Formatter;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.Locale;
 import java.util.TimeZone;
 
 public class EditEvent extends Activity implements View.OnClickListener,
@@ -221,6 +223,10 @@
     private TextView mDescriptionTextView;
     private TextView mTimezoneTextView;
     private TextView mTimezoneFooterView;
+    private TextView mStartTimeHome;
+    private TextView mStartDateHome;
+    private TextView mEndTimeHome;
+    private TextView mEndDateHome;
     private View mRemindersSeparator;
     private LinearLayout mRemindersContainer;
     private LinearLayout mExtraOptions;
@@ -268,6 +274,9 @@
     private DeleteEventHelper mDeleteEventHelper;
     private QueryHandler mQueryHandler;
 
+    private static StringBuilder mSB = new StringBuilder(50);
+    private static Formatter mF = new Formatter(mSB, Locale.getDefault());
+
     // This is here in case we need to update tz info later
     private Runnable mUpdateTZ = null;
 
@@ -318,6 +327,7 @@
             setDate(mEndDateButton, endMillis);
             setTime(mStartTimeButton, startMillis);
             setTime(mEndTimeButton, endMillis);
+            updateHomeTime();
         }
     }
 
@@ -389,6 +399,7 @@
             setDate(mStartDateButton, startMillis);
             setDate(mEndDateButton, endMillis);
             setTime(mEndTimeButton, endMillis); // In case end time had to be reset
+            updateHomeTime();
         }
     }
 
@@ -500,6 +511,7 @@
         } else if (dialog == mTimezoneDialog) {
             if (which >= 0 && which < mTimezoneAdapter.getCount()) {
                 setTimezone(which);
+                updateHomeTime();
                 dialog.dismiss();
             }
         }
@@ -627,7 +639,7 @@
 
         mStartTime = new Time();
         mEndTime = new Time();
-        mTimezone = Utils.getTimeZone(this, mUpdateTZ); //TimeZone.getDefault().getID();
+        mTimezone = Utils.getTimeZone(this, mUpdateTZ);
 
         Intent intent = getIntent();
         mUri = intent.getData();
@@ -734,6 +746,10 @@
         mEndDateButton = (Button) findViewById(R.id.end_date);
         mStartTimeButton = (Button) findViewById(R.id.start_time);
         mEndTimeButton = (Button) findViewById(R.id.end_time);
+        mStartTimeHome = (TextView) findViewById(R.id.start_time_home);
+        mStartDateHome = (TextView) findViewById(R.id.start_date_home);
+        mEndTimeHome = (TextView) findViewById(R.id.end_time_home);
+        mEndDateHome = (TextView) findViewById(R.id.end_date_home);
         mAllDayCheckBox = (CheckBox) findViewById(R.id.is_all_day);
         mTimezoneButton = (Button) findViewById(R.id.timezone);
         mCalendarsSpinner = (Spinner) findViewById(R.id.calendars);
@@ -785,6 +801,7 @@
                     mTimezoneButton.setVisibility(View.VISIBLE);
                     mTimezoneTextView.setVisibility(View.VISIBLE);
                 }
+                updateHomeTime();
             }
         });
 
@@ -1106,6 +1123,7 @@
         updateRemindersVisibility();
         populateWhen();
         populateTimezone();
+        updateHomeTime();
         populateRepeats();
     }
 
@@ -1218,6 +1236,76 @@
         setTimezone(mTimezoneAdapter.getRowById(mTimezone));
     }
 
+    /**
+     * Checks if the start and end times for this event should be
+     * displayed in the Calendar app's time zone as well and
+     * formats and displays them.
+     */
+    private void updateHomeTime() {
+        String tz = Utils.getTimeZone(this, mUpdateTZ);
+        if (!mAllDayCheckBox.isChecked() && !TextUtils.equals(tz, mTimezone)) {
+            int flags = DateUtils.FORMAT_SHOW_TIME;
+            boolean is24Format = DateFormat.is24HourFormat(this);
+            if (is24Format) {
+                flags |= DateUtils.FORMAT_24HOUR;
+            }
+            long millisStart = mStartTime.toMillis(false);
+            long millisEnd = mEndTime.toMillis(false);
+
+            boolean isDSTStart = mStartTime.isDst != 0;
+            boolean isDSTEnd = mEndTime.isDst != 0;
+
+            // First update the start date and times
+            String tzDisplay = TimeZone.getTimeZone(tz).getDisplayName(isDSTStart,
+                    TimeZone.SHORT, Locale.getDefault());
+            StringBuilder time = new StringBuilder();
+
+            mSB.setLength(0);
+            time.append(DateUtils.formatDateRange(this, mF, millisStart, millisStart, flags, tz))
+                    .append(" ").append(tzDisplay);
+            mStartTimeHome.setText(time.toString());
+
+            flags = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE |
+                    DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_WEEKDAY;
+            mSB.setLength(0);
+            mStartDateHome.setText(DateUtils.formatDateRange(this, mF, millisStart, millisStart,
+                    flags, tz).toString());
+
+            // Make any adjustments needed for the end times
+            if (isDSTEnd != isDSTStart) {
+                tzDisplay = TimeZone.getTimeZone(tz).getDisplayName(isDSTEnd,
+                        TimeZone.SHORT, Locale.getDefault());
+            }
+            flags = DateUtils.FORMAT_SHOW_TIME;
+            if (is24Format) {
+                flags |= DateUtils.FORMAT_24HOUR;
+            }
+
+            // Then update the end times
+            time.setLength(0);
+            mSB.setLength(0);
+            time.append(DateUtils.formatDateRange(this, mF, millisEnd, millisEnd, flags, tz))
+                    .append(" ").append(tzDisplay);
+            mEndTimeHome.setText(time.toString());
+
+            flags = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE |
+            DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_WEEKDAY;
+            mSB.setLength(0);
+            mEndDateHome.setText(DateUtils.formatDateRange(this, mF, millisEnd, millisEnd,
+                    flags, tz).toString());
+
+            mStartTimeHome.setVisibility(View.VISIBLE);
+            mStartDateHome.setVisibility(View.VISIBLE);
+            mEndTimeHome.setVisibility(View.VISIBLE);
+            mEndDateHome.setVisibility(View.VISIBLE);
+        } else {
+            mStartTimeHome.setVisibility(View.GONE);
+            mStartDateHome.setVisibility(View.GONE);
+            mEndTimeHome.setVisibility(View.GONE);
+            mEndDateHome.setVisibility(View.GONE);
+        }
+    }
+
     private void showTimezoneDialog() {
         mTimezoneAdapter = new TimezoneAdapter(this, mTimezone);
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
@@ -1465,20 +1553,9 @@
                 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_MONTH |
                 DateUtils.FORMAT_ABBREV_WEEKDAY;
 
-        // Unfortunately, DateUtils doesn't support a timezone other than the
-        // default timezone provided by the system, so we have this ugly hack
-        // here to trick it into formatting our time correctly. In order to
-        // prevent all sorts of craziness, we synchronize on the TimeZone class
-        // to prevent other threads from reading an incorrect timezone from
-        // calls to TimeZone#getDefault()
-        // TODO fix this if/when DateUtils allows for passing in a timezone
-        String dateString;
-        synchronized (TimeZone.class) {
-            TimeZone.setDefault(TimeZone.getTimeZone(mTimezone));
-            dateString = DateUtils.formatDateTime(this, millis, flags);
-            // setting the default back to null restores the correct behavior
-            TimeZone.setDefault(null);
-        }
+        mSB.setLength(0);
+        String dateString = DateUtils.formatDateRange(this, mF, millis, millis, flags, mTimezone)
+                .toString();
         view.setText(dateString);
     }
 
@@ -1487,20 +1564,9 @@
         if (DateFormat.is24HourFormat(this)) {
             flags |= DateUtils.FORMAT_24HOUR;
         }
-
-        // Unfortunately, DateUtils doesn't support a timezone other than the
-        // default timezone provided by the system, so we have this ugly hack
-        // here to trick it into formatting our time correctly. In order to
-        // prevent all sorts of craziness, we synchronize on the TimeZone class
-        // to prevent other threads from reading an incorrect timezone from
-        // calls to TimeZone#getDefault()
-        // TODO fix this if/when DateUtils allows for passing in a timezone
-        String timeString;
-        synchronized (TimeZone.class) {
-            TimeZone.setDefault(TimeZone.getTimeZone(mTimezone));
-            timeString = DateUtils.formatDateTime(this, millis, flags);
-            TimeZone.setDefault(null);
-        }
+        mSB.setLength(0);
+        String timeString = DateUtils.formatDateRange(this, mF, millis, millis, flags, mTimezone)
+                .toString();
         view.setText(timeString);
     }