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; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 73 | private Time mTime = new Time(); |
| 74 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 75 | private AsyncQueryService mService; |
| 76 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 77 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 78 | * 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] | 79 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 80 | public interface EventType { |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 81 | final long CREATE_EVENT = 1L; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 82 | final long VIEW_EVENT = 1L << 1; |
| 83 | final long EDIT_EVENT = 1L << 2; |
| 84 | final long DELETE_EVENT = 1L << 3; |
| 85 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 86 | final long GO_TO = 1L << 4; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 87 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 88 | final long LAUNCH_MANAGE_CALENDARS = 1L << 5; |
| 89 | final long LAUNCH_SETTINGS = 1L << 6; |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 90 | |
| 91 | final long EVENTS_CHANGED = 1L << 7; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 92 | |
| 93 | final long SEARCH = 1L << 8; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 94 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 97 | * One of the Agenda/Day/Week/Month view types |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 98 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 99 | public interface ViewType { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 100 | final int DETAIL = -1; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 101 | final int CURRENT = 0; |
| 102 | final int AGENDA = 1; |
| 103 | final int DAY = 2; |
| 104 | final int WEEK = 3; |
| 105 | final int MONTH = 4; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 106 | final int EDIT = 5; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 109 | public static class EventInfo { |
Mason Tang | 00b8c1a | 2010-08-23 12:02:00 -0700 | [diff] [blame] | 110 | 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 Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame^] | 119 | public ComponentName componentName; // used in combination with query |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 122 | // FRAG_TODO remove unneeded api's |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 123 | public interface EventHandler { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 124 | 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 | /** |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 153 | * This notifies the handler that the database has changed and it should |
| 154 | * update its view. |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 155 | */ |
| 156 | void eventsChanged(); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 159 | /** |
| 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 | |
Erik | ed61b48 | 2010-08-13 13:59:50 -0700 | [diff] [blame] | 176 | /** |
| 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 Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 186 | private CalendarController(Context context) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 187 | mContext = context; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 188 | mTime.setToNow(); |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 189 | mDetailViewType = Utils.getSharedPreference(mContext, |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 190 | GeneralPreferences.KEY_DETAILED_VIEW, |
| 191 | GeneralPreferences.DEFAULT_DETAILED_VIEW); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 192 | mService = new AsyncQueryService(context) { |
| 193 | @Override |
| 194 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 195 | new RefreshInBackground().execute(cursor); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 196 | } |
| 197 | }; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 198 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 201 | * 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 Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 206 | * @param startMillis start time |
| 207 | * @param endMillis end time |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 208 | * @param x x coordinate in the activity space |
| 209 | * @param y y coordinate in the activity space |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 210 | */ |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 211 | public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis, |
| 212 | long endMillis, int x, int y) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 213 | EventInfo info = new EventInfo(); |
| 214 | info.eventType = eventType; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 215 | if (eventType == EventType.EDIT_EVENT) { |
| 216 | info.viewType = ViewType.EDIT; |
| 217 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 218 | info.id = eventId; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 219 | info.startTime = new Time(); |
| 220 | info.startTime.set(startMillis); |
| 221 | info.endTime = new Time(); |
| 222 | info.endTime.set(endMillis); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 223 | info.x = x; |
| 224 | info.y = y; |
| 225 | this.sendEvent(sender, info); |
| 226 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 229 | * Helper for sending non-calendar-event events |
| 230 | * |
| 231 | * @param sender object of the caller |
| 232 | * @param eventType one of {@link EventType} |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 233 | * @param start start time |
| 234 | * @param end end time |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 235 | * @param eventId event id |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 236 | * @param viewType {@link ViewType} |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 237 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 238 | 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] | 239 | int viewType) { |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame^] | 240 | 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 Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 248 | 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 Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame^] | 254 | info.query = query; |
| 255 | info.componentName = componentName; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 256 | this.sendEvent(sender, info); |
| 257 | } |
| 258 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 259 | public void sendEvent(Object sender, final EventInfo event) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 260 | // TODO Throw exception on invalid events |
| 261 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 262 | if (DEBUG) { |
| 263 | Log.d(TAG, eventInfoToString(event)); |
| 264 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 265 | |
| 266 | Long filteredTypes = filters.get(sender); |
| 267 | if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) { |
| 268 | // Suppress event per filter |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 269 | if (DEBUG) { |
| 270 | Log.d(TAG, "Event suppressed"); |
| 271 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 275 | mPreviousViewType = mViewType; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 276 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 277 | // Fix up view if not specified |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 278 | if (event.viewType == ViewType.DETAIL) { |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 279 | event.viewType = mDetailViewType; |
| 280 | mViewType = mDetailViewType; |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 281 | } else if (event.viewType == ViewType.CURRENT) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 282 | event.viewType = mViewType; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 283 | } else { |
| 284 | mViewType = event.viewType; |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 285 | |
| 286 | if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) { |
| 287 | mDetailViewType = mViewType; |
| 288 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 291 | // 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 298 | synchronized (this) { |
| 299 | mDispatchInProgress = true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 300 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 301 | if (DEBUG) { |
| 302 | Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers"); |
| 303 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 304 | // Dispatch to event handler(s) |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 305 | 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 310 | if (eventHandler != null |
| 311 | && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 312 | if (mToBeRemovedEventHandlers.contains(key)) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 313 | continue; |
| 314 | } |
| 315 | eventHandler.handleEvent(event); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 316 | handled = true; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 319 | |
| 320 | // Deregister removed handlers |
| 321 | if (mToBeRemovedEventHandlers.size() > 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 322 | for (Integer zombie : mToBeRemovedEventHandlers) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 323 | eventHandlers.remove(zombie); |
| 324 | } |
| 325 | mToBeRemovedEventHandlers.clear(); |
| 326 | } |
| 327 | mDispatchInProgress = false; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 328 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 329 | |
| 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 Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame^] | 354 | } else if (event.eventType == EventType.SEARCH) { |
| 355 | launchSearch(event.id, event.query, event.componentName); |
| 356 | return; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 361 | /** |
| 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 369 | synchronized (this) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 370 | eventHandlers.put(key, eventHandler); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 371 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 374 | public void deregisterEventHandler(Integer key) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 375 | synchronized (this) { |
| 376 | if (mDispatchInProgress) { |
| 377 | // To avoid ConcurrencyException, stash away the event handler for now. |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 378 | mToBeRemovedEventHandlers.add(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 379 | } else { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 380 | eventHandlers.remove(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 385 | // FRAG_TODO doesn't work yet |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 386 | public void filterBroadcasts(Object sender, long eventTypes) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 387 | filters.put(sender, eventTypes); |
| 388 | } |
| 389 | |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 390 | /** |
| 391 | * @return the time that this controller is currently pointed at |
| 392 | */ |
| 393 | public long getTime() { |
| 394 | return mTime.toMillis(false); |
| 395 | } |
| 396 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 397 | public int getViewType() { |
| 398 | return mViewType; |
| 399 | } |
| 400 | |
| 401 | public int getPreviousViewType() { |
| 402 | return mPreviousViewType; |
| 403 | } |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 404 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 405 | private void launchManageCalendars() { |
| 406 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 407 | intent.setClass(mContext, SelectCalendarsActivity.class); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 408 | 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] | 409 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | private void launchSettings() { |
| 413 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 414 | intent.setClassName(mContext, CalendarSettingsActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 415 | 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] | 416 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | private void launchCreateEvent(long startMillis, long endMillis) { |
| 420 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 421 | intent.setClassName(mContext, EditEventActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 422 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 423 | intent.putExtra(EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 424 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 425 | } |
| 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 431 | intent.setClassName(mContext, EventInfoActivity.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); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 434 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 435 | } |
| 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 442 | intent.setClass(mContext, EditEventActivity.class); |
| 443 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 444 | } |
| 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 Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 452 | DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity, |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 453 | parentActivity != null /* exit when done */); |
| 454 | deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich); |
| 455 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 456 | |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame^] | 457 | 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 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 467 | 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); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 478 | // Forces the viewType. Should only be used for initialization. |
| 479 | public void setViewType(int viewType) { |
| 480 | mViewType = viewType; |
| 481 | } |
| 482 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 483 | 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; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 491 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 492 | Cursor cursor = params[0]; |
| 493 | if (cursor == null) { |
| 494 | return null; |
| 495 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 496 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 497 | 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; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 520 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 521 | |
| 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); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 541 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 544 | private String eventInfoToString(EventInfo eventInfo) { |
| 545 | String tmp = "Unknown"; |
| 546 | |
| 547 | StringBuilder builder = new StringBuilder(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 548 | if ((eventInfo.eventType & EventType.GO_TO) != 0) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 549 | 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"; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 562 | } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) { |
| 563 | tmp = "Refresh events"; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 564 | } else if ((eventInfo.eventType & EventType.SEARCH) != 0) { |
| 565 | tmp = "Search"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 566 | } |
| 567 | builder.append(tmp); |
| 568 | builder.append(": id="); |
| 569 | builder.append(eventInfo.id); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 570 | builder.append(", selected="); |
| 571 | builder.append(eventInfo.selectedTime); |
| 572 | builder.append(", start="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 573 | builder.append(eventInfo.startTime); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 574 | builder.append(", end="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 575 | 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 Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 584 | } |