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