The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 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 | |
Mason Tang | 23e7da3 | 2010-07-27 15:24:02 -0700 | [diff] [blame] | 17 | package com.android.calendar.alerts; |
| 18 | |
Michael Chan | 5487f88 | 2012-05-17 17:00:11 -0700 | [diff] [blame] | 19 | import com.android.calendar.R; |
| 20 | import com.android.calendar.Utils; |
| 21 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
| 24 | import android.database.Cursor; |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 26 | import android.text.format.DateFormat; |
| 27 | import android.text.format.DateUtils; |
RoboErik | a99feb9 | 2011-02-09 18:03:24 -0800 | [diff] [blame] | 28 | import android.text.format.Time; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 29 | import android.view.View; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 30 | import android.widget.ResourceCursorAdapter; |
| 31 | import android.widget.TextView; |
| 32 | |
RoboErik | a99feb9 | 2011-02-09 18:03:24 -0800 | [diff] [blame] | 33 | import java.util.Locale; |
| 34 | import java.util.TimeZone; |
| 35 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 36 | public class AlertAdapter extends ResourceCursorAdapter { |
| 37 | |
Sara Ting | c912980 | 2012-07-26 13:59:25 -0700 | [diff] [blame] | 38 | private static AlertActivity alertActivity; |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 39 | private static boolean mFirstTime = true; |
| 40 | private static int mTitleColor; |
| 41 | private static int mOtherColor; // non-title fields |
Michael Chan | 5487f88 | 2012-05-17 17:00:11 -0700 | [diff] [blame] | 42 | private static int mPastEventColor; |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 43 | |
Sara Ting | c912980 | 2012-07-26 13:59:25 -0700 | [diff] [blame] | 44 | public AlertAdapter(AlertActivity activity, int resource) { |
| 45 | super(activity, resource, null); |
Michael Chan | 48bcc4c | 2012-12-12 11:38:16 -0800 | [diff] [blame] | 46 | alertActivity = activity; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 47 | } |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 48 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 49 | @Override |
| 50 | public void bindView(View view, Context context, Cursor cursor) { |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 51 | View square = view.findViewById(R.id.color_square); |
RoboErik | 4acb2fd | 2011-07-18 15:39:49 -0700 | [diff] [blame] | 52 | int color = Utils.getDisplayColorFromColor(cursor.getInt(AlertActivity.INDEX_COLOR)); |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 53 | square.setBackgroundColor(color); |
The Android Open Source Project | 7abd856 | 2009-03-05 14:34:37 -0800 | [diff] [blame] | 54 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 55 | // Repeating info |
| 56 | View repeatContainer = view.findViewById(R.id.repeat_icon); |
| 57 | String rrule = cursor.getString(AlertActivity.INDEX_RRULE); |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 58 | if (!TextUtils.isEmpty(rrule)) { |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 59 | repeatContainer.setVisibility(View.VISIBLE); |
| 60 | } else { |
| 61 | repeatContainer.setVisibility(View.GONE); |
| 62 | } |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 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 | */ |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 72 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 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; |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 79 | updateView(context, view, eventName, location, startMillis, endMillis, allDay); |
| 80 | } |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 81 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 82 | public static void updateView(Context context, View view, String eventName, String location, |
| 83 | long startMillis, long endMillis, boolean allDay) { |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 84 | Resources res = context.getResources(); |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 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) { |
Michael Chan | 5487f88 | 2012-05-17 17:00:11 -0700 | [diff] [blame] | 90 | mPastEventColor = res.getColor(R.color.alert_past_event); |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 91 | mTitleColor = res.getColor(R.color.alert_event_title); |
| 92 | mOtherColor = res.getColor(R.color.alert_event_other); |
| 93 | mFirstTime = false; |
| 94 | } |
| 95 | |
Michael Chan | 5487f88 | 2012-05-17 17:00:11 -0700 | [diff] [blame] | 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 | } |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 105 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 106 | // What |
| 107 | if (eventName == null || eventName.length() == 0) { |
| 108 | eventName = res.getString(R.string.no_title_label); |
| 109 | } |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 110 | titleView.setText(eventName); |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 112 | // When |
| 113 | String when; |
| 114 | int flags; |
RoboErik | a99feb9 | 2011-02-09 18:03:24 -0800 | [diff] [blame] | 115 | String tz = Utils.getTimeZone(context, null); |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 116 | if (allDay) { |
| 117 | flags = DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_WEEKDAY | |
| 118 | DateUtils.FORMAT_SHOW_DATE; |
RoboErik | a99feb9 | 2011-02-09 18:03:24 -0800 | [diff] [blame] | 119 | tz = Time.TIMEZONE_UTC; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 120 | } else { |
| 121 | flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE; |
| 122 | } |
| 123 | if (DateFormat.is24HourFormat(context)) { |
| 124 | flags |= DateUtils.FORMAT_24HOUR; |
| 125 | } |
RoboErik | a99feb9 | 2011-02-09 18:03:24 -0800 | [diff] [blame] | 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(); |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 138 | whenView.setText(when); |
Michael Chan | 66b2067 | 2010-12-20 13:50:28 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 140 | // Where |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 141 | if (location == null || location.length() == 0) { |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 142 | whereView.setVisibility(View.GONE); |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 143 | } else { |
Michael Chan | 10d9f11 | 2011-01-17 21:17:08 -0800 | [diff] [blame] | 144 | whereView.setText(location); |
| 145 | whereView.setVisibility(View.VISIBLE); |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
Sara Ting | c912980 | 2012-07-26 13:59:25 -0700 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | protected void onContentChanged () { |
| 151 | super.onContentChanged(); |
| 152 | |
| 153 | // Prevent empty popup notification. |
| 154 | alertActivity.closeActivityIfEmpty(); |
| 155 | } |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 156 | } |