OSDN Git Service

Code drop from //branches/cupcake/...@124589
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / AgendaAdapter.java
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.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.Cursor;
23 import android.graphics.PorterDuff;
24 import android.net.Uri;
25 import android.provider.Calendar.Reminders;
26 import android.text.format.DateFormat;
27 import android.text.format.DateUtils;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.ImageView;
31 import android.widget.LinearLayout;
32 import android.widget.ResourceCursorAdapter;
33 import android.widget.TextView;
34
35 import java.util.ArrayList;
36
37 public class AgendaAdapter extends ResourceCursorAdapter {
38     
39     private static final String[] REMINDERS_PROJECTION = new String[] {
40         Reminders._ID,      // 0
41         Reminders.MINUTES,  // 1
42     };
43     private static final int REMINDERS_INDEX_MINUTES = 1;
44     private static final String REMINDERS_WHERE = Reminders.EVENT_ID + "=%d AND (" + 
45             Reminders.METHOD + "=" + Reminders.METHOD_ALERT + " OR " + Reminders.METHOD + "=" +
46             Reminders.METHOD_DEFAULT + ")";
47     
48     private Resources mResources;
49     private static ArrayList<Integer> sReminderValues;
50     private static String[] sReminderLabels;
51
52     public AgendaAdapter(Context context, int resource) {
53         super(context, resource, null);
54         mResources = context.getResources();
55     }
56     
57     @Override
58     public void bindView(View view, Context context, Cursor cursor) {
59         ImageView stripe = (ImageView) view.findViewById(R.id.vertical_stripe);
60         int color = cursor.getInt(AgendaActivity.INDEX_COLOR) & 0xbbffffff;
61         stripe.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
62         
63         // What
64         TextView title = (TextView) view.findViewById(R.id.title);
65         String titleString = cursor.getString(AgendaActivity.INDEX_TITLE);
66         if (titleString == null || titleString.length() == 0) {
67             titleString = mResources.getString(R.string.no_title_label);
68         }
69         title.setText(titleString);
70         
71         // When
72         TextView when = (TextView) view.findViewById(R.id.when);
73         long begin = cursor.getLong(AgendaActivity.INDEX_BEGIN);
74         long end = cursor.getLong(AgendaActivity.INDEX_END);
75         boolean allDay = cursor.getInt(AgendaActivity.INDEX_ALL_DAY) != 0;
76         int flags;
77         String whenString;
78         if (allDay) {
79             flags = DateUtils.FORMAT_UTC;
80         } else {
81             flags = DateUtils.FORMAT_SHOW_TIME;
82         }
83         if (DateFormat.is24HourFormat(context)) {
84             flags |= DateUtils.FORMAT_24HOUR;
85         }
86         whenString = DateUtils.formatDateRange(context, begin, end, flags);
87         when.setText(whenString);
88         
89         // Repeating info
90         View repeatContainer = view.findViewById(R.id.repeat_icon);
91         String rrule = cursor.getString(AgendaActivity.INDEX_RRULE);
92         if (rrule != null) {
93             repeatContainer.setVisibility(View.VISIBLE);
94         } else {
95             repeatContainer.setVisibility(View.GONE);
96         }
97         
98         // Reminder
99         boolean hasAlarm = cursor.getInt(AgendaActivity.INDEX_HAS_ALARM) != 0;
100         if (hasAlarm) {
101             updateReminder(view, context, begin, cursor.getLong(AgendaActivity.INDEX_EVENT_ID));
102         }
103         
104         // Where
105         TextView where = (TextView) view.findViewById(R.id.where);
106         String whereString = cursor.getString(AgendaActivity.INDEX_EVENT_LOCATION);
107         if (whereString != null && whereString.length() > 0) {
108             where.setVisibility(View.VISIBLE);
109             where.setText(whereString);
110         } else {
111             where.setVisibility(View.GONE);
112         }
113     }
114
115     public static void updateReminder(View view, Context context, long begin, long eventId) {
116         ContentResolver cr = context.getContentResolver();
117         Uri uri = Reminders.CONTENT_URI;
118         String where = String.format(REMINDERS_WHERE, eventId);
119         
120         Cursor remindersCursor = cr.query(uri, REMINDERS_PROJECTION, where, null, null);
121         if (remindersCursor != null) {
122             LayoutInflater inflater =
123                     (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
124             LinearLayout parent = (LinearLayout) view.findViewById(R.id.reminders_container);
125             parent.removeAllViews();
126             while (remindersCursor.moveToNext()) {
127                 int alarm = remindersCursor.getInt(REMINDERS_INDEX_MINUTES);
128                 String before = EditEvent.constructReminderLabel(context, alarm, true);
129                 LinearLayout reminderItem = (LinearLayout)
130                         inflater.inflate(R.layout.agenda_reminder_item, null);
131                 TextView reminderItemText = (TextView) reminderItem.findViewById(R.id.reminder);
132                 reminderItemText.setText(before);
133                 parent.addView(reminderItem);
134             }
135         }
136         remindersCursor.close();
137     }
138 }
139