blob: 7fd54ae1bc8535efbce1adc888c3595a345ba273 [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
RoboErika27a8862011-06-23 15:26:23 -070019import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
20import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
Michael Chanedecd9a2011-08-24 23:58:48 -070021import static android.provider.CalendarContract.EXTRA_EVENT_ALL_DAY;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070022import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
Michael Chan9e89dca2010-07-13 17:56:09 -070023
Erik5f620792010-10-27 12:42:01 -070024import com.android.calendar.event.EditEventActivity;
Michael Chan2aeb8d92011-07-10 13:32:09 -070025import com.android.calendar.selectcalendars.SelectVisibleCalendarsActivity;
Erik5f620792010-10-27 12:42:01 -070026
Erikba1b94a2010-07-20 17:50:50 -070027import android.accounts.Account;
Andy McFadden7dcd3012011-11-17 13:47:13 -080028import android.accounts.AccountManager;
Michael Chan83b0fe32010-07-08 16:46:26 -070029import android.app.Activity;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070030import android.app.SearchManager;
31import android.app.SearchableInfo;
32import android.content.ComponentName;
Erikba1b94a2010-07-20 17:50:50 -070033import android.content.ContentResolver;
Michael Chan9e89dca2010-07-13 17:56:09 -070034import android.content.ContentUris;
Michael Chanab29d9e2010-07-21 06:08:47 -070035import android.content.Context;
Michael Chan9e89dca2010-07-13 17:56:09 -070036import android.content.Intent;
Erikba1b94a2010-07-20 17:50:50 -070037import android.database.Cursor;
Michael Chan9e89dca2010-07-13 17:56:09 -070038import android.net.Uri;
Erik7116ba42010-07-23 10:13:36 -070039import android.os.AsyncTask;
Erikba1b94a2010-07-20 17:50:50 -070040import android.os.Bundle;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070041import android.provider.CalendarContract.Attendees;
RoboErika7c03902011-06-14 11:06:44 -070042import android.provider.CalendarContract.Calendars;
43import android.provider.CalendarContract.Events;
Erikba1b94a2010-07-20 17:50:50 -070044import android.text.TextUtils;
Michael Chan49701592010-06-30 11:04:03 -070045import android.text.format.Time;
Michael Chan83b0fe32010-07-08 16:46:26 -070046import android.util.Log;
RoboErik3864be02011-07-25 15:56:50 -070047import android.util.Pair;
Michael Chan49701592010-06-30 11:04:03 -070048
Erik3f348f32010-08-10 13:17:19 -070049import java.util.Iterator;
50import java.util.LinkedHashMap;
Michael Chanab29d9e2010-07-21 06:08:47 -070051import java.util.LinkedList;
Erik3f348f32010-08-10 13:17:19 -070052import java.util.Map.Entry;
Michael Chan83b0fe32010-07-08 16:46:26 -070053import java.util.WeakHashMap;
54
Erik25251192010-07-12 15:30:14 -070055public class CalendarController {
Michael Chanf0868f62011-01-11 19:37:35 -080056 private static final boolean DEBUG = false;
Michael Chan83b0fe32010-07-08 16:46:26 -070057 private static final String TAG = "CalendarController";
Erikba1b94a2010-07-20 17:50:50 -070058 private static final String REFRESH_SELECTION = Calendars.SYNC_EVENTS + "=?";
59 private static final String[] REFRESH_ARGS = new String[] { "1" };
RoboErik0e1e6242011-05-05 15:25:02 -070060 private static final String REFRESH_ORDER = Calendars.ACCOUNT_NAME + ","
61 + Calendars.ACCOUNT_TYPE;
Michael Chan83b0fe32010-07-08 16:46:26 -070062
Erika7694ee2010-12-07 15:46:18 -080063 public static final String EVENT_EDIT_ON_LAUNCH = "editMode";
64
Erik981874e2010-10-05 16:52:52 -070065 public static final int MIN_CALENDAR_YEAR = 1970;
66 public static final int MAX_CALENDAR_YEAR = 2036;
67 public static final int MIN_CALENDAR_WEEK = 0;
68 public static final int MAX_CALENDAR_WEEK = 3497; // weeks between 1/1/1970 and 1/1/2037
69
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070070 private final Context mContext;
Michael Chanab29d9e2010-07-21 06:08:47 -070071
Erik3f348f32010-08-10 13:17:19 -070072 // This uses a LinkedHashMap so that we can replace fragments based on the
73 // view id they are being expanded into since we can't guarantee a reference
74 // to the handler will be findable
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070075 private final LinkedHashMap<Integer,EventHandler> eventHandlers =
Erik3f348f32010-08-10 13:17:19 -070076 new LinkedHashMap<Integer,EventHandler>(5);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070077 private final LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>();
78 private final LinkedHashMap<Integer, EventHandler> mToBeAddedEventHandlers = new LinkedHashMap<
Erikcb811892010-09-28 13:44:19 -070079 Integer, EventHandler>();
RoboErik3864be02011-07-25 15:56:50 -070080 private Pair<Integer, EventHandler> mFirstEventHandler;
81 private Pair<Integer, EventHandler> mToBeAddedFirstEventHandler;
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -070082 private volatile int mDispatchInProgressCounter = 0;
Michael Chanab29d9e2010-07-21 06:08:47 -070083
Michael Chan0558def2010-07-22 21:30:32 -070084 private static WeakHashMap<Context, CalendarController> instances =
85 new WeakHashMap<Context, CalendarController>();
86
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070087 private final WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
Michael Chan83b0fe32010-07-08 16:46:26 -070088
Michael Chan3458a172010-07-13 17:58:21 -070089 private int mViewType = -1;
Michael Chan85e55092010-07-27 15:18:40 -070090 private int mDetailViewType = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -070091 private int mPreviousViewType = -1;
Erik7b92da22010-09-23 14:55:22 -070092 private long mEventId = -1;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070093 private final Time mTime = new Time();
RoboErikc8e0f212011-10-13 17:57:39 -070094 private long mDateFlags = 0;
Michael Chan3458a172010-07-13 17:58:21 -070095
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070096 private final Runnable mUpdateTimezone = new Runnable() {
RoboErik4679f602011-03-24 15:39:40 -070097 @Override
98 public void run() {
99 mTime.switchTimezone(Utils.getTimeZone(mContext, this));
100 }
101 };
102
Michael Chan49701592010-06-30 11:04:03 -0700103 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700104 * One of the event types that are sent to or from the controller
Michael Chan49701592010-06-30 11:04:03 -0700105 */
Erik25251192010-07-12 15:30:14 -0700106 public interface EventType {
Michael Chan9e89dca2010-07-13 17:56:09 -0700107 final long CREATE_EVENT = 1L;
Michael Chandeced892010-12-10 15:59:35 -0800108
109 // Simple view of an event
Michael Chan83b0fe32010-07-08 16:46:26 -0700110 final long VIEW_EVENT = 1L << 1;
Michael Chandeced892010-12-10 15:59:35 -0800111
112 // Full detail view in read only mode
113 final long VIEW_EVENT_DETAILS = 1L << 2;
114
Erika7694ee2010-12-07 15:46:18 -0800115 // full detail view in edit mode
Michael Chandeced892010-12-10 15:59:35 -0800116 final long EDIT_EVENT = 1L << 3;
Michael Chan83b0fe32010-07-08 16:46:26 -0700117
Michael Chandeced892010-12-10 15:59:35 -0800118 final long DELETE_EVENT = 1L << 4;
Michael Chan83b0fe32010-07-08 16:46:26 -0700119
Michael Chandeced892010-12-10 15:59:35 -0800120 final long GO_TO = 1L << 5;
Erik954c8712010-08-06 10:12:34 -0700121
Michael Chandeced892010-12-10 15:59:35 -0800122 final long LAUNCH_SETTINGS = 1L << 6;
Mason Tang4003d1c2010-08-17 13:50:45 -0700123
Michael Chandeced892010-12-10 15:59:35 -0800124 final long EVENTS_CHANGED = 1L << 7;
125
126 final long SEARCH = 1L << 8;
Erik7b92da22010-09-23 14:55:22 -0700127
128 // User has pressed the home key
Michael Chandeced892010-12-10 15:59:35 -0800129 final long USER_HOME = 1L << 9;
Erikeaafa2b2010-12-23 14:30:37 -0800130
131 // date range has changed, update the title
132 final long UPDATE_TITLE = 1L << 10;
Michael Chan2aeb8d92011-07-10 13:32:09 -0700133
134 // select which calendars to display
135 final long LAUNCH_SELECT_VISIBLE_CALENDARS = 1L << 11;
Michael Chan83b0fe32010-07-08 16:46:26 -0700136 }
Michael Chan49701592010-06-30 11:04:03 -0700137
138 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700139 * One of the Agenda/Day/Week/Month view types
Michael Chan49701592010-06-30 11:04:03 -0700140 */
Erik25251192010-07-12 15:30:14 -0700141 public interface ViewType {
Michael Chand6734db2010-07-22 00:48:08 -0700142 final int DETAIL = -1;
Michael Chan3458a172010-07-13 17:58:21 -0700143 final int CURRENT = 0;
144 final int AGENDA = 1;
145 final int DAY = 2;
146 final int WEEK = 3;
147 final int MONTH = 4;
Erikdd95df52010-08-27 09:31:18 -0700148 final int EDIT = 5;
James Kunge744c602012-10-30 15:46:58 -0700149 final int MAX_VALUE = 5;
Michael Chan83b0fe32010-07-08 16:46:26 -0700150 }
151
Erik25251192010-07-12 15:30:14 -0700152 public static class EventInfo {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700153
154 private static final long ATTENTEE_STATUS_MASK = 0xFF;
155 private static final long ALL_DAY_MASK = 0x100;
156 private static final int ATTENDEE_STATUS_NONE_MASK = 0x01;
157 private static final int ATTENDEE_STATUS_ACCEPTED_MASK = 0x02;
158 private static final int ATTENDEE_STATUS_DECLINED_MASK = 0x04;
159 private static final int ATTENDEE_STATUS_TENTATIVE_MASK = 0x08;
160
Mason Tang00b8c1a2010-08-23 12:02:00 -0700161 public long eventType; // one of the EventType
162 public int viewType; // one of the ViewType
163 public long id; // event id
164 public Time selectedTime; // the selected time in focus
165 public Time startTime; // start of a range of time.
166 public Time endTime; // end of a range of time.
167 public int x; // x coordinate in the activity space
168 public int y; // y coordinate in the activity space
169 public String query; // query for a user search
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700170 public ComponentName componentName; // used in combination with query
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) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700339 EventInfo info = new EventInfo();
340 info.eventType = eventType;
Erika7694ee2010-12-07 15:46:18 -0800341 if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) {
Erik5f620792010-10-27 12:42:01 -0700342 info.viewType = ViewType.CURRENT;
Erikdd95df52010-08-27 09:31:18 -0700343 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700344 info.id = eventId;
RoboErik4679f602011-03-24 15:39:40 -0700345 info.startTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700346 info.startTime.set(startMillis);
RoboErik87f993f2011-02-03 17:47:20 -0800347 if (selectedMillis != -1) {
RoboErik4679f602011-03-24 15:39:40 -0700348 info.selectedTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
RoboErik87f993f2011-02-03 17:47:20 -0800349 info.selectedTime.set(selectedMillis);
350 } else {
351 info.selectedTime = info.startTime;
352 }
RoboErik4679f602011-03-24 15:39:40 -0700353 info.endTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700354 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700355 info.x = x;
356 info.y = y;
Michael Chandeced892010-12-10 15:59:35 -0800357 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700358 this.sendEvent(sender, info);
359 }
Michael Chan49701592010-06-30 11:04:03 -0700360
361 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700362 * Helper for sending non-calendar-event events
363 *
364 * @param sender object of the caller
365 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700366 * @param start start time
367 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700368 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700369 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700370 */
Erik25251192010-07-12 15:30:14 -0700371 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700372 int viewType) {
Michael Chanf0868f62011-01-11 19:37:35 -0800373 sendEvent(sender, eventType, start, end, start, eventId, viewType, EXTRA_GOTO_TIME, null,
374 null);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700375 }
376
377 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800378 * sendEvent() variant with extraLong, search query, and search component name.
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700379 */
380 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan46a8b112010-12-14 16:36:27 -0800381 int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chanf0868f62011-01-11 19:37:35 -0800382 sendEvent(sender, eventType, start, end, start, eventId, viewType, extraLong, query,
383 componentName);
384 }
385
386 public void sendEvent(Object sender, long eventType, Time start, Time end, Time selected,
387 long eventId, int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700388 EventInfo info = new EventInfo();
389 info.eventType = eventType;
390 info.startTime = start;
Michael Chanf0868f62011-01-11 19:37:35 -0800391 info.selectedTime = selected;
Michael Chan83b0fe32010-07-08 16:46:26 -0700392 info.endTime = end;
393 info.id = eventId;
394 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700395 info.query = query;
396 info.componentName = componentName;
Michael Chan46a8b112010-12-14 16:36:27 -0800397 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700398 this.sendEvent(sender, info);
399 }
400
Erik25251192010-07-12 15:30:14 -0700401 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700402 // TODO Throw exception on invalid events
403
Erik3f348f32010-08-10 13:17:19 -0700404 if (DEBUG) {
405 Log.d(TAG, eventInfoToString(event));
406 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700407
408 Long filteredTypes = filters.get(sender);
409 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
410 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700411 if (DEBUG) {
412 Log.d(TAG, "Event suppressed");
413 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700414 return;
415 }
416
Michael Chanab29d9e2010-07-21 06:08:47 -0700417 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700418
Michael Chan3458a172010-07-13 17:58:21 -0700419 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700420 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700421 event.viewType = mDetailViewType;
422 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700423 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700424 event.viewType = mViewType;
Michael Chanf0868f62011-01-11 19:37:35 -0800425 } else if (event.viewType != ViewType.EDIT) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700426 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700427
Erik63cd0532011-01-26 14:16:03 -0800428 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY
429 || (Utils.getAllowWeekForDetailView() && event.viewType == ViewType.WEEK)) {
Michael Chan85e55092010-07-27 15:18:40 -0700430 mDetailViewType = mViewType;
431 }
Michael Chan3458a172010-07-13 17:58:21 -0700432 }
433
Michael Chanf0868f62011-01-11 19:37:35 -0800434 if (DEBUG) {
435 Log.e(TAG, "vvvvvvvvvvvvvvv");
436 Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
437 Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
438 Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
439 Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
Mason Tang4003d1c2010-08-17 13:50:45 -0700440 }
Michael Chanf0868f62011-01-11 19:37:35 -0800441
442 long startMillis = 0;
443 if (event.startTime != null) {
444 startMillis = event.startTime.toMillis(false);
445 }
446
447 // Set mTime if selectedTime is set
448 if (event.selectedTime != null && event.selectedTime.toMillis(false) != 0) {
449 mTime.set(event.selectedTime);
450 } else {
451 if (startMillis != 0) {
452 // selectedTime is not set so set mTime to startTime iff it is not
453 // within start and end times
454 long mtimeMillis = mTime.toMillis(false);
455 if (mtimeMillis < startMillis
456 || (event.endTime != null && mtimeMillis > event.endTime.toMillis(false))) {
457 mTime.set(event.startTime);
458 }
459 }
460 event.selectedTime = mTime;
461 }
RoboErikc8e0f212011-10-13 17:57:39 -0700462 // Store the formatting flags if this is an update to the title
463 if (event.eventType == EventType.UPDATE_TITLE) {
464 mDateFlags = event.extraLong;
465 }
Michael Chanf0868f62011-01-11 19:37:35 -0800466
467 // Fix up start time if not specified
468 if (startMillis == 0) {
469 event.startTime = mTime;
470 }
471 if (DEBUG) {
472 Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
473 Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
474 Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
475 Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
476 Log.e(TAG, "^^^^^^^^^^^^^^^");
477 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700478
Erik7b92da22010-09-23 14:55:22 -0700479 // Store the eventId if we're entering edit event
Erika7694ee2010-12-07 15:46:18 -0800480 if ((event.eventType
481 & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS))
482 != 0) {
Erik7b92da22010-09-23 14:55:22 -0700483 if (event.id > 0) {
484 mEventId = event.id;
485 } else {
486 mEventId = -1;
487 }
488 }
489
Mason Tang4003d1c2010-08-17 13:50:45 -0700490 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700491 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700492 mDispatchInProgressCounter ++;
Michael Chan3458a172010-07-13 17:58:21 -0700493
Erik3f348f32010-08-10 13:17:19 -0700494 if (DEBUG) {
495 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
496 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700497 // Dispatch to event handler(s)
RoboErik3864be02011-07-25 15:56:50 -0700498 if (mFirstEventHandler != null) {
499 // Handle the 'first' one before handling the others
500 EventHandler handler = mFirstEventHandler.second;
501 if (handler != null && (handler.getSupportedEventTypes() & event.eventType) != 0
502 && !mToBeRemovedEventHandlers.contains(mFirstEventHandler.first)) {
503 handler.handleEvent(event);
504 handled = true;
505 }
506 }
Erik3f348f32010-08-10 13:17:19 -0700507 for (Iterator<Entry<Integer, EventHandler>> handlers =
508 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
509 Entry<Integer, EventHandler> entry = handlers.next();
510 int key = entry.getKey();
RoboErik3864be02011-07-25 15:56:50 -0700511 if (mFirstEventHandler != null && key == mFirstEventHandler.first) {
512 // If this was the 'first' handler it was already handled
513 continue;
514 }
Erik3f348f32010-08-10 13:17:19 -0700515 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700516 if (eventHandler != null
517 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700518 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700519 continue;
520 }
521 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700522 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700523 }
524 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700525
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700526 mDispatchInProgressCounter --;
Erikcb811892010-09-28 13:44:19 -0700527
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700528 if (mDispatchInProgressCounter == 0) {
529
530 // Deregister removed handlers
531 if (mToBeRemovedEventHandlers.size() > 0) {
532 for (Integer zombie : mToBeRemovedEventHandlers) {
533 eventHandlers.remove(zombie);
RoboErik3864be02011-07-25 15:56:50 -0700534 if (mFirstEventHandler != null && zombie.equals(mFirstEventHandler.first)) {
535 mFirstEventHandler = null;
536 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700537 }
538 mToBeRemovedEventHandlers.clear();
Michael Chanab29d9e2010-07-21 06:08:47 -0700539 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700540 // Add new handlers
RoboErik3864be02011-07-25 15:56:50 -0700541 if (mToBeAddedFirstEventHandler != null) {
542 mFirstEventHandler = mToBeAddedFirstEventHandler;
543 mToBeAddedFirstEventHandler = null;
544 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700545 if (mToBeAddedEventHandlers.size() > 0) {
546 for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) {
547 eventHandlers.put(food.getKey(), food.getValue());
548 }
Erikcb811892010-09-28 13:44:19 -0700549 }
550 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700551 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700552
553 if (!handled) {
Erik6b858fc2010-09-15 18:59:04 -0700554 // Launch Settings
555 if (event.eventType == EventType.LAUNCH_SETTINGS) {
Mason Tang4003d1c2010-08-17 13:50:45 -0700556 launchSettings();
557 return;
558 }
559
Michael Chan2aeb8d92011-07-10 13:32:09 -0700560 // Launch Calendar Visible Selector
561 if (event.eventType == EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) {
562 launchSelectVisibleCalendars();
563 return;
564 }
565
Mason Tang4003d1c2010-08-17 13:50:45 -0700566 // Create/View/Edit/Delete Event
567 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
568 if (event.eventType == EventType.CREATE_EVENT) {
Michael Chanedecd9a2011-08-24 23:58:48 -0700569 launchCreateEvent(event.startTime.toMillis(false), endTime,
570 event.extraLong == EXTRA_CREATE_ALL_DAY);
Mason Tang4003d1c2010-08-17 13:50:45 -0700571 return;
572 } else if (event.eventType == EventType.VIEW_EVENT) {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700573 launchViewEvent(event.id, event.startTime.toMillis(false), endTime,
574 event.getResponse());
Mason Tang4003d1c2010-08-17 13:50:45 -0700575 return;
576 } else if (event.eventType == EventType.EDIT_EVENT) {
Erika7694ee2010-12-07 15:46:18 -0800577 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true);
578 return;
579 } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) {
580 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false);
Mason Tang4003d1c2010-08-17 13:50:45 -0700581 return;
582 } else if (event.eventType == EventType.DELETE_EVENT) {
583 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
584 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700585 } else if (event.eventType == EventType.SEARCH) {
586 launchSearch(event.id, event.query, event.componentName);
587 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700588 }
589 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700590 }
591
Erik3f348f32010-08-10 13:17:19 -0700592 /**
593 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
594 * replace fragments based on the view id they are being expanded into.
595 *
596 * @param key The view id or placeholder for this handler
597 * @param eventHandler Typically a fragment or activity in the calendar app
598 */
599 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700600 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700601 if (mDispatchInProgressCounter > 0) {
Erikcb811892010-09-28 13:44:19 -0700602 mToBeAddedEventHandlers.put(key, eventHandler);
603 } else {
604 eventHandlers.put(key, eventHandler);
605 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700606 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700607 }
608
RoboErik3864be02011-07-25 15:56:50 -0700609 public void registerFirstEventHandler(int key, EventHandler eventHandler) {
610 synchronized (this) {
611 registerEventHandler(key, eventHandler);
612 if (mDispatchInProgressCounter > 0) {
613 mToBeAddedFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
614 } else {
615 mFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
616 }
617 }
618 }
619
Erik3f348f32010-08-10 13:17:19 -0700620 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700621 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700622 if (mDispatchInProgressCounter > 0) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700623 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700624 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700625 } else {
Erik3f348f32010-08-10 13:17:19 -0700626 eventHandlers.remove(key);
RoboErik3864be02011-07-25 15:56:50 -0700627 if (mFirstEventHandler != null && mFirstEventHandler.first == key) {
628 mFirstEventHandler = null;
629 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700630 }
631 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700632 }
633
Paul Westbrook61310b72011-10-07 15:43:00 -0700634 public void deregisterAllEventHandlers() {
635 synchronized (this) {
636 if (mDispatchInProgressCounter > 0) {
637 // To avoid ConcurrencyException, stash away the event handler for now.
638 mToBeRemovedEventHandlers.addAll(eventHandlers.keySet());
639 } else {
640 eventHandlers.clear();
641 mFirstEventHandler = null;
642 }
643 }
644 }
645
Michael Chan3458a172010-07-13 17:58:21 -0700646 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700647 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700648 filters.put(sender, eventTypes);
649 }
650
Mason Tang8e3d4302010-07-12 17:39:30 -0700651 /**
652 * @return the time that this controller is currently pointed at
653 */
654 public long getTime() {
655 return mTime.toMillis(false);
656 }
657
Erik7b92da22010-09-23 14:55:22 -0700658 /**
RoboErikc8e0f212011-10-13 17:57:39 -0700659 * @return the last set of date flags sent with
660 * {@link EventType#UPDATE_TITLE}
661 */
662 public long getDateFlags() {
663 return mDateFlags;
664 }
665
666 /**
Erik144edfa2010-11-22 14:23:49 -0800667 * Set the time this controller is currently pointed at
668 *
669 * @param millisTime Time since epoch in millis
670 */
671 public void setTime(long millisTime) {
672 mTime.set(millisTime);
673 }
674
675 /**
Erik7b92da22010-09-23 14:55:22 -0700676 * @return the last event ID the edit view was launched with
677 */
678 public long getEventId() {
679 return mEventId;
680 }
681
Michael Chanab29d9e2010-07-21 06:08:47 -0700682 public int getViewType() {
683 return mViewType;
684 }
685
686 public int getPreviousViewType() {
687 return mPreviousViewType;
688 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700689
Michael Chan2aeb8d92011-07-10 13:32:09 -0700690 private void launchSelectVisibleCalendars() {
691 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700692 intent.setClass(mContext, SelectVisibleCalendarsActivity.class);
Michael Chan2aeb8d92011-07-10 13:32:09 -0700693 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
694 mContext.startActivity(intent);
695 }
696
Michael Chan9e89dca2010-07-13 17:56:09 -0700697 private void launchSettings() {
698 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700699 intent.setClass(mContext, CalendarSettingsActivity.class);
Michael Chan9e89dca2010-07-13 17:56:09 -0700700 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700701 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700702 }
703
Michael Chanedecd9a2011-08-24 23:58:48 -0700704 private void launchCreateEvent(long startMillis, long endMillis, boolean allDayEvent) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700705 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700706 intent.setClass(mContext, EditEventActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700707 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
708 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanedecd9a2011-08-24 23:58:48 -0700709 intent.putExtra(EXTRA_EVENT_ALL_DAY, allDayEvent);
Erik7b92da22010-09-23 14:55:22 -0700710 mEventId = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -0700711 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700712 }
713
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700714 public void launchViewEvent(long eventId, long startMillis, long endMillis, int response) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700715 Intent intent = new Intent(Intent.ACTION_VIEW);
716 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
717 intent.setData(eventUri);
Michael Chan07d9fee2011-07-25 09:41:21 -0700718 intent.setClass(mContext, AllInOneActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700719 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
720 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700721 intent.putExtra(ATTENDEE_STATUS, response);
Isaac Katzenelson4024dee2011-09-29 14:40:46 -0700722 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700723 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700724 }
725
Erika7694ee2010-12-07 15:46:18 -0800726 private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700727 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
728 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
RoboErika27a8862011-06-23 15:26:23 -0700729 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
730 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700731 intent.setClass(mContext, EditEventActivity.class);
Erika7694ee2010-12-07 15:46:18 -0800732 intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
Erik7b92da22010-09-23 14:55:22 -0700733 mEventId = eventId;
Michael Chanab29d9e2010-07-21 06:08:47 -0700734 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700735 }
736
Michael Chan1881a892011-02-08 18:16:58 -0800737// private void launchAlerts() {
738// Intent intent = new Intent();
739// intent.setClass(mContext, AlertActivity.class);
740// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
741// mContext.startActivity(intent);
742// }
743
Michael Chan9e89dca2010-07-13 17:56:09 -0700744 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
745 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
746 }
747
748 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
749 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700750 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700751 parentActivity != null /* exit when done */);
752 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
753 }
Michael Chan3458a172010-07-13 17:58:21 -0700754
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700755 private void launchSearch(long eventId, String query, ComponentName componentName) {
756 final SearchManager searchManager =
757 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
758 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
759 final Intent intent = new Intent(Intent.ACTION_SEARCH);
760 intent.putExtra(SearchManager.QUERY, query);
761 intent.setComponent(searchableInfo.getSearchActivity());
RoboErik2bd5cc02011-07-28 14:19:54 -0700762 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700763 mContext.startActivity(intent);
764 }
765
Andy McFadden7dcd3012011-11-17 13:47:13 -0800766 /**
767 * Performs a manual refresh of calendars in all known accounts.
768 */
Erikba1b94a2010-07-20 17:50:50 -0700769 public void refreshCalendars() {
Andy McFadden7dcd3012011-11-17 13:47:13 -0800770 Account[] accounts = AccountManager.get(mContext).getAccounts();
771 Log.d(TAG, "Refreshing " + accounts.length + " accounts");
772
773 String authority = Calendars.CONTENT_URI.getAuthority();
774 for (int i = 0; i < accounts.length; i++) {
775 if (Log.isLoggable(TAG, Log.DEBUG)) {
776 Log.d(TAG, "Refreshing calendars for: " + accounts[i]);
777 }
778 Bundle extras = new Bundle();
779 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
780 ContentResolver.requestSync(accounts[i], authority, extras);
781 }
Erikba1b94a2010-07-20 17:50:50 -0700782 }
783
Erikdd95df52010-08-27 09:31:18 -0700784 // Forces the viewType. Should only be used for initialization.
785 public void setViewType(int viewType) {
786 mViewType = viewType;
787 }
788
Erik7b92da22010-09-23 14:55:22 -0700789 // Sets the eventId. Should only be used for initialization.
790 public void setEventId(long eventId) {
791 mEventId = eventId;
792 }
793
Michael Chan3458a172010-07-13 17:58:21 -0700794 private String eventInfoToString(EventInfo eventInfo) {
795 String tmp = "Unknown";
796
797 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700798 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700799 tmp = "Go to time/event";
800 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
801 tmp = "New event";
802 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
803 tmp = "View event";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800804 } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) {
805 tmp = "View details";
Michael Chan3458a172010-07-13 17:58:21 -0700806 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
807 tmp = "Edit event";
808 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
809 tmp = "Delete event";
Michael Chan2aeb8d92011-07-10 13:32:09 -0700810 } else if ((eventInfo.eventType & EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) != 0) {
811 tmp = "Launch select visible calendars";
Michael Chan3458a172010-07-13 17:58:21 -0700812 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
813 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700814 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
815 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700816 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
817 tmp = "Search";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800818 } else if ((eventInfo.eventType & EventType.USER_HOME) != 0) {
819 tmp = "Gone home";
820 } else if ((eventInfo.eventType & EventType.UPDATE_TITLE) != 0) {
821 tmp = "Update title";
Michael Chan3458a172010-07-13 17:58:21 -0700822 }
823 builder.append(tmp);
824 builder.append(": id=");
825 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700826 builder.append(", selected=");
827 builder.append(eventInfo.selectedTime);
828 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700829 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700830 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700831 builder.append(eventInfo.endTime);
832 builder.append(", viewType=");
833 builder.append(eventInfo.viewType);
834 builder.append(", x=");
835 builder.append(eventInfo.x);
836 builder.append(", y=");
837 builder.append(eventInfo.y);
838 return builder.toString();
839 }
Michael Chan49701592010-06-30 11:04:03 -0700840}