Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.calendar; |
| 18 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 19 | import static android.provider.Calendar.EVENT_BEGIN_TIME; |
| 20 | import static android.provider.Calendar.EVENT_END_TIME; |
| 21 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 22 | import android.accounts.Account; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 24 | import android.app.SearchManager; |
| 25 | import android.app.SearchableInfo; |
| 26 | import android.content.ComponentName; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 28 | import android.content.ContentUris; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 30 | import android.content.Intent; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 31 | import android.database.Cursor; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 33 | import android.os.AsyncTask; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.provider.Calendar.Calendars; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 36 | import android.provider.Calendar.Events; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 37 | import android.text.TextUtils; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 38 | import android.text.format.Time; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 40 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 41 | import java.util.Iterator; |
| 42 | import java.util.LinkedHashMap; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 43 | import java.util.LinkedList; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 44 | import java.util.Map.Entry; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 45 | import java.util.WeakHashMap; |
| 46 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 47 | public class CalendarController { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 48 | private static final boolean DEBUG = true; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 49 | private static final String TAG = "CalendarController"; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 50 | 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 + "," |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 53 | + Calendars._SYNC_ACCOUNT_TYPE; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 54 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 55 | private Context mContext; |
| 56 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 57 | // 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 63 | private boolean mDispatchInProgress; |
| 64 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 65 | private static WeakHashMap<Context, CalendarController> instances = |
| 66 | new WeakHashMap<Context, CalendarController>(); |
| 67 | |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 68 | private WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1); |
| 69 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 70 | private int mViewType = -1; |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 71 | private int mDetailViewType = -1; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 72 | private int mPreviousViewType = -1; |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 73 | private long mEventId = -1; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 74 | private Time mTime = new Time(); |
| 75 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 76 | private AsyncQueryService mService; |
| 77 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 78 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 79 | * One of the event types that are sent to or from the controller |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 80 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 81 | public interface EventType { |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 82 | final long CREATE_EVENT = 1L; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 83 | final long VIEW_EVENT = 1L << 1; |
| 84 | final long EDIT_EVENT = 1L << 2; |
| 85 | final long DELETE_EVENT = 1L << 3; |
| 86 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 87 | final long GO_TO = 1L << 4; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 88 | |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 89 | final long LAUNCH_SETTINGS = 1L << 5; |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 90 | |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 91 | final long EVENTS_CHANGED = 1L << 6; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 92 | |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 93 | final long SEARCH = 1L << 7; |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 94 | |
| 95 | // User has pressed the home key |
| 96 | final long USER_HOME = 1L << 8; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 97 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 100 | * One of the Agenda/Day/Week/Month view types |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 101 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 102 | public interface ViewType { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 103 | final int DETAIL = -1; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 104 | final int CURRENT = 0; |
| 105 | final int AGENDA = 1; |
| 106 | final int DAY = 2; |
| 107 | final int WEEK = 3; |
| 108 | final int MONTH = 4; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 109 | final int EDIT = 5; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 112 | public static class EventInfo { |
Mason Tang | 00b8c1a | 2010-08-23 12:02:00 -0700 | [diff] [blame] | 113 | public long eventType; // one of the EventType |
| 114 | public int viewType; // one of the ViewType |
| 115 | public long id; // event id |
| 116 | public Time selectedTime; // the selected time in focus |
| 117 | public Time startTime; // start of a range of time. |
| 118 | public Time endTime; // end of a range of time. |
| 119 | public int x; // x coordinate in the activity space |
| 120 | public int y; // y coordinate in the activity space |
| 121 | public String query; // query for a user search |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 122 | public ComponentName componentName; // used in combination with query |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 125 | // FRAG_TODO remove unneeded api's |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 126 | public interface EventHandler { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 127 | long getSupportedEventTypes(); |
| 128 | void handleEvent(EventInfo event); |
| 129 | |
| 130 | /** |
| 131 | * Returns the time in millis of the selected event in this view. |
| 132 | * @return the selected time in UTC milliseconds. |
| 133 | */ |
| 134 | long getSelectedTime(); |
| 135 | |
| 136 | /** |
| 137 | * Changes the view to include the given time. |
| 138 | * @param time the desired time to view. |
| 139 | * @animate enable animation |
| 140 | */ |
| 141 | void goTo(Time time, boolean animate); |
| 142 | |
| 143 | /** |
| 144 | * Changes the view to include today's date. |
| 145 | */ |
| 146 | void goToToday(); |
| 147 | |
| 148 | /** |
| 149 | * This is called when the user wants to create a new event and returns |
| 150 | * true if the new event should default to an all-day event. |
| 151 | * @return true if the new event should be an all-day event. |
| 152 | */ |
| 153 | boolean getAllDay(); |
| 154 | |
| 155 | /** |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 156 | * This notifies the handler that the database has changed and it should |
| 157 | * update its view. |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 158 | */ |
| 159 | void eventsChanged(); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Creates and/or returns an instance of CalendarController associated with |
| 164 | * the supplied context. It is best to pass in the current Activity. |
| 165 | * |
| 166 | * @param context The activity if at all possible. |
| 167 | */ |
| 168 | public static CalendarController getInstance(Context context) { |
| 169 | synchronized (instances) { |
| 170 | CalendarController controller = instances.get(context); |
| 171 | if (controller == null) { |
| 172 | controller = new CalendarController(context); |
| 173 | instances.put(context, controller); |
| 174 | } |
| 175 | return controller; |
| 176 | } |
| 177 | } |
| 178 | |
Erik | ed61b48 | 2010-08-13 13:59:50 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Removes an instance when it is no longer needed. This should be called in |
| 181 | * an activity's onDestroy method. |
| 182 | * |
| 183 | * @param context The activity used to create the controller |
| 184 | */ |
| 185 | public static void removeInstance(Context context) { |
| 186 | instances.remove(context); |
| 187 | } |
| 188 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 189 | private CalendarController(Context context) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 190 | mContext = context; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 191 | mTime.setToNow(); |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 192 | mDetailViewType = Utils.getSharedPreference(mContext, |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 193 | GeneralPreferences.KEY_DETAILED_VIEW, |
| 194 | GeneralPreferences.DEFAULT_DETAILED_VIEW); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 195 | mService = new AsyncQueryService(context) { |
| 196 | @Override |
| 197 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 198 | new RefreshInBackground().execute(cursor); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 199 | } |
| 200 | }; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 201 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 204 | * Helper for sending New/View/Edit/Delete events |
| 205 | * |
| 206 | * @param sender object of the caller |
| 207 | * @param eventType one of {@link EventType} |
| 208 | * @param eventId event id |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 209 | * @param startMillis start time |
| 210 | * @param endMillis end time |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 211 | * @param x x coordinate in the activity space |
| 212 | * @param y y coordinate in the activity space |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 213 | */ |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 214 | public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis, |
| 215 | long endMillis, int x, int y) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 216 | EventInfo info = new EventInfo(); |
| 217 | info.eventType = eventType; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 218 | if (eventType == EventType.EDIT_EVENT) { |
| 219 | info.viewType = ViewType.EDIT; |
| 220 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 221 | info.id = eventId; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 222 | info.startTime = new Time(); |
| 223 | info.startTime.set(startMillis); |
| 224 | info.endTime = new Time(); |
| 225 | info.endTime.set(endMillis); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 226 | info.x = x; |
| 227 | info.y = y; |
| 228 | this.sendEvent(sender, info); |
| 229 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 230 | |
| 231 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 232 | * Helper for sending non-calendar-event events |
| 233 | * |
| 234 | * @param sender object of the caller |
| 235 | * @param eventType one of {@link EventType} |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 236 | * @param start start time |
| 237 | * @param end end time |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 238 | * @param eventId event id |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 239 | * @param viewType {@link ViewType} |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 240 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 241 | public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId, |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 242 | int viewType) { |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 243 | sendEvent(sender, eventType, start, end, eventId, viewType, null, null); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * sendEvent() variant mainly used for search. |
| 248 | */ |
| 249 | public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId, |
| 250 | int viewType, String query, ComponentName componentName) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 251 | EventInfo info = new EventInfo(); |
| 252 | info.eventType = eventType; |
| 253 | info.startTime = start; |
| 254 | info.endTime = end; |
| 255 | info.id = eventId; |
| 256 | info.viewType = viewType; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 257 | info.query = query; |
| 258 | info.componentName = componentName; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 259 | this.sendEvent(sender, info); |
| 260 | } |
| 261 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 262 | public void sendEvent(Object sender, final EventInfo event) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 263 | // TODO Throw exception on invalid events |
| 264 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 265 | if (DEBUG) { |
| 266 | Log.d(TAG, eventInfoToString(event)); |
| 267 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 268 | |
| 269 | Long filteredTypes = filters.get(sender); |
| 270 | if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) { |
| 271 | // Suppress event per filter |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 272 | if (DEBUG) { |
| 273 | Log.d(TAG, "Event suppressed"); |
| 274 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 278 | mPreviousViewType = mViewType; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 279 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 280 | // Fix up view if not specified |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 281 | if (event.viewType == ViewType.DETAIL) { |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 282 | event.viewType = mDetailViewType; |
| 283 | mViewType = mDetailViewType; |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 284 | } else if (event.viewType == ViewType.CURRENT) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 285 | event.viewType = mViewType; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 286 | } else { |
| 287 | mViewType = event.viewType; |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 288 | |
| 289 | if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) { |
| 290 | mDetailViewType = mViewType; |
| 291 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 294 | // Fix up start time if not specified |
| 295 | if (event.startTime != null && event.startTime.toMillis(false) != 0) { |
| 296 | mTime.set(event.startTime); |
| 297 | } |
| 298 | event.startTime = mTime; |
| 299 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 300 | // Store the eventId if we're entering edit event |
| 301 | if ((event.eventType & (EventType.CREATE_EVENT | EventType.EDIT_EVENT)) != 0 || |
| 302 | (event.eventType == EventType.GO_TO && event.viewType == ViewType.EDIT)) { |
| 303 | if (event.id > 0) { |
| 304 | mEventId = event.id; |
| 305 | } else { |
| 306 | mEventId = -1; |
| 307 | } |
| 308 | } |
| 309 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 310 | boolean handled = false; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 311 | synchronized (this) { |
| 312 | mDispatchInProgress = true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 313 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 314 | if (DEBUG) { |
| 315 | Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers"); |
| 316 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 317 | // Dispatch to event handler(s) |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 318 | for (Iterator<Entry<Integer, EventHandler>> handlers = |
| 319 | eventHandlers.entrySet().iterator(); handlers.hasNext();) { |
| 320 | Entry<Integer, EventHandler> entry = handlers.next(); |
| 321 | int key = entry.getKey(); |
| 322 | EventHandler eventHandler = entry.getValue(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 323 | if (eventHandler != null |
| 324 | && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 325 | if (mToBeRemovedEventHandlers.contains(key)) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 326 | continue; |
| 327 | } |
| 328 | eventHandler.handleEvent(event); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 329 | handled = true; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 332 | |
| 333 | // Deregister removed handlers |
| 334 | if (mToBeRemovedEventHandlers.size() > 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 335 | for (Integer zombie : mToBeRemovedEventHandlers) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 336 | eventHandlers.remove(zombie); |
| 337 | } |
| 338 | mToBeRemovedEventHandlers.clear(); |
| 339 | } |
| 340 | mDispatchInProgress = false; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 341 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 342 | |
| 343 | if (!handled) { |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 344 | // Launch Settings |
| 345 | if (event.eventType == EventType.LAUNCH_SETTINGS) { |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 346 | launchSettings(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | // Create/View/Edit/Delete Event |
| 351 | long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false); |
| 352 | if (event.eventType == EventType.CREATE_EVENT) { |
| 353 | launchCreateEvent(event.startTime.toMillis(false), endTime); |
| 354 | return; |
| 355 | } else if (event.eventType == EventType.VIEW_EVENT) { |
| 356 | launchViewEvent(event.id, event.startTime.toMillis(false), endTime); |
| 357 | return; |
| 358 | } else if (event.eventType == EventType.EDIT_EVENT) { |
| 359 | launchEditEvent(event.id, event.startTime.toMillis(false), endTime); |
| 360 | return; |
| 361 | } else if (event.eventType == EventType.DELETE_EVENT) { |
| 362 | launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime); |
| 363 | return; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 364 | } else if (event.eventType == EventType.SEARCH) { |
| 365 | launchSearch(event.id, event.query, event.componentName); |
| 366 | return; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 371 | /** |
| 372 | * Adds or updates an event handler. This uses a LinkedHashMap so that we can |
| 373 | * replace fragments based on the view id they are being expanded into. |
| 374 | * |
| 375 | * @param key The view id or placeholder for this handler |
| 376 | * @param eventHandler Typically a fragment or activity in the calendar app |
| 377 | */ |
| 378 | public void registerEventHandler(int key, EventHandler eventHandler) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 379 | synchronized (this) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 380 | eventHandlers.put(key, eventHandler); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 381 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 384 | public void deregisterEventHandler(Integer key) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 385 | synchronized (this) { |
| 386 | if (mDispatchInProgress) { |
| 387 | // To avoid ConcurrencyException, stash away the event handler for now. |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 388 | mToBeRemovedEventHandlers.add(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 389 | } else { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 390 | eventHandlers.remove(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 395 | // FRAG_TODO doesn't work yet |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 396 | public void filterBroadcasts(Object sender, long eventTypes) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 397 | filters.put(sender, eventTypes); |
| 398 | } |
| 399 | |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 400 | /** |
| 401 | * @return the time that this controller is currently pointed at |
| 402 | */ |
| 403 | public long getTime() { |
| 404 | return mTime.toMillis(false); |
| 405 | } |
| 406 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 407 | /** |
| 408 | * @return the last event ID the edit view was launched with |
| 409 | */ |
| 410 | public long getEventId() { |
| 411 | return mEventId; |
| 412 | } |
| 413 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 414 | public int getViewType() { |
| 415 | return mViewType; |
| 416 | } |
| 417 | |
| 418 | public int getPreviousViewType() { |
| 419 | return mPreviousViewType; |
| 420 | } |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 421 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 422 | private void launchSettings() { |
| 423 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 424 | intent.setClassName(mContext, CalendarSettingsActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 425 | intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 426 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private void launchCreateEvent(long startMillis, long endMillis) { |
| 430 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 431 | intent.setClassName(mContext, EditEventActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 432 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 433 | intent.putExtra(EVENT_END_TIME, endMillis); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 434 | mEventId = -1; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 435 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | private void launchViewEvent(long eventId, long startMillis, long endMillis) { |
| 439 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 440 | Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 441 | intent.setData(eventUri); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 442 | intent.setClassName(mContext, EventInfoActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 443 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 444 | intent.putExtra(EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 445 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | private void launchEditEvent(long eventId, long startMillis, long endMillis) { |
| 449 | Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 450 | Intent intent = new Intent(Intent.ACTION_EDIT, uri); |
| 451 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 452 | intent.putExtra(EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 453 | intent.setClass(mContext, EditEventActivity.class); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 454 | mEventId = eventId; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 455 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | private void launchDeleteEvent(long eventId, long startMillis, long endMillis) { |
| 459 | launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1); |
| 460 | } |
| 461 | |
| 462 | private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis, |
| 463 | long endMillis, int deleteWhich) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 464 | DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity, |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 465 | parentActivity != null /* exit when done */); |
| 466 | deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich); |
| 467 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 468 | |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 469 | private void launchSearch(long eventId, String query, ComponentName componentName) { |
| 470 | final SearchManager searchManager = |
| 471 | (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE); |
| 472 | final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName); |
| 473 | final Intent intent = new Intent(Intent.ACTION_SEARCH); |
| 474 | intent.putExtra(SearchManager.QUERY, query); |
| 475 | intent.setComponent(searchableInfo.getSearchActivity()); |
| 476 | mContext.startActivity(intent); |
| 477 | } |
| 478 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 479 | public void refreshCalendars() { |
| 480 | Log.d(TAG, "RefreshCalendars starting"); |
| 481 | // get the account, url, and current sync state |
| 482 | mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI, |
| 483 | new String[] {Calendars._ID, // 0 |
| 484 | Calendars._SYNC_ACCOUNT, // 1 |
| 485 | Calendars._SYNC_ACCOUNT_TYPE, // 2 |
| 486 | }, |
| 487 | REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 490 | // Forces the viewType. Should only be used for initialization. |
| 491 | public void setViewType(int viewType) { |
| 492 | mViewType = viewType; |
| 493 | } |
| 494 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame^] | 495 | // Sets the eventId. Should only be used for initialization. |
| 496 | public void setEventId(long eventId) { |
| 497 | mEventId = eventId; |
| 498 | } |
| 499 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 500 | private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> { |
| 501 | /* (non-Javadoc) |
| 502 | * @see android.os.AsyncTask#doInBackground(Params[]) |
| 503 | */ |
| 504 | @Override |
| 505 | protected Integer doInBackground(Cursor... params) { |
| 506 | if (params.length != 1) { |
| 507 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 508 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 509 | Cursor cursor = params[0]; |
| 510 | if (cursor == null) { |
| 511 | return null; |
| 512 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 513 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 514 | String previousAccount = null; |
| 515 | String previousType = null; |
| 516 | Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars"); |
| 517 | try { |
| 518 | while (cursor.moveToNext()) { |
| 519 | Account account = null; |
| 520 | String accountName = cursor.getString(1); |
| 521 | String accountType = cursor.getString(2); |
| 522 | // Only need to schedule one sync per account and they're |
| 523 | // ordered by account,type |
| 524 | if (TextUtils.equals(accountName, previousAccount) && |
| 525 | TextUtils.equals(accountType, previousType)) { |
| 526 | continue; |
| 527 | } |
| 528 | previousAccount = accountName; |
| 529 | previousType = accountType; |
| 530 | account = new Account(accountName, accountType); |
| 531 | scheduleSync(account, false /* two-way sync */, null); |
| 532 | } |
| 533 | } finally { |
| 534 | cursor.close(); |
| 535 | } |
| 536 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 537 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * Schedule a calendar sync for the account. |
| 541 | * @param account the account for which to schedule a sync |
| 542 | * @param uploadChangesOnly if set, specify that the sync should only send |
| 543 | * up local changes. This is typically used for a local sync, a user override of |
| 544 | * too many deletions, or a sync after a calendar is unselected. |
| 545 | * @param url the url feed for the calendar to sync (may be null, in which case a poll of |
| 546 | * all feeds is done.) |
| 547 | */ |
| 548 | void scheduleSync(Account account, boolean uploadChangesOnly, String url) { |
| 549 | Bundle extras = new Bundle(); |
| 550 | if (uploadChangesOnly) { |
| 551 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly); |
| 552 | } |
| 553 | if (url != null) { |
| 554 | extras.putString("feed", url); |
| 555 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 556 | } |
| 557 | ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 558 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 561 | private String eventInfoToString(EventInfo eventInfo) { |
| 562 | String tmp = "Unknown"; |
| 563 | |
| 564 | StringBuilder builder = new StringBuilder(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 565 | if ((eventInfo.eventType & EventType.GO_TO) != 0) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 566 | tmp = "Go to time/event"; |
| 567 | } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) { |
| 568 | tmp = "New event"; |
| 569 | } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) { |
| 570 | tmp = "View event"; |
| 571 | } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) { |
| 572 | tmp = "Edit event"; |
| 573 | } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) { |
| 574 | tmp = "Delete event"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 575 | } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) { |
| 576 | tmp = "Launch settings"; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 577 | } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) { |
| 578 | tmp = "Refresh events"; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 579 | } else if ((eventInfo.eventType & EventType.SEARCH) != 0) { |
| 580 | tmp = "Search"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 581 | } |
| 582 | builder.append(tmp); |
| 583 | builder.append(": id="); |
| 584 | builder.append(eventInfo.id); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 585 | builder.append(", selected="); |
| 586 | builder.append(eventInfo.selectedTime); |
| 587 | builder.append(", start="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 588 | builder.append(eventInfo.startTime); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 589 | builder.append(", end="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 590 | builder.append(eventInfo.endTime); |
| 591 | builder.append(", viewType="); |
| 592 | builder.append(eventInfo.viewType); |
| 593 | builder.append(", x="); |
| 594 | builder.append(eventInfo.x); |
| 595 | builder.append(", y="); |
| 596 | builder.append(eventInfo.y); |
| 597 | return builder.toString(); |
| 598 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 599 | } |