blob: 1573371502ff340dcab9ec04bd928451e101ea47 [file] [log] [blame]
Michael Chan49701592010-06-30 11:04:03 -07001/*
2 * Copyright (C) 2010 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;
18
James Kung1d423f72013-01-14 13:40:38 -080019import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
RoboErika27a8862011-06-23 15:26:23 -070020import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
21import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070022import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
Michael Chan9e89dca2010-07-13 17:56:09 -070023
Erikba1b94a2010-07-20 17:50:50 -070024import android.accounts.Account;
Andy McFadden7dcd3012011-11-17 13:47:13 -080025import android.accounts.AccountManager;
Michael Chan83b0fe32010-07-08 16:46:26 -070026import android.app.Activity;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070027import android.app.SearchManager;
28import android.app.SearchableInfo;
29import android.content.ComponentName;
Erikba1b94a2010-07-20 17:50:50 -070030import android.content.ContentResolver;
Michael Chan9e89dca2010-07-13 17:56:09 -070031import android.content.ContentUris;
Michael Chanab29d9e2010-07-21 06:08:47 -070032import android.content.Context;
Michael Chan9e89dca2010-07-13 17:56:09 -070033import android.content.Intent;
34import android.net.Uri;
Erikba1b94a2010-07-20 17:50:50 -070035import android.os.Bundle;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070036import android.provider.CalendarContract.Attendees;
RoboErika7c03902011-06-14 11:06:44 -070037import android.provider.CalendarContract.Calendars;
38import android.provider.CalendarContract.Events;
Michael Chan49701592010-06-30 11:04:03 -070039import android.text.format.Time;
Michael Chan83b0fe32010-07-08 16:46:26 -070040import android.util.Log;
RoboErik3864be02011-07-25 15:56:50 -070041import android.util.Pair;
Michael Chan49701592010-06-30 11:04:03 -070042
James Kung1d423f72013-01-14 13:40:38 -080043import com.android.calendar.event.EditEventActivity;
44import com.android.calendar.selectcalendars.SelectVisibleCalendarsActivity;
45
Erik3f348f32010-08-10 13:17:19 -070046import java.util.Iterator;
47import java.util.LinkedHashMap;
Michael Chanab29d9e2010-07-21 06:08:47 -070048import java.util.LinkedList;
Erik3f348f32010-08-10 13:17:19 -070049import java.util.Map.Entry;
Michael Chan83b0fe32010-07-08 16:46:26 -070050import java.util.WeakHashMap;
51
Erik25251192010-07-12 15:30:14 -070052public class CalendarController {
Michael Chan48bcc4c2012-12-12 11:38:16 -080053 private static final boolean DEBUG = false;
Michael Chan83b0fe32010-07-08 16:46:26 -070054 private static final String TAG = "CalendarController";
55
Erika7694ee2010-12-07 15:46:18 -080056 public static final String EVENT_EDIT_ON_LAUNCH = "editMode";
57
Erik981874e2010-10-05 16:52:52 -070058 public static final int MIN_CALENDAR_YEAR = 1970;
59 public static final int MAX_CALENDAR_YEAR = 2036;
60 public static final int MIN_CALENDAR_WEEK = 0;
61 public static final int MAX_CALENDAR_WEEK = 3497; // weeks between 1/1/1970 and 1/1/2037
62
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070063 private final Context mContext;
Michael Chanab29d9e2010-07-21 06:08:47 -070064
Erik3f348f32010-08-10 13:17:19 -070065 // This uses a LinkedHashMap so that we can replace fragments based on the
66 // view id they are being expanded into since we can't guarantee a reference
67 // to the handler will be findable
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070068 private final LinkedHashMap<Integer,EventHandler> eventHandlers =
Erik3f348f32010-08-10 13:17:19 -070069 new LinkedHashMap<Integer,EventHandler>(5);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070070 private final LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>();
71 private final LinkedHashMap<Integer, EventHandler> mToBeAddedEventHandlers = new LinkedHashMap<
Erikcb811892010-09-28 13:44:19 -070072 Integer, EventHandler>();
RoboErik3864be02011-07-25 15:56:50 -070073 private Pair<Integer, EventHandler> mFirstEventHandler;
74 private Pair<Integer, EventHandler> mToBeAddedFirstEventHandler;
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -070075 private volatile int mDispatchInProgressCounter = 0;
Michael Chanab29d9e2010-07-21 06:08:47 -070076
Michael Chan0558def2010-07-22 21:30:32 -070077 private static WeakHashMap<Context, CalendarController> instances =
78 new WeakHashMap<Context, CalendarController>();
79
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070080 private final WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
Michael Chan83b0fe32010-07-08 16:46:26 -070081
Michael Chan3458a172010-07-13 17:58:21 -070082 private int mViewType = -1;
Michael Chan85e55092010-07-27 15:18:40 -070083 private int mDetailViewType = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -070084 private int mPreviousViewType = -1;
Erik7b92da22010-09-23 14:55:22 -070085 private long mEventId = -1;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070086 private final Time mTime = new Time();
RoboErikc8e0f212011-10-13 17:57:39 -070087 private long mDateFlags = 0;
Michael Chan3458a172010-07-13 17:58:21 -070088
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070089 private final Runnable mUpdateTimezone = new Runnable() {
RoboErik4679f602011-03-24 15:39:40 -070090 @Override
91 public void run() {
92 mTime.switchTimezone(Utils.getTimeZone(mContext, this));
93 }
94 };
95
Michael Chan49701592010-06-30 11:04:03 -070096 /**
Michael Chan83b0fe32010-07-08 16:46:26 -070097 * One of the event types that are sent to or from the controller
Michael Chan49701592010-06-30 11:04:03 -070098 */
Erik25251192010-07-12 15:30:14 -070099 public interface EventType {
Michael Chan9e89dca2010-07-13 17:56:09 -0700100 final long CREATE_EVENT = 1L;
Michael Chandeced892010-12-10 15:59:35 -0800101
102 // Simple view of an event
Michael Chan83b0fe32010-07-08 16:46:26 -0700103 final long VIEW_EVENT = 1L << 1;
Michael Chandeced892010-12-10 15:59:35 -0800104
105 // Full detail view in read only mode
106 final long VIEW_EVENT_DETAILS = 1L << 2;
107
Erika7694ee2010-12-07 15:46:18 -0800108 // full detail view in edit mode
Michael Chandeced892010-12-10 15:59:35 -0800109 final long EDIT_EVENT = 1L << 3;
Michael Chan83b0fe32010-07-08 16:46:26 -0700110
Michael Chandeced892010-12-10 15:59:35 -0800111 final long DELETE_EVENT = 1L << 4;
Michael Chan83b0fe32010-07-08 16:46:26 -0700112
Michael Chandeced892010-12-10 15:59:35 -0800113 final long GO_TO = 1L << 5;
Erik954c8712010-08-06 10:12:34 -0700114
Michael Chandeced892010-12-10 15:59:35 -0800115 final long LAUNCH_SETTINGS = 1L << 6;
Mason Tang4003d1c2010-08-17 13:50:45 -0700116
Michael Chandeced892010-12-10 15:59:35 -0800117 final long EVENTS_CHANGED = 1L << 7;
118
119 final long SEARCH = 1L << 8;
Erik7b92da22010-09-23 14:55:22 -0700120
121 // User has pressed the home key
Michael Chandeced892010-12-10 15:59:35 -0800122 final long USER_HOME = 1L << 9;
Erikeaafa2b2010-12-23 14:30:37 -0800123
124 // date range has changed, update the title
125 final long UPDATE_TITLE = 1L << 10;
Michael Chan2aeb8d92011-07-10 13:32:09 -0700126
127 // select which calendars to display
128 final long LAUNCH_SELECT_VISIBLE_CALENDARS = 1L << 11;
Michael Chan83b0fe32010-07-08 16:46:26 -0700129 }
Michael Chan49701592010-06-30 11:04:03 -0700130
131 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700132 * One of the Agenda/Day/Week/Month view types
Michael Chan49701592010-06-30 11:04:03 -0700133 */
Erik25251192010-07-12 15:30:14 -0700134 public interface ViewType {
Michael Chand6734db2010-07-22 00:48:08 -0700135 final int DETAIL = -1;
Michael Chan3458a172010-07-13 17:58:21 -0700136 final int CURRENT = 0;
137 final int AGENDA = 1;
138 final int DAY = 2;
139 final int WEEK = 3;
140 final int MONTH = 4;
Erikdd95df52010-08-27 09:31:18 -0700141 final int EDIT = 5;
James Kunge744c602012-10-30 15:46:58 -0700142 final int MAX_VALUE = 5;
Michael Chan83b0fe32010-07-08 16:46:26 -0700143 }
144
Erik25251192010-07-12 15:30:14 -0700145 public static class EventInfo {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700146
147 private static final long ATTENTEE_STATUS_MASK = 0xFF;
148 private static final long ALL_DAY_MASK = 0x100;
149 private static final int ATTENDEE_STATUS_NONE_MASK = 0x01;
150 private static final int ATTENDEE_STATUS_ACCEPTED_MASK = 0x02;
151 private static final int ATTENDEE_STATUS_DECLINED_MASK = 0x04;
152 private static final int ATTENDEE_STATUS_TENTATIVE_MASK = 0x08;
153
Mason Tang00b8c1a2010-08-23 12:02:00 -0700154 public long eventType; // one of the EventType
155 public int viewType; // one of the ViewType
156 public long id; // event id
157 public Time selectedTime; // the selected time in focus
James Kung1d423f72013-01-14 13:40:38 -0800158
159 // Event start and end times. All-day events are represented in:
160 // - local time for GO_TO commands
161 // - UTC time for VIEW_EVENT and other event-related commands
162 public Time startTime;
163 public Time endTime;
164
Mason Tang00b8c1a2010-08-23 12:02:00 -0700165 public int x; // x coordinate in the activity space
166 public int y; // y coordinate in the activity space
167 public String query; // query for a user search
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700168 public ComponentName componentName; // used in combination with query
James Kung09fbd8e2012-11-12 11:19:46 -0800169 public String eventTitle;
170 public long calendarId;
Michael Chan46a8b112010-12-14 16:36:27 -0800171
172 /**
173 * For EventType.VIEW_EVENT:
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700174 * It is the default attendee response and an all day event indicator.
175 * Set to Attendees.ATTENDEE_STATUS_NONE, Attendees.ATTENDEE_STATUS_ACCEPTED,
176 * Attendees.ATTENDEE_STATUS_DECLINED, or Attendees.ATTENDEE_STATUS_TENTATIVE.
177 * To signal the event is an all-day event, "or" ALL_DAY_MASK with the response.
178 * Alternatively, use buildViewExtraLong(), getResponse(), and isAllDay().
Michael Chan46a8b112010-12-14 16:36:27 -0800179 * <p>
Michael Chanedecd9a2011-08-24 23:58:48 -0700180 * For EventType.CREATE_EVENT:
181 * Set to {@link #EXTRA_CREATE_ALL_DAY} for creating an all-day event.
182 * <p>
Michael Chan46a8b112010-12-14 16:36:27 -0800183 * For EventType.GO_TO:
184 * Set to {@link #EXTRA_GOTO_TIME} to go to the specified date/time.
185 * Set to {@link #EXTRA_GOTO_DATE} to consider the date but ignore the time.
Michael Chanfb0ec222011-08-12 17:39:30 -0700186 * Set to {@link #EXTRA_GOTO_BACK_TO_PREVIOUS} if back should bring back previous view.
RoboErik5b872522011-10-03 17:24:50 -0700187 * Set to {@link #EXTRA_GOTO_TODAY} if this is a user request to go to the current time.
Michael Chanfb0ec222011-08-12 17:39:30 -0700188 * <p>
189 * For EventType.UPDATE_TITLE:
190 * Set formatting flags for Utils.formatDateRange
Michael Chan46a8b112010-12-14 16:36:27 -0800191 */
192 public long extraLong;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700193
194 public boolean isAllDay() {
195 if (eventType != EventType.VIEW_EVENT) {
196 Log.wtf(TAG, "illegal call to isAllDay , wrong event type " + eventType);
197 return false;
198 }
199 return ((extraLong & ALL_DAY_MASK) != 0) ? true : false;
200 }
201
202 public int getResponse() {
203 if (eventType != EventType.VIEW_EVENT) {
204 Log.wtf(TAG, "illegal call to getResponse , wrong event type " + eventType);
205 return Attendees.ATTENDEE_STATUS_NONE;
206 }
207
208 int response = (int)(extraLong & ATTENTEE_STATUS_MASK);
209 switch (response) {
210 case ATTENDEE_STATUS_NONE_MASK:
211 return Attendees.ATTENDEE_STATUS_NONE;
212 case ATTENDEE_STATUS_ACCEPTED_MASK:
213 return Attendees.ATTENDEE_STATUS_ACCEPTED;
214 case ATTENDEE_STATUS_DECLINED_MASK:
215 return Attendees.ATTENDEE_STATUS_DECLINED;
216 case ATTENDEE_STATUS_TENTATIVE_MASK:
217 return Attendees.ATTENDEE_STATUS_TENTATIVE;
218 default:
219 Log.wtf(TAG,"Unknown attendee response " + response);
220 }
221 return ATTENDEE_STATUS_NONE_MASK;
222 }
223
224 // Used to build the extra long for a VIEW event.
225 public static long buildViewExtraLong(int response, boolean allDay) {
226 long extra = allDay ? ALL_DAY_MASK : 0;
227
228 switch (response) {
229 case Attendees.ATTENDEE_STATUS_NONE:
230 extra |= ATTENDEE_STATUS_NONE_MASK;
231 break;
232 case Attendees.ATTENDEE_STATUS_ACCEPTED:
233 extra |= ATTENDEE_STATUS_ACCEPTED_MASK;
234 break;
235 case Attendees.ATTENDEE_STATUS_DECLINED:
236 extra |= ATTENDEE_STATUS_DECLINED_MASK;
237 break;
238 case Attendees.ATTENDEE_STATUS_TENTATIVE:
239 extra |= ATTENDEE_STATUS_TENTATIVE_MASK;
240 break;
241 default:
242 Log.wtf(TAG,"Unknown attendee response " + response);
243 extra |= ATTENDEE_STATUS_NONE_MASK;
244 break;
245 }
246 return extra;
247 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700248 }
249
Michael Chan46a8b112010-12-14 16:36:27 -0800250 /**
Michael Chanedecd9a2011-08-24 23:58:48 -0700251 * Pass to the ExtraLong parameter for EventType.CREATE_EVENT to create
252 * an all-day event
253 */
254 public static final long EXTRA_CREATE_ALL_DAY = 0x10;
255
256 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800257 * Pass to the ExtraLong parameter for EventType.GO_TO to signal the time
258 * can be ignored
259 */
260 public static final long EXTRA_GOTO_DATE = 1;
Michael Chanfb0ec222011-08-12 17:39:30 -0700261 public static final long EXTRA_GOTO_TIME = 2;
262 public static final long EXTRA_GOTO_BACK_TO_PREVIOUS = 4;
RoboErik5b872522011-10-03 17:24:50 -0700263 public static final long EXTRA_GOTO_TODAY = 8;
Michael Chan46a8b112010-12-14 16:36:27 -0800264
Erik25251192010-07-12 15:30:14 -0700265 public interface EventHandler {
Michael Chan83b0fe32010-07-08 16:46:26 -0700266 long getSupportedEventTypes();
267 void handleEvent(EventInfo event);
268
269 /**
Erik954c8712010-08-06 10:12:34 -0700270 * This notifies the handler that the database has changed and it should
271 * update its view.
Michael Chan83b0fe32010-07-08 16:46:26 -0700272 */
273 void eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700274 }
275
Michael Chan0558def2010-07-22 21:30:32 -0700276 /**
277 * Creates and/or returns an instance of CalendarController associated with
278 * the supplied context. It is best to pass in the current Activity.
279 *
280 * @param context The activity if at all possible.
281 */
282 public static CalendarController getInstance(Context context) {
283 synchronized (instances) {
284 CalendarController controller = instances.get(context);
285 if (controller == null) {
286 controller = new CalendarController(context);
287 instances.put(context, controller);
288 }
289 return controller;
290 }
291 }
292
Eriked61b482010-08-13 13:59:50 -0700293 /**
294 * Removes an instance when it is no longer needed. This should be called in
295 * an activity's onDestroy method.
296 *
297 * @param context The activity used to create the controller
298 */
299 public static void removeInstance(Context context) {
300 instances.remove(context);
301 }
302
Michael Chan0558def2010-07-22 21:30:32 -0700303 private CalendarController(Context context) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700304 mContext = context;
RoboErik4679f602011-03-24 15:39:40 -0700305 mUpdateTimezone.run();
Michael Chan3458a172010-07-13 17:58:21 -0700306 mTime.setToNow();
Michael Chan85e55092010-07-27 15:18:40 -0700307 mDetailViewType = Utils.getSharedPreference(mContext,
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700308 GeneralPreferences.KEY_DETAILED_VIEW,
309 GeneralPreferences.DEFAULT_DETAILED_VIEW);
Michael Chan83b0fe32010-07-08 16:46:26 -0700310 }
Michael Chan49701592010-06-30 11:04:03 -0700311
Michael Chandeced892010-12-10 15:59:35 -0800312 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
RoboErik87f993f2011-02-03 17:47:20 -0800313 long endMillis, int x, int y, long selectedMillis) {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700314 // TODO: pass the real allDay status or at least a status that says we don't know the
315 // status and have the receiver query the data.
316 // The current use of this method for VIEW_EVENT is by the day view to show an EventInfo
317 // so currently the missing allDay status has no effect.
Michael Chanedecd9a2011-08-24 23:58:48 -0700318 sendEventRelatedEventWithExtra(sender, eventType, eventId, startMillis, endMillis, x, y,
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700319 EventInfo.buildViewExtraLong(Attendees.ATTENDEE_STATUS_NONE, false),
320 selectedMillis);
Michael Chandeced892010-12-10 15:59:35 -0800321 }
322
Michael Chan49701592010-06-30 11:04:03 -0700323 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700324 * Helper for sending New/View/Edit/Delete events
325 *
326 * @param sender object of the caller
327 * @param eventType one of {@link EventType}
328 * @param eventId event id
Michael Chan3458a172010-07-13 17:58:21 -0700329 * @param startMillis start time
330 * @param endMillis end time
Michael Chan83b0fe32010-07-08 16:46:26 -0700331 * @param x x coordinate in the activity space
332 * @param y y coordinate in the activity space
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700333 * @param extraLong default response value for the "simple event view" and all day indication.
334 * Use Attendees.ATTENDEE_STATUS_NONE for no response.
RoboErik87f993f2011-02-03 17:47:20 -0800335 * @param selectedMillis The time to specify as selected
Michael Chan49701592010-06-30 11:04:03 -0700336 */
Michael Chanedecd9a2011-08-24 23:58:48 -0700337 public void sendEventRelatedEventWithExtra(Object sender, long eventType, long eventId,
RoboErik87f993f2011-02-03 17:47:20 -0800338 long startMillis, long endMillis, int x, int y, long extraLong, long selectedMillis) {
James Kung09fbd8e2012-11-12 11:19:46 -0800339 sendEventRelatedEventWithExtraWithTitleWithCalendarId(sender, eventType, eventId,
340 startMillis, endMillis, x, y, extraLong, selectedMillis, null, -1);
341 }
342
343 /**
344 * Helper for sending New/View/Edit/Delete events
345 *
346 * @param sender object of the caller
347 * @param eventType one of {@link EventType}
348 * @param eventId event id
349 * @param startMillis start time
350 * @param endMillis end time
351 * @param x x coordinate in the activity space
352 * @param y y coordinate in the activity space
353 * @param extraLong default response value for the "simple event view" and all day indication.
354 * Use Attendees.ATTENDEE_STATUS_NONE for no response.
355 * @param selectedMillis The time to specify as selected
356 * @param title The title of the event
357 * @param calendarId The id of the calendar which the event belongs to
358 */
359 public void sendEventRelatedEventWithExtraWithTitleWithCalendarId(Object sender, long eventType,
360 long eventId, long startMillis, long endMillis, int x, int y, long extraLong,
361 long selectedMillis, String title, long calendarId) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700362 EventInfo info = new EventInfo();
363 info.eventType = eventType;
Erika7694ee2010-12-07 15:46:18 -0800364 if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) {
Erik5f620792010-10-27 12:42:01 -0700365 info.viewType = ViewType.CURRENT;
Erikdd95df52010-08-27 09:31:18 -0700366 }
James Kung09fbd8e2012-11-12 11:19:46 -0800367
Michael Chan83b0fe32010-07-08 16:46:26 -0700368 info.id = eventId;
James Kungf068eab2013-01-14 11:36:17 -0800369 info.startTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700370 info.startTime.set(startMillis);
RoboErik87f993f2011-02-03 17:47:20 -0800371 if (selectedMillis != -1) {
RoboErik4679f602011-03-24 15:39:40 -0700372 info.selectedTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
RoboErik87f993f2011-02-03 17:47:20 -0800373 info.selectedTime.set(selectedMillis);
374 } else {
375 info.selectedTime = info.startTime;
376 }
James Kungf068eab2013-01-14 11:36:17 -0800377 info.endTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700378 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700379 info.x = x;
380 info.y = y;
Michael Chandeced892010-12-10 15:59:35 -0800381 info.extraLong = extraLong;
James Kung09fbd8e2012-11-12 11:19:46 -0800382 info.eventTitle = title;
383 info.calendarId = calendarId;
Michael Chan83b0fe32010-07-08 16:46:26 -0700384 this.sendEvent(sender, info);
385 }
Michael Chan49701592010-06-30 11:04:03 -0700386 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700387 * Helper for sending non-calendar-event events
388 *
389 * @param sender object of the caller
390 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700391 * @param start start time
392 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700393 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700394 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700395 */
Erik25251192010-07-12 15:30:14 -0700396 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700397 int viewType) {
Michael Chanf0868f62011-01-11 19:37:35 -0800398 sendEvent(sender, eventType, start, end, start, eventId, viewType, EXTRA_GOTO_TIME, null,
399 null);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700400 }
401
402 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800403 * sendEvent() variant with extraLong, search query, and search component name.
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700404 */
405 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan46a8b112010-12-14 16:36:27 -0800406 int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chanf0868f62011-01-11 19:37:35 -0800407 sendEvent(sender, eventType, start, end, start, eventId, viewType, extraLong, query,
408 componentName);
409 }
410
411 public void sendEvent(Object sender, long eventType, Time start, Time end, Time selected,
412 long eventId, int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700413 EventInfo info = new EventInfo();
414 info.eventType = eventType;
415 info.startTime = start;
Michael Chanf0868f62011-01-11 19:37:35 -0800416 info.selectedTime = selected;
Michael Chan83b0fe32010-07-08 16:46:26 -0700417 info.endTime = end;
418 info.id = eventId;
419 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700420 info.query = query;
421 info.componentName = componentName;
Michael Chan46a8b112010-12-14 16:36:27 -0800422 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700423 this.sendEvent(sender, info);
424 }
425
Erik25251192010-07-12 15:30:14 -0700426 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700427 // TODO Throw exception on invalid events
428
Erik3f348f32010-08-10 13:17:19 -0700429 if (DEBUG) {
James Kungf068eab2013-01-14 11:36:17 -0800430 Log.d(TAG, eventInfoToString(event));
Erik3f348f32010-08-10 13:17:19 -0700431 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700432
433 Long filteredTypes = filters.get(sender);
434 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
435 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700436 if (DEBUG) {
437 Log.d(TAG, "Event suppressed");
438 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700439 return;
440 }
441
Michael Chanab29d9e2010-07-21 06:08:47 -0700442 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700443
Michael Chan3458a172010-07-13 17:58:21 -0700444 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700445 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700446 event.viewType = mDetailViewType;
447 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700448 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700449 event.viewType = mViewType;
Michael Chanf0868f62011-01-11 19:37:35 -0800450 } else if (event.viewType != ViewType.EDIT) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700451 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700452
Erik63cd0532011-01-26 14:16:03 -0800453 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY
454 || (Utils.getAllowWeekForDetailView() && event.viewType == ViewType.WEEK)) {
Michael Chan85e55092010-07-27 15:18:40 -0700455 mDetailViewType = mViewType;
456 }
Michael Chan3458a172010-07-13 17:58:21 -0700457 }
458
Michael Chanf0868f62011-01-11 19:37:35 -0800459 if (DEBUG) {
Michael Chan48bcc4c2012-12-12 11:38:16 -0800460 Log.d(TAG, "vvvvvvvvvvvvvvv");
461 Log.d(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
462 Log.d(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
463 Log.d(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
464 Log.d(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
Mason Tang4003d1c2010-08-17 13:50:45 -0700465 }
Michael Chanf0868f62011-01-11 19:37:35 -0800466
467 long startMillis = 0;
468 if (event.startTime != null) {
469 startMillis = event.startTime.toMillis(false);
470 }
471
472 // Set mTime if selectedTime is set
473 if (event.selectedTime != null && event.selectedTime.toMillis(false) != 0) {
474 mTime.set(event.selectedTime);
475 } else {
476 if (startMillis != 0) {
477 // selectedTime is not set so set mTime to startTime iff it is not
478 // within start and end times
479 long mtimeMillis = mTime.toMillis(false);
480 if (mtimeMillis < startMillis
481 || (event.endTime != null && mtimeMillis > event.endTime.toMillis(false))) {
482 mTime.set(event.startTime);
483 }
484 }
485 event.selectedTime = mTime;
486 }
RoboErikc8e0f212011-10-13 17:57:39 -0700487 // Store the formatting flags if this is an update to the title
488 if (event.eventType == EventType.UPDATE_TITLE) {
489 mDateFlags = event.extraLong;
490 }
Michael Chanf0868f62011-01-11 19:37:35 -0800491
492 // Fix up start time if not specified
493 if (startMillis == 0) {
494 event.startTime = mTime;
495 }
496 if (DEBUG) {
Michael Chan48bcc4c2012-12-12 11:38:16 -0800497 Log.d(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
498 Log.d(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
499 Log.d(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
500 Log.d(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
501 Log.d(TAG, "^^^^^^^^^^^^^^^");
Michael Chanf0868f62011-01-11 19:37:35 -0800502 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700503
Erik7b92da22010-09-23 14:55:22 -0700504 // Store the eventId if we're entering edit event
Erika7694ee2010-12-07 15:46:18 -0800505 if ((event.eventType
506 & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS))
507 != 0) {
Erik7b92da22010-09-23 14:55:22 -0700508 if (event.id > 0) {
509 mEventId = event.id;
510 } else {
511 mEventId = -1;
512 }
513 }
514
Mason Tang4003d1c2010-08-17 13:50:45 -0700515 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700516 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700517 mDispatchInProgressCounter ++;
Michael Chan3458a172010-07-13 17:58:21 -0700518
Erik3f348f32010-08-10 13:17:19 -0700519 if (DEBUG) {
520 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
521 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700522 // Dispatch to event handler(s)
RoboErik3864be02011-07-25 15:56:50 -0700523 if (mFirstEventHandler != null) {
524 // Handle the 'first' one before handling the others
525 EventHandler handler = mFirstEventHandler.second;
526 if (handler != null && (handler.getSupportedEventTypes() & event.eventType) != 0
527 && !mToBeRemovedEventHandlers.contains(mFirstEventHandler.first)) {
528 handler.handleEvent(event);
529 handled = true;
530 }
531 }
Erik3f348f32010-08-10 13:17:19 -0700532 for (Iterator<Entry<Integer, EventHandler>> handlers =
533 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
534 Entry<Integer, EventHandler> entry = handlers.next();
535 int key = entry.getKey();
RoboErik3864be02011-07-25 15:56:50 -0700536 if (mFirstEventHandler != null && key == mFirstEventHandler.first) {
537 // If this was the 'first' handler it was already handled
538 continue;
539 }
Erik3f348f32010-08-10 13:17:19 -0700540 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700541 if (eventHandler != null
542 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700543 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700544 continue;
545 }
546 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700547 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700548 }
549 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700550
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700551 mDispatchInProgressCounter --;
Erikcb811892010-09-28 13:44:19 -0700552
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700553 if (mDispatchInProgressCounter == 0) {
554
555 // Deregister removed handlers
556 if (mToBeRemovedEventHandlers.size() > 0) {
557 for (Integer zombie : mToBeRemovedEventHandlers) {
558 eventHandlers.remove(zombie);
RoboErik3864be02011-07-25 15:56:50 -0700559 if (mFirstEventHandler != null && zombie.equals(mFirstEventHandler.first)) {
560 mFirstEventHandler = null;
561 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700562 }
563 mToBeRemovedEventHandlers.clear();
Michael Chanab29d9e2010-07-21 06:08:47 -0700564 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700565 // Add new handlers
RoboErik3864be02011-07-25 15:56:50 -0700566 if (mToBeAddedFirstEventHandler != null) {
567 mFirstEventHandler = mToBeAddedFirstEventHandler;
568 mToBeAddedFirstEventHandler = null;
569 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700570 if (mToBeAddedEventHandlers.size() > 0) {
571 for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) {
572 eventHandlers.put(food.getKey(), food.getValue());
573 }
Erikcb811892010-09-28 13:44:19 -0700574 }
575 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700576 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700577
578 if (!handled) {
Erik6b858fc2010-09-15 18:59:04 -0700579 // Launch Settings
580 if (event.eventType == EventType.LAUNCH_SETTINGS) {
Mason Tang4003d1c2010-08-17 13:50:45 -0700581 launchSettings();
582 return;
583 }
584
Michael Chan2aeb8d92011-07-10 13:32:09 -0700585 // Launch Calendar Visible Selector
586 if (event.eventType == EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) {
587 launchSelectVisibleCalendars();
588 return;
589 }
590
Mason Tang4003d1c2010-08-17 13:50:45 -0700591 // Create/View/Edit/Delete Event
592 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
593 if (event.eventType == EventType.CREATE_EVENT) {
Michael Chanedecd9a2011-08-24 23:58:48 -0700594 launchCreateEvent(event.startTime.toMillis(false), endTime,
James Kung09fbd8e2012-11-12 11:19:46 -0800595 event.extraLong == EXTRA_CREATE_ALL_DAY, event.eventTitle,
596 event.calendarId);
Mason Tang4003d1c2010-08-17 13:50:45 -0700597 return;
598 } else if (event.eventType == EventType.VIEW_EVENT) {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700599 launchViewEvent(event.id, event.startTime.toMillis(false), endTime,
600 event.getResponse());
Mason Tang4003d1c2010-08-17 13:50:45 -0700601 return;
602 } else if (event.eventType == EventType.EDIT_EVENT) {
Erika7694ee2010-12-07 15:46:18 -0800603 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true);
604 return;
605 } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) {
606 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false);
Mason Tang4003d1c2010-08-17 13:50:45 -0700607 return;
608 } else if (event.eventType == EventType.DELETE_EVENT) {
609 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
610 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700611 } else if (event.eventType == EventType.SEARCH) {
612 launchSearch(event.id, event.query, event.componentName);
613 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700614 }
615 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700616 }
617
Erik3f348f32010-08-10 13:17:19 -0700618 /**
619 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
620 * replace fragments based on the view id they are being expanded into.
621 *
622 * @param key The view id or placeholder for this handler
623 * @param eventHandler Typically a fragment or activity in the calendar app
624 */
625 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700626 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700627 if (mDispatchInProgressCounter > 0) {
Erikcb811892010-09-28 13:44:19 -0700628 mToBeAddedEventHandlers.put(key, eventHandler);
629 } else {
630 eventHandlers.put(key, eventHandler);
631 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700632 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700633 }
634
RoboErik3864be02011-07-25 15:56:50 -0700635 public void registerFirstEventHandler(int key, EventHandler eventHandler) {
636 synchronized (this) {
637 registerEventHandler(key, eventHandler);
638 if (mDispatchInProgressCounter > 0) {
639 mToBeAddedFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
640 } else {
641 mFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
642 }
643 }
644 }
645
Erik3f348f32010-08-10 13:17:19 -0700646 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700647 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700648 if (mDispatchInProgressCounter > 0) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700649 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700650 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700651 } else {
Erik3f348f32010-08-10 13:17:19 -0700652 eventHandlers.remove(key);
RoboErik3864be02011-07-25 15:56:50 -0700653 if (mFirstEventHandler != null && mFirstEventHandler.first == key) {
654 mFirstEventHandler = null;
655 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700656 }
657 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700658 }
659
Paul Westbrook61310b72011-10-07 15:43:00 -0700660 public void deregisterAllEventHandlers() {
661 synchronized (this) {
662 if (mDispatchInProgressCounter > 0) {
663 // To avoid ConcurrencyException, stash away the event handler for now.
664 mToBeRemovedEventHandlers.addAll(eventHandlers.keySet());
665 } else {
666 eventHandlers.clear();
667 mFirstEventHandler = null;
668 }
669 }
670 }
671
Michael Chan3458a172010-07-13 17:58:21 -0700672 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700673 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700674 filters.put(sender, eventTypes);
675 }
676
Mason Tang8e3d4302010-07-12 17:39:30 -0700677 /**
678 * @return the time that this controller is currently pointed at
679 */
680 public long getTime() {
681 return mTime.toMillis(false);
682 }
683
Erik7b92da22010-09-23 14:55:22 -0700684 /**
RoboErikc8e0f212011-10-13 17:57:39 -0700685 * @return the last set of date flags sent with
686 * {@link EventType#UPDATE_TITLE}
687 */
688 public long getDateFlags() {
689 return mDateFlags;
690 }
691
692 /**
Erik144edfa2010-11-22 14:23:49 -0800693 * Set the time this controller is currently pointed at
694 *
695 * @param millisTime Time since epoch in millis
696 */
697 public void setTime(long millisTime) {
698 mTime.set(millisTime);
699 }
700
701 /**
Erik7b92da22010-09-23 14:55:22 -0700702 * @return the last event ID the edit view was launched with
703 */
704 public long getEventId() {
705 return mEventId;
706 }
707
Michael Chanab29d9e2010-07-21 06:08:47 -0700708 public int getViewType() {
709 return mViewType;
710 }
711
712 public int getPreviousViewType() {
713 return mPreviousViewType;
714 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700715
Michael Chan2aeb8d92011-07-10 13:32:09 -0700716 private void launchSelectVisibleCalendars() {
717 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700718 intent.setClass(mContext, SelectVisibleCalendarsActivity.class);
Michael Chan2aeb8d92011-07-10 13:32:09 -0700719 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
720 mContext.startActivity(intent);
721 }
722
Michael Chan9e89dca2010-07-13 17:56:09 -0700723 private void launchSettings() {
724 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700725 intent.setClass(mContext, CalendarSettingsActivity.class);
Michael Chan9e89dca2010-07-13 17:56:09 -0700726 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700727 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700728 }
729
James Kung09fbd8e2012-11-12 11:19:46 -0800730 private void launchCreateEvent(long startMillis, long endMillis, boolean allDayEvent,
731 String title, long calendarId) {
732 Intent intent = generateCreateEventIntent(startMillis, endMillis, allDayEvent, title,
733 calendarId);
734 mEventId = -1;
735 mContext.startActivity(intent);
736 }
737
738 public Intent generateCreateEventIntent(long startMillis, long endMillis,
739 boolean allDayEvent, String title, long calendarId) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700740 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700741 intent.setClass(mContext, EditEventActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700742 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
743 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanedecd9a2011-08-24 23:58:48 -0700744 intent.putExtra(EXTRA_EVENT_ALL_DAY, allDayEvent);
James Kung09fbd8e2012-11-12 11:19:46 -0800745 intent.putExtra(Events.CALENDAR_ID, calendarId);
746 intent.putExtra(Events.TITLE, title);
747 return intent;
Michael Chan9e89dca2010-07-13 17:56:09 -0700748 }
749
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700750 public void launchViewEvent(long eventId, long startMillis, long endMillis, int response) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700751 Intent intent = new Intent(Intent.ACTION_VIEW);
752 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
753 intent.setData(eventUri);
Michael Chan07d9fee2011-07-25 09:41:21 -0700754 intent.setClass(mContext, AllInOneActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700755 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
756 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700757 intent.putExtra(ATTENDEE_STATUS, response);
Isaac Katzenelson4024dee2011-09-29 14:40:46 -0700758 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700759 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700760 }
761
Erika7694ee2010-12-07 15:46:18 -0800762 private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700763 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
764 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
RoboErika27a8862011-06-23 15:26:23 -0700765 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
766 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700767 intent.setClass(mContext, EditEventActivity.class);
Erika7694ee2010-12-07 15:46:18 -0800768 intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
Erik7b92da22010-09-23 14:55:22 -0700769 mEventId = eventId;
Michael Chanab29d9e2010-07-21 06:08:47 -0700770 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700771 }
772
Michael Chan1881a892011-02-08 18:16:58 -0800773// private void launchAlerts() {
774// Intent intent = new Intent();
775// intent.setClass(mContext, AlertActivity.class);
776// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
777// mContext.startActivity(intent);
778// }
779
Michael Chan9e89dca2010-07-13 17:56:09 -0700780 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
781 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
782 }
783
784 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
785 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700786 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700787 parentActivity != null /* exit when done */);
788 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
789 }
Michael Chan3458a172010-07-13 17:58:21 -0700790
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700791 private void launchSearch(long eventId, String query, ComponentName componentName) {
792 final SearchManager searchManager =
793 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
794 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
795 final Intent intent = new Intent(Intent.ACTION_SEARCH);
796 intent.putExtra(SearchManager.QUERY, query);
797 intent.setComponent(searchableInfo.getSearchActivity());
RoboErik2bd5cc02011-07-28 14:19:54 -0700798 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700799 mContext.startActivity(intent);
800 }
801
Andy McFadden7dcd3012011-11-17 13:47:13 -0800802 /**
803 * Performs a manual refresh of calendars in all known accounts.
804 */
Erikba1b94a2010-07-20 17:50:50 -0700805 public void refreshCalendars() {
Andy McFadden7dcd3012011-11-17 13:47:13 -0800806 Account[] accounts = AccountManager.get(mContext).getAccounts();
807 Log.d(TAG, "Refreshing " + accounts.length + " accounts");
808
809 String authority = Calendars.CONTENT_URI.getAuthority();
810 for (int i = 0; i < accounts.length; i++) {
811 if (Log.isLoggable(TAG, Log.DEBUG)) {
812 Log.d(TAG, "Refreshing calendars for: " + accounts[i]);
813 }
814 Bundle extras = new Bundle();
815 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
816 ContentResolver.requestSync(accounts[i], authority, extras);
817 }
Erikba1b94a2010-07-20 17:50:50 -0700818 }
819
Erikdd95df52010-08-27 09:31:18 -0700820 // Forces the viewType. Should only be used for initialization.
821 public void setViewType(int viewType) {
822 mViewType = viewType;
823 }
824
Erik7b92da22010-09-23 14:55:22 -0700825 // Sets the eventId. Should only be used for initialization.
826 public void setEventId(long eventId) {
827 mEventId = eventId;
828 }
829
James Kungf068eab2013-01-14 11:36:17 -0800830 private String eventInfoToString(EventInfo eventInfo) {
Michael Chan3458a172010-07-13 17:58:21 -0700831 String tmp = "Unknown";
James Kungf068eab2013-01-14 11:36:17 -0800832
833 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700834 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700835 tmp = "Go to time/event";
836 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
837 tmp = "New event";
838 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
839 tmp = "View event";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800840 } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) {
841 tmp = "View details";
Michael Chan3458a172010-07-13 17:58:21 -0700842 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
843 tmp = "Edit event";
844 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
845 tmp = "Delete event";
Michael Chan2aeb8d92011-07-10 13:32:09 -0700846 } else if ((eventInfo.eventType & EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) != 0) {
847 tmp = "Launch select visible calendars";
Michael Chan3458a172010-07-13 17:58:21 -0700848 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
849 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700850 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
851 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700852 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
853 tmp = "Search";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800854 } else if ((eventInfo.eventType & EventType.USER_HOME) != 0) {
855 tmp = "Gone home";
856 } else if ((eventInfo.eventType & EventType.UPDATE_TITLE) != 0) {
857 tmp = "Update title";
Michael Chan3458a172010-07-13 17:58:21 -0700858 }
James Kungf068eab2013-01-14 11:36:17 -0800859 builder.append(tmp);
Michael Chan3458a172010-07-13 17:58:21 -0700860 builder.append(": id=");
861 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700862 builder.append(", selected=");
863 builder.append(eventInfo.selectedTime);
864 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700865 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700866 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700867 builder.append(eventInfo.endTime);
868 builder.append(", viewType=");
869 builder.append(eventInfo.viewType);
870 builder.append(", x=");
871 builder.append(eventInfo.x);
872 builder.append(", y=");
873 builder.append(eventInfo.y);
874 return builder.toString();
875 }
Michael Chan49701592010-06-30 11:04:03 -0700876}