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