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