blob: c4d171bbd23779f53f53e2bc0067ceabde92c2c7 [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 Katzenelson60f01c22011-06-03 15:42:01 -070070 public static final String EVENT_ATTENDEE_RESPONSE = "attendeeResponse";
Michael Chandeced892010-12-10 15:59:35 -080071
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070072 private final Context mContext;
Michael Chanab29d9e2010-07-21 06:08:47 -070073
Erik3f348f32010-08-10 13:17:19 -070074 // This uses a LinkedHashMap so that we can replace fragments based on the
75 // view id they are being expanded into since we can't guarantee a reference
76 // to the handler will be findable
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070077 private final LinkedHashMap<Integer,EventHandler> eventHandlers =
Erik3f348f32010-08-10 13:17:19 -070078 new LinkedHashMap<Integer,EventHandler>(5);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070079 private final LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>();
80 private final LinkedHashMap<Integer, EventHandler> mToBeAddedEventHandlers = new LinkedHashMap<
Erikcb811892010-09-28 13:44:19 -070081 Integer, EventHandler>();
RoboErik3864be02011-07-25 15:56:50 -070082 private Pair<Integer, EventHandler> mFirstEventHandler;
83 private Pair<Integer, EventHandler> mToBeAddedFirstEventHandler;
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -070084 private volatile int mDispatchInProgressCounter = 0;
Michael Chanab29d9e2010-07-21 06:08:47 -070085
Michael Chan0558def2010-07-22 21:30:32 -070086 private static WeakHashMap<Context, CalendarController> instances =
87 new WeakHashMap<Context, CalendarController>();
88
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070089 private final WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
Michael Chan83b0fe32010-07-08 16:46:26 -070090
Michael Chan3458a172010-07-13 17:58:21 -070091 private int mViewType = -1;
Michael Chan85e55092010-07-27 15:18:40 -070092 private int mDetailViewType = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -070093 private int mPreviousViewType = -1;
Erik7b92da22010-09-23 14:55:22 -070094 private long mEventId = -1;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070095 private final Time mTime = new Time();
RoboErikc8e0f212011-10-13 17:57:39 -070096 private long mDateFlags = 0;
Michael Chan3458a172010-07-13 17:58:21 -070097
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -070098 private final Runnable mUpdateTimezone = new Runnable() {
RoboErik4679f602011-03-24 15:39:40 -070099 @Override
100 public void run() {
101 mTime.switchTimezone(Utils.getTimeZone(mContext, this));
102 }
103 };
104
Michael Chan49701592010-06-30 11:04:03 -0700105 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700106 * One of the event types that are sent to or from the controller
Michael Chan49701592010-06-30 11:04:03 -0700107 */
Erik25251192010-07-12 15:30:14 -0700108 public interface EventType {
Michael Chan9e89dca2010-07-13 17:56:09 -0700109 final long CREATE_EVENT = 1L;
Michael Chandeced892010-12-10 15:59:35 -0800110
111 // Simple view of an event
Michael Chan83b0fe32010-07-08 16:46:26 -0700112 final long VIEW_EVENT = 1L << 1;
Michael Chandeced892010-12-10 15:59:35 -0800113
114 // Full detail view in read only mode
115 final long VIEW_EVENT_DETAILS = 1L << 2;
116
Erika7694ee2010-12-07 15:46:18 -0800117 // full detail view in edit mode
Michael Chandeced892010-12-10 15:59:35 -0800118 final long EDIT_EVENT = 1L << 3;
Michael Chan83b0fe32010-07-08 16:46:26 -0700119
Michael Chandeced892010-12-10 15:59:35 -0800120 final long DELETE_EVENT = 1L << 4;
Michael Chan83b0fe32010-07-08 16:46:26 -0700121
Michael Chandeced892010-12-10 15:59:35 -0800122 final long GO_TO = 1L << 5;
Erik954c8712010-08-06 10:12:34 -0700123
Michael Chandeced892010-12-10 15:59:35 -0800124 final long LAUNCH_SETTINGS = 1L << 6;
Mason Tang4003d1c2010-08-17 13:50:45 -0700125
Michael Chandeced892010-12-10 15:59:35 -0800126 final long EVENTS_CHANGED = 1L << 7;
127
128 final long SEARCH = 1L << 8;
Erik7b92da22010-09-23 14:55:22 -0700129
130 // User has pressed the home key
Michael Chandeced892010-12-10 15:59:35 -0800131 final long USER_HOME = 1L << 9;
Erikeaafa2b2010-12-23 14:30:37 -0800132
133 // date range has changed, update the title
134 final long UPDATE_TITLE = 1L << 10;
Michael Chan2aeb8d92011-07-10 13:32:09 -0700135
136 // select which calendars to display
137 final long LAUNCH_SELECT_VISIBLE_CALENDARS = 1L << 11;
Michael Chan83b0fe32010-07-08 16:46:26 -0700138 }
Michael Chan49701592010-06-30 11:04:03 -0700139
140 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700141 * One of the Agenda/Day/Week/Month view types
Michael Chan49701592010-06-30 11:04:03 -0700142 */
Erik25251192010-07-12 15:30:14 -0700143 public interface ViewType {
Michael Chand6734db2010-07-22 00:48:08 -0700144 final int DETAIL = -1;
Michael Chan3458a172010-07-13 17:58:21 -0700145 final int CURRENT = 0;
146 final int AGENDA = 1;
147 final int DAY = 2;
148 final int WEEK = 3;
149 final int MONTH = 4;
Erikdd95df52010-08-27 09:31:18 -0700150 final int EDIT = 5;
Michael Chan83b0fe32010-07-08 16:46:26 -0700151 }
152
Erik25251192010-07-12 15:30:14 -0700153 public static class EventInfo {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700154
155 private static final long ATTENTEE_STATUS_MASK = 0xFF;
156 private static final long ALL_DAY_MASK = 0x100;
157 private static final int ATTENDEE_STATUS_NONE_MASK = 0x01;
158 private static final int ATTENDEE_STATUS_ACCEPTED_MASK = 0x02;
159 private static final int ATTENDEE_STATUS_DECLINED_MASK = 0x04;
160 private static final int ATTENDEE_STATUS_TENTATIVE_MASK = 0x08;
161
Mason Tang00b8c1a2010-08-23 12:02:00 -0700162 public long eventType; // one of the EventType
163 public int viewType; // one of the ViewType
164 public long id; // event id
165 public Time selectedTime; // the selected time in focus
166 public Time startTime; // start of a range of time.
167 public Time endTime; // end of a range of time.
168 public int x; // x coordinate in the activity space
169 public int y; // y coordinate in the activity space
170 public String query; // query for a user search
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700171 public ComponentName componentName; // used in combination with query
Michael Chan46a8b112010-12-14 16:36:27 -0800172
173 /**
174 * For EventType.VIEW_EVENT:
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700175 * It is the default attendee response and an all day event indicator.
176 * Set to Attendees.ATTENDEE_STATUS_NONE, Attendees.ATTENDEE_STATUS_ACCEPTED,
177 * Attendees.ATTENDEE_STATUS_DECLINED, or Attendees.ATTENDEE_STATUS_TENTATIVE.
178 * To signal the event is an all-day event, "or" ALL_DAY_MASK with the response.
179 * Alternatively, use buildViewExtraLong(), getResponse(), and isAllDay().
Michael Chan46a8b112010-12-14 16:36:27 -0800180 * <p>
Michael Chanedecd9a2011-08-24 23:58:48 -0700181 * For EventType.CREATE_EVENT:
182 * Set to {@link #EXTRA_CREATE_ALL_DAY} for creating an all-day event.
183 * <p>
Michael Chan46a8b112010-12-14 16:36:27 -0800184 * For EventType.GO_TO:
185 * Set to {@link #EXTRA_GOTO_TIME} to go to the specified date/time.
186 * Set to {@link #EXTRA_GOTO_DATE} to consider the date but ignore the time.
Michael Chanfb0ec222011-08-12 17:39:30 -0700187 * Set to {@link #EXTRA_GOTO_BACK_TO_PREVIOUS} if back should bring back previous view.
RoboErik5b872522011-10-03 17:24:50 -0700188 * 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 -0700189 * <p>
190 * For EventType.UPDATE_TITLE:
191 * Set formatting flags for Utils.formatDateRange
Michael Chan46a8b112010-12-14 16:36:27 -0800192 */
193 public long extraLong;
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700194
195 public boolean isAllDay() {
196 if (eventType != EventType.VIEW_EVENT) {
197 Log.wtf(TAG, "illegal call to isAllDay , wrong event type " + eventType);
198 return false;
199 }
200 return ((extraLong & ALL_DAY_MASK) != 0) ? true : false;
201 }
202
203 public int getResponse() {
204 if (eventType != EventType.VIEW_EVENT) {
205 Log.wtf(TAG, "illegal call to getResponse , wrong event type " + eventType);
206 return Attendees.ATTENDEE_STATUS_NONE;
207 }
208
209 int response = (int)(extraLong & ATTENTEE_STATUS_MASK);
210 switch (response) {
211 case ATTENDEE_STATUS_NONE_MASK:
212 return Attendees.ATTENDEE_STATUS_NONE;
213 case ATTENDEE_STATUS_ACCEPTED_MASK:
214 return Attendees.ATTENDEE_STATUS_ACCEPTED;
215 case ATTENDEE_STATUS_DECLINED_MASK:
216 return Attendees.ATTENDEE_STATUS_DECLINED;
217 case ATTENDEE_STATUS_TENTATIVE_MASK:
218 return Attendees.ATTENDEE_STATUS_TENTATIVE;
219 default:
220 Log.wtf(TAG,"Unknown attendee response " + response);
221 }
222 return ATTENDEE_STATUS_NONE_MASK;
223 }
224
225 // Used to build the extra long for a VIEW event.
226 public static long buildViewExtraLong(int response, boolean allDay) {
227 long extra = allDay ? ALL_DAY_MASK : 0;
228
229 switch (response) {
230 case Attendees.ATTENDEE_STATUS_NONE:
231 extra |= ATTENDEE_STATUS_NONE_MASK;
232 break;
233 case Attendees.ATTENDEE_STATUS_ACCEPTED:
234 extra |= ATTENDEE_STATUS_ACCEPTED_MASK;
235 break;
236 case Attendees.ATTENDEE_STATUS_DECLINED:
237 extra |= ATTENDEE_STATUS_DECLINED_MASK;
238 break;
239 case Attendees.ATTENDEE_STATUS_TENTATIVE:
240 extra |= ATTENDEE_STATUS_TENTATIVE_MASK;
241 break;
242 default:
243 Log.wtf(TAG,"Unknown attendee response " + response);
244 extra |= ATTENDEE_STATUS_NONE_MASK;
245 break;
246 }
247 return extra;
248 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700249 }
250
Michael Chan46a8b112010-12-14 16:36:27 -0800251 /**
Michael Chanedecd9a2011-08-24 23:58:48 -0700252 * Pass to the ExtraLong parameter for EventType.CREATE_EVENT to create
253 * an all-day event
254 */
255 public static final long EXTRA_CREATE_ALL_DAY = 0x10;
256
257 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800258 * Pass to the ExtraLong parameter for EventType.GO_TO to signal the time
259 * can be ignored
260 */
261 public static final long EXTRA_GOTO_DATE = 1;
Michael Chanfb0ec222011-08-12 17:39:30 -0700262 public static final long EXTRA_GOTO_TIME = 2;
263 public static final long EXTRA_GOTO_BACK_TO_PREVIOUS = 4;
RoboErik5b872522011-10-03 17:24:50 -0700264 public static final long EXTRA_GOTO_TODAY = 8;
Michael Chan46a8b112010-12-14 16:36:27 -0800265
Erik25251192010-07-12 15:30:14 -0700266 public interface EventHandler {
Michael Chan83b0fe32010-07-08 16:46:26 -0700267 long getSupportedEventTypes();
268 void handleEvent(EventInfo event);
269
270 /**
Erik954c8712010-08-06 10:12:34 -0700271 * This notifies the handler that the database has changed and it should
272 * update its view.
Michael Chan83b0fe32010-07-08 16:46:26 -0700273 */
274 void eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700275 }
276
Michael Chan0558def2010-07-22 21:30:32 -0700277 /**
278 * Creates and/or returns an instance of CalendarController associated with
279 * the supplied context. It is best to pass in the current Activity.
280 *
281 * @param context The activity if at all possible.
282 */
283 public static CalendarController getInstance(Context context) {
284 synchronized (instances) {
285 CalendarController controller = instances.get(context);
286 if (controller == null) {
287 controller = new CalendarController(context);
288 instances.put(context, controller);
289 }
290 return controller;
291 }
292 }
293
Eriked61b482010-08-13 13:59:50 -0700294 /**
295 * Removes an instance when it is no longer needed. This should be called in
296 * an activity's onDestroy method.
297 *
298 * @param context The activity used to create the controller
299 */
300 public static void removeInstance(Context context) {
301 instances.remove(context);
302 }
303
Michael Chan0558def2010-07-22 21:30:32 -0700304 private CalendarController(Context context) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700305 mContext = context;
RoboErik4679f602011-03-24 15:39:40 -0700306 mUpdateTimezone.run();
Michael Chan3458a172010-07-13 17:58:21 -0700307 mTime.setToNow();
Michael Chan85e55092010-07-27 15:18:40 -0700308 mDetailViewType = Utils.getSharedPreference(mContext,
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700309 GeneralPreferences.KEY_DETAILED_VIEW,
310 GeneralPreferences.DEFAULT_DETAILED_VIEW);
Michael Chan83b0fe32010-07-08 16:46:26 -0700311 }
Michael Chan49701592010-06-30 11:04:03 -0700312
Michael Chandeced892010-12-10 15:59:35 -0800313 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
RoboErik87f993f2011-02-03 17:47:20 -0800314 long endMillis, int x, int y, long selectedMillis) {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700315 // TODO: pass the real allDay status or at least a status that says we don't know the
316 // status and have the receiver query the data.
317 // The current use of this method for VIEW_EVENT is by the day view to show an EventInfo
318 // so currently the missing allDay status has no effect.
Michael Chanedecd9a2011-08-24 23:58:48 -0700319 sendEventRelatedEventWithExtra(sender, eventType, eventId, startMillis, endMillis, x, y,
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700320 EventInfo.buildViewExtraLong(Attendees.ATTENDEE_STATUS_NONE, false),
321 selectedMillis);
Michael Chandeced892010-12-10 15:59:35 -0800322 }
323
Michael Chan49701592010-06-30 11:04:03 -0700324 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700325 * Helper for sending New/View/Edit/Delete events
326 *
327 * @param sender object of the caller
328 * @param eventType one of {@link EventType}
329 * @param eventId event id
Michael Chan3458a172010-07-13 17:58:21 -0700330 * @param startMillis start time
331 * @param endMillis end time
Michael Chan83b0fe32010-07-08 16:46:26 -0700332 * @param x x coordinate in the activity space
333 * @param y y coordinate in the activity space
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700334 * @param extraLong default response value for the "simple event view" and all day indication.
335 * Use Attendees.ATTENDEE_STATUS_NONE for no response.
RoboErik87f993f2011-02-03 17:47:20 -0800336 * @param selectedMillis The time to specify as selected
Michael Chan49701592010-06-30 11:04:03 -0700337 */
Michael Chanedecd9a2011-08-24 23:58:48 -0700338 public void sendEventRelatedEventWithExtra(Object sender, long eventType, long eventId,
RoboErik87f993f2011-02-03 17:47:20 -0800339 long startMillis, long endMillis, int x, int y, long extraLong, long selectedMillis) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700340 EventInfo info = new EventInfo();
341 info.eventType = eventType;
Erika7694ee2010-12-07 15:46:18 -0800342 if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) {
Erik5f620792010-10-27 12:42:01 -0700343 info.viewType = ViewType.CURRENT;
Erikdd95df52010-08-27 09:31:18 -0700344 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700345 info.id = eventId;
RoboErik4679f602011-03-24 15:39:40 -0700346 info.startTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700347 info.startTime.set(startMillis);
RoboErik87f993f2011-02-03 17:47:20 -0800348 if (selectedMillis != -1) {
RoboErik4679f602011-03-24 15:39:40 -0700349 info.selectedTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
RoboErik87f993f2011-02-03 17:47:20 -0800350 info.selectedTime.set(selectedMillis);
351 } else {
352 info.selectedTime = info.startTime;
353 }
RoboErik4679f602011-03-24 15:39:40 -0700354 info.endTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone));
Michael Chan9e89dca2010-07-13 17:56:09 -0700355 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700356 info.x = x;
357 info.y = y;
Michael Chandeced892010-12-10 15:59:35 -0800358 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700359 this.sendEvent(sender, info);
360 }
Michael Chan49701592010-06-30 11:04:03 -0700361
362 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700363 * Helper for sending non-calendar-event events
364 *
365 * @param sender object of the caller
366 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700367 * @param start start time
368 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700369 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700370 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700371 */
Erik25251192010-07-12 15:30:14 -0700372 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700373 int viewType) {
Michael Chanf0868f62011-01-11 19:37:35 -0800374 sendEvent(sender, eventType, start, end, start, eventId, viewType, EXTRA_GOTO_TIME, null,
375 null);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700376 }
377
378 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800379 * sendEvent() variant with extraLong, search query, and search component name.
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700380 */
381 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan46a8b112010-12-14 16:36:27 -0800382 int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chanf0868f62011-01-11 19:37:35 -0800383 sendEvent(sender, eventType, start, end, start, eventId, viewType, extraLong, query,
384 componentName);
385 }
386
387 public void sendEvent(Object sender, long eventType, Time start, Time end, Time selected,
388 long eventId, int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700389 EventInfo info = new EventInfo();
390 info.eventType = eventType;
391 info.startTime = start;
Michael Chanf0868f62011-01-11 19:37:35 -0800392 info.selectedTime = selected;
Michael Chan83b0fe32010-07-08 16:46:26 -0700393 info.endTime = end;
394 info.id = eventId;
395 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700396 info.query = query;
397 info.componentName = componentName;
Michael Chan46a8b112010-12-14 16:36:27 -0800398 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700399 this.sendEvent(sender, info);
400 }
401
Erik25251192010-07-12 15:30:14 -0700402 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700403 // TODO Throw exception on invalid events
404
Erik3f348f32010-08-10 13:17:19 -0700405 if (DEBUG) {
406 Log.d(TAG, eventInfoToString(event));
407 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700408
409 Long filteredTypes = filters.get(sender);
410 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
411 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700412 if (DEBUG) {
413 Log.d(TAG, "Event suppressed");
414 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700415 return;
416 }
417
Michael Chanab29d9e2010-07-21 06:08:47 -0700418 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700419
Michael Chan3458a172010-07-13 17:58:21 -0700420 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700421 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700422 event.viewType = mDetailViewType;
423 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700424 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700425 event.viewType = mViewType;
Michael Chanf0868f62011-01-11 19:37:35 -0800426 } else if (event.viewType != ViewType.EDIT) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700427 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700428
Erik63cd0532011-01-26 14:16:03 -0800429 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY
430 || (Utils.getAllowWeekForDetailView() && event.viewType == ViewType.WEEK)) {
Michael Chan85e55092010-07-27 15:18:40 -0700431 mDetailViewType = mViewType;
432 }
Michael Chan3458a172010-07-13 17:58:21 -0700433 }
434
Michael Chanf0868f62011-01-11 19:37:35 -0800435 if (DEBUG) {
436 Log.e(TAG, "vvvvvvvvvvvvvvv");
437 Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
438 Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
439 Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
440 Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
Mason Tang4003d1c2010-08-17 13:50:45 -0700441 }
Michael Chanf0868f62011-01-11 19:37:35 -0800442
443 long startMillis = 0;
444 if (event.startTime != null) {
445 startMillis = event.startTime.toMillis(false);
446 }
447
448 // Set mTime if selectedTime is set
449 if (event.selectedTime != null && event.selectedTime.toMillis(false) != 0) {
450 mTime.set(event.selectedTime);
451 } else {
452 if (startMillis != 0) {
453 // selectedTime is not set so set mTime to startTime iff it is not
454 // within start and end times
455 long mtimeMillis = mTime.toMillis(false);
456 if (mtimeMillis < startMillis
457 || (event.endTime != null && mtimeMillis > event.endTime.toMillis(false))) {
458 mTime.set(event.startTime);
459 }
460 }
461 event.selectedTime = mTime;
462 }
RoboErikc8e0f212011-10-13 17:57:39 -0700463 // Store the formatting flags if this is an update to the title
464 if (event.eventType == EventType.UPDATE_TITLE) {
465 mDateFlags = event.extraLong;
466 }
Michael Chanf0868f62011-01-11 19:37:35 -0800467
468 // Fix up start time if not specified
469 if (startMillis == 0) {
470 event.startTime = mTime;
471 }
472 if (DEBUG) {
473 Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString()));
474 Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString()));
475 Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString()));
476 Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString()));
477 Log.e(TAG, "^^^^^^^^^^^^^^^");
478 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700479
Erik7b92da22010-09-23 14:55:22 -0700480 // Store the eventId if we're entering edit event
Erika7694ee2010-12-07 15:46:18 -0800481 if ((event.eventType
482 & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS))
483 != 0) {
Erik7b92da22010-09-23 14:55:22 -0700484 if (event.id > 0) {
485 mEventId = event.id;
486 } else {
487 mEventId = -1;
488 }
489 }
490
Mason Tang4003d1c2010-08-17 13:50:45 -0700491 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700492 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700493 mDispatchInProgressCounter ++;
Michael Chan3458a172010-07-13 17:58:21 -0700494
Erik3f348f32010-08-10 13:17:19 -0700495 if (DEBUG) {
496 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
497 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700498 // Dispatch to event handler(s)
RoboErik3864be02011-07-25 15:56:50 -0700499 if (mFirstEventHandler != null) {
500 // Handle the 'first' one before handling the others
501 EventHandler handler = mFirstEventHandler.second;
502 if (handler != null && (handler.getSupportedEventTypes() & event.eventType) != 0
503 && !mToBeRemovedEventHandlers.contains(mFirstEventHandler.first)) {
504 handler.handleEvent(event);
505 handled = true;
506 }
507 }
Erik3f348f32010-08-10 13:17:19 -0700508 for (Iterator<Entry<Integer, EventHandler>> handlers =
509 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
510 Entry<Integer, EventHandler> entry = handlers.next();
511 int key = entry.getKey();
RoboErik3864be02011-07-25 15:56:50 -0700512 if (mFirstEventHandler != null && key == mFirstEventHandler.first) {
513 // If this was the 'first' handler it was already handled
514 continue;
515 }
Erik3f348f32010-08-10 13:17:19 -0700516 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700517 if (eventHandler != null
518 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700519 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700520 continue;
521 }
522 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700523 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700524 }
525 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700526
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700527 mDispatchInProgressCounter --;
Erikcb811892010-09-28 13:44:19 -0700528
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700529 if (mDispatchInProgressCounter == 0) {
530
531 // Deregister removed handlers
532 if (mToBeRemovedEventHandlers.size() > 0) {
533 for (Integer zombie : mToBeRemovedEventHandlers) {
534 eventHandlers.remove(zombie);
RoboErik3864be02011-07-25 15:56:50 -0700535 if (mFirstEventHandler != null && zombie.equals(mFirstEventHandler.first)) {
536 mFirstEventHandler = null;
537 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700538 }
539 mToBeRemovedEventHandlers.clear();
Michael Chanab29d9e2010-07-21 06:08:47 -0700540 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700541 // Add new handlers
RoboErik3864be02011-07-25 15:56:50 -0700542 if (mToBeAddedFirstEventHandler != null) {
543 mFirstEventHandler = mToBeAddedFirstEventHandler;
544 mToBeAddedFirstEventHandler = null;
545 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700546 if (mToBeAddedEventHandlers.size() > 0) {
547 for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) {
548 eventHandlers.put(food.getKey(), food.getValue());
549 }
Erikcb811892010-09-28 13:44:19 -0700550 }
551 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700552 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700553
554 if (!handled) {
Erik6b858fc2010-09-15 18:59:04 -0700555 // Launch Settings
556 if (event.eventType == EventType.LAUNCH_SETTINGS) {
Mason Tang4003d1c2010-08-17 13:50:45 -0700557 launchSettings();
558 return;
559 }
560
Michael Chan2aeb8d92011-07-10 13:32:09 -0700561 // Launch Calendar Visible Selector
562 if (event.eventType == EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) {
563 launchSelectVisibleCalendars();
564 return;
565 }
566
Mason Tang4003d1c2010-08-17 13:50:45 -0700567 // Create/View/Edit/Delete Event
568 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
569 if (event.eventType == EventType.CREATE_EVENT) {
Michael Chanedecd9a2011-08-24 23:58:48 -0700570 launchCreateEvent(event.startTime.toMillis(false), endTime,
571 event.extraLong == EXTRA_CREATE_ALL_DAY);
Mason Tang4003d1c2010-08-17 13:50:45 -0700572 return;
573 } else if (event.eventType == EventType.VIEW_EVENT) {
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700574 launchViewEvent(event.id, event.startTime.toMillis(false), endTime,
575 event.getResponse());
Mason Tang4003d1c2010-08-17 13:50:45 -0700576 return;
577 } else if (event.eventType == EventType.EDIT_EVENT) {
Erika7694ee2010-12-07 15:46:18 -0800578 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true);
579 return;
580 } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) {
581 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false);
Mason Tang4003d1c2010-08-17 13:50:45 -0700582 return;
583 } else if (event.eventType == EventType.DELETE_EVENT) {
584 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
585 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700586 } else if (event.eventType == EventType.SEARCH) {
587 launchSearch(event.id, event.query, event.componentName);
588 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700589 }
590 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700591 }
592
Erik3f348f32010-08-10 13:17:19 -0700593 /**
594 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
595 * replace fragments based on the view id they are being expanded into.
596 *
597 * @param key The view id or placeholder for this handler
598 * @param eventHandler Typically a fragment or activity in the calendar app
599 */
600 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700601 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700602 if (mDispatchInProgressCounter > 0) {
Erikcb811892010-09-28 13:44:19 -0700603 mToBeAddedEventHandlers.put(key, eventHandler);
604 } else {
605 eventHandlers.put(key, eventHandler);
606 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700607 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700608 }
609
RoboErik3864be02011-07-25 15:56:50 -0700610 public void registerFirstEventHandler(int key, EventHandler eventHandler) {
611 synchronized (this) {
612 registerEventHandler(key, eventHandler);
613 if (mDispatchInProgressCounter > 0) {
614 mToBeAddedFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
615 } else {
616 mFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler);
617 }
618 }
619 }
620
Erik3f348f32010-08-10 13:17:19 -0700621 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700622 synchronized (this) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700623 if (mDispatchInProgressCounter > 0) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700624 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700625 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700626 } else {
Erik3f348f32010-08-10 13:17:19 -0700627 eventHandlers.remove(key);
RoboErik3864be02011-07-25 15:56:50 -0700628 if (mFirstEventHandler != null && mFirstEventHandler.first == key) {
629 mFirstEventHandler = null;
630 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700631 }
632 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700633 }
634
Paul Westbrook61310b72011-10-07 15:43:00 -0700635 public void deregisterAllEventHandlers() {
636 synchronized (this) {
637 if (mDispatchInProgressCounter > 0) {
638 // To avoid ConcurrencyException, stash away the event handler for now.
639 mToBeRemovedEventHandlers.addAll(eventHandlers.keySet());
640 } else {
641 eventHandlers.clear();
642 mFirstEventHandler = null;
643 }
644 }
645 }
646
Michael Chan3458a172010-07-13 17:58:21 -0700647 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700648 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700649 filters.put(sender, eventTypes);
650 }
651
Mason Tang8e3d4302010-07-12 17:39:30 -0700652 /**
653 * @return the time that this controller is currently pointed at
654 */
655 public long getTime() {
656 return mTime.toMillis(false);
657 }
658
Erik7b92da22010-09-23 14:55:22 -0700659 /**
RoboErikc8e0f212011-10-13 17:57:39 -0700660 * @return the last set of date flags sent with
661 * {@link EventType#UPDATE_TITLE}
662 */
663 public long getDateFlags() {
664 return mDateFlags;
665 }
666
667 /**
Erik144edfa2010-11-22 14:23:49 -0800668 * Set the time this controller is currently pointed at
669 *
670 * @param millisTime Time since epoch in millis
671 */
672 public void setTime(long millisTime) {
673 mTime.set(millisTime);
674 }
675
676 /**
Erik7b92da22010-09-23 14:55:22 -0700677 * @return the last event ID the edit view was launched with
678 */
679 public long getEventId() {
680 return mEventId;
681 }
682
Michael Chanab29d9e2010-07-21 06:08:47 -0700683 public int getViewType() {
684 return mViewType;
685 }
686
687 public int getPreviousViewType() {
688 return mPreviousViewType;
689 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700690
Michael Chan2aeb8d92011-07-10 13:32:09 -0700691 private void launchSelectVisibleCalendars() {
692 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700693 intent.setClass(mContext, SelectVisibleCalendarsActivity.class);
Michael Chan2aeb8d92011-07-10 13:32:09 -0700694 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
695 mContext.startActivity(intent);
696 }
697
Michael Chan9e89dca2010-07-13 17:56:09 -0700698 private void launchSettings() {
699 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700700 intent.setClass(mContext, CalendarSettingsActivity.class);
Michael Chan9e89dca2010-07-13 17:56:09 -0700701 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700702 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700703 }
704
Michael Chanedecd9a2011-08-24 23:58:48 -0700705 private void launchCreateEvent(long startMillis, long endMillis, boolean allDayEvent) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700706 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chan07d9fee2011-07-25 09:41:21 -0700707 intent.setClass(mContext, EditEventActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700708 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
709 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanedecd9a2011-08-24 23:58:48 -0700710 intent.putExtra(EXTRA_EVENT_ALL_DAY, allDayEvent);
Erik7b92da22010-09-23 14:55:22 -0700711 mEventId = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -0700712 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700713 }
714
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700715 public void launchViewEvent(long eventId, long startMillis, long endMillis, int response) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700716 Intent intent = new Intent(Intent.ACTION_VIEW);
717 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
718 intent.setData(eventUri);
Michael Chan07d9fee2011-07-25 09:41:21 -0700719 intent.setClass(mContext, AllInOneActivity.class);
RoboErika27a8862011-06-23 15:26:23 -0700720 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
721 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Isaac Katzenelson6bcafcf2012-05-02 18:25:36 -0700722 intent.putExtra(ATTENDEE_STATUS, response);
Isaac Katzenelson4024dee2011-09-29 14:40:46 -0700723 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700724 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700725 }
726
Erika7694ee2010-12-07 15:46:18 -0800727 private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700728 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
729 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
RoboErika27a8862011-06-23 15:26:23 -0700730 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
731 intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700732 intent.setClass(mContext, EditEventActivity.class);
Erika7694ee2010-12-07 15:46:18 -0800733 intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
Erik7b92da22010-09-23 14:55:22 -0700734 mEventId = eventId;
Michael Chanab29d9e2010-07-21 06:08:47 -0700735 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700736 }
737
Michael Chan1881a892011-02-08 18:16:58 -0800738// private void launchAlerts() {
739// Intent intent = new Intent();
740// intent.setClass(mContext, AlertActivity.class);
741// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
742// mContext.startActivity(intent);
743// }
744
Michael Chan9e89dca2010-07-13 17:56:09 -0700745 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
746 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
747 }
748
749 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
750 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700751 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700752 parentActivity != null /* exit when done */);
753 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
754 }
Michael Chan3458a172010-07-13 17:58:21 -0700755
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700756 private void launchSearch(long eventId, String query, ComponentName componentName) {
757 final SearchManager searchManager =
758 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
759 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
760 final Intent intent = new Intent(Intent.ACTION_SEARCH);
761 intent.putExtra(SearchManager.QUERY, query);
762 intent.setComponent(searchableInfo.getSearchActivity());
RoboErik2bd5cc02011-07-28 14:19:54 -0700763 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700764 mContext.startActivity(intent);
765 }
766
Andy McFadden7dcd3012011-11-17 13:47:13 -0800767 /**
768 * Performs a manual refresh of calendars in all known accounts.
769 */
Erikba1b94a2010-07-20 17:50:50 -0700770 public void refreshCalendars() {
Andy McFadden7dcd3012011-11-17 13:47:13 -0800771 Account[] accounts = AccountManager.get(mContext).getAccounts();
772 Log.d(TAG, "Refreshing " + accounts.length + " accounts");
773
774 String authority = Calendars.CONTENT_URI.getAuthority();
775 for (int i = 0; i < accounts.length; i++) {
776 if (Log.isLoggable(TAG, Log.DEBUG)) {
777 Log.d(TAG, "Refreshing calendars for: " + accounts[i]);
778 }
779 Bundle extras = new Bundle();
780 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
781 ContentResolver.requestSync(accounts[i], authority, extras);
782 }
Erikba1b94a2010-07-20 17:50:50 -0700783 }
784
Erikdd95df52010-08-27 09:31:18 -0700785 // Forces the viewType. Should only be used for initialization.
786 public void setViewType(int viewType) {
787 mViewType = viewType;
788 }
789
Erik7b92da22010-09-23 14:55:22 -0700790 // Sets the eventId. Should only be used for initialization.
791 public void setEventId(long eventId) {
792 mEventId = eventId;
793 }
794
Michael Chan3458a172010-07-13 17:58:21 -0700795 private String eventInfoToString(EventInfo eventInfo) {
796 String tmp = "Unknown";
797
798 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700799 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700800 tmp = "Go to time/event";
801 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
802 tmp = "New event";
803 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
804 tmp = "View event";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800805 } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) {
806 tmp = "View details";
Michael Chan3458a172010-07-13 17:58:21 -0700807 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
808 tmp = "Edit event";
809 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
810 tmp = "Delete event";
Michael Chan2aeb8d92011-07-10 13:32:09 -0700811 } else if ((eventInfo.eventType & EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) != 0) {
812 tmp = "Launch select visible calendars";
Michael Chan3458a172010-07-13 17:58:21 -0700813 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
814 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700815 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
816 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700817 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
818 tmp = "Search";
Michael Chan6d4ce6e2011-01-11 11:15:16 -0800819 } else if ((eventInfo.eventType & EventType.USER_HOME) != 0) {
820 tmp = "Gone home";
821 } else if ((eventInfo.eventType & EventType.UPDATE_TITLE) != 0) {
822 tmp = "Update title";
Michael Chan3458a172010-07-13 17:58:21 -0700823 }
824 builder.append(tmp);
825 builder.append(": id=");
826 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700827 builder.append(", selected=");
828 builder.append(eventInfo.selectedTime);
829 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700830 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700831 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700832 builder.append(eventInfo.endTime);
833 builder.append(", viewType=");
834 builder.append(eventInfo.viewType);
835 builder.append(", x=");
836 builder.append(eventInfo.x);
837 builder.append(", y=");
838 builder.append(eventInfo.y);
839 return builder.toString();
840 }
Michael Chan49701592010-06-30 11:04:03 -0700841}