Home | History | Annotate | Download | only in calendar
      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 android.content.res.Resources;
     20 import android.text.format.DateUtils;
     21 import android.pim.EventRecurrence;
     22 
     23 import java.util.Calendar;
     24 
     25 public class EventRecurrenceFormatter
     26 {
     27     public static String getRepeatString(Resources r, EventRecurrence recurrence) {
     28         // TODO Implement "Until" portion of string, as well as custom settings
     29         switch (recurrence.freq) {
     30             case EventRecurrence.DAILY:
     31                 return r.getString(R.string.daily);
     32             case EventRecurrence.WEEKLY: {
     33                 if (recurrence.repeatsOnEveryWeekDay()) {
     34                     return r.getString(R.string.every_weekday);
     35                 } else {
     36                     String format = r.getString(R.string.weekly);
     37                     StringBuilder days = new StringBuilder();
     38 
     39                     // Do one less iteration in the loop so the last element is added out of the
     40                     // loop. This is done so the comma is not placed after the last item.
     41                     int count = recurrence.bydayCount - 1;
     42                     if (count >= 0) {
     43                         for (int i = 0 ; i < count ; i++) {
     44                             days.append(dayToString(recurrence.byday[i]));
     45                             days.append(",");
     46                         }
     47                         days.append(dayToString(recurrence.byday[count]));
     48 
     49                         return String.format(format, days.toString());
     50                     }
     51 
     52                     // There is no "BYDAY" specifier, so use the day of the
     53                     // first event.  For this to work, the setStartDate()
     54                     // method must have been used by the caller to set the
     55                     // date of the first event in the recurrence.
     56                     if (recurrence.startDate == null) {
     57                         return null;
     58                     }
     59 
     60                     int day = EventRecurrence.timeDay2Day(recurrence.startDate.weekDay);
     61                     return String.format(format, dayToString(day));
     62                 }
     63             }
     64             case EventRecurrence.MONTHLY: {
     65                 return r.getString(R.string.monthly);
     66             }
     67             case EventRecurrence.YEARLY:
     68                 return r.getString(R.string.yearly_plain);
     69         }
     70 
     71         return null;
     72     }
     73 
     74     /**
     75      * Converts day of week to a String.
     76      * @param day a EventRecurrence constant
     77      * @return day of week as a string
     78      */
     79     private static String dayToString(int day) {
     80         return DateUtils.getDayOfWeekString(dayToUtilDay(day), DateUtils.LENGTH_LONG);
     81     }
     82 
     83     /**
     84      * Converts EventRecurrence's day of week to DateUtil's day of week.
     85      * @param day of week as an EventRecurrence value
     86      * @return day of week as a DateUtil value.
     87      */
     88     private static int dayToUtilDay(int day) {
     89         switch (day) {
     90         case EventRecurrence.SU: return Calendar.SUNDAY;
     91         case EventRecurrence.MO: return Calendar.MONDAY;
     92         case EventRecurrence.TU: return Calendar.TUESDAY;
     93         case EventRecurrence.WE: return Calendar.WEDNESDAY;
     94         case EventRecurrence.TH: return Calendar.THURSDAY;
     95         case EventRecurrence.FR: return Calendar.FRIDAY;
     96         case EventRecurrence.SA: return Calendar.SATURDAY;
     97         default: throw new IllegalArgumentException("bad day argument: " + day);
     98         }
     99     }
    100 }
    101