blob: 098906bc8e24848d2ac900abb2dd72d2359c9f0f [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
Michael Chan9e89dca2010-07-13 17:56:09 -070019import static android.provider.Calendar.EVENT_BEGIN_TIME;
20import static android.provider.Calendar.EVENT_END_TIME;
21
Erik5f620792010-10-27 12:42:01 -070022import com.android.calendar.event.EditEventActivity;
23
Erikba1b94a2010-07-20 17:50:50 -070024import android.accounts.Account;
Michael Chan83b0fe32010-07-08 16:46:26 -070025import android.app.Activity;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070026import android.app.SearchManager;
27import android.app.SearchableInfo;
28import android.content.ComponentName;
Erikba1b94a2010-07-20 17:50:50 -070029import android.content.ContentResolver;
Michael Chan9e89dca2010-07-13 17:56:09 -070030import android.content.ContentUris;
Michael Chanab29d9e2010-07-21 06:08:47 -070031import android.content.Context;
Michael Chan9e89dca2010-07-13 17:56:09 -070032import android.content.Intent;
Erikba1b94a2010-07-20 17:50:50 -070033import android.database.Cursor;
Michael Chan9e89dca2010-07-13 17:56:09 -070034import android.net.Uri;
Erik7116ba42010-07-23 10:13:36 -070035import android.os.AsyncTask;
Erikba1b94a2010-07-20 17:50:50 -070036import android.os.Bundle;
37import android.provider.Calendar.Calendars;
Michael Chan9e89dca2010-07-13 17:56:09 -070038import android.provider.Calendar.Events;
Erikba1b94a2010-07-20 17:50:50 -070039import android.text.TextUtils;
Michael Chan49701592010-06-30 11:04:03 -070040import android.text.format.Time;
Michael Chan83b0fe32010-07-08 16:46:26 -070041import android.util.Log;
Michael Chan49701592010-06-30 11:04:03 -070042
Erik3f348f32010-08-10 13:17:19 -070043import java.util.Iterator;
44import java.util.LinkedHashMap;
Michael Chanab29d9e2010-07-21 06:08:47 -070045import java.util.LinkedList;
Erik3f348f32010-08-10 13:17:19 -070046import java.util.Map.Entry;
Michael Chan83b0fe32010-07-08 16:46:26 -070047import java.util.WeakHashMap;
48
Erik25251192010-07-12 15:30:14 -070049public class CalendarController {
Erik3f348f32010-08-10 13:17:19 -070050 private static final boolean DEBUG = true;
Michael Chan83b0fe32010-07-08 16:46:26 -070051 private static final String TAG = "CalendarController";
Erikba1b94a2010-07-20 17:50:50 -070052 private static final String REFRESH_SELECTION = Calendars.SYNC_EVENTS + "=?";
53 private static final String[] REFRESH_ARGS = new String[] { "1" };
54 private static final String REFRESH_ORDER = Calendars._SYNC_ACCOUNT + ","
Erik7116ba42010-07-23 10:13:36 -070055 + Calendars._SYNC_ACCOUNT_TYPE;
Michael Chan83b0fe32010-07-08 16:46:26 -070056
Erika7694ee2010-12-07 15:46:18 -080057 public static final String EVENT_EDIT_ON_LAUNCH = "editMode";
58
Erik981874e2010-10-05 16:52:52 -070059 public static final int MIN_CALENDAR_YEAR = 1970;
60 public static final int MAX_CALENDAR_YEAR = 2036;
61 public static final int MIN_CALENDAR_WEEK = 0;
62 public static final int MAX_CALENDAR_WEEK = 3497; // weeks between 1/1/1970 and 1/1/2037
63
Michael Chandeced892010-12-10 15:59:35 -080064 public static final int ATTENDEE_NO_RESPONSE = -1;
65
Michael Chanab29d9e2010-07-21 06:08:47 -070066 private Context mContext;
67
Erik3f348f32010-08-10 13:17:19 -070068 // This uses a LinkedHashMap so that we can replace fragments based on the
69 // view id they are being expanded into since we can't guarantee a reference
70 // to the handler will be findable
71 private LinkedHashMap<Integer,EventHandler> eventHandlers =
72 new LinkedHashMap<Integer,EventHandler>(5);
73 private LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>();
Erikcb811892010-09-28 13:44:19 -070074 private LinkedHashMap<Integer, EventHandler> mToBeAddedEventHandlers = new LinkedHashMap<
75 Integer, EventHandler>();
Michael Chanab29d9e2010-07-21 06:08:47 -070076 private boolean mDispatchInProgress;
77
Michael Chan0558def2010-07-22 21:30:32 -070078 private static WeakHashMap<Context, CalendarController> instances =
79 new WeakHashMap<Context, CalendarController>();
80
Michael Chan83b0fe32010-07-08 16:46:26 -070081 private WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
82
Michael Chan3458a172010-07-13 17:58:21 -070083 private int mViewType = -1;
Michael Chan85e55092010-07-27 15:18:40 -070084 private int mDetailViewType = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -070085 private int mPreviousViewType = -1;
Erik7b92da22010-09-23 14:55:22 -070086 private long mEventId = -1;
Michael Chan3458a172010-07-13 17:58:21 -070087 private Time mTime = new Time();
88
Erikba1b94a2010-07-20 17:50:50 -070089 private AsyncQueryService mService;
90
Michael Chan49701592010-06-30 11:04:03 -070091 /**
Michael Chan83b0fe32010-07-08 16:46:26 -070092 * One of the event types that are sent to or from the controller
Michael Chan49701592010-06-30 11:04:03 -070093 */
Erik25251192010-07-12 15:30:14 -070094 public interface EventType {
Michael Chan9e89dca2010-07-13 17:56:09 -070095 final long CREATE_EVENT = 1L;
Michael Chandeced892010-12-10 15:59:35 -080096
97 // Simple view of an event
Michael Chan83b0fe32010-07-08 16:46:26 -070098 final long VIEW_EVENT = 1L << 1;
Michael Chandeced892010-12-10 15:59:35 -080099
100 // Full detail view in read only mode
101 final long VIEW_EVENT_DETAILS = 1L << 2;
102
Erika7694ee2010-12-07 15:46:18 -0800103 // full detail view in edit mode
Michael Chandeced892010-12-10 15:59:35 -0800104 final long EDIT_EVENT = 1L << 3;
Michael Chan83b0fe32010-07-08 16:46:26 -0700105
Michael Chandeced892010-12-10 15:59:35 -0800106 final long DELETE_EVENT = 1L << 4;
Michael Chan83b0fe32010-07-08 16:46:26 -0700107
Michael Chandeced892010-12-10 15:59:35 -0800108 final long GO_TO = 1L << 5;
Erik954c8712010-08-06 10:12:34 -0700109
Michael Chandeced892010-12-10 15:59:35 -0800110 final long LAUNCH_SETTINGS = 1L << 6;
Mason Tang4003d1c2010-08-17 13:50:45 -0700111
Michael Chandeced892010-12-10 15:59:35 -0800112 final long EVENTS_CHANGED = 1L << 7;
113
114 final long SEARCH = 1L << 8;
Erik7b92da22010-09-23 14:55:22 -0700115
116 // User has pressed the home key
Michael Chandeced892010-12-10 15:59:35 -0800117 final long USER_HOME = 1L << 9;
Michael Chan83b0fe32010-07-08 16:46:26 -0700118 }
Michael Chan49701592010-06-30 11:04:03 -0700119
120 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700121 * One of the Agenda/Day/Week/Month view types
Michael Chan49701592010-06-30 11:04:03 -0700122 */
Erik25251192010-07-12 15:30:14 -0700123 public interface ViewType {
Michael Chand6734db2010-07-22 00:48:08 -0700124 final int DETAIL = -1;
Michael Chan3458a172010-07-13 17:58:21 -0700125 final int CURRENT = 0;
126 final int AGENDA = 1;
127 final int DAY = 2;
128 final int WEEK = 3;
129 final int MONTH = 4;
Erikdd95df52010-08-27 09:31:18 -0700130 final int EDIT = 5;
Michael Chan83b0fe32010-07-08 16:46:26 -0700131 }
132
Erik25251192010-07-12 15:30:14 -0700133 public static class EventInfo {
Mason Tang00b8c1a2010-08-23 12:02:00 -0700134 public long eventType; // one of the EventType
135 public int viewType; // one of the ViewType
136 public long id; // event id
137 public Time selectedTime; // the selected time in focus
138 public Time startTime; // start of a range of time.
139 public Time endTime; // end of a range of time.
140 public int x; // x coordinate in the activity space
141 public int y; // y coordinate in the activity space
142 public String query; // query for a user search
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700143 public ComponentName componentName; // used in combination with query
Michael Chan46a8b112010-12-14 16:36:27 -0800144
145 /**
146 * For EventType.VIEW_EVENT:
147 * It is the default attendee response.
148 * Set to {@link #ATTENDEE_NO_RESPONSE}, Calendar.ATTENDEE_STATUS_ACCEPTED,
149 * Calendar.ATTENDEE_STATUS_DECLINED, or Calendar.ATTENDEE_STATUS_TENTATIVE.
150 * <p>
151 * For EventType.GO_TO:
152 * Set to {@link #EXTRA_GOTO_TIME} to go to the specified date/time.
153 * Set to {@link #EXTRA_GOTO_DATE} to consider the date but ignore the time.
154 */
155 public long extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700156 }
157
Michael Chan46a8b112010-12-14 16:36:27 -0800158 /**
159 * Pass to the ExtraLong parameter for EventType.GO_TO to signal the time
160 * can be ignored
161 */
162 public static final long EXTRA_GOTO_DATE = 1;
163 public static final long EXTRA_GOTO_TIME = -1;
164
Erik25251192010-07-12 15:30:14 -0700165 public interface EventHandler {
Michael Chan83b0fe32010-07-08 16:46:26 -0700166 long getSupportedEventTypes();
167 void handleEvent(EventInfo event);
168
169 /**
Erik954c8712010-08-06 10:12:34 -0700170 * This notifies the handler that the database has changed and it should
171 * update its view.
Michael Chan83b0fe32010-07-08 16:46:26 -0700172 */
173 void eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700174 }
175
Michael Chan0558def2010-07-22 21:30:32 -0700176 /**
177 * Creates and/or returns an instance of CalendarController associated with
178 * the supplied context. It is best to pass in the current Activity.
179 *
180 * @param context The activity if at all possible.
181 */
182 public static CalendarController getInstance(Context context) {
183 synchronized (instances) {
184 CalendarController controller = instances.get(context);
185 if (controller == null) {
186 controller = new CalendarController(context);
187 instances.put(context, controller);
188 }
189 return controller;
190 }
191 }
192
Eriked61b482010-08-13 13:59:50 -0700193 /**
194 * Removes an instance when it is no longer needed. This should be called in
195 * an activity's onDestroy method.
196 *
197 * @param context The activity used to create the controller
198 */
199 public static void removeInstance(Context context) {
200 instances.remove(context);
201 }
202
Michael Chan0558def2010-07-22 21:30:32 -0700203 private CalendarController(Context context) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700204 mContext = context;
Michael Chan3458a172010-07-13 17:58:21 -0700205 mTime.setToNow();
Michael Chan85e55092010-07-27 15:18:40 -0700206 mDetailViewType = Utils.getSharedPreference(mContext,
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700207 GeneralPreferences.KEY_DETAILED_VIEW,
208 GeneralPreferences.DEFAULT_DETAILED_VIEW);
Erikba1b94a2010-07-20 17:50:50 -0700209 mService = new AsyncQueryService(context) {
210 @Override
211 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Erik7116ba42010-07-23 10:13:36 -0700212 new RefreshInBackground().execute(cursor);
Erikba1b94a2010-07-20 17:50:50 -0700213 }
214 };
Michael Chan83b0fe32010-07-08 16:46:26 -0700215 }
Michael Chan49701592010-06-30 11:04:03 -0700216
Michael Chandeced892010-12-10 15:59:35 -0800217 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
218 long endMillis, int x, int y) {
219 sendEventRelatedEvent( sender, eventType, eventId, startMillis,
220 endMillis, x, y, CalendarController.ATTENDEE_NO_RESPONSE);
221 }
222
Michael Chan49701592010-06-30 11:04:03 -0700223 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700224 * Helper for sending New/View/Edit/Delete events
225 *
226 * @param sender object of the caller
227 * @param eventType one of {@link EventType}
228 * @param eventId event id
Michael Chan3458a172010-07-13 17:58:21 -0700229 * @param startMillis start time
230 * @param endMillis end time
Michael Chan83b0fe32010-07-08 16:46:26 -0700231 * @param x x coordinate in the activity space
232 * @param y y coordinate in the activity space
Michael Chan46a8b112010-12-14 16:36:27 -0800233 * @param extraLong default response value for the "simple event view". Use
234 * CalendarController.ATTENDEE_NO_RESPONSE for no response.
Michael Chan49701592010-06-30 11:04:03 -0700235 */
Michael Chan9e89dca2010-07-13 17:56:09 -0700236 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
Michael Chandeced892010-12-10 15:59:35 -0800237 long endMillis, int x, int y, long extraLong) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700238 EventInfo info = new EventInfo();
239 info.eventType = eventType;
Erika7694ee2010-12-07 15:46:18 -0800240 if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) {
Erik5f620792010-10-27 12:42:01 -0700241 info.viewType = ViewType.CURRENT;
Erikdd95df52010-08-27 09:31:18 -0700242 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700243 info.id = eventId;
Michael Chan9e89dca2010-07-13 17:56:09 -0700244 info.startTime = new Time();
245 info.startTime.set(startMillis);
Erik981874e2010-10-05 16:52:52 -0700246 info.selectedTime = info.startTime;
Michael Chan9e89dca2010-07-13 17:56:09 -0700247 info.endTime = new Time();
248 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700249 info.x = x;
250 info.y = y;
Michael Chandeced892010-12-10 15:59:35 -0800251 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700252 this.sendEvent(sender, info);
253 }
Michael Chan49701592010-06-30 11:04:03 -0700254
255 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700256 * Helper for sending non-calendar-event events
257 *
258 * @param sender object of the caller
259 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700260 * @param start start time
261 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700262 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700263 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700264 */
Erik25251192010-07-12 15:30:14 -0700265 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700266 int viewType) {
Michael Chan46a8b112010-12-14 16:36:27 -0800267 sendEvent(sender, eventType, start, end, eventId, viewType, EXTRA_GOTO_TIME, null, null);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700268 }
269
270 /**
Michael Chan46a8b112010-12-14 16:36:27 -0800271 * sendEvent() variant with extraLong, search query, and search component name.
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700272 */
273 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan46a8b112010-12-14 16:36:27 -0800274 int viewType, long extraLong, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700275 EventInfo info = new EventInfo();
276 info.eventType = eventType;
277 info.startTime = start;
Erik981874e2010-10-05 16:52:52 -0700278 info.selectedTime = start;
Michael Chan83b0fe32010-07-08 16:46:26 -0700279 info.endTime = end;
280 info.id = eventId;
281 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700282 info.query = query;
283 info.componentName = componentName;
Michael Chan46a8b112010-12-14 16:36:27 -0800284 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700285 this.sendEvent(sender, info);
286 }
287
Erik25251192010-07-12 15:30:14 -0700288 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700289 // TODO Throw exception on invalid events
290
Erik3f348f32010-08-10 13:17:19 -0700291 if (DEBUG) {
292 Log.d(TAG, eventInfoToString(event));
293 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700294
295 Long filteredTypes = filters.get(sender);
296 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
297 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700298 if (DEBUG) {
299 Log.d(TAG, "Event suppressed");
300 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700301 return;
302 }
303
Michael Chanab29d9e2010-07-21 06:08:47 -0700304 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700305
Michael Chan3458a172010-07-13 17:58:21 -0700306 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700307 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700308 event.viewType = mDetailViewType;
309 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700310 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700311 event.viewType = mViewType;
Erik5f620792010-10-27 12:42:01 -0700312 } else if (event.viewType != ViewType.EDIT){
Michael Chanab29d9e2010-07-21 06:08:47 -0700313 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700314
315 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) {
316 mDetailViewType = mViewType;
317 }
Michael Chan3458a172010-07-13 17:58:21 -0700318 }
319
Mason Tang4003d1c2010-08-17 13:50:45 -0700320 // Fix up start time if not specified
321 if (event.startTime != null && event.startTime.toMillis(false) != 0) {
322 mTime.set(event.startTime);
323 }
324 event.startTime = mTime;
325
Erik7b92da22010-09-23 14:55:22 -0700326 // Store the eventId if we're entering edit event
Erika7694ee2010-12-07 15:46:18 -0800327 if ((event.eventType
328 & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS))
329 != 0) {
Erik7b92da22010-09-23 14:55:22 -0700330 if (event.id > 0) {
331 mEventId = event.id;
332 } else {
333 mEventId = -1;
334 }
335 }
336
Mason Tang4003d1c2010-08-17 13:50:45 -0700337 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700338 synchronized (this) {
339 mDispatchInProgress = true;
Michael Chan3458a172010-07-13 17:58:21 -0700340
Erik3f348f32010-08-10 13:17:19 -0700341 if (DEBUG) {
342 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
343 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700344 // Dispatch to event handler(s)
Erik3f348f32010-08-10 13:17:19 -0700345 for (Iterator<Entry<Integer, EventHandler>> handlers =
346 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
347 Entry<Integer, EventHandler> entry = handlers.next();
348 int key = entry.getKey();
349 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700350 if (eventHandler != null
351 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700352 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700353 continue;
354 }
355 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700356 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700357 }
358 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700359
Erikcb811892010-09-28 13:44:19 -0700360 mDispatchInProgress = false;
361
Michael Chanab29d9e2010-07-21 06:08:47 -0700362 // Deregister removed handlers
363 if (mToBeRemovedEventHandlers.size() > 0) {
Erik3f348f32010-08-10 13:17:19 -0700364 for (Integer zombie : mToBeRemovedEventHandlers) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700365 eventHandlers.remove(zombie);
366 }
367 mToBeRemovedEventHandlers.clear();
368 }
Erikcb811892010-09-28 13:44:19 -0700369 // Add new handlers
370 if (mToBeAddedEventHandlers.size() > 0) {
371 for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) {
372 eventHandlers.put(food.getKey(), food.getValue());
373 }
374 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700375 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700376
377 if (!handled) {
Erik6b858fc2010-09-15 18:59:04 -0700378 // Launch Settings
379 if (event.eventType == EventType.LAUNCH_SETTINGS) {
Mason Tang4003d1c2010-08-17 13:50:45 -0700380 launchSettings();
381 return;
382 }
383
384 // Create/View/Edit/Delete Event
385 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
386 if (event.eventType == EventType.CREATE_EVENT) {
387 launchCreateEvent(event.startTime.toMillis(false), endTime);
388 return;
389 } else if (event.eventType == EventType.VIEW_EVENT) {
390 launchViewEvent(event.id, event.startTime.toMillis(false), endTime);
391 return;
392 } else if (event.eventType == EventType.EDIT_EVENT) {
Erika7694ee2010-12-07 15:46:18 -0800393 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true);
394 return;
395 } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) {
396 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false);
Mason Tang4003d1c2010-08-17 13:50:45 -0700397 return;
398 } else if (event.eventType == EventType.DELETE_EVENT) {
399 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
400 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700401 } else if (event.eventType == EventType.SEARCH) {
402 launchSearch(event.id, event.query, event.componentName);
403 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700404 }
405 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700406 }
407
Erik3f348f32010-08-10 13:17:19 -0700408 /**
409 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
410 * replace fragments based on the view id they are being expanded into.
411 *
412 * @param key The view id or placeholder for this handler
413 * @param eventHandler Typically a fragment or activity in the calendar app
414 */
415 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700416 synchronized (this) {
Erikcb811892010-09-28 13:44:19 -0700417 if (mDispatchInProgress) {
418 mToBeAddedEventHandlers.put(key, eventHandler);
419 } else {
420 eventHandlers.put(key, eventHandler);
421 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700422 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700423 }
424
Erik3f348f32010-08-10 13:17:19 -0700425 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700426 synchronized (this) {
427 if (mDispatchInProgress) {
428 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700429 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700430 } else {
Erik3f348f32010-08-10 13:17:19 -0700431 eventHandlers.remove(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700432 }
433 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700434 }
435
Michael Chan3458a172010-07-13 17:58:21 -0700436 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700437 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700438 filters.put(sender, eventTypes);
439 }
440
Mason Tang8e3d4302010-07-12 17:39:30 -0700441 /**
442 * @return the time that this controller is currently pointed at
443 */
444 public long getTime() {
445 return mTime.toMillis(false);
446 }
447
Erik7b92da22010-09-23 14:55:22 -0700448 /**
Erik144edfa2010-11-22 14:23:49 -0800449 * Set the time this controller is currently pointed at
450 *
451 * @param millisTime Time since epoch in millis
452 */
453 public void setTime(long millisTime) {
454 mTime.set(millisTime);
455 }
456
457 /**
Erik7b92da22010-09-23 14:55:22 -0700458 * @return the last event ID the edit view was launched with
459 */
460 public long getEventId() {
461 return mEventId;
462 }
463
Michael Chanab29d9e2010-07-21 06:08:47 -0700464 public int getViewType() {
465 return mViewType;
466 }
467
468 public int getPreviousViewType() {
469 return mPreviousViewType;
470 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700471
Michael Chan9e89dca2010-07-13 17:56:09 -0700472 private void launchSettings() {
473 Intent intent = new Intent(Intent.ACTION_VIEW);
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700474 intent.setClassName(mContext, CalendarSettingsActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700475 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700476 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700477 }
478
479 private void launchCreateEvent(long startMillis, long endMillis) {
480 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chanab29d9e2010-07-21 06:08:47 -0700481 intent.setClassName(mContext, EditEventActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700482 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
483 intent.putExtra(EVENT_END_TIME, endMillis);
Erik7b92da22010-09-23 14:55:22 -0700484 mEventId = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -0700485 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700486 }
487
488 private void launchViewEvent(long eventId, long startMillis, long endMillis) {
489 Intent intent = new Intent(Intent.ACTION_VIEW);
490 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
491 intent.setData(eventUri);
Michael Chan2c7c8512010-12-10 14:12:57 -0800492// intent.setClassName(mContext, EventInfoActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700493 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
494 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700495 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700496 }
497
Erika7694ee2010-12-07 15:46:18 -0800498 private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700499 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
500 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
501 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
502 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700503 intent.setClass(mContext, EditEventActivity.class);
Erika7694ee2010-12-07 15:46:18 -0800504 intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
Erik7b92da22010-09-23 14:55:22 -0700505 mEventId = eventId;
Michael Chanab29d9e2010-07-21 06:08:47 -0700506 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700507 }
508
509 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
510 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
511 }
512
513 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
514 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700515 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700516 parentActivity != null /* exit when done */);
517 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
518 }
Michael Chan3458a172010-07-13 17:58:21 -0700519
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700520 private void launchSearch(long eventId, String query, ComponentName componentName) {
521 final SearchManager searchManager =
522 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
523 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
524 final Intent intent = new Intent(Intent.ACTION_SEARCH);
525 intent.putExtra(SearchManager.QUERY, query);
526 intent.setComponent(searchableInfo.getSearchActivity());
527 mContext.startActivity(intent);
528 }
529
Erikba1b94a2010-07-20 17:50:50 -0700530 public void refreshCalendars() {
531 Log.d(TAG, "RefreshCalendars starting");
532 // get the account, url, and current sync state
533 mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI,
534 new String[] {Calendars._ID, // 0
535 Calendars._SYNC_ACCOUNT, // 1
536 Calendars._SYNC_ACCOUNT_TYPE, // 2
537 },
538 REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER);
Erikba1b94a2010-07-20 17:50:50 -0700539 }
540
Erikdd95df52010-08-27 09:31:18 -0700541 // Forces the viewType. Should only be used for initialization.
542 public void setViewType(int viewType) {
543 mViewType = viewType;
544 }
545
Erik7b92da22010-09-23 14:55:22 -0700546 // Sets the eventId. Should only be used for initialization.
547 public void setEventId(long eventId) {
548 mEventId = eventId;
549 }
550
Erik7116ba42010-07-23 10:13:36 -0700551 private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> {
552 /* (non-Javadoc)
553 * @see android.os.AsyncTask#doInBackground(Params[])
554 */
555 @Override
556 protected Integer doInBackground(Cursor... params) {
557 if (params.length != 1) {
558 return null;
Erikba1b94a2010-07-20 17:50:50 -0700559 }
Erik7116ba42010-07-23 10:13:36 -0700560 Cursor cursor = params[0];
561 if (cursor == null) {
562 return null;
563 }
Erikba1b94a2010-07-20 17:50:50 -0700564
Erik7116ba42010-07-23 10:13:36 -0700565 String previousAccount = null;
566 String previousType = null;
567 Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars");
568 try {
569 while (cursor.moveToNext()) {
570 Account account = null;
571 String accountName = cursor.getString(1);
572 String accountType = cursor.getString(2);
573 // Only need to schedule one sync per account and they're
574 // ordered by account,type
575 if (TextUtils.equals(accountName, previousAccount) &&
576 TextUtils.equals(accountType, previousType)) {
577 continue;
578 }
579 previousAccount = accountName;
580 previousType = accountType;
581 account = new Account(accountName, accountType);
582 scheduleSync(account, false /* two-way sync */, null);
583 }
584 } finally {
585 cursor.close();
586 }
587 return null;
Erikba1b94a2010-07-20 17:50:50 -0700588 }
Erik7116ba42010-07-23 10:13:36 -0700589
590 /**
591 * Schedule a calendar sync for the account.
592 * @param account the account for which to schedule a sync
593 * @param uploadChangesOnly if set, specify that the sync should only send
594 * up local changes. This is typically used for a local sync, a user override of
595 * too many deletions, or a sync after a calendar is unselected.
596 * @param url the url feed for the calendar to sync (may be null, in which case a poll of
597 * all feeds is done.)
598 */
599 void scheduleSync(Account account, boolean uploadChangesOnly, String url) {
600 Bundle extras = new Bundle();
601 if (uploadChangesOnly) {
602 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly);
603 }
604 if (url != null) {
605 extras.putString("feed", url);
606 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
607 }
608 ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras);
Erikba1b94a2010-07-20 17:50:50 -0700609 }
Erikba1b94a2010-07-20 17:50:50 -0700610 }
611
Michael Chan3458a172010-07-13 17:58:21 -0700612 private String eventInfoToString(EventInfo eventInfo) {
613 String tmp = "Unknown";
614
615 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700616 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700617 tmp = "Go to time/event";
618 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
619 tmp = "New event";
620 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
621 tmp = "View event";
622 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
623 tmp = "Edit event";
624 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
625 tmp = "Delete event";
Michael Chan3458a172010-07-13 17:58:21 -0700626 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
627 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700628 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
629 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700630 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
631 tmp = "Search";
Erika7694ee2010-12-07 15:46:18 -0800632 } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) {
633 tmp = "View details";
Michael Chan3458a172010-07-13 17:58:21 -0700634 }
635 builder.append(tmp);
636 builder.append(": id=");
637 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700638 builder.append(", selected=");
639 builder.append(eventInfo.selectedTime);
640 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700641 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700642 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700643 builder.append(eventInfo.endTime);
644 builder.append(", viewType=");
645 builder.append(eventInfo.viewType);
646 builder.append(", x=");
647 builder.append(eventInfo.x);
648 builder.append(", y=");
649 builder.append(eventInfo.y);
650 return builder.toString();
651 }
Michael Chan49701592010-06-30 11:04:03 -0700652}