AlertAdapter.java revision 10d9f11fec9cb578185e4455eeaef289c0dff8b9
1/*
2 * Copyright (C) 2008 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
17package com.android.calendar.alerts;
18
19import com.android.calendar.R;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.database.Cursor;
24import android.text.TextUtils;
25import android.text.format.DateFormat;
26import android.text.format.DateUtils;
27import android.view.View;
28import android.widget.ResourceCursorAdapter;
29import android.widget.TextView;
30
31public class AlertAdapter extends ResourceCursorAdapter {
32
33    private static boolean mFirstTime = true;
34    private static int mTitleColor;
35    private static int mOtherColor; // non-title fields
36    private static int mPastEventColor;
37
38    public AlertAdapter(Context context, int resource) {
39        super(context, resource, null);
40    }
41
42    @Override
43    public void bindView(View view, Context context, Cursor cursor) {
44        View square = view.findViewById(R.id.color_square);
45        int color = cursor.getInt(AlertActivity.INDEX_COLOR);
46        square.setBackgroundColor(color);
47
48        // Repeating info
49        View repeatContainer = view.findViewById(R.id.repeat_icon);
50        String rrule = cursor.getString(AlertActivity.INDEX_RRULE);
51        if (!TextUtils.isEmpty(rrule)) {
52            repeatContainer.setVisibility(View.VISIBLE);
53        } else {
54            repeatContainer.setVisibility(View.GONE);
55        }
56
57        /*
58        // Reminder
59        boolean hasAlarm = cursor.getInt(AlertActivity.INDEX_HAS_ALARM) != 0;
60        if (hasAlarm) {
61            AgendaAdapter.updateReminder(view, context, cursor.getLong(AlertActivity.INDEX_BEGIN),
62                    cursor.getLong(AlertActivity.INDEX_EVENT_ID));
63        }
64        */
65
66        String eventName = cursor.getString(AlertActivity.INDEX_TITLE);
67        String location = cursor.getString(AlertActivity.INDEX_EVENT_LOCATION);
68        long startMillis = cursor.getLong(AlertActivity.INDEX_BEGIN);
69        long endMillis = cursor.getLong(AlertActivity.INDEX_END);
70        boolean allDay = cursor.getInt(AlertActivity.INDEX_ALL_DAY) != 0;
71
72        updateView(context, view, eventName, location, startMillis, endMillis, allDay);
73    }
74
75    public static void updateView(Context context, View view, String eventName, String location,
76            long startMillis, long endMillis, boolean allDay) {
77        Resources res = context.getResources();
78
79        TextView titleView = (TextView) view.findViewById(R.id.event_title);
80        TextView whenView = (TextView) view.findViewById(R.id.when);
81        TextView whereView = (TextView) view.findViewById(R.id.where);
82        if (mFirstTime) {
83            mPastEventColor = res.getColor(R.color.alert_past_event);
84            mTitleColor = res.getColor(R.color.alert_event_title);
85            mOtherColor = res.getColor(R.color.alert_event_other);
86            mFirstTime = false;
87        }
88
89        if (endMillis < System.currentTimeMillis()) {
90            titleView.setTextColor(mPastEventColor);
91            whenView.setTextColor(mPastEventColor);
92            whereView.setTextColor(mPastEventColor);
93        } else {
94            titleView.setTextColor(mTitleColor);
95            whenView.setTextColor(mOtherColor);
96            whereView.setTextColor(mOtherColor);
97        }
98
99        // What
100        if (eventName == null || eventName.length() == 0) {
101            eventName = res.getString(R.string.no_title_label);
102        }
103        titleView.setText(eventName);
104
105        // When
106        String when;
107        int flags;
108        if (allDay) {
109            flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY |
110                    DateUtils.FORMAT_SHOW_DATE;
111        } else {
112            flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
113        }
114        if (DateFormat.is24HourFormat(context)) {
115            flags |= DateUtils.FORMAT_24HOUR;
116        }
117        when = DateUtils.formatDateRange(context, startMillis, endMillis, flags);
118        whenView.setText(when);
119
120        // Where
121        if (location == null || location.length() == 0) {
122            whereView.setVisibility(View.GONE);
123        } else {
124            whereView.setText(location);
125            whereView.setVisibility(View.VISIBLE);
126        }
127    }
128}
129