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 | |
RoboErik | a27a886 | 2011-06-23 15:26:23 -0700 | [diff] [blame] | 19 | import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME; |
| 20 | import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 21 | |
Erik | 5f62079 | 2010-10-27 12:42:01 -0700 | [diff] [blame] | 22 | import com.android.calendar.event.EditEventActivity; |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 23 | import com.android.calendar.selectcalendars.SelectVisibleCalendarsActivity; |
Erik | 5f62079 | 2010-10-27 12:42:01 -0700 | [diff] [blame] | 24 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 25 | import android.accounts.Account; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 27 | import android.app.SearchManager; |
| 28 | import android.app.SearchableInfo; |
| 29 | import android.content.ComponentName; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 30 | import android.content.ContentResolver; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 31 | import android.content.ContentUris; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 34 | import android.database.Cursor; |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 36 | import android.os.AsyncTask; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
RoboErik | a7c0390 | 2011-06-14 11:06:44 -0700 | [diff] [blame] | 38 | import android.provider.CalendarContract.Calendars; |
| 39 | import android.provider.CalendarContract.Events; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 41 | import android.text.format.Time; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 42 | import android.util.Log; |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 43 | import android.util.Pair; |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 44 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 45 | import java.util.Iterator; |
| 46 | import java.util.LinkedHashMap; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 47 | import java.util.LinkedList; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 48 | import java.util.Map.Entry; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 49 | import java.util.WeakHashMap; |
| 50 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 51 | public class CalendarController { |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 52 | private static final boolean DEBUG = false; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 53 | private static final String TAG = "CalendarController"; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 54 | private static final String REFRESH_SELECTION = Calendars.SYNC_EVENTS + "=?"; |
| 55 | private static final String[] REFRESH_ARGS = new String[] { "1" }; |
RoboErik | 0e1e624 | 2011-05-05 15:25:02 -0700 | [diff] [blame] | 56 | private static final String REFRESH_ORDER = Calendars.ACCOUNT_NAME + "," |
| 57 | + Calendars.ACCOUNT_TYPE; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 58 | |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 59 | public static final String EVENT_EDIT_ON_LAUNCH = "editMode"; |
| 60 | |
Erik | 981874e | 2010-10-05 16:52:52 -0700 | [diff] [blame] | 61 | public static final int MIN_CALENDAR_YEAR = 1970; |
| 62 | public static final int MAX_CALENDAR_YEAR = 2036; |
| 63 | public static final int MIN_CALENDAR_WEEK = 0; |
| 64 | public static final int MAX_CALENDAR_WEEK = 3497; // weeks between 1/1/1970 and 1/1/2037 |
| 65 | |
Isaac Katzenelson | 60f01c2 | 2011-06-03 15:42:01 -0700 | [diff] [blame] | 66 | public static final String EVENT_ATTENDEE_RESPONSE = "attendeeResponse"; |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 67 | public static final int ATTENDEE_NO_RESPONSE = -1; |
| 68 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 69 | private Context mContext; |
| 70 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 71 | // This uses a LinkedHashMap so that we can replace fragments based on the |
| 72 | // view id they are being expanded into since we can't guarantee a reference |
| 73 | // to the handler will be findable |
| 74 | private LinkedHashMap<Integer,EventHandler> eventHandlers = |
| 75 | new LinkedHashMap<Integer,EventHandler>(5); |
| 76 | private LinkedList<Integer> mToBeRemovedEventHandlers = new LinkedList<Integer>(); |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 77 | private LinkedHashMap<Integer, EventHandler> mToBeAddedEventHandlers = new LinkedHashMap< |
| 78 | Integer, EventHandler>(); |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 79 | private Pair<Integer, EventHandler> mFirstEventHandler; |
| 80 | private Pair<Integer, EventHandler> mToBeAddedFirstEventHandler; |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 81 | private volatile int mDispatchInProgressCounter = 0; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 82 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 83 | private static WeakHashMap<Context, CalendarController> instances = |
| 84 | new WeakHashMap<Context, CalendarController>(); |
| 85 | |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 86 | private WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1); |
| 87 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 88 | private int mViewType = -1; |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 89 | private int mDetailViewType = -1; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 90 | private int mPreviousViewType = -1; |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 91 | private long mEventId = -1; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 92 | private Time mTime = new Time(); |
| 93 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 94 | private AsyncQueryService mService; |
| 95 | |
RoboErik | 4679f60 | 2011-03-24 15:39:40 -0700 | [diff] [blame] | 96 | private Runnable mUpdateTimezone = new Runnable() { |
| 97 | @Override |
| 98 | public void run() { |
| 99 | mTime.switchTimezone(Utils.getTimeZone(mContext, this)); |
| 100 | } |
| 101 | }; |
| 102 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 103 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 104 | * 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] | 105 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 106 | public interface EventType { |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 107 | final long CREATE_EVENT = 1L; |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 108 | |
| 109 | // Simple view of an event |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 110 | final long VIEW_EVENT = 1L << 1; |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 111 | |
| 112 | // Full detail view in read only mode |
| 113 | final long VIEW_EVENT_DETAILS = 1L << 2; |
| 114 | |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 115 | // full detail view in edit mode |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 116 | final long EDIT_EVENT = 1L << 3; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 117 | |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 118 | final long DELETE_EVENT = 1L << 4; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 119 | |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 120 | final long GO_TO = 1L << 5; |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 121 | |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 122 | final long LAUNCH_SETTINGS = 1L << 6; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 123 | |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 124 | final long EVENTS_CHANGED = 1L << 7; |
| 125 | |
| 126 | final long SEARCH = 1L << 8; |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 127 | |
| 128 | // User has pressed the home key |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 129 | final long USER_HOME = 1L << 9; |
Erik | eaafa2b | 2010-12-23 14:30:37 -0800 | [diff] [blame] | 130 | |
| 131 | // date range has changed, update the title |
| 132 | final long UPDATE_TITLE = 1L << 10; |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 133 | |
| 134 | // select which calendars to display |
| 135 | final long LAUNCH_SELECT_VISIBLE_CALENDARS = 1L << 11; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 136 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 139 | * One of the Agenda/Day/Week/Month view types |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 140 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 141 | public interface ViewType { |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 142 | final int DETAIL = -1; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 143 | final int CURRENT = 0; |
| 144 | final int AGENDA = 1; |
| 145 | final int DAY = 2; |
| 146 | final int WEEK = 3; |
| 147 | final int MONTH = 4; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 148 | final int EDIT = 5; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 151 | public static class EventInfo { |
Mason Tang | 00b8c1a | 2010-08-23 12:02:00 -0700 | [diff] [blame] | 152 | public long eventType; // one of the EventType |
| 153 | public int viewType; // one of the ViewType |
| 154 | public long id; // event id |
| 155 | public Time selectedTime; // the selected time in focus |
| 156 | public Time startTime; // start of a range of time. |
| 157 | public Time endTime; // end of a range of time. |
| 158 | public int x; // x coordinate in the activity space |
| 159 | public int y; // y coordinate in the activity space |
| 160 | public String query; // query for a user search |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 161 | public ComponentName componentName; // used in combination with query |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * For EventType.VIEW_EVENT: |
| 165 | * It is the default attendee response. |
| 166 | * Set to {@link #ATTENDEE_NO_RESPONSE}, Calendar.ATTENDEE_STATUS_ACCEPTED, |
| 167 | * Calendar.ATTENDEE_STATUS_DECLINED, or Calendar.ATTENDEE_STATUS_TENTATIVE. |
| 168 | * <p> |
| 169 | * For EventType.GO_TO: |
| 170 | * Set to {@link #EXTRA_GOTO_TIME} to go to the specified date/time. |
| 171 | * Set to {@link #EXTRA_GOTO_DATE} to consider the date but ignore the time. |
| 172 | */ |
| 173 | public long extraLong; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Pass to the ExtraLong parameter for EventType.GO_TO to signal the time |
| 178 | * can be ignored |
| 179 | */ |
| 180 | public static final long EXTRA_GOTO_DATE = 1; |
| 181 | public static final long EXTRA_GOTO_TIME = -1; |
| 182 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 183 | public interface EventHandler { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 184 | long getSupportedEventTypes(); |
| 185 | void handleEvent(EventInfo event); |
| 186 | |
| 187 | /** |
Erik | 954c871 | 2010-08-06 10:12:34 -0700 | [diff] [blame] | 188 | * This notifies the handler that the database has changed and it should |
| 189 | * update its view. |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 190 | */ |
| 191 | void eventsChanged(); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Creates and/or returns an instance of CalendarController associated with |
| 196 | * the supplied context. It is best to pass in the current Activity. |
| 197 | * |
| 198 | * @param context The activity if at all possible. |
| 199 | */ |
| 200 | public static CalendarController getInstance(Context context) { |
| 201 | synchronized (instances) { |
| 202 | CalendarController controller = instances.get(context); |
| 203 | if (controller == null) { |
| 204 | controller = new CalendarController(context); |
| 205 | instances.put(context, controller); |
| 206 | } |
| 207 | return controller; |
| 208 | } |
| 209 | } |
| 210 | |
Erik | ed61b48 | 2010-08-13 13:59:50 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Removes an instance when it is no longer needed. This should be called in |
| 213 | * an activity's onDestroy method. |
| 214 | * |
| 215 | * @param context The activity used to create the controller |
| 216 | */ |
| 217 | public static void removeInstance(Context context) { |
| 218 | instances.remove(context); |
| 219 | } |
| 220 | |
Michael Chan | 0558def | 2010-07-22 21:30:32 -0700 | [diff] [blame] | 221 | private CalendarController(Context context) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 222 | mContext = context; |
RoboErik | 4679f60 | 2011-03-24 15:39:40 -0700 | [diff] [blame] | 223 | mUpdateTimezone.run(); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 224 | mTime.setToNow(); |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 225 | mDetailViewType = Utils.getSharedPreference(mContext, |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 226 | GeneralPreferences.KEY_DETAILED_VIEW, |
| 227 | GeneralPreferences.DEFAULT_DETAILED_VIEW); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 228 | mService = new AsyncQueryService(context) { |
| 229 | @Override |
| 230 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 231 | new RefreshInBackground().execute(cursor); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 232 | } |
| 233 | }; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 234 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 235 | |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 236 | public void sendEventRelatedEvent(Object sender, long eventType, long eventId, long startMillis, |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 237 | long endMillis, int x, int y, long selectedMillis) { |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 238 | sendEventRelatedEventWithResponse(sender, eventType, eventId, startMillis, endMillis, x, y, |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 239 | CalendarController.ATTENDEE_NO_RESPONSE, selectedMillis); |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 242 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 243 | * Helper for sending New/View/Edit/Delete events |
| 244 | * |
| 245 | * @param sender object of the caller |
| 246 | * @param eventType one of {@link EventType} |
| 247 | * @param eventId event id |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 248 | * @param startMillis start time |
| 249 | * @param endMillis end time |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 250 | * @param x x coordinate in the activity space |
| 251 | * @param y y coordinate in the activity space |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 252 | * @param extraLong default response value for the "simple event view". Use |
| 253 | * CalendarController.ATTENDEE_NO_RESPONSE for no response. |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 254 | * @param selectedMillis The time to specify as selected |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 255 | */ |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 256 | public void sendEventRelatedEventWithResponse(Object sender, long eventType, long eventId, |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 257 | long startMillis, long endMillis, int x, int y, long extraLong, long selectedMillis) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 258 | EventInfo info = new EventInfo(); |
| 259 | info.eventType = eventType; |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 260 | if (eventType == EventType.EDIT_EVENT || eventType == EventType.VIEW_EVENT_DETAILS) { |
Erik | 5f62079 | 2010-10-27 12:42:01 -0700 | [diff] [blame] | 261 | info.viewType = ViewType.CURRENT; |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 262 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 263 | info.id = eventId; |
RoboErik | 4679f60 | 2011-03-24 15:39:40 -0700 | [diff] [blame] | 264 | info.startTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone)); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 265 | info.startTime.set(startMillis); |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 266 | if (selectedMillis != -1) { |
RoboErik | 4679f60 | 2011-03-24 15:39:40 -0700 | [diff] [blame] | 267 | info.selectedTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone)); |
RoboErik | 87f993f | 2011-02-03 17:47:20 -0800 | [diff] [blame] | 268 | info.selectedTime.set(selectedMillis); |
| 269 | } else { |
| 270 | info.selectedTime = info.startTime; |
| 271 | } |
RoboErik | 4679f60 | 2011-03-24 15:39:40 -0700 | [diff] [blame] | 272 | info.endTime = new Time(Utils.getTimeZone(mContext, mUpdateTimezone)); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 273 | info.endTime.set(endMillis); |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 274 | info.x = x; |
| 275 | info.y = y; |
Michael Chan | deced89 | 2010-12-10 15:59:35 -0800 | [diff] [blame] | 276 | info.extraLong = extraLong; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 277 | this.sendEvent(sender, info); |
| 278 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 281 | * Helper for sending non-calendar-event events |
| 282 | * |
| 283 | * @param sender object of the caller |
| 284 | * @param eventType one of {@link EventType} |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 285 | * @param start start time |
| 286 | * @param end end time |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 287 | * @param eventId event id |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 288 | * @param viewType {@link ViewType} |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 289 | */ |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 290 | 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] | 291 | int viewType) { |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 292 | sendEvent(sender, eventType, start, end, start, eventId, viewType, EXTRA_GOTO_TIME, null, |
| 293 | null); |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 297 | * sendEvent() variant with extraLong, search query, and search component name. |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 298 | */ |
| 299 | public void sendEvent(Object sender, long eventType, Time start, Time end, long eventId, |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 300 | int viewType, long extraLong, String query, ComponentName componentName) { |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 301 | sendEvent(sender, eventType, start, end, start, eventId, viewType, extraLong, query, |
| 302 | componentName); |
| 303 | } |
| 304 | |
| 305 | public void sendEvent(Object sender, long eventType, Time start, Time end, Time selected, |
| 306 | long eventId, int viewType, long extraLong, String query, ComponentName componentName) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 307 | EventInfo info = new EventInfo(); |
| 308 | info.eventType = eventType; |
| 309 | info.startTime = start; |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 310 | info.selectedTime = selected; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 311 | info.endTime = end; |
| 312 | info.id = eventId; |
| 313 | info.viewType = viewType; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 314 | info.query = query; |
| 315 | info.componentName = componentName; |
Michael Chan | 46a8b11 | 2010-12-14 16:36:27 -0800 | [diff] [blame] | 316 | info.extraLong = extraLong; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 317 | this.sendEvent(sender, info); |
| 318 | } |
| 319 | |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 320 | public void sendEvent(Object sender, final EventInfo event) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 321 | // TODO Throw exception on invalid events |
| 322 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 323 | if (DEBUG) { |
| 324 | Log.d(TAG, eventInfoToString(event)); |
| 325 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 326 | |
| 327 | Long filteredTypes = filters.get(sender); |
| 328 | if (filteredTypes != null && (filteredTypes.longValue() & event.eventType) != 0) { |
| 329 | // Suppress event per filter |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 330 | if (DEBUG) { |
| 331 | Log.d(TAG, "Event suppressed"); |
| 332 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 336 | mPreviousViewType = mViewType; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 337 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 338 | // Fix up view if not specified |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 339 | if (event.viewType == ViewType.DETAIL) { |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 340 | event.viewType = mDetailViewType; |
| 341 | mViewType = mDetailViewType; |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 342 | } else if (event.viewType == ViewType.CURRENT) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 343 | event.viewType = mViewType; |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 344 | } else if (event.viewType != ViewType.EDIT) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 345 | mViewType = event.viewType; |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 346 | |
Erik | 63cd053 | 2011-01-26 14:16:03 -0800 | [diff] [blame] | 347 | if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY |
| 348 | || (Utils.getAllowWeekForDetailView() && event.viewType == ViewType.WEEK)) { |
Michael Chan | 85e5509 | 2010-07-27 15:18:40 -0700 | [diff] [blame] | 349 | mDetailViewType = mViewType; |
| 350 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 353 | if (DEBUG) { |
| 354 | Log.e(TAG, "vvvvvvvvvvvvvvv"); |
| 355 | Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString())); |
| 356 | Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString())); |
| 357 | Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString())); |
| 358 | Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString())); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 359 | } |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame] | 360 | |
| 361 | long startMillis = 0; |
| 362 | if (event.startTime != null) { |
| 363 | startMillis = event.startTime.toMillis(false); |
| 364 | } |
| 365 | |
| 366 | // Set mTime if selectedTime is set |
| 367 | if (event.selectedTime != null && event.selectedTime.toMillis(false) != 0) { |
| 368 | mTime.set(event.selectedTime); |
| 369 | } else { |
| 370 | if (startMillis != 0) { |
| 371 | // selectedTime is not set so set mTime to startTime iff it is not |
| 372 | // within start and end times |
| 373 | long mtimeMillis = mTime.toMillis(false); |
| 374 | if (mtimeMillis < startMillis |
| 375 | || (event.endTime != null && mtimeMillis > event.endTime.toMillis(false))) { |
| 376 | mTime.set(event.startTime); |
| 377 | } |
| 378 | } |
| 379 | event.selectedTime = mTime; |
| 380 | } |
| 381 | |
| 382 | // Fix up start time if not specified |
| 383 | if (startMillis == 0) { |
| 384 | event.startTime = mTime; |
| 385 | } |
| 386 | if (DEBUG) { |
| 387 | Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString())); |
| 388 | Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString())); |
| 389 | Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString())); |
| 390 | Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString())); |
| 391 | Log.e(TAG, "^^^^^^^^^^^^^^^"); |
| 392 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 393 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 394 | // Store the eventId if we're entering edit event |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 395 | if ((event.eventType |
| 396 | & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS)) |
| 397 | != 0) { |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 398 | if (event.id > 0) { |
| 399 | mEventId = event.id; |
| 400 | } else { |
| 401 | mEventId = -1; |
| 402 | } |
| 403 | } |
| 404 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 405 | boolean handled = false; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 406 | synchronized (this) { |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 407 | mDispatchInProgressCounter ++; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 408 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 409 | if (DEBUG) { |
| 410 | Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers"); |
| 411 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 412 | // Dispatch to event handler(s) |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 413 | if (mFirstEventHandler != null) { |
| 414 | // Handle the 'first' one before handling the others |
| 415 | EventHandler handler = mFirstEventHandler.second; |
| 416 | if (handler != null && (handler.getSupportedEventTypes() & event.eventType) != 0 |
| 417 | && !mToBeRemovedEventHandlers.contains(mFirstEventHandler.first)) { |
| 418 | handler.handleEvent(event); |
| 419 | handled = true; |
| 420 | } |
| 421 | } |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 422 | for (Iterator<Entry<Integer, EventHandler>> handlers = |
| 423 | eventHandlers.entrySet().iterator(); handlers.hasNext();) { |
| 424 | Entry<Integer, EventHandler> entry = handlers.next(); |
| 425 | int key = entry.getKey(); |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 426 | if (mFirstEventHandler != null && key == mFirstEventHandler.first) { |
| 427 | // If this was the 'first' handler it was already handled |
| 428 | continue; |
| 429 | } |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 430 | EventHandler eventHandler = entry.getValue(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 431 | if (eventHandler != null |
| 432 | && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 433 | if (mToBeRemovedEventHandlers.contains(key)) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 434 | continue; |
| 435 | } |
| 436 | eventHandler.handleEvent(event); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 437 | handled = true; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 440 | |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 441 | mDispatchInProgressCounter --; |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 442 | |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 443 | if (mDispatchInProgressCounter == 0) { |
| 444 | |
| 445 | // Deregister removed handlers |
| 446 | if (mToBeRemovedEventHandlers.size() > 0) { |
| 447 | for (Integer zombie : mToBeRemovedEventHandlers) { |
| 448 | eventHandlers.remove(zombie); |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 449 | if (mFirstEventHandler != null && zombie.equals(mFirstEventHandler.first)) { |
| 450 | mFirstEventHandler = null; |
| 451 | } |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 452 | } |
| 453 | mToBeRemovedEventHandlers.clear(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 454 | } |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 455 | // Add new handlers |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 456 | if (mToBeAddedFirstEventHandler != null) { |
| 457 | mFirstEventHandler = mToBeAddedFirstEventHandler; |
| 458 | mToBeAddedFirstEventHandler = null; |
| 459 | } |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 460 | if (mToBeAddedEventHandlers.size() > 0) { |
| 461 | for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) { |
| 462 | eventHandlers.put(food.getKey(), food.getValue()); |
| 463 | } |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 466 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 467 | |
| 468 | if (!handled) { |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 469 | // Launch Settings |
| 470 | if (event.eventType == EventType.LAUNCH_SETTINGS) { |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 471 | launchSettings(); |
| 472 | return; |
| 473 | } |
| 474 | |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 475 | // Launch Calendar Visible Selector |
| 476 | if (event.eventType == EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) { |
| 477 | launchSelectVisibleCalendars(); |
| 478 | return; |
| 479 | } |
| 480 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 481 | // Create/View/Edit/Delete Event |
| 482 | long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false); |
| 483 | if (event.eventType == EventType.CREATE_EVENT) { |
| 484 | launchCreateEvent(event.startTime.toMillis(false), endTime); |
| 485 | return; |
| 486 | } else if (event.eventType == EventType.VIEW_EVENT) { |
| 487 | launchViewEvent(event.id, event.startTime.toMillis(false), endTime); |
| 488 | return; |
| 489 | } else if (event.eventType == EventType.EDIT_EVENT) { |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 490 | launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true); |
| 491 | return; |
| 492 | } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) { |
| 493 | launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 494 | return; |
| 495 | } else if (event.eventType == EventType.DELETE_EVENT) { |
| 496 | launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime); |
| 497 | return; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 498 | } else if (event.eventType == EventType.SEARCH) { |
| 499 | launchSearch(event.id, event.query, event.componentName); |
| 500 | return; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 505 | /** |
| 506 | * Adds or updates an event handler. This uses a LinkedHashMap so that we can |
| 507 | * replace fragments based on the view id they are being expanded into. |
| 508 | * |
| 509 | * @param key The view id or placeholder for this handler |
| 510 | * @param eventHandler Typically a fragment or activity in the calendar app |
| 511 | */ |
| 512 | public void registerEventHandler(int key, EventHandler eventHandler) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 513 | synchronized (this) { |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 514 | if (mDispatchInProgressCounter > 0) { |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 515 | mToBeAddedEventHandlers.put(key, eventHandler); |
| 516 | } else { |
| 517 | eventHandlers.put(key, eventHandler); |
| 518 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 519 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 520 | } |
| 521 | |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 522 | public void registerFirstEventHandler(int key, EventHandler eventHandler) { |
| 523 | synchronized (this) { |
| 524 | registerEventHandler(key, eventHandler); |
| 525 | if (mDispatchInProgressCounter > 0) { |
| 526 | mToBeAddedFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler); |
| 527 | } else { |
| 528 | mFirstEventHandler = new Pair<Integer, EventHandler>(key, eventHandler); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 533 | public void deregisterEventHandler(Integer key) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 534 | synchronized (this) { |
Isaac Katzenelson | ccf565b | 2011-04-27 14:40:08 -0700 | [diff] [blame] | 535 | if (mDispatchInProgressCounter > 0) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 536 | // To avoid ConcurrencyException, stash away the event handler for now. |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 537 | mToBeRemovedEventHandlers.add(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 538 | } else { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 539 | eventHandlers.remove(key); |
RoboErik | 3864be0 | 2011-07-25 15:56:50 -0700 | [diff] [blame^] | 540 | if (mFirstEventHandler != null && mFirstEventHandler.first == key) { |
| 541 | mFirstEventHandler = null; |
| 542 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 547 | // FRAG_TODO doesn't work yet |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 548 | public void filterBroadcasts(Object sender, long eventTypes) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 549 | filters.put(sender, eventTypes); |
| 550 | } |
| 551 | |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 552 | /** |
| 553 | * @return the time that this controller is currently pointed at |
| 554 | */ |
| 555 | public long getTime() { |
| 556 | return mTime.toMillis(false); |
| 557 | } |
| 558 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 559 | /** |
Erik | 144edfa | 2010-11-22 14:23:49 -0800 | [diff] [blame] | 560 | * Set the time this controller is currently pointed at |
| 561 | * |
| 562 | * @param millisTime Time since epoch in millis |
| 563 | */ |
| 564 | public void setTime(long millisTime) { |
| 565 | mTime.set(millisTime); |
| 566 | } |
| 567 | |
| 568 | /** |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 569 | * @return the last event ID the edit view was launched with |
| 570 | */ |
| 571 | public long getEventId() { |
| 572 | return mEventId; |
| 573 | } |
| 574 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 575 | public int getViewType() { |
| 576 | return mViewType; |
| 577 | } |
| 578 | |
| 579 | public int getPreviousViewType() { |
| 580 | return mPreviousViewType; |
| 581 | } |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 582 | |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 583 | private void launchSelectVisibleCalendars() { |
| 584 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | 07d9fee | 2011-07-25 09:41:21 -0700 | [diff] [blame] | 585 | intent.setClass(mContext, SelectVisibleCalendarsActivity.class); |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 586 | intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 587 | mContext.startActivity(intent); |
| 588 | } |
| 589 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 590 | private void launchSettings() { |
| 591 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | 07d9fee | 2011-07-25 09:41:21 -0700 | [diff] [blame] | 592 | intent.setClass(mContext, CalendarSettingsActivity.class); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 593 | 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] | 594 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | private void launchCreateEvent(long startMillis, long endMillis) { |
| 598 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | 07d9fee | 2011-07-25 09:41:21 -0700 | [diff] [blame] | 599 | intent.setClass(mContext, EditEventActivity.class); |
RoboErik | a27a886 | 2011-06-23 15:26:23 -0700 | [diff] [blame] | 600 | intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis); |
| 601 | intent.putExtra(EXTRA_EVENT_END_TIME, endMillis); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 602 | mEventId = -1; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 603 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | private void launchViewEvent(long eventId, long startMillis, long endMillis) { |
| 607 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 608 | Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 609 | intent.setData(eventUri); |
Michael Chan | 07d9fee | 2011-07-25 09:41:21 -0700 | [diff] [blame] | 610 | intent.setClass(mContext, AllInOneActivity.class); |
RoboErik | a27a886 | 2011-06-23 15:26:23 -0700 | [diff] [blame] | 611 | intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis); |
| 612 | intent.putExtra(EXTRA_EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 613 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 616 | private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) { |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 617 | Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 618 | Intent intent = new Intent(Intent.ACTION_EDIT, uri); |
RoboErik | a27a886 | 2011-06-23 15:26:23 -0700 | [diff] [blame] | 619 | intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis); |
| 620 | intent.putExtra(EXTRA_EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 621 | intent.setClass(mContext, EditEventActivity.class); |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 622 | intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 623 | mEventId = eventId; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 624 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Michael Chan | 1881a89 | 2011-02-08 18:16:58 -0800 | [diff] [blame] | 627 | // private void launchAlerts() { |
| 628 | // Intent intent = new Intent(); |
| 629 | // intent.setClass(mContext, AlertActivity.class); |
| 630 | // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 631 | // mContext.startActivity(intent); |
| 632 | // } |
| 633 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 634 | private void launchDeleteEvent(long eventId, long startMillis, long endMillis) { |
| 635 | launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1); |
| 636 | } |
| 637 | |
| 638 | private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis, |
| 639 | long endMillis, int deleteWhich) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 640 | DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity, |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 641 | parentActivity != null /* exit when done */); |
| 642 | deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich); |
| 643 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 644 | |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 645 | private void launchSearch(long eventId, String query, ComponentName componentName) { |
| 646 | final SearchManager searchManager = |
| 647 | (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE); |
| 648 | final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName); |
| 649 | final Intent intent = new Intent(Intent.ACTION_SEARCH); |
| 650 | intent.putExtra(SearchManager.QUERY, query); |
| 651 | intent.setComponent(searchableInfo.getSearchActivity()); |
| 652 | mContext.startActivity(intent); |
| 653 | } |
| 654 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 655 | public void refreshCalendars() { |
| 656 | Log.d(TAG, "RefreshCalendars starting"); |
| 657 | // get the account, url, and current sync state |
| 658 | mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI, |
| 659 | new String[] {Calendars._ID, // 0 |
RoboErik | 0e1e624 | 2011-05-05 15:25:02 -0700 | [diff] [blame] | 660 | Calendars.ACCOUNT_NAME, // 1 |
| 661 | Calendars.ACCOUNT_TYPE, // 2 |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 662 | }, |
| 663 | REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 666 | // Forces the viewType. Should only be used for initialization. |
| 667 | public void setViewType(int viewType) { |
| 668 | mViewType = viewType; |
| 669 | } |
| 670 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 671 | // Sets the eventId. Should only be used for initialization. |
| 672 | public void setEventId(long eventId) { |
| 673 | mEventId = eventId; |
| 674 | } |
| 675 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 676 | private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> { |
| 677 | /* (non-Javadoc) |
| 678 | * @see android.os.AsyncTask#doInBackground(Params[]) |
| 679 | */ |
| 680 | @Override |
| 681 | protected Integer doInBackground(Cursor... params) { |
| 682 | if (params.length != 1) { |
| 683 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 684 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 685 | Cursor cursor = params[0]; |
| 686 | if (cursor == null) { |
| 687 | return null; |
| 688 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 689 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 690 | String previousAccount = null; |
| 691 | String previousType = null; |
| 692 | Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars"); |
| 693 | try { |
| 694 | while (cursor.moveToNext()) { |
| 695 | Account account = null; |
| 696 | String accountName = cursor.getString(1); |
| 697 | String accountType = cursor.getString(2); |
| 698 | // Only need to schedule one sync per account and they're |
| 699 | // ordered by account,type |
| 700 | if (TextUtils.equals(accountName, previousAccount) && |
| 701 | TextUtils.equals(accountType, previousType)) { |
| 702 | continue; |
| 703 | } |
| 704 | previousAccount = accountName; |
| 705 | previousType = accountType; |
| 706 | account = new Account(accountName, accountType); |
| 707 | scheduleSync(account, false /* two-way sync */, null); |
| 708 | } |
| 709 | } finally { |
| 710 | cursor.close(); |
| 711 | } |
| 712 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 713 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * Schedule a calendar sync for the account. |
| 717 | * @param account the account for which to schedule a sync |
| 718 | * @param uploadChangesOnly if set, specify that the sync should only send |
| 719 | * up local changes. This is typically used for a local sync, a user override of |
| 720 | * too many deletions, or a sync after a calendar is unselected. |
| 721 | * @param url the url feed for the calendar to sync (may be null, in which case a poll of |
| 722 | * all feeds is done.) |
| 723 | */ |
| 724 | void scheduleSync(Account account, boolean uploadChangesOnly, String url) { |
| 725 | Bundle extras = new Bundle(); |
| 726 | if (uploadChangesOnly) { |
| 727 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly); |
| 728 | } |
| 729 | if (url != null) { |
| 730 | extras.putString("feed", url); |
| 731 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 732 | } |
| 733 | ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 734 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 737 | private String eventInfoToString(EventInfo eventInfo) { |
| 738 | String tmp = "Unknown"; |
| 739 | |
| 740 | StringBuilder builder = new StringBuilder(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 741 | if ((eventInfo.eventType & EventType.GO_TO) != 0) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 742 | tmp = "Go to time/event"; |
| 743 | } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) { |
| 744 | tmp = "New event"; |
| 745 | } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) { |
| 746 | tmp = "View event"; |
Michael Chan | 6d4ce6e | 2011-01-11 11:15:16 -0800 | [diff] [blame] | 747 | } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) { |
| 748 | tmp = "View details"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 749 | } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) { |
| 750 | tmp = "Edit event"; |
| 751 | } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) { |
| 752 | tmp = "Delete event"; |
Michael Chan | 2aeb8d9 | 2011-07-10 13:32:09 -0700 | [diff] [blame] | 753 | } else if ((eventInfo.eventType & EventType.LAUNCH_SELECT_VISIBLE_CALENDARS) != 0) { |
| 754 | tmp = "Launch select visible calendars"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 755 | } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) { |
| 756 | tmp = "Launch settings"; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 757 | } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) { |
| 758 | tmp = "Refresh events"; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 759 | } else if ((eventInfo.eventType & EventType.SEARCH) != 0) { |
| 760 | tmp = "Search"; |
Michael Chan | 6d4ce6e | 2011-01-11 11:15:16 -0800 | [diff] [blame] | 761 | } else if ((eventInfo.eventType & EventType.USER_HOME) != 0) { |
| 762 | tmp = "Gone home"; |
| 763 | } else if ((eventInfo.eventType & EventType.UPDATE_TITLE) != 0) { |
| 764 | tmp = "Update title"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 765 | } |
| 766 | builder.append(tmp); |
| 767 | builder.append(": id="); |
| 768 | builder.append(eventInfo.id); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 769 | builder.append(", selected="); |
| 770 | builder.append(eventInfo.selectedTime); |
| 771 | builder.append(", start="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 772 | builder.append(eventInfo.startTime); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 773 | builder.append(", end="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 774 | builder.append(eventInfo.endTime); |
| 775 | builder.append(", viewType="); |
| 776 | builder.append(eventInfo.viewType); |
| 777 | builder.append(", x="); |
| 778 | builder.append(eventInfo.x); |
| 779 | builder.append(", y="); |
| 780 | builder.append(eventInfo.y); |
| 781 | return builder.toString(); |
| 782 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 783 | } |