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