blob: 83a181dd611b64464edab152f1333ac4af3a85fc [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
Erikba1b94a2010-07-20 17:50:50 -070022import android.accounts.Account;
Michael Chan83b0fe32010-07-08 16:46:26 -070023import android.app.Activity;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070024import android.app.SearchManager;
25import android.app.SearchableInfo;
26import android.content.ComponentName;
Erikba1b94a2010-07-20 17:50:50 -070027import android.content.ContentResolver;
Michael Chan9e89dca2010-07-13 17:56:09 -070028import android.content.ContentUris;
Michael Chanab29d9e2010-07-21 06:08:47 -070029import android.content.Context;
Michael Chan9e89dca2010-07-13 17:56:09 -070030import android.content.Intent;
Erikba1b94a2010-07-20 17:50:50 -070031import android.database.Cursor;
Michael Chan9e89dca2010-07-13 17:56:09 -070032import android.net.Uri;
Erik7116ba42010-07-23 10:13:36 -070033import android.os.AsyncTask;
Erikba1b94a2010-07-20 17:50:50 -070034import android.os.Bundle;
35import android.provider.Calendar.Calendars;
Michael Chan9e89dca2010-07-13 17:56:09 -070036import android.provider.Calendar.Events;
Erikba1b94a2010-07-20 17:50:50 -070037import android.text.TextUtils;
Michael Chan49701592010-06-30 11:04:03 -070038import android.text.format.Time;
Michael Chan83b0fe32010-07-08 16:46:26 -070039import android.util.Log;
Michael Chan49701592010-06-30 11:04:03 -070040
Erik3f348f32010-08-10 13:17:19 -070041import java.util.Iterator;
42import java.util.LinkedHashMap;
Michael Chanab29d9e2010-07-21 06:08:47 -070043import java.util.LinkedList;
Erik3f348f32010-08-10 13:17:19 -070044import java.util.Map.Entry;
Michael Chan83b0fe32010-07-08 16:46:26 -070045import java.util.WeakHashMap;
46
Erik25251192010-07-12 15:30:14 -070047public class CalendarController {
Erik3f348f32010-08-10 13:17:19 -070048 private static final boolean DEBUG = true;
Michael Chan83b0fe32010-07-08 16:46:26 -070049 private static final String TAG = "CalendarController";
Erikba1b94a2010-07-20 17:50:50 -070050 private static final String REFRESH_SELECTION = Calendars.SYNC_EVENTS + "=?";
51 private static final String[] REFRESH_ARGS = new String[] { "1" };
52 private static final String REFRESH_ORDER = Calendars._SYNC_ACCOUNT + ","
Erik7116ba42010-07-23 10:13:36 -070053 + Calendars._SYNC_ACCOUNT_TYPE;
Michael Chan83b0fe32010-07-08 16:46:26 -070054
Michael Chanab29d9e2010-07-21 06:08:47 -070055 private Context mContext;
56
Erik3f348f32010-08-10 13:17:19 -070057 // This uses a LinkedHashMap so that we can replace fragments based on the
58 // view id they are being expanded into since we can't guarantee a reference
59 // to the handler will be findable
60 private LinkedHashMap<Integer,EventHandler> eventHandlers =
61 new LinkedHashMap<Integer,EventHandler>(5);
62 private LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>();
Michael Chanab29d9e2010-07-21 06:08:47 -070063 private boolean mDispatchInProgress;
64
Michael Chan0558def2010-07-22 21:30:32 -070065 private static WeakHashMap<Context, CalendarController> instances =
66 new WeakHashMap<Context, CalendarController>();
67
Michael Chan83b0fe32010-07-08 16:46:26 -070068 private WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
69
Michael Chan3458a172010-07-13 17:58:21 -070070 private int mViewType = -1;
Michael Chan85e55092010-07-27 15:18:40 -070071 private int mDetailViewType = -1;
Michael Chanab29d9e2010-07-21 06:08:47 -070072 private int mPreviousViewType = -1;
Michael Chan3458a172010-07-13 17:58:21 -070073 private Time mTime = new Time();
74
Erikba1b94a2010-07-20 17:50:50 -070075 private AsyncQueryService mService;
76
Michael Chan49701592010-06-30 11:04:03 -070077 /**
Michael Chan83b0fe32010-07-08 16:46:26 -070078 * One of the event types that are sent to or from the controller
Michael Chan49701592010-06-30 11:04:03 -070079 */
Erik25251192010-07-12 15:30:14 -070080 public interface EventType {
Michael Chan9e89dca2010-07-13 17:56:09 -070081 final long CREATE_EVENT = 1L;
Michael Chan83b0fe32010-07-08 16:46:26 -070082 final long VIEW_EVENT = 1L << 1;
83 final long EDIT_EVENT = 1L << 2;
84 final long DELETE_EVENT = 1L << 3;
85
Michael Chanab29d9e2010-07-21 06:08:47 -070086 final long GO_TO = 1L << 4;
Michael Chan83b0fe32010-07-08 16:46:26 -070087
Michael Chanab29d9e2010-07-21 06:08:47 -070088 final long LAUNCH_MANAGE_CALENDARS = 1L << 5;
89 final long LAUNCH_SETTINGS = 1L << 6;
Erik954c8712010-08-06 10:12:34 -070090
91 final long EVENTS_CHANGED = 1L << 7;
Mason Tang4003d1c2010-08-17 13:50:45 -070092
93 final long SEARCH = 1L << 8;
Michael Chan83b0fe32010-07-08 16:46:26 -070094 }
Michael Chan49701592010-06-30 11:04:03 -070095
96 /**
Michael Chan83b0fe32010-07-08 16:46:26 -070097 * One of the Agenda/Day/Week/Month view types
Michael Chan49701592010-06-30 11:04:03 -070098 */
Erik25251192010-07-12 15:30:14 -070099 public interface ViewType {
Michael Chand6734db2010-07-22 00:48:08 -0700100 final int DETAIL = -1;
Michael Chan3458a172010-07-13 17:58:21 -0700101 final int CURRENT = 0;
102 final int AGENDA = 1;
103 final int DAY = 2;
104 final int WEEK = 3;
105 final int MONTH = 4;
Erikdd95df52010-08-27 09:31:18 -0700106 final int EDIT = 5;
Michael Chan83b0fe32010-07-08 16:46:26 -0700107 }
108
Erik25251192010-07-12 15:30:14 -0700109 public static class EventInfo {
Mason Tang00b8c1a2010-08-23 12:02:00 -0700110 public long eventType; // one of the EventType
111 public int viewType; // one of the ViewType
112 public long id; // event id
113 public Time selectedTime; // the selected time in focus
114 public Time startTime; // start of a range of time.
115 public Time endTime; // end of a range of time.
116 public int x; // x coordinate in the activity space
117 public int y; // y coordinate in the activity space
118 public String query; // query for a user search
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700119 public ComponentName componentName; // used in combination with query
Michael Chan83b0fe32010-07-08 16:46:26 -0700120 }
121
Michael Chanab29d9e2010-07-21 06:08:47 -0700122 // FRAG_TODO remove unneeded api's
Erik25251192010-07-12 15:30:14 -0700123 public interface EventHandler {
Michael Chan83b0fe32010-07-08 16:46:26 -0700124 long getSupportedEventTypes();
125 void handleEvent(EventInfo event);
126
127 /**
128 * Returns the time in millis of the selected event in this view.
129 * @return the selected time in UTC milliseconds.
130 */
131 long getSelectedTime();
132
133 /**
134 * Changes the view to include the given time.
135 * @param time the desired time to view.
136 * @animate enable animation
137 */
138 void goTo(Time time, boolean animate);
139
140 /**
141 * Changes the view to include today's date.
142 */
143 void goToToday();
144
145 /**
146 * This is called when the user wants to create a new event and returns
147 * true if the new event should default to an all-day event.
148 * @return true if the new event should be an all-day event.
149 */
150 boolean getAllDay();
151
152 /**
Erik954c8712010-08-06 10:12:34 -0700153 * This notifies the handler that the database has changed and it should
154 * update its view.
Michael Chan83b0fe32010-07-08 16:46:26 -0700155 */
156 void eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700157 }
158
Michael Chan0558def2010-07-22 21:30:32 -0700159 /**
160 * Creates and/or returns an instance of CalendarController associated with
161 * the supplied context. It is best to pass in the current Activity.
162 *
163 * @param context The activity if at all possible.
164 */
165 public static CalendarController getInstance(Context context) {
166 synchronized (instances) {
167 CalendarController controller = instances.get(context);
168 if (controller == null) {
169 controller = new CalendarController(context);
170 instances.put(context, controller);
171 }
172 return controller;
173 }
174 }
175
Eriked61b482010-08-13 13:59:50 -0700176 /**
177 * Removes an instance when it is no longer needed. This should be called in
178 * an activity's onDestroy method.
179 *
180 * @param context The activity used to create the controller
181 */
182 public static void removeInstance(Context context) {
183 instances.remove(context);
184 }
185
Michael Chan0558def2010-07-22 21:30:32 -0700186 private CalendarController(Context context) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700187 mContext = context;
Michael Chan3458a172010-07-13 17:58:21 -0700188 mTime.setToNow();
Michael Chan85e55092010-07-27 15:18:40 -0700189 mDetailViewType = Utils.getSharedPreference(mContext,
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700190 GeneralPreferences.KEY_DETAILED_VIEW,
191 GeneralPreferences.DEFAULT_DETAILED_VIEW);
Erikba1b94a2010-07-20 17:50:50 -0700192 mService = new AsyncQueryService(context) {
193 @Override
194 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Erik7116ba42010-07-23 10:13:36 -0700195 new RefreshInBackground().execute(cursor);
Erikba1b94a2010-07-20 17:50:50 -0700196 }
197 };
Michael Chan83b0fe32010-07-08 16:46:26 -0700198 }
Michael Chan49701592010-06-30 11:04:03 -0700199
200 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700201 * Helper for sending New/View/Edit/Delete events
202 *
203 * @param sender object of the caller
204 * @param eventType one of {@link EventType}
205 * @param eventId event id
Michael Chan3458a172010-07-13 17:58:21 -0700206 * @param startMillis start time
207 * @param endMillis end time
Michael Chan83b0fe32010-07-08 16:46:26 -0700208 * @param x x coordinate in the activity space
209 * @param y y coordinate in the activity space
Michael Chan49701592010-06-30 11:04:03 -0700210 */
Michael Chan9e89dca2010-07-13 17:56:09 -0700211 public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis,
212 long endMillis, int x, int y) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700213 EventInfo info = new EventInfo();
214 info.eventType = eventType;
Erikdd95df52010-08-27 09:31:18 -0700215 if (eventType == EventType.EDIT_EVENT) {
216 info.viewType = ViewType.EDIT;
217 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700218 info.id = eventId;
Michael Chan9e89dca2010-07-13 17:56:09 -0700219 info.startTime = new Time();
220 info.startTime.set(startMillis);
221 info.endTime = new Time();
222 info.endTime.set(endMillis);
Michael Chan83b0fe32010-07-08 16:46:26 -0700223 info.x = x;
224 info.y = y;
225 this.sendEvent(sender, info);
226 }
Michael Chan49701592010-06-30 11:04:03 -0700227
228 /**
Michael Chan83b0fe32010-07-08 16:46:26 -0700229 * Helper for sending non-calendar-event events
230 *
231 * @param sender object of the caller
232 * @param eventType one of {@link EventType}
Michael Chan83b0fe32010-07-08 16:46:26 -0700233 * @param start start time
234 * @param end end time
Michael Chand6734db2010-07-22 00:48:08 -0700235 * @param eventId event id
Michael Chan83b0fe32010-07-08 16:46:26 -0700236 * @param viewType {@link ViewType}
Michael Chan49701592010-06-30 11:04:03 -0700237 */
Erik25251192010-07-12 15:30:14 -0700238 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
Michael Chan3458a172010-07-13 17:58:21 -0700239 int viewType) {
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700240 sendEvent(sender, eventType, start, end, eventId, viewType, null, null);
241 }
242
243 /**
244 * sendEvent() variant mainly used for search.
245 */
246 public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId,
247 int viewType, String query, ComponentName componentName) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700248 EventInfo info = new EventInfo();
249 info.eventType = eventType;
250 info.startTime = start;
251 info.endTime = end;
252 info.id = eventId;
253 info.viewType = viewType;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700254 info.query = query;
255 info.componentName = componentName;
Michael Chan83b0fe32010-07-08 16:46:26 -0700256 this.sendEvent(sender, info);
257 }
258
Erik25251192010-07-12 15:30:14 -0700259 public void sendEvent(Object sender, final EventInfo event) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700260 // TODO Throw exception on invalid events
261
Erik3f348f32010-08-10 13:17:19 -0700262 if (DEBUG) {
263 Log.d(TAG, eventInfoToString(event));
264 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700265
266 Long filteredTypes = filters.get(sender);
267 if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) {
268 // Suppress event per filter
Erik3f348f32010-08-10 13:17:19 -0700269 if (DEBUG) {
270 Log.d(TAG, "Event suppressed");
271 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700272 return;
273 }
274
Michael Chanab29d9e2010-07-21 06:08:47 -0700275 mPreviousViewType = mViewType;
Michael Chan83b0fe32010-07-08 16:46:26 -0700276
Michael Chan3458a172010-07-13 17:58:21 -0700277 // Fix up view if not specified
Michael Chand6734db2010-07-22 00:48:08 -0700278 if (event.viewType == ViewType.DETAIL) {
Michael Chan85e55092010-07-27 15:18:40 -0700279 event.viewType = mDetailViewType;
280 mViewType = mDetailViewType;
Michael Chand6734db2010-07-22 00:48:08 -0700281 } else if (event.viewType == ViewType.CURRENT) {
Michael Chan3458a172010-07-13 17:58:21 -0700282 event.viewType = mViewType;
Michael Chanab29d9e2010-07-21 06:08:47 -0700283 } else {
284 mViewType = event.viewType;
Michael Chan85e55092010-07-27 15:18:40 -0700285
286 if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) {
287 mDetailViewType = mViewType;
288 }
Michael Chan3458a172010-07-13 17:58:21 -0700289 }
290
Mason Tang4003d1c2010-08-17 13:50:45 -0700291 // Fix up start time if not specified
292 if (event.startTime != null && event.startTime.toMillis(false) != 0) {
293 mTime.set(event.startTime);
294 }
295 event.startTime = mTime;
296
297 boolean handled = false;
Michael Chanab29d9e2010-07-21 06:08:47 -0700298 synchronized (this) {
299 mDispatchInProgress = true;
Michael Chan3458a172010-07-13 17:58:21 -0700300
Erik3f348f32010-08-10 13:17:19 -0700301 if (DEBUG) {
302 Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers");
303 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700304 // Dispatch to event handler(s)
Erik3f348f32010-08-10 13:17:19 -0700305 for (Iterator<Entry<Integer, EventHandler>> handlers =
306 eventHandlers.entrySet().iterator(); handlers.hasNext();) {
307 Entry<Integer, EventHandler> entry = handlers.next();
308 int key = entry.getKey();
309 EventHandler eventHandler = entry.getValue();
Michael Chanab29d9e2010-07-21 06:08:47 -0700310 if (eventHandler != null
311 && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) {
Erik3f348f32010-08-10 13:17:19 -0700312 if (mToBeRemovedEventHandlers.contains(key)) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700313 continue;
314 }
315 eventHandler.handleEvent(event);
Mason Tang4003d1c2010-08-17 13:50:45 -0700316 handled = true;
Michael Chan83b0fe32010-07-08 16:46:26 -0700317 }
318 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700319
320 // Deregister removed handlers
321 if (mToBeRemovedEventHandlers.size() > 0) {
Erik3f348f32010-08-10 13:17:19 -0700322 for (Integer zombie : mToBeRemovedEventHandlers) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700323 eventHandlers.remove(zombie);
324 }
325 mToBeRemovedEventHandlers.clear();
326 }
327 mDispatchInProgress = false;
Michael Chan83b0fe32010-07-08 16:46:26 -0700328 }
Mason Tang4003d1c2010-08-17 13:50:45 -0700329
330 if (!handled) {
331 // Launch Calendars, and Settings
332 if (event.eventType == EventType.LAUNCH_MANAGE_CALENDARS) {
333 launchManageCalendars();
334 return;
335 } else if (event.eventType == EventType.LAUNCH_SETTINGS) {
336 launchSettings();
337 return;
338 }
339
340 // Create/View/Edit/Delete Event
341 long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false);
342 if (event.eventType == EventType.CREATE_EVENT) {
343 launchCreateEvent(event.startTime.toMillis(false), endTime);
344 return;
345 } else if (event.eventType == EventType.VIEW_EVENT) {
346 launchViewEvent(event.id, event.startTime.toMillis(false), endTime);
347 return;
348 } else if (event.eventType == EventType.EDIT_EVENT) {
349 launchEditEvent(event.id, event.startTime.toMillis(false), endTime);
350 return;
351 } else if (event.eventType == EventType.DELETE_EVENT) {
352 launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime);
353 return;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700354 } else if (event.eventType == EventType.SEARCH) {
355 launchSearch(event.id, event.query, event.componentName);
356 return;
Mason Tang4003d1c2010-08-17 13:50:45 -0700357 }
358 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700359 }
360
Erik3f348f32010-08-10 13:17:19 -0700361 /**
362 * Adds or updates an event handler. This uses a LinkedHashMap so that we can
363 * replace fragments based on the view id they are being expanded into.
364 *
365 * @param key The view id or placeholder for this handler
366 * @param eventHandler Typically a fragment or activity in the calendar app
367 */
368 public void registerEventHandler(int key, EventHandler eventHandler) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700369 synchronized (this) {
Erik3f348f32010-08-10 13:17:19 -0700370 eventHandlers.put(key, eventHandler);
Michael Chanab29d9e2010-07-21 06:08:47 -0700371 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700372 }
373
Erik3f348f32010-08-10 13:17:19 -0700374 public void deregisterEventHandler(Integer key) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700375 synchronized (this) {
376 if (mDispatchInProgress) {
377 // To avoid ConcurrencyException, stash away the event handler for now.
Erik3f348f32010-08-10 13:17:19 -0700378 mToBeRemovedEventHandlers.add(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700379 } else {
Erik3f348f32010-08-10 13:17:19 -0700380 eventHandlers.remove(key);
Michael Chanab29d9e2010-07-21 06:08:47 -0700381 }
382 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700383 }
384
Michael Chan3458a172010-07-13 17:58:21 -0700385 // FRAG_TODO doesn't work yet
Erik25251192010-07-12 15:30:14 -0700386 public void filterBroadcasts(Object sender, long eventTypes) {
Michael Chan83b0fe32010-07-08 16:46:26 -0700387 filters.put(sender, eventTypes);
388 }
389
Mason Tang8e3d4302010-07-12 17:39:30 -0700390 /**
391 * @return the time that this controller is currently pointed at
392 */
393 public long getTime() {
394 return mTime.toMillis(false);
395 }
396
Michael Chanab29d9e2010-07-21 06:08:47 -0700397 public int getViewType() {
398 return mViewType;
399 }
400
401 public int getPreviousViewType() {
402 return mPreviousViewType;
403 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700404
Michael Chan9e89dca2010-07-13 17:56:09 -0700405 private void launchManageCalendars() {
406 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chanab29d9e2010-07-21 06:08:47 -0700407 intent.setClass(mContext, SelectCalendarsActivity.class);
Michael Chan9e89dca2010-07-13 17:56:09 -0700408 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700409 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700410 }
411
412 private void launchSettings() {
413 Intent intent = new Intent(Intent.ACTION_VIEW);
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700414 intent.setClassName(mContext, CalendarSettingsActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700415 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Michael Chanab29d9e2010-07-21 06:08:47 -0700416 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700417 }
418
419 private void launchCreateEvent(long startMillis, long endMillis) {
420 Intent intent = new Intent(Intent.ACTION_VIEW);
Michael Chanab29d9e2010-07-21 06:08:47 -0700421 intent.setClassName(mContext, EditEventActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700422 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
423 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700424 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700425 }
426
427 private void launchViewEvent(long eventId, long startMillis, long endMillis) {
428 Intent intent = new Intent(Intent.ACTION_VIEW);
429 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
430 intent.setData(eventUri);
Michael Chanab29d9e2010-07-21 06:08:47 -0700431 intent.setClassName(mContext, EventInfoActivity.class.getName());
Michael Chan9e89dca2010-07-13 17:56:09 -0700432 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
433 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700434 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700435 }
436
437 private void launchEditEvent(long eventId, long startMillis, long endMillis) {
438 Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
439 Intent intent = new Intent(Intent.ACTION_EDIT, uri);
440 intent.putExtra(EVENT_BEGIN_TIME, startMillis);
441 intent.putExtra(EVENT_END_TIME, endMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700442 intent.setClass(mContext, EditEventActivity.class);
443 mContext.startActivity(intent);
Michael Chan9e89dca2010-07-13 17:56:09 -0700444 }
445
446 private void launchDeleteEvent(long eventId, long startMillis, long endMillis) {
447 launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1);
448 }
449
450 private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis,
451 long endMillis, int deleteWhich) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700452 DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity,
Michael Chan9e89dca2010-07-13 17:56:09 -0700453 parentActivity != null /* exit when done */);
454 deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich);
455 }
Michael Chan3458a172010-07-13 17:58:21 -0700456
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700457 private void launchSearch(long eventId, String query, ComponentName componentName) {
458 final SearchManager searchManager =
459 (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
460 final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
461 final Intent intent = new Intent(Intent.ACTION_SEARCH);
462 intent.putExtra(SearchManager.QUERY, query);
463 intent.setComponent(searchableInfo.getSearchActivity());
464 mContext.startActivity(intent);
465 }
466
Erikba1b94a2010-07-20 17:50:50 -0700467 public void refreshCalendars() {
468 Log.d(TAG, "RefreshCalendars starting");
469 // get the account, url, and current sync state
470 mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI,
471 new String[] {Calendars._ID, // 0
472 Calendars._SYNC_ACCOUNT, // 1
473 Calendars._SYNC_ACCOUNT_TYPE, // 2
474 },
475 REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER);
Erikba1b94a2010-07-20 17:50:50 -0700476 }
477
Erikdd95df52010-08-27 09:31:18 -0700478 // Forces the viewType. Should only be used for initialization.
479 public void setViewType(int viewType) {
480 mViewType = viewType;
481 }
482
Erik7116ba42010-07-23 10:13:36 -0700483 private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> {
484 /* (non-Javadoc)
485 * @see android.os.AsyncTask#doInBackground(Params[])
486 */
487 @Override
488 protected Integer doInBackground(Cursor... params) {
489 if (params.length != 1) {
490 return null;
Erikba1b94a2010-07-20 17:50:50 -0700491 }
Erik7116ba42010-07-23 10:13:36 -0700492 Cursor cursor = params[0];
493 if (cursor == null) {
494 return null;
495 }
Erikba1b94a2010-07-20 17:50:50 -0700496
Erik7116ba42010-07-23 10:13:36 -0700497 String previousAccount = null;
498 String previousType = null;
499 Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars");
500 try {
501 while (cursor.moveToNext()) {
502 Account account = null;
503 String accountName = cursor.getString(1);
504 String accountType = cursor.getString(2);
505 // Only need to schedule one sync per account and they're
506 // ordered by account,type
507 if (TextUtils.equals(accountName, previousAccount) &&
508 TextUtils.equals(accountType, previousType)) {
509 continue;
510 }
511 previousAccount = accountName;
512 previousType = accountType;
513 account = new Account(accountName, accountType);
514 scheduleSync(account, false /* two-way sync */, null);
515 }
516 } finally {
517 cursor.close();
518 }
519 return null;
Erikba1b94a2010-07-20 17:50:50 -0700520 }
Erik7116ba42010-07-23 10:13:36 -0700521
522 /**
523 * Schedule a calendar sync for the account.
524 * @param account the account for which to schedule a sync
525 * @param uploadChangesOnly if set, specify that the sync should only send
526 * up local changes. This is typically used for a local sync, a user override of
527 * too many deletions, or a sync after a calendar is unselected.
528 * @param url the url feed for the calendar to sync (may be null, in which case a poll of
529 * all feeds is done.)
530 */
531 void scheduleSync(Account account, boolean uploadChangesOnly, String url) {
532 Bundle extras = new Bundle();
533 if (uploadChangesOnly) {
534 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly);
535 }
536 if (url != null) {
537 extras.putString("feed", url);
538 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
539 }
540 ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras);
Erikba1b94a2010-07-20 17:50:50 -0700541 }
Erikba1b94a2010-07-20 17:50:50 -0700542 }
543
Michael Chan3458a172010-07-13 17:58:21 -0700544 private String eventInfoToString(EventInfo eventInfo) {
545 String tmp = "Unknown";
546
547 StringBuilder builder = new StringBuilder();
Michael Chanab29d9e2010-07-21 06:08:47 -0700548 if ((eventInfo.eventType & EventType.GO_TO) != 0) {
Michael Chan3458a172010-07-13 17:58:21 -0700549 tmp = "Go to time/event";
550 } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) {
551 tmp = "New event";
552 } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) {
553 tmp = "View event";
554 } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) {
555 tmp = "Edit event";
556 } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) {
557 tmp = "Delete event";
558 } else if ((eventInfo.eventType & EventType.LAUNCH_MANAGE_CALENDARS) != 0) {
559 tmp = "Launch select calendar";
560 } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) {
561 tmp = "Launch settings";
Erik3f348f32010-08-10 13:17:19 -0700562 } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) {
563 tmp = "Refresh events";
Mason Tang4003d1c2010-08-17 13:50:45 -0700564 } else if ((eventInfo.eventType & EventType.SEARCH) != 0) {
565 tmp = "Search";
Michael Chan3458a172010-07-13 17:58:21 -0700566 }
567 builder.append(tmp);
568 builder.append(": id=");
569 builder.append(eventInfo.id);
Michael Chand6734db2010-07-22 00:48:08 -0700570 builder.append(", selected=");
571 builder.append(eventInfo.selectedTime);
572 builder.append(", start=");
Michael Chan3458a172010-07-13 17:58:21 -0700573 builder.append(eventInfo.startTime);
Michael Chand6734db2010-07-22 00:48:08 -0700574 builder.append(", end=");
Michael Chan3458a172010-07-13 17:58:21 -0700575 builder.append(eventInfo.endTime);
576 builder.append(", viewType=");
577 builder.append(eventInfo.viewType);
578 builder.append(", x=");
579 builder.append(eventInfo.x);
580 builder.append(", y=");
581 builder.append(eventInfo.y);
582 return builder.toString();
583 }
Michael Chan49701592010-06-30 11:04:03 -0700584}