Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2007 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.Context;
     20 import android.content.res.Resources;
     21 import android.database.Cursor;
     22 import android.provider.Calendar.Attendees;
     23 import android.text.format.DateFormat;
     24 import android.text.format.DateUtils;
     25 import android.view.View;
     26 import android.widget.ResourceCursorAdapter;
     27 import android.widget.TextView;
     28 
     29 public class AgendaAdapter extends ResourceCursorAdapter {
     30     private String mNoTitleLabel;
     31     private Resources mResources;
     32     private int mDeclinedColor;
     33 
     34     static class ViewHolder {
     35         int overLayColor; // Used by AgendaItemView to gray out the entire item if so desired
     36 
     37         /* Event */
     38         TextView title;
     39         TextView when;
     40         TextView where;
     41         int calendarColor; // Used by AgendaItemView to color the vertical stripe
     42     }
     43 
     44     public AgendaAdapter(Context context, int resource) {
     45         super(context, resource, null);
     46         mResources = context.getResources();
     47         mNoTitleLabel = mResources.getString(R.string.no_title_label);
     48         mDeclinedColor = mResources.getColor(R.drawable.agenda_item_declined);
     49     }
     50 
     51     @Override
     52     public void bindView(View view, Context context, Cursor cursor) {
     53         ViewHolder holder = null;
     54 
     55         // Listview may get confused and pass in a different type of view since
     56         // we keep shifting data around. Not a big problem.
     57         Object tag = view.getTag();
     58         if (tag instanceof ViewHolder) {
     59             holder = (ViewHolder) view.getTag();
     60         }
     61 
     62         if (holder == null) {
     63             holder = new ViewHolder();
     64             view.setTag(holder);
     65             holder.title = (TextView) view.findViewById(R.id.title);
     66             holder.when = (TextView) view.findViewById(R.id.when);
     67             holder.where = (TextView) view.findViewById(R.id.where);
     68         }
     69 
     70         // Fade text if event was declined.
     71         int selfAttendeeStatus = cursor.getInt(AgendaWindowAdapter.INDEX_SELF_ATTENDEE_STATUS);
     72         if (selfAttendeeStatus == Attendees.ATTENDEE_STATUS_DECLINED) {
     73             holder.overLayColor = mDeclinedColor;
     74         } else {
     75             holder.overLayColor = 0;
     76         }
     77 
     78         TextView title = holder.title;
     79         TextView when = holder.when;
     80         TextView where = holder.where;
     81 
     82         /* Calendar Color */
     83         int color = cursor.getInt(AgendaWindowAdapter.INDEX_COLOR);
     84         holder.calendarColor = color;
     85 
     86         // What
     87         String titleString = cursor.getString(AgendaWindowAdapter.INDEX_TITLE);
     88         if (titleString == null || titleString.length() == 0) {
     89             titleString = mNoTitleLabel;
     90         }
     91         title.setText(titleString);
     92         title.setTextColor(color);
     93 
     94         // When
     95         long begin = cursor.getLong(AgendaWindowAdapter.INDEX_BEGIN);
     96         long end = cursor.getLong(AgendaWindowAdapter.INDEX_END);
     97         boolean allDay = cursor.getInt(AgendaWindowAdapter.INDEX_ALL_DAY) != 0;
     98         int flags;
     99         String whenString;
    100         if (allDay) {
    101             flags = DateUtils.FORMAT_UTC;
    102         } else {
    103             flags = DateUtils.FORMAT_SHOW_TIME;
    104         }
    105         if (DateFormat.is24HourFormat(context)) {
    106             flags |= DateUtils.FORMAT_24HOUR;
    107         }
    108 
    109         whenString = Utils.formatDateRange(context, begin, end, flags).toString();
    110         when.setText(whenString);
    111 
    112         String rrule = cursor.getString(AgendaWindowAdapter.INDEX_RRULE);
    113         if (rrule != null) {
    114             when.setCompoundDrawablesWithIntrinsicBounds(null, null,
    115                     context.getResources().getDrawable(R.drawable.ic_repeat_dark), null);
    116             when.setCompoundDrawablePadding(5);
    117         } else {
    118             when.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    119         }
    120 
    121         /*
    122         // Repeating info
    123         View repeatContainer = view.findViewById(R.id.repeat_icon);
    124         String rrule = cursor.getString(AgendaActivity.INDEX_RRULE);
    125         if (rrule != null) {
    126             repeatContainer.setVisibility(View.VISIBLE);
    127         } else {
    128             repeatContainer.setVisibility(View.GONE);
    129         }
    130         */
    131 
    132         /*
    133         // Reminder
    134         boolean hasAlarm = cursor.getInt(AgendaActivity.INDEX_HAS_ALARM) != 0;
    135         if (hasAlarm) {
    136             updateReminder(view, context, begin, cursor.getLong(AgendaActivity.INDEX_EVENT_ID));
    137         }
    138         */
    139 
    140         // Where
    141         String whereString = cursor.getString(AgendaWindowAdapter.INDEX_EVENT_LOCATION);
    142         if (whereString != null && whereString.length() > 0) {
    143             where.setVisibility(View.VISIBLE);
    144             where.setText(whereString);
    145         } else {
    146             where.setVisibility(View.GONE);
    147         }
    148     }
    149 
    150     /*
    151     public static void updateReminder(View view, Context context, long begin, long eventId) {
    152         ContentResolver cr = context.getContentResolver();
    153         Uri uri = Reminders.CONTENT_URI;
    154         String where = String.format(REMINDERS_WHERE, eventId);
    155 
    156         Cursor remindersCursor = cr.query(uri, REMINDERS_PROJECTION, where, null, null);
    157         if (remindersCursor != null) {
    158             LayoutInflater inflater =
    159                     (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    160             LinearLayout parent = (LinearLayout) view.findViewById(R.id.reminders_container);
    161             parent.removeAllViews();
    162             while (remindersCursor.moveToNext()) {
    163                 int alarm = remindersCursor.getInt(REMINDERS_INDEX_MINUTES);
    164                 String before = EditEvent.constructReminderLabel(context, alarm, true);
    165                 LinearLayout reminderItem = (LinearLayout)
    166                         inflater.inflate(R.layout.agenda_reminder_item, null);
    167                 TextView reminderItemText = (TextView) reminderItem.findViewById(R.id.reminder);
    168                 reminderItemText.setText(before);
    169                 parent.addView(reminderItem);
    170             }
    171         }
    172         remindersCursor.close();
    173     }
    174     */
    175 }
    176 
    177