• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.calendar;
18 
19 import com.android.calendarcommon.EventRecurrence;
20 
21 import android.content.res.Resources;
22 import android.text.format.DateUtils;
23 
24 import java.util.Calendar;
25 
26 public class EventRecurrenceFormatter
27 {
getRepeatString(Resources r, EventRecurrence recurrence)28     public static String getRepeatString(Resources r, EventRecurrence recurrence) {
29         // TODO Implement "Until" portion of string, as well as custom settings
30         switch (recurrence.freq) {
31             case EventRecurrence.DAILY:
32                 return r.getString(R.string.daily);
33             case EventRecurrence.WEEKLY: {
34                 if (recurrence.repeatsOnEveryWeekDay()) {
35                     return r.getString(R.string.every_weekday);
36                 } else {
37                     String format = r.getString(R.string.weekly);
38                     StringBuilder days = new StringBuilder();
39 
40                     // Do one less iteration in the loop so the last element is added out of the
41                     // loop. This is done so the comma is not placed after the last item.
42                     int count = recurrence.bydayCount - 1;
43                     if (count >= 0) {
44                         for (int i = 0 ; i < count ; i++) {
45                             days.append(dayToString(recurrence.byday[i]));
46                             days.append(",");
47                         }
48                         days.append(dayToString(recurrence.byday[count]));
49 
50                         return String.format(format, days.toString());
51                     }
52 
53                     // There is no "BYDAY" specifier, so use the day of the
54                     // first event.  For this to work, the setStartDate()
55                     // method must have been used by the caller to set the
56                     // date of the first event in the recurrence.
57                     if (recurrence.startDate == null) {
58                         return null;
59                     }
60 
61                     int day = EventRecurrence.timeDay2Day(recurrence.startDate.weekDay);
62                     return String.format(format, dayToString(day));
63                 }
64             }
65             case EventRecurrence.MONTHLY: {
66                 return r.getString(R.string.monthly);
67             }
68             case EventRecurrence.YEARLY:
69                 return r.getString(R.string.yearly_plain);
70         }
71 
72         return null;
73     }
74 
75     /**
76      * Converts day of week to a String.
77      * @param day a EventRecurrence constant
78      * @return day of week as a string
79      */
dayToString(int day)80     private static String dayToString(int day) {
81         return DateUtils.getDayOfWeekString(dayToUtilDay(day), DateUtils.LENGTH_LONG);
82     }
83 
84     /**
85      * Converts EventRecurrence's day of week to DateUtil's day of week.
86      * @param day of week as an EventRecurrence value
87      * @return day of week as a DateUtil value.
88      */
dayToUtilDay(int day)89     private static int dayToUtilDay(int day) {
90         switch (day) {
91         case EventRecurrence.SU: return Calendar.SUNDAY;
92         case EventRecurrence.MO: return Calendar.MONDAY;
93         case EventRecurrence.TU: return Calendar.TUESDAY;
94         case EventRecurrence.WE: return Calendar.WEDNESDAY;
95         case EventRecurrence.TH: return Calendar.THURSDAY;
96         case EventRecurrence.FR: return Calendar.FRIDAY;
97         case EventRecurrence.SA: return Calendar.SATURDAY;
98         default: throw new IllegalArgumentException("bad day argument: " + day);
99         }
100     }
101 }
102