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