Don't move "Provide a year" around on screen anymore, center instead

 - Also fixes the title when no year is used. Doesn't show year "1' anymore

Bug:6009385
Change-Id: If0a23d852112970456dc3f5c4061d5c578fff48a
diff --git a/res/layout/date_picker.xml b/res/layout/date_picker.xml
index 4fb2318..5eb3f7a 100644
--- a/res/layout/date_picker.xml
+++ b/res/layout/date_picker.xml
@@ -19,8 +19,6 @@
 
 <!-- Layout of date picker-->
 
-<!-- Warning: everything within the parent is removed and re-ordered depending
-     on the date format selected by the user. -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_gravity="center_horizontal"
@@ -33,8 +31,11 @@
         android:paddingTop="5dip"
         android:paddingBottom="5dip"
         android:textAppearance="?android:attr/textAppearanceLarge"
+        android:layout_gravity="center_horizontal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
+    <!-- Warning: everything within the parent is removed and re-ordered depending
+         on the date format selected by the user. -->
     <LinearLayout
         android:id="@+id/parent"
         android:orientation="horizontal"
diff --git a/src/com/android/contacts/datepicker/DatePicker.java b/src/com/android/contacts/datepicker/DatePicker.java
index 7ea9641..268243d 100644
--- a/src/com/android/contacts/datepicker/DatePicker.java
+++ b/src/com/android/contacts/datepicker/DatePicker.java
@@ -108,6 +108,7 @@
         mDayPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
         mDayPicker.setOnLongPressUpdateInterval(100);
         mDayPicker.setOnValueChangedListener(new OnValueChangeListener() {
+            @Override
             public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                 mDay = newVal;
                 notifyDateChanged();
@@ -137,6 +138,7 @@
 
         mMonthPicker.setOnLongPressUpdateInterval(200);
         mMonthPicker.setOnValueChangedListener(new OnValueChangeListener() {
+            @Override
             public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
 
                 /* We display the month 1-12 but store it 0-11 so always
@@ -152,6 +154,7 @@
         mYearPicker = (NumberPicker) findViewById(R.id.year);
         mYearPicker.setOnLongPressUpdateInterval(100);
         mYearPicker.setOnValueChangedListener(new OnValueChangeListener() {
+            @Override
             public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                 mYear = newVal;
                 // Adjust max day for leap years if needed
@@ -353,10 +356,12 @@
         public static final Parcelable.Creator<SavedState> CREATOR =
                 new Creator<SavedState>() {
 
+                    @Override
                     public SavedState createFromParcel(Parcel in) {
                         return new SavedState(in);
                     }
 
+                    @Override
                     public SavedState[] newArray(int size) {
                         return new SavedState[size];
                     }
diff --git a/src/com/android/contacts/datepicker/DatePickerDialog.java b/src/com/android/contacts/datepicker/DatePickerDialog.java
index 112b96e..b0c4ed6 100644
--- a/src/com/android/contacts/datepicker/DatePickerDialog.java
+++ b/src/com/android/contacts/datepicker/DatePickerDialog.java
@@ -19,6 +19,10 @@
 // This is a fork of the standard Android DatePicker that additionally allows toggling the year
 // on/off. It uses some private API so that not everything has to be copied.
 
+import com.android.contacts.R;
+import com.android.contacts.datepicker.DatePicker.OnDateChangedListener;
+import com.android.contacts.util.DateUtils;
+
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -30,10 +34,8 @@
 import android.view.View;
 import android.widget.TextView;
 
-import com.android.contacts.R;
-import com.android.contacts.datepicker.DatePicker.OnDateChangedListener;
-
-import java.text.DateFormatSymbols;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
 /**
@@ -53,8 +55,8 @@
     private final DatePicker mDatePicker;
     private final OnDateSetListener mCallBack;
     private final Calendar mCalendar;
-    private final java.text.DateFormat mTitleDateFormat;
-    private final String[] mWeekDays;
+    private final DateFormat mTitleDateFormat;
+    private final DateFormat mTitleNoYearDateFormat;
 
     private int mInitialYear;
     private int mInitialMonth;
@@ -148,11 +150,10 @@
         mInitialYear = year;
         mInitialMonth = monthOfYear;
         mInitialDay = dayOfMonth;
-        DateFormatSymbols symbols = new DateFormatSymbols();
-        mWeekDays = symbols.getShortWeekdays();
 
-        mTitleDateFormat = java.text.DateFormat.
-                                getDateInstance(java.text.DateFormat.FULL);
+        mTitleDateFormat = DateFormat.getDateInstance(DateFormat.FULL);
+        mTitleNoYearDateFormat = new SimpleDateFormat(
+                DateUtils.isMonthBeforeDay(getContext()) ? "MMMM dd" : "dd MMMM");
         mCalendar = Calendar.getInstance();
         updateTitle(mInitialYear, mInitialMonth, mInitialDay);
 
@@ -182,6 +183,7 @@
         title.setEllipsize(TruncateAt.END);
     }
 
+    @Override
     public void onClick(DialogInterface dialog, int which) {
         if (mCallBack != null) {
             mDatePicker.clearFocus();
@@ -190,8 +192,8 @@
         }
     }
 
-    public void onDateChanged(DatePicker view, int year,
-            int month, int day) {
+    @Override
+    public void onDateChanged(DatePicker view, int year, int month, int day) {
         updateTitle(year, month, day);
     }
 
@@ -206,7 +208,9 @@
         mCalendar.set(Calendar.YEAR, year);
         mCalendar.set(Calendar.MONTH, month);
         mCalendar.set(Calendar.DAY_OF_MONTH, day);
-        setTitle(mTitleDateFormat.format(mCalendar.getTime()));
+        final DateFormat dateFormat =
+                year == 0 ? mTitleNoYearDateFormat : mTitleDateFormat;
+        setTitle(dateFormat.format(mCalendar.getTime()));
     }
 
     @Override
diff --git a/src/com/android/contacts/editor/EventFieldEditorView.java b/src/com/android/contacts/editor/EventFieldEditorView.java
index 08cbaef..538d4dc 100644
--- a/src/com/android/contacts/editor/EventFieldEditorView.java
+++ b/src/com/android/contacts/editor/EventFieldEditorView.java
@@ -29,7 +29,6 @@
 
 import android.app.Dialog;
 import android.content.Context;
-import android.content.res.ColorStateList;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.text.TextUtils;
@@ -238,10 +237,11 @@
                 final Calendar outCalendar =
                         Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
 
-                // If no year specified, set it to 1900. The format string will ignore that year
+                // If no year specified, set it to 2000 (we could pick any leap year here).
+                // The format string will ignore that year.
                 // For formats other than Exchange, the time of the day is ignored
                 outCalendar.clear();
-                outCalendar.set(year == 0 ? 1900 : year, monthOfYear, dayOfMonth,
+                outCalendar.set(year == 0 ? 2000 : year, monthOfYear, dayOfMonth,
                         DEFAULT_HOUR, 0, 0);
 
                 final String resultString;
diff --git a/src/com/android/contacts/util/DateUtils.java b/src/com/android/contacts/util/DateUtils.java
index 1ea84a1..d0bb68f 100644
--- a/src/com/android/contacts/util/DateUtils.java
+++ b/src/com/android/contacts/util/DateUtils.java
@@ -56,7 +56,7 @@
     private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_MONTH_FIRST =
             new SimpleDateFormat("MMMM dd");
 
-    private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_DATE_FIRST =
+    private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_DAY_FIRST =
             new SimpleDateFormat("dd MMMM");
 
     static {
@@ -66,7 +66,7 @@
         }
         NO_YEAR_DATE_FORMAT.setTimeZone(UTC_TIMEZONE);
         FORMAT_WITHOUT_YEAR_MONTH_FIRST.setTimeZone(UTC_TIMEZONE);
-        FORMAT_WITHOUT_YEAR_DATE_FIRST.setTimeZone(UTC_TIMEZONE);
+        FORMAT_WITHOUT_YEAR_DAY_FIRST.setTimeZone(UTC_TIMEZONE);
     }
 
     /**
@@ -112,9 +112,9 @@
         }
 
         if (parsePosition.getIndex() == string.length()) {
-            java.text.DateFormat outFormat = isMonthBeforeDate(context)
+            java.text.DateFormat outFormat = isMonthBeforeDay(context)
                     ? FORMAT_WITHOUT_YEAR_MONTH_FIRST
-                    : FORMAT_WITHOUT_YEAR_DATE_FIRST;
+                    : FORMAT_WITHOUT_YEAR_DAY_FIRST;
             synchronized (outFormat) {
                 return outFormat.format(date);
             }
@@ -135,7 +135,7 @@
         return string;
     }
 
-    private static boolean isMonthBeforeDate(Context context) {
+    public static boolean isMonthBeforeDay(Context context) {
         char[] dateFormatOrder = DateFormat.getDateFormatOrder(context);
         for (int i = 0; i < dateFormatOrder.length; i++) {
             if (dateFormatOrder[i] == DateFormat.DATE) {
diff --git a/src/com/android/contacts/util/MoreMath.java b/src/com/android/contacts/util/MoreMath.java
index 6f28ccd..db76fe4 100644
--- a/src/com/android/contacts/util/MoreMath.java
+++ b/src/com/android/contacts/util/MoreMath.java
@@ -24,6 +24,16 @@
      * If the input value lies outside of the specified range, return the nearer
      * bound. Otherwise, return the input value, unchanged.
      */
+    public static int clamp(int input, int lowerBound, int upperBound) {
+        if (input < lowerBound) return lowerBound;
+        if (input > upperBound) return upperBound;
+        return input;
+    }
+
+    /**
+     * If the input value lies outside of the specified range, return the nearer
+     * bound. Otherwise, return the input value, unchanged.
+     */
     public static float clamp(float input, float lowerBound, float upperBound) {
         if (input < lowerBound) return lowerBound;
         if (input > upperBound) return upperBound;