blob: 49e3aa0a6a7fe869d287c875a12f53b86a8b4c39 [file] [log] [blame]
The Android Open Source Project146de362009-03-03 19:32:18 -08001/*
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
Mason Tang23e7da32010-07-27 15:24:02 -070017package com.android.calendar.alerts;
18
Michael Chan5487f882012-05-17 17:00:11 -070019import com.android.calendar.R;
20import com.android.calendar.Utils;
21
The Android Open Source Project146de362009-03-03 19:32:18 -080022import android.content.Context;
23import android.content.res.Resources;
24import android.database.Cursor;
Michael Chan66b20672010-12-20 13:50:28 -080025import android.text.TextUtils;
The Android Open Source Project146de362009-03-03 19:32:18 -080026import android.text.format.DateFormat;
27import android.text.format.DateUtils;
RoboErika99feb92011-02-09 18:03:24 -080028import android.text.format.Time;
The Android Open Source Project146de362009-03-03 19:32:18 -080029import android.view.View;
The Android Open Source Project146de362009-03-03 19:32:18 -080030import android.widget.ResourceCursorAdapter;
31import android.widget.TextView;
32
RoboErika99feb92011-02-09 18:03:24 -080033import java.util.Locale;
34import java.util.TimeZone;
35
The Android Open Source Project146de362009-03-03 19:32:18 -080036public class AlertAdapter extends ResourceCursorAdapter {
37
Sara Tingc9129802012-07-26 13:59:25 -070038 private static AlertActivity alertActivity;
Michael Chan10d9f112011-01-17 21:17:08 -080039 private static boolean mFirstTime = true;
40 private static int mTitleColor;
41 private static int mOtherColor; // non-title fields
Michael Chan5487f882012-05-17 17:00:11 -070042 private static int mPastEventColor;
Michael Chan10d9f112011-01-17 21:17:08 -080043
Sara Tingc9129802012-07-26 13:59:25 -070044 public AlertAdapter(AlertActivity activity, int resource) {
45 super(activity, resource, null);
Michael Chan48bcc4c2012-12-12 11:38:16 -080046 alertActivity = activity;
The Android Open Source Project146de362009-03-03 19:32:18 -080047 }
Michael Chan66b20672010-12-20 13:50:28 -080048
The Android Open Source Project146de362009-03-03 19:32:18 -080049 @Override
50 public void bindView(View view, Context context, Cursor cursor) {
Michael Chan10d9f112011-01-17 21:17:08 -080051 View square = view.findViewById(R.id.color_square);
RoboErik4acb2fd2011-07-18 15:39:49 -070052 int color = Utils.getDisplayColorFromColor(cursor.getInt(AlertActivity.INDEX_COLOR));
Michael Chan10d9f112011-01-17 21:17:08 -080053 square.setBackgroundColor(color);
The Android Open Source Project7abd8562009-03-05 14:34:37 -080054
The Android Open Source Project146de362009-03-03 19:32:18 -080055 // Repeating info
56 View repeatContainer = view.findViewById(R.id.repeat_icon);
57 String rrule = cursor.getString(AlertActivity.INDEX_RRULE);
Michael Chan66b20672010-12-20 13:50:28 -080058 if (!TextUtils.isEmpty(rrule)) {
The Android Open Source Project146de362009-03-03 19:32:18 -080059 repeatContainer.setVisibility(View.VISIBLE);
60 } else {
61 repeatContainer.setVisibility(View.GONE);
62 }
Michael Chan66b20672010-12-20 13:50:28 -080063
The Android Open Source Project146de362009-03-03 19:32:18 -080064 /*
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 */
Michael Chan66b20672010-12-20 13:50:28 -080072
The Android Open Source Project146de362009-03-03 19:32:18 -080073 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;
Michael Chan66b20672010-12-20 13:50:28 -080078
The Android Open Source Project146de362009-03-03 19:32:18 -080079 updateView(context, view, eventName, location, startMillis, endMillis, allDay);
80 }
Michael Chan66b20672010-12-20 13:50:28 -080081
The Android Open Source Project146de362009-03-03 19:32:18 -080082 public static void updateView(Context context, View view, String eventName, String location,
83 long startMillis, long endMillis, boolean allDay) {
The Android Open Source Project146de362009-03-03 19:32:18 -080084 Resources res = context.getResources();
Michael Chan10d9f112011-01-17 21:17:08 -080085
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) {
Michael Chan5487f882012-05-17 17:00:11 -070090 mPastEventColor = res.getColor(R.color.alert_past_event);
Michael Chan10d9f112011-01-17 21:17:08 -080091 mTitleColor = res.getColor(R.color.alert_event_title);
92 mOtherColor = res.getColor(R.color.alert_event_other);
93 mFirstTime = false;
94 }
95
Michael Chan5487f882012-05-17 17:00:11 -070096 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 }
Michael Chan66b20672010-12-20 13:50:28 -0800105
The Android Open Source Project146de362009-03-03 19:32:18 -0800106 // What
107 if (eventName == null || eventName.length() == 0) {
108 eventName = res.getString(R.string.no_title_label);
109 }
Michael Chan10d9f112011-01-17 21:17:08 -0800110 titleView.setText(eventName);
Michael Chan66b20672010-12-20 13:50:28 -0800111
The Android Open Source Project146de362009-03-03 19:32:18 -0800112 // When
113 String when;
114 int flags;
RoboErika99feb92011-02-09 18:03:24 -0800115 String tz = Utils.getTimeZone(context, null);
The Android Open Source Project146de362009-03-03 19:32:18 -0800116 if (allDay) {
117 flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY |
118 DateUtils.FORMAT_SHOW_DATE;
RoboErika99feb92011-02-09 18:03:24 -0800119 tz = Time.TIMEZONE_UTC;
The Android Open Source Project146de362009-03-03 19:32:18 -0800120 } else {
121 flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
122 }
123 if (DateFormat.is24HourFormat(context)) {
124 flags |= DateUtils.FORMAT_24HOUR;
125 }
RoboErika99feb92011-02-09 18:03:24 -0800126
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();
Michael Chan10d9f112011-01-17 21:17:08 -0800138 whenView.setText(when);
Michael Chan66b20672010-12-20 13:50:28 -0800139
The Android Open Source Project146de362009-03-03 19:32:18 -0800140 // Where
The Android Open Source Project146de362009-03-03 19:32:18 -0800141 if (location == null || location.length() == 0) {
Michael Chan10d9f112011-01-17 21:17:08 -0800142 whereView.setVisibility(View.GONE);
The Android Open Source Project146de362009-03-03 19:32:18 -0800143 } else {
Michael Chan10d9f112011-01-17 21:17:08 -0800144 whereView.setText(location);
145 whereView.setVisibility(View.VISIBLE);
The Android Open Source Project146de362009-03-03 19:32:18 -0800146 }
147 }
Sara Tingc9129802012-07-26 13:59:25 -0700148
149 @Override
150 protected void onContentChanged () {
151 super.onContentChanged();
152
153 // Prevent empty popup notification.
154 alertActivity.closeActivityIfEmpty();
155 }
The Android Open Source Project146de362009-03-03 19:32:18 -0800156}