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