blob: 7032d17e02f9d38c5ec43d82bbc70321029f9923 [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 Chandeced892010-12-10 15:59:35 -0800144 public long extraLong; // pass through
Michael Chan83b0fe32010-07-08 16:46:26 -0700145 }
146
Erik25251192010-07-12 15:30:14 -0700147 public interface EventHandler {
Michael Chan83b0fe32010-07-08 16:46:26 -0700148 long getSupportedEventTypes();
149 void handleEvent(EventInfo event);
150
151 /**
Erik954c8712010-08-06 10:12:34 -0700152 * This notifies the handler that the database has changed and it should
153 * update its view.
Michael Chan83b0fe32010-07-08 16:46:26 -0700154 */
155 void eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700156 }
157
Michael Chan0558def2010-07-22 21:30:32 -0700158 /**
159 * Creates and/or returns an instance of CalendarController associated with
160 * the supplied context. It is best to pass in the current Activity.
161 *
162 * @param context The activity if at all possible.
163 */
164 public static CalendarController getInstance(Context context) {
165 synchronized (instances) {
166 CalendarController controller = instances.get(context);
167 if (controller == null) {
168 controller = new CalendarController(context);
169 instances.put(context, controller);
170 }
171 return controller;
172 }
173 }
174
Eriked61b482010-08-13 13:59:50 -0700175 /**
176 * Removes an instance when it is no longer needed. This should be called in
177 * an activity's onDestroy method.
178 *
179 * @param context The activity used to create the controller
180 */
181 public static void removeInstance(Context context) {
182 instances.remove(context);
183 }
184
Michael Chan0558def2010-07-22 21:30:32 -0700185 private CalendarController(Context context) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700186 mContext = context;
Michael Chan3458a172010-07-13 17:58:21 -0700187 mTime.setToNow();
Michael Chan85e55092010-07-27 15:18:40 -0700188 mDetailViewType = Utils.getSharedPreference(mContext,
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700189 GeneralPreferences.KEY_DETAILED_VIEW,
190 GeneralPreferences.DEFAULT_DETAILED_VIEW);
Erikba1b94a2010-07-20 17:50:50 -0700191 mService = new AsyncQueryService(context) {
192 @Override
193 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Erik7116ba42010-07-23 10:13:36 -0700194 new RefreshInBackground().execute(cursor);
Erikba1b94a2010-07-20 17:50:50 -0700195 }
196 };
Michael Chan83b0fe32010-07-08 16:46:26 -0700197 }
Michael Chan49701592010-06-30 11:04:03 -0700198
Michael Chandeced892010-12-10 15:59:35 -0800199 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
200 long endMillis, int x, int y) {
201 sendEventRelatedEvent( sender, eventType, eventId, startMillis,
202 endMillis, x, y, CalendarController.ATTENDEE_NO_RESPONSE);
203 }
204
Michael Chan49701592010-06-30 11:04:03 -0700205 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700206 * Helper for sending New/View/Edit/Delete events
207 *
208 * @param sender object of the caller
209 * @param eventType one of {@link EventType}
210 * @param eventId event id
Michael Chan3458a172010-07-13 17:58:21 -0700211 * @param startMillis start time
212 * @param endMillis end time
Michael Chan83b0fe32010-07-08 16:46:26 -0700213 * @param x x coordinate in the activity space
214 * @param y y coordinate in the activity space
Michael Chandeced892010-12-10 15:59:35 -0800215 * @param extraLong default response value // currently only works for the simple event view
Michael Chan49701592010-06-30 11:04:03 -0700216 */
Michael Chan9e89dca2010-07-13 17:56:09 -0700217 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
Michael Chandeced892010-12-10 15:59:35 -0800218 long endMillis, int x, int y, long extraLong) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700219 EventInfo info = new EventInfo();
220 info.eventType = eventType;
Erika7694ee2010-12-07 15:46:18 -0800221 if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) {
Erik5f620792010-10-27 12:42:01 -0700222 info.viewType = ViewType.CURRENT;
Erikdd95df52010-08-27 09:31:18 -0700223 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700224 info.id = eventId;
Michael Chan9e89dca2010-07-13 17:56:09 -0700225 info.startTime = new Time();
226 info.startTime.set(startMillis);
Erik981874e2010-10-05 16:52:52 -0700227 info.selectedTime = info.startTime;
Michael Chan9e89dca2010-07-13 17:56:09 -0700228 info.endTime = new Time();
229 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700230 info.x = x;
231 info.y = y;
Michael Chandeced892010-12-10 15:59:35 -0800232 info.extraLong = extraLong;
Michael Chan83b0fe32010-07-08 16:46:26 -0700233 this.sendEvent(sender, info);
234 }
Michael Chan49701592010-06-30 11:04:03 -0700235
236 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700237 * Helper for sending non-calendar-event events
238 *
239 * @param sender object of the caller
240 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700241 * @param start start time
242 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700243 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700244 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700245 */
Erik25251192010-07-12 15:30:14 -0700246 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700247 int viewType) {
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700248 sendEvent(sender, eventType, start, end, eventId, viewType, null, null);
249 }
250
251 /**
252 * sendEvent() variant mainly used for search.
253 */
254 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
255 int viewType, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700256 EventInfo info = new EventInfo();
257 info.eventType = eventType;
258 info.startTime = start;
Erik981874e2010-10-05 16:52:52 -0700259 info.selectedTime = start;
Michael Chan83b0fe32010-07-08 16:46:26 -0700260 info.endTime = end;
261 info.id = eventId;
262 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700263 info.query = query;
264 info.componentName = componentName;
Michael Chan83b0fe32010-07-08 16:46:26 -0700265 this.sendEvent(sender, info);
266 }
267
Erik25251192010-07-12 15:30:14 -0700268 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700269 // TODO Throw exception on invalid events
270
Erik3f348f32010-08-10 13:17:19 -0700271 if (DEBUG) {
272 Log.d(TAG, eventInfoToString(event));
273 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700274
275 Long filteredTypes = filters.get(sender);
276 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
277 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700278 if (DEBUG) {
279 Log.d(TAG, "Event suppressed");
280 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700281 return;
282 }
283
Michael Chanab29d9e2010-07-21 06:08:47 -0700284 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700285
Michael Chan3458a172010-07-13 17:58:21 -0700286 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700287 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700288 event.viewType = mDetailViewType;
289 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700290 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700291 event.viewType = mViewType;
Erik5f620792010-10-27 12:42:01 -0700292 } else if (event.viewType != ViewType.EDIT){
Michael Chanab29d9e2010-07-21 06:08:47 -0700293 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700294
295 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) {
296 mDetailViewType = mViewType;
297 }
Michael Chan3458a172010-07-13 17:58:21 -0700298 }
299
Mason Tang4003d1c2010-08-17 13:50:45 -0700300 // Fix up start time if not specified
301 if (event.startTime != null && event.startTime.toMillis(false) != 0) {
302 mTime.set(event.startTime);
303 }
304 event.startTime = mTime;
305
Erik7b92da22010-09-23 14:55:22 -0700306 // Store the eventId if we're entering edit event
Erika7694ee2010-12-07 15:46:18 -0800307 if ((event.eventType
308 & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS))
309 != 0) {
Erik7b92da22010-09-23 14:55:22 -0700310 if (event.id > 0) {
311 mEventId = event.id;
312 } else {
313 mEventId = -1;
314 }
315 }
316
Mason Tang4003d1c2010-08-17 13:50:45 -0700317 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700318 synchronized (this) {
319 mDispatchInProgress = true;
Michael Chan3458a172010-07-13 17:58:21 -0700320
Erik3f348f32010-08-10 13:17:19 -0700321 if (DEBUG) {
322 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
323 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700324 // Dispatch to event handler(s)
Erik3f348f32010-08-10 13:17:19 -0700325 for (Iterator<Entry<Integer, EventHandler>> handlers =
326 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
327 Entry<Integer, EventHandler> entry = handlers.next();
328 int key = entry.getKey();
329 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700330 if (eventHandler != null
331 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700332 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700333 continue;
334 }
335 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700336 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700337 }
338 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700339
Erikcb811892010-09-28 13:44:19 -0700340 mDispatchInProgress = false;
341
Michael Chanab29d9e2010-07-21 06:08:47 -0700342 // Deregister removed handlers
343 if (mToBeRemovedEventHandlers.size() > 0) {
Erik3f348f32010-08-10 13:17:19 -0700344 for (Integer zombie : mToBeRemovedEventHandlers) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700345 eventHandlers.remove(zombie);
346 }
347 mToBeRemovedEventHandlers.clear();
348 }
Erikcb811892010-09-28 13:44:19 -0700349 // Add new handlers
350 if (mToBeAddedEventHandlers.size() > 0) {
351 for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) {
352 eventHandlers.put(food.getKey(), food.getValue());
353 }
354 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700355 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700356
357 if (!handled) {
Erik6b858fc2010-09-15 18:59:04 -0700358 // Launch Settings
359 if (event.eventType == EventType.LAUNCH_SETTINGS) {
Mason Tang4003d1c2010-08-17 13:50:45 -0700360 launchSettings();
361 return;
362 }
363
364 // Create/View/Edit/Delete Event
365 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
366 if (event.eventType == EventType.CREATE_EVENT) {
367 launchCreateEvent(event.startTime.toMillis(false), endTime);
368 return;
369 } else if (event.eventType == EventType.VIEW_EVENT) {
370 launchViewEvent(event.id, event.startTime.toMillis(false), endTime);
371 return;
372 } else if (event.eventType == EventType.EDIT_EVENT) {
Erika7694ee2010-12-07 15:46:18 -0800373 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true);
374 return;
375 } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) {
376 launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false);
Mason Tang4003d1c2010-08-17 13:50:45 -0700377 return;
378 } else if (event.eventType == EventType.DELETE_EVENT) {
379 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
380 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700381 } else if (event.eventType == EventType.SEARCH) {
382 launchSearch(event.id, event.query, event.componentName);
383 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700384 }
385 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700386 }
387
Erik3f348f32010-08-10 13:17:19 -0700388 /**
389 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
390 * replace fragments based on the view id they are being expanded into.
391 *
392 * @param key The view id or placeholder for this handler
393 * @param eventHandler Typically a fragment or activity in the calendar app
394 */
395 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700396 synchronized (this) {
Erikcb811892010-09-28 13:44:19 -0700397 if (mDispatchInProgress) {
398 mToBeAddedEventHandlers.put(key, eventHandler);
399 } else {
400 eventHandlers.put(key, eventHandler);
401 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700402 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700403 }
404
Erik3f348f32010-08-10 13:17:19 -0700405 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700406 synchronized (this) {
407 if (mDispatchInProgress) {
408 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700409 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700410 } else {
Erik3f348f32010-08-10 13:17:19 -0700411 eventHandlers.remove(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700412 }
413 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700414 }
415
Michael Chan3458a172010-07-13 17:58:21 -0700416 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700417 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700418 filters.put(sender, eventTypes);
419 }
420
Mason Tang8e3d4302010-07-12 17:39:30 -0700421 /**
422 * @return the time that this controller is currently pointed at
423 */
424 public long getTime() {
425 return mTime.toMillis(false);
426 }
427
Erik7b92da22010-09-23 14:55:22 -0700428 /**
Erik144edfa2010-11-22 14:23:49 -0800429 * Set the time this controller is currently pointed at
430 *
431 * @param millisTime Time since epoch in millis
432 */
433 public void setTime(long millisTime) {
434 mTime.set(millisTime);
435 }
436
437 /**
Erik7b92da22010-09-23 14:55:22 -0700438 * @return the last event ID the edit view was launched with
439 */
440 public long getEventId() {
441 return mEventId;
442 }
443
Michael Chanab29d9e2010-07-21 06:08:47 -0700444 public int getViewType() {
445 return mViewType;
446 }
447
448 public int getPreviousViewType() {
449 return mPreviousViewType;
450 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700451
Michael Chan9e89dca2010-07-13 17:56:09 -0700452 private void launchSettings() {
453 Intent intent = new Intent(Intent.ACTION_VIEW);
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700454 intent.setClassName(mContext, CalendarSettingsActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700455 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700456 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700457 }
458
459 private void launchCreateEvent(long startMillis, long endMillis) {
460 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chanab29d9e2010-07-21 06:08:47 -0700461 intent.setClassName(mContext, EditEventActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700462 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
463 intent.putExtra(EVENT_END_TIME, endMillis);
Erik7b92da22010-09-23 14:55:22 -0700464 mEventId = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -0700465 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700466 }
467
468 private void launchViewEvent(long eventId, long startMillis, long endMillis) {
469 Intent intent = new Intent(Intent.ACTION_VIEW);
470 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
471 intent.setData(eventUri);
Michael Chan2c7c8512010-12-10 14:12:57 -0800472// intent.setClassName(mContext, EventInfoActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700473 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
474 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700475 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700476 }
477
Erika7694ee2010-12-07 15:46:18 -0800478 private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Michael Chan9e89dca2010-07-13 17:56:09 -0700479 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
480 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
481 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
482 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700483 intent.setClass(mContext, EditEventActivity.class);
Erika7694ee2010-12-07 15:46:18 -0800484 intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
Erik7b92da22010-09-23 14:55:22 -0700485 mEventId = eventId;
Michael Chanab29d9e2010-07-21 06:08:47 -0700486 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700487 }
488
489 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
490 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
491 }
492
493 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
494 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700495 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700496 parentActivity != null /* exit when done */);
497 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
498 }
Michael Chan3458a172010-07-13 17:58:21 -0700499
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700500 private void launchSearch(long eventId, String query, ComponentName componentName) {
501 final SearchManager searchManager =
502 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
503 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
504 final Intent intent = new Intent(Intent.ACTION_SEARCH);
505 intent.putExtra(SearchManager.QUERY, query);
506 intent.setComponent(searchableInfo.getSearchActivity());
507 mContext.startActivity(intent);
508 }
509
Erikba1b94a2010-07-20 17:50:50 -0700510 public void refreshCalendars() {
511 Log.d(TAG, "RefreshCalendars starting");
512 // get the account, url, and current sync state
513 mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI,
514 new String[] {Calendars._ID, // 0
515 Calendars._SYNC_ACCOUNT, // 1
516 Calendars._SYNC_ACCOUNT_TYPE, // 2
517 },
518 REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER);
Erikba1b94a2010-07-20 17:50:50 -0700519 }
520
Erikdd95df52010-08-27 09:31:18 -0700521 // Forces the viewType. Should only be used for initialization.
522 public void setViewType(int viewType) {
523 mViewType = viewType;
524 }
525
Erik7b92da22010-09-23 14:55:22 -0700526 // Sets the eventId. Should only be used for initialization.
527 public void setEventId(long eventId) {
528 mEventId = eventId;
529 }
530
Erik7116ba42010-07-23 10:13:36 -0700531 private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> {
532 /* (non-Javadoc)
533 * @see android.os.AsyncTask#doInBackground(Params[])
534 */
535 @Override
536 protected Integer doInBackground(Cursor... params) {
537 if (params.length != 1) {
538 return null;
Erikba1b94a2010-07-20 17:50:50 -0700539 }
Erik7116ba42010-07-23 10:13:36 -0700540 Cursor cursor = params[0];
541 if (cursor == null) {
542 return null;
543 }
Erikba1b94a2010-07-20 17:50:50 -0700544
Erik7116ba42010-07-23 10:13:36 -0700545 String previousAccount = null;
546 String previousType = null;
547 Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars");
548 try {
549 while (cursor.moveToNext()) {
550 Account account = null;
551 String accountName = cursor.getString(1);
552 String accountType = cursor.getString(2);
553 // Only need to schedule one sync per account and they're
554 // ordered by account,type
555 if (TextUtils.equals(accountName, previousAccount) &&
556 TextUtils.equals(accountType, previousType)) {
557 continue;
558 }
559 previousAccount = accountName;
560 previousType = accountType;
561 account = new Account(accountName, accountType);
562 scheduleSync(account, false /* two-way sync */, null);
563 }
564 } finally {
565 cursor.close();
566 }
567 return null;
Erikba1b94a2010-07-20 17:50:50 -0700568 }
Erik7116ba42010-07-23 10:13:36 -0700569
570 /**
571 * Schedule a calendar sync for the account.
572 * @param account the account for which to schedule a sync
573 * @param uploadChangesOnly if set, specify that the sync should only send
574 * up local changes. This is typically used for a local sync, a user override of
575 * too many deletions, or a sync after a calendar is unselected.
576 * @param url the url feed for the calendar to sync (may be null, in which case a poll of
577 * all feeds is done.)
578 */
579 void scheduleSync(Account account, boolean uploadChangesOnly, String url) {
580 Bundle extras = new Bundle();
581 if (uploadChangesOnly) {
582 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly);
583 }
584 if (url != null) {
585 extras.putString("feed", url);
586 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
587 }
588 ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras);
Erikba1b94a2010-07-20 17:50:50 -0700589 }
Erikba1b94a2010-07-20 17:50:50 -0700590 }
591
Michael Chan3458a172010-07-13 17:58:21 -0700592 private String eventInfoToString(EventInfo eventInfo) {
593 String tmp = "Unknown";
594
595 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700596 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700597 tmp = "Go to time/event";
598 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
599 tmp = "New event";
600 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
601 tmp = "View event";
602 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
603 tmp = "Edit event";
604 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
605 tmp = "Delete event";
Michael Chan3458a172010-07-13 17:58:21 -0700606 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
607 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700608 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
609 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700610 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
611 tmp = "Search";
Erika7694ee2010-12-07 15:46:18 -0800612 } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) {
613 tmp = "View details";
Michael Chan3458a172010-07-13 17:58:21 -0700614 }
615 builder.append(tmp);
616 builder.append(": id=");
617 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700618 builder.append(", selected=");
619 builder.append(eventInfo.selectedTime);
620 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700621 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700622 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700623 builder.append(eventInfo.endTime);
624 builder.append(", viewType=");
625 builder.append(eventInfo.viewType);
626 builder.append(", x=");
627 builder.append(eventInfo.x);
628 builder.append(", y=");
629 builder.append(eventInfo.y);
630 return builder.toString();
631 }
Michael Chan49701592010-06-30 11:04:03 -0700632}