Merge "Fixing gender-matching issues in some languages for a recurrence string." into ics-ub-calendar-denim
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index f4a78a5..b14854a 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -244,4 +244,66 @@
         <item >REPEAT MONTHLY</item>
         <item >REPEAT YEARLY</item>
     </string-array>
+
+    <!-- The following sets of strings describe a monthly recurring event, which will repeat
+         on the Nth WEEKDAY of every month. For example, the 3rd Monday of every month, or
+         the last Sunday. These are set up like this to resolve any gender-matching issues
+         that were present in some languages.
+     -->
+    <!-- Repeat a monthly event on the same nth day of every Sunday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_sun">
+        <item >on every first Sunday</item>
+        <item >on every second Sunday</item>
+        <item >on every third Sunday</item>
+        <item >on every fourth Sunday</item>
+        <item >on every last Sunday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Monday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_mon">
+        <item >on every first Monday</item>
+        <item >on every second Monday</item>
+        <item >on every third Monday</item>
+        <item >on every fourth Monday</item>
+        <item >on every last Monday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Tuesday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_tues">
+        <item >on every first Tuesday</item>
+        <item >on every second Tuesday</item>
+        <item >on every third Tuesday</item>
+        <item >on every fourth Tuesday</item>
+        <item >on every last Tuesday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Wednesday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_wed">
+        <item >on every first Wednesday</item>
+        <item >on every second Wednesday</item>
+        <item >on every third Wednesday</item>
+        <item >on every fourth Wednesday</item>
+        <item >on every last Wednesday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Thursday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_thurs">
+        <item >on every first Thursday</item>
+        <item >on every second Thursday</item>
+        <item >on every third Thursday</item>
+        <item >on every fourth Thursday</item>
+        <item >on every last Thursday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Friday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_fri">
+        <item >on every first Friday</item>
+        <item >on every second Friday</item>
+        <item >on every third Friday</item>
+        <item >on every fourth Friday</item>
+        <item >on every last Friday</item>
+    </string-array>
+    <!-- Repeat a monthly event on the same nth day of every Saturday. [CHAR LIMIT=30] -->
+    <string-array name="repeat_by_nth_sat">
+        <item >on every first Saturday</item>
+        <item >on every second Saturday</item>
+        <item >on every third Saturday</item>
+        <item >on every fourth Saturday</item>
+        <item >on every last Saturday</item>
+    </string-array>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 6fcd7bf..5a71aae 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -429,10 +429,6 @@
         <item quantity="other">Every <xliff:g id="number">%1$d</xliff:g> weeks on <xliff:g id="days_of_week">%2$s</xliff:g></item>
     </plurals>
 
-    <!-- Example: 'Monthly (every first Sunday)' -->
-    <!--   1st parameter is an ordinal number, like 'first' -->
-    <!--   2nd parameter is a day of the week, like 'Sunday' -->
-    <string name="monthly_on_day_count">"Monthly (every <xliff:g id="ordinal_number">%1$s</xliff:g> <xliff:g id="day_of_week">%2$s</xliff:g>)"</string>
     <!-- The common portion of a string describing how often an event repeats,
          example: 'Monthly (on day 2)' -->
     <string name="monthly">Monthly</string>
@@ -721,9 +717,6 @@
 
     <!-- Repeat an monthly event on the same day of every month [CHAR LIMIT=20] -->
     <string name="recurrence_month_pattern_by_day">on the same day each month</string>
-    <!-- Repeat an monthly event on the same nth day of the week of every month.
-         For example, on every second Tuesday [CHAR LIMIT=30] -->
-    <string name="recurrence_month_pattern_by_day_of_week">on every <xliff:g id="nth">%1$s</xliff:g> <xliff:g id="day_of_week">%2$s</xliff:g></string>
 
     <!-- Specifies that a repeating event to repeat forever (based on the defined frequency) instead of ending at a future date[CHAR LIMIT=25] -->
     <string name="recurrence_end_continously">Forever</string>
diff --git a/src/com/android/calendar/EventRecurrenceFormatter.java b/src/com/android/calendar/EventRecurrenceFormatter.java
index 1d3df70..b9e33fd 100644
--- a/src/com/android/calendar/EventRecurrenceFormatter.java
+++ b/src/com/android/calendar/EventRecurrenceFormatter.java
@@ -28,6 +28,10 @@
 
 public class EventRecurrenceFormatter
 {
+
+    private static int[] mMonthRepeatByDayOfWeekIds;
+    private static String[][] mMonthRepeatByDayOfWeekStrs;
+
     public static String getRepeatString(Context context, Resources r, EventRecurrence recurrence,
             boolean includeEndString) {
         String endString = "";
@@ -99,11 +103,17 @@
             }
             case EventRecurrence.MONTHLY: {
                 if (recurrence.bydayCount == 1) {
-                    String[] ordinals = r.getStringArray(R.array.ordinal_labels);
+                    int weekday = recurrence.startDate.weekDay;
+                    // Cache this stuff so we won't have to redo work again later.
+                    cacheMonthRepeatStrings(r, weekday);
                     int dayNumber = (recurrence.startDate.monthDay - 1) / 7;
-                    int day = EventRecurrence.timeDay2Day(recurrence.startDate.weekDay);
-                    return r.getString(R.string.monthly_on_day_count, ordinals[dayNumber],
-                            dayToString(day, DateUtils.LENGTH_LONG)) + endString;
+                    StringBuilder sb = new StringBuilder();
+                    sb.append(r.getString(R.string.monthly));
+                    sb.append(" (");
+                    sb.append(mMonthRepeatByDayOfWeekStrs[weekday][dayNumber]);
+                    sb.append(")");
+                    sb.append(endString);
+                    return sb.toString();
                 }
                 return r.getString(R.string.monthly) + endString;
             }
@@ -114,6 +124,26 @@
         return null;
     }
 
+    private static void cacheMonthRepeatStrings(Resources r, int weekday) {
+        if (mMonthRepeatByDayOfWeekIds == null) {
+            mMonthRepeatByDayOfWeekIds = new int[7];
+            mMonthRepeatByDayOfWeekIds[0] = R.array.repeat_by_nth_sun;
+            mMonthRepeatByDayOfWeekIds[1] = R.array.repeat_by_nth_mon;
+            mMonthRepeatByDayOfWeekIds[2] = R.array.repeat_by_nth_tues;
+            mMonthRepeatByDayOfWeekIds[3] = R.array.repeat_by_nth_wed;
+            mMonthRepeatByDayOfWeekIds[4] = R.array.repeat_by_nth_thurs;
+            mMonthRepeatByDayOfWeekIds[5] = R.array.repeat_by_nth_fri;
+            mMonthRepeatByDayOfWeekIds[6] = R.array.repeat_by_nth_sat;
+        }
+        if (mMonthRepeatByDayOfWeekStrs == null) {
+            mMonthRepeatByDayOfWeekStrs = new String[7][];
+        }
+        if (mMonthRepeatByDayOfWeekStrs[weekday] == null) {
+            mMonthRepeatByDayOfWeekStrs[weekday] =
+                    r.getStringArray(mMonthRepeatByDayOfWeekIds[weekday]);
+        }
+    }
+
     /**
      * Converts day of week to a String.
      * @param day a EventRecurrence constant
diff --git a/src/com/android/calendar/recurrencepicker/RecurrencePickerDialog.java b/src/com/android/calendar/recurrencepicker/RecurrencePickerDialog.java
index 6de8439..580d211 100644
--- a/src/com/android/calendar/recurrencepicker/RecurrencePickerDialog.java
+++ b/src/com/android/calendar/recurrencepicker/RecurrencePickerDialog.java
@@ -327,8 +327,10 @@
     private LinearLayout mWeekGroup2;
     // Sun = 0
     private ToggleButton[] mWeekByDayButtons = new ToggleButton[7];
-    private String[] mDayOfWeekString;
-    private String[] mOrdinalArray;
+    /** A double array of Strings to hold the 7x5 list of possible strings of the form:
+     *  "on every [Nth] [DAY_OF_WEEK]", e.g. "on every second Monday",
+     *  where [Nth] can be [first, second, third, fourth, last] */
+    private String[][] mMonthRepeatByDayOfWeekStrs;
 
     private LinearLayout mMonthGroup;
     private RadioGroup mMonthRepeatByRadioGroup;
@@ -730,14 +732,18 @@
         mWeekGroup = (LinearLayout) mView.findViewById(R.id.weekGroup);
         mWeekGroup2 = (LinearLayout) mView.findViewById(R.id.weekGroup2);
 
-        mOrdinalArray = mResources.getStringArray(R.array.ordinal_labels);
-
         // In Calendar.java day of week order e.g Sun = 1 ... Sat = 7
         String[] dayOfWeekString = new DateFormatSymbols().getWeekdays();
-        mDayOfWeekString = new String[7];
-        for (int i = 0; i < 7; i++) {
-            mDayOfWeekString[i] = dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[i]];
-        }
+
+        mMonthRepeatByDayOfWeekStrs = new String[7][];
+        // from Time.SUNDAY as 0 through Time.SATURDAY as 6
+        mMonthRepeatByDayOfWeekStrs[0] = mResources.getStringArray(R.array.repeat_by_nth_sun);
+        mMonthRepeatByDayOfWeekStrs[1] = mResources.getStringArray(R.array.repeat_by_nth_mon);
+        mMonthRepeatByDayOfWeekStrs[2] = mResources.getStringArray(R.array.repeat_by_nth_tues);
+        mMonthRepeatByDayOfWeekStrs[3] = mResources.getStringArray(R.array.repeat_by_nth_wed);
+        mMonthRepeatByDayOfWeekStrs[4] = mResources.getStringArray(R.array.repeat_by_nth_thurs);
+        mMonthRepeatByDayOfWeekStrs[5] = mResources.getStringArray(R.array.repeat_by_nth_fri);
+        mMonthRepeatByDayOfWeekStrs[6] = mResources.getStringArray(R.array.repeat_by_nth_sat);
 
         // In Time.java day of week order e.g. Sun = 0
         int idx = Utils.getFirstDayOfWeek(getActivity());
@@ -932,10 +938,10 @@
                         mModel.monthlyByDayOfWeek = mTime.weekDay;
                     }
 
-                    mMonthRepeatByDayOfWeekStr = mResources.getString(
-                            R.string.recurrence_month_pattern_by_day_of_week,
-                            mOrdinalArray[mModel.monthlyByNthDayOfWeek - 1],
-                            mDayOfWeekString[mModel.monthlyByDayOfWeek]);
+                    String[] monthlyByNthDayOfWeekStrs =
+                            mMonthRepeatByDayOfWeekStrs[mModel.monthlyByDayOfWeek];
+                    mMonthRepeatByDayOfWeekStr =
+                            monthlyByNthDayOfWeekStrs[mModel.monthlyByNthDayOfWeek - 1];
                     mRepeatMonthlyByNthDayOfWeek.setText(mMonthRepeatByDayOfWeekStr);
                 }
                 break;