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 | |
| 325 | if (event.viewType == ViewType.AGENDA || event.viewType == ViewType.DAY) { |
| 326 | mDetailViewType = mViewType; |
| 327 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame^] | 330 | if (DEBUG) { |
| 331 | Log.e(TAG, "vvvvvvvvvvvvvvv"); |
| 332 | Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString())); |
| 333 | Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString())); |
| 334 | Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString())); |
| 335 | Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString())); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 336 | } |
Michael Chan | f0868f6 | 2011-01-11 19:37:35 -0800 | [diff] [blame^] | 337 | |
| 338 | long startMillis = 0; |
| 339 | if (event.startTime != null) { |
| 340 | startMillis = event.startTime.toMillis(false); |
| 341 | } |
| 342 | |
| 343 | // Set mTime if selectedTime is set |
| 344 | if (event.selectedTime != null && event.selectedTime.toMillis(false) != 0) { |
| 345 | mTime.set(event.selectedTime); |
| 346 | } else { |
| 347 | if (startMillis != 0) { |
| 348 | // selectedTime is not set so set mTime to startTime iff it is not |
| 349 | // within start and end times |
| 350 | long mtimeMillis = mTime.toMillis(false); |
| 351 | if (mtimeMillis < startMillis |
| 352 | || (event.endTime != null && mtimeMillis > event.endTime.toMillis(false))) { |
| 353 | mTime.set(event.startTime); |
| 354 | } |
| 355 | } |
| 356 | event.selectedTime = mTime; |
| 357 | } |
| 358 | |
| 359 | // Fix up start time if not specified |
| 360 | if (startMillis == 0) { |
| 361 | event.startTime = mTime; |
| 362 | } |
| 363 | if (DEBUG) { |
| 364 | Log.e(TAG, "Start " + (event.startTime == null ? "null" : event.startTime.toString())); |
| 365 | Log.e(TAG, "End " + (event.endTime == null ? "null" : event.endTime.toString())); |
| 366 | Log.e(TAG, "Select " + (event.selectedTime == null ? "null" : event.selectedTime.toString())); |
| 367 | Log.e(TAG, "mTime " + (mTime == null ? "null" : mTime.toString())); |
| 368 | Log.e(TAG, "^^^^^^^^^^^^^^^"); |
| 369 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 370 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 371 | // Store the eventId if we're entering edit event |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 372 | if ((event.eventType |
| 373 | & (EventType.CREATE_EVENT | EventType.EDIT_EVENT | EventType.VIEW_EVENT_DETAILS)) |
| 374 | != 0) { |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 375 | if (event.id > 0) { |
| 376 | mEventId = event.id; |
| 377 | } else { |
| 378 | mEventId = -1; |
| 379 | } |
| 380 | } |
| 381 | |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 382 | boolean handled = false; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 383 | synchronized (this) { |
| 384 | mDispatchInProgress = true; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 385 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 386 | if (DEBUG) { |
| 387 | Log.d(TAG, "sendEvent: Dispatching to " + eventHandlers.size() + " handlers"); |
| 388 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 389 | // Dispatch to event handler(s) |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 390 | for (Iterator<Entry<Integer, EventHandler>> handlers = |
| 391 | eventHandlers.entrySet().iterator(); handlers.hasNext();) { |
| 392 | Entry<Integer, EventHandler> entry = handlers.next(); |
| 393 | int key = entry.getKey(); |
| 394 | EventHandler eventHandler = entry.getValue(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 395 | if (eventHandler != null |
| 396 | && (eventHandler.getSupportedEventTypes() & event.eventType) != 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 397 | if (mToBeRemovedEventHandlers.contains(key)) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 398 | continue; |
| 399 | } |
| 400 | eventHandler.handleEvent(event); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 401 | handled = true; |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 404 | |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 405 | mDispatchInProgress = false; |
| 406 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 407 | // Deregister removed handlers |
| 408 | if (mToBeRemovedEventHandlers.size() > 0) { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 409 | for (Integer zombie : mToBeRemovedEventHandlers) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 410 | eventHandlers.remove(zombie); |
| 411 | } |
| 412 | mToBeRemovedEventHandlers.clear(); |
| 413 | } |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 414 | // Add new handlers |
| 415 | if (mToBeAddedEventHandlers.size() > 0) { |
| 416 | for (Entry<Integer, EventHandler> food : mToBeAddedEventHandlers.entrySet()) { |
| 417 | eventHandlers.put(food.getKey(), food.getValue()); |
| 418 | } |
| 419 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 420 | } |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 421 | |
| 422 | if (!handled) { |
Erik | 6b858fc | 2010-09-15 18:59:04 -0700 | [diff] [blame] | 423 | // Launch Settings |
| 424 | if (event.eventType == EventType.LAUNCH_SETTINGS) { |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 425 | launchSettings(); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | // Create/View/Edit/Delete Event |
| 430 | long endTime = (event.endTime == null) ? -1 : event.endTime.toMillis(false); |
| 431 | if (event.eventType == EventType.CREATE_EVENT) { |
| 432 | launchCreateEvent(event.startTime.toMillis(false), endTime); |
| 433 | return; |
| 434 | } else if (event.eventType == EventType.VIEW_EVENT) { |
| 435 | launchViewEvent(event.id, event.startTime.toMillis(false), endTime); |
| 436 | return; |
| 437 | } else if (event.eventType == EventType.EDIT_EVENT) { |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 438 | launchEditEvent(event.id, event.startTime.toMillis(false), endTime, true); |
| 439 | return; |
| 440 | } else if (event.eventType == EventType.VIEW_EVENT_DETAILS) { |
| 441 | launchEditEvent(event.id, event.startTime.toMillis(false), endTime, false); |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 442 | return; |
| 443 | } else if (event.eventType == EventType.DELETE_EVENT) { |
| 444 | launchDeleteEvent(event.id, event.startTime.toMillis(false), endTime); |
| 445 | return; |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 446 | } else if (event.eventType == EventType.SEARCH) { |
| 447 | launchSearch(event.id, event.query, event.componentName); |
| 448 | return; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 453 | /** |
| 454 | * Adds or updates an event handler. This uses a LinkedHashMap so that we can |
| 455 | * replace fragments based on the view id they are being expanded into. |
| 456 | * |
| 457 | * @param key The view id or placeholder for this handler |
| 458 | * @param eventHandler Typically a fragment or activity in the calendar app |
| 459 | */ |
| 460 | public void registerEventHandler(int key, EventHandler eventHandler) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 461 | synchronized (this) { |
Erik | cb81189 | 2010-09-28 13:44:19 -0700 | [diff] [blame] | 462 | if (mDispatchInProgress) { |
| 463 | mToBeAddedEventHandlers.put(key, eventHandler); |
| 464 | } else { |
| 465 | eventHandlers.put(key, eventHandler); |
| 466 | } |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 467 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 470 | public void deregisterEventHandler(Integer key) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 471 | synchronized (this) { |
| 472 | if (mDispatchInProgress) { |
| 473 | // To avoid ConcurrencyException, stash away the event handler for now. |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 474 | mToBeRemovedEventHandlers.add(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 475 | } else { |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 476 | eventHandlers.remove(key); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 481 | // FRAG_TODO doesn't work yet |
Erik | 2525119 | 2010-07-12 15:30:14 -0700 | [diff] [blame] | 482 | public void filterBroadcasts(Object sender, long eventTypes) { |
Michael Chan | 83b0fe3 | 2010-07-08 16:46:26 -0700 | [diff] [blame] | 483 | filters.put(sender, eventTypes); |
| 484 | } |
| 485 | |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 486 | /** |
| 487 | * @return the time that this controller is currently pointed at |
| 488 | */ |
| 489 | public long getTime() { |
| 490 | return mTime.toMillis(false); |
| 491 | } |
| 492 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 493 | /** |
Erik | 144edfa | 2010-11-22 14:23:49 -0800 | [diff] [blame] | 494 | * Set the time this controller is currently pointed at |
| 495 | * |
| 496 | * @param millisTime Time since epoch in millis |
| 497 | */ |
| 498 | public void setTime(long millisTime) { |
| 499 | mTime.set(millisTime); |
| 500 | } |
| 501 | |
| 502 | /** |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 503 | * @return the last event ID the edit view was launched with |
| 504 | */ |
| 505 | public long getEventId() { |
| 506 | return mEventId; |
| 507 | } |
| 508 | |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 509 | public int getViewType() { |
| 510 | return mViewType; |
| 511 | } |
| 512 | |
| 513 | public int getPreviousViewType() { |
| 514 | return mPreviousViewType; |
| 515 | } |
Mason Tang | 8e3d430 | 2010-07-12 17:39:30 -0700 | [diff] [blame] | 516 | |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 517 | private void launchSettings() { |
| 518 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Daisuke Miyakawa | 4b441bd | 2010-09-16 14:55:36 -0700 | [diff] [blame] | 519 | intent.setClassName(mContext, CalendarSettingsActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 520 | 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] | 521 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | private void launchCreateEvent(long startMillis, long endMillis) { |
| 525 | Intent intent = new Intent(Intent.ACTION_VIEW); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 526 | intent.setClassName(mContext, EditEventActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 527 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 528 | intent.putExtra(EVENT_END_TIME, endMillis); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 529 | mEventId = -1; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 530 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | private void launchViewEvent(long eventId, long startMillis, long endMillis) { |
| 534 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 535 | Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 536 | intent.setData(eventUri); |
Michael Chan | 2c7c851 | 2010-12-10 14:12:57 -0800 | [diff] [blame] | 537 | // intent.setClassName(mContext, EventInfoActivity.class.getName()); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 538 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 539 | intent.putExtra(EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 540 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 543 | private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) { |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 544 | Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); |
| 545 | Intent intent = new Intent(Intent.ACTION_EDIT, uri); |
| 546 | intent.putExtra(EVENT_BEGIN_TIME, startMillis); |
| 547 | intent.putExtra(EVENT_END_TIME, endMillis); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 548 | intent.setClass(mContext, EditEventActivity.class); |
Erik | a7694ee | 2010-12-07 15:46:18 -0800 | [diff] [blame] | 549 | intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit); |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 550 | mEventId = eventId; |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 551 | mContext.startActivity(intent); |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | private void launchDeleteEvent(long eventId, long startMillis, long endMillis) { |
| 555 | launchDeleteEventAndFinish(null, eventId, startMillis, endMillis, -1); |
| 556 | } |
| 557 | |
| 558 | private void launchDeleteEventAndFinish(Activity parentActivity, long eventId, long startMillis, |
| 559 | long endMillis, int deleteWhich) { |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 560 | DeleteEventHelper deleteEventHelper = new DeleteEventHelper(mContext, parentActivity, |
Michael Chan | 9e89dca | 2010-07-13 17:56:09 -0700 | [diff] [blame] | 561 | parentActivity != null /* exit when done */); |
| 562 | deleteEventHelper.delete(startMillis, endMillis, eventId, deleteWhich); |
| 563 | } |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 564 | |
Daisuke Miyakawa | 6d2e6f7 | 2010-09-17 10:30:54 -0700 | [diff] [blame] | 565 | private void launchSearch(long eventId, String query, ComponentName componentName) { |
| 566 | final SearchManager searchManager = |
| 567 | (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE); |
| 568 | final SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName); |
| 569 | final Intent intent = new Intent(Intent.ACTION_SEARCH); |
| 570 | intent.putExtra(SearchManager.QUERY, query); |
| 571 | intent.setComponent(searchableInfo.getSearchActivity()); |
| 572 | mContext.startActivity(intent); |
| 573 | } |
| 574 | |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 575 | public void refreshCalendars() { |
| 576 | Log.d(TAG, "RefreshCalendars starting"); |
| 577 | // get the account, url, and current sync state |
| 578 | mService.startQuery(mService.getNextToken(), null, Calendars.CONTENT_URI, |
| 579 | new String[] {Calendars._ID, // 0 |
| 580 | Calendars._SYNC_ACCOUNT, // 1 |
| 581 | Calendars._SYNC_ACCOUNT_TYPE, // 2 |
| 582 | }, |
| 583 | REFRESH_SELECTION, REFRESH_ARGS, REFRESH_ORDER); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Erik | dd95df5 | 2010-08-27 09:31:18 -0700 | [diff] [blame] | 586 | // Forces the viewType. Should only be used for initialization. |
| 587 | public void setViewType(int viewType) { |
| 588 | mViewType = viewType; |
| 589 | } |
| 590 | |
Erik | 7b92da2 | 2010-09-23 14:55:22 -0700 | [diff] [blame] | 591 | // Sets the eventId. Should only be used for initialization. |
| 592 | public void setEventId(long eventId) { |
| 593 | mEventId = eventId; |
| 594 | } |
| 595 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 596 | private class RefreshInBackground extends AsyncTask<Cursor, Integer, Integer> { |
| 597 | /* (non-Javadoc) |
| 598 | * @see android.os.AsyncTask#doInBackground(Params[]) |
| 599 | */ |
| 600 | @Override |
| 601 | protected Integer doInBackground(Cursor... params) { |
| 602 | if (params.length != 1) { |
| 603 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 604 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 605 | Cursor cursor = params[0]; |
| 606 | if (cursor == null) { |
| 607 | return null; |
| 608 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 609 | |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 610 | String previousAccount = null; |
| 611 | String previousType = null; |
| 612 | Log.d(TAG, "Refreshing " + cursor.getCount() + " calendars"); |
| 613 | try { |
| 614 | while (cursor.moveToNext()) { |
| 615 | Account account = null; |
| 616 | String accountName = cursor.getString(1); |
| 617 | String accountType = cursor.getString(2); |
| 618 | // Only need to schedule one sync per account and they're |
| 619 | // ordered by account,type |
| 620 | if (TextUtils.equals(accountName, previousAccount) && |
| 621 | TextUtils.equals(accountType, previousType)) { |
| 622 | continue; |
| 623 | } |
| 624 | previousAccount = accountName; |
| 625 | previousType = accountType; |
| 626 | account = new Account(accountName, accountType); |
| 627 | scheduleSync(account, false /* two-way sync */, null); |
| 628 | } |
| 629 | } finally { |
| 630 | cursor.close(); |
| 631 | } |
| 632 | return null; |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 633 | } |
Erik | 7116ba4 | 2010-07-23 10:13:36 -0700 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * Schedule a calendar sync for the account. |
| 637 | * @param account the account for which to schedule a sync |
| 638 | * @param uploadChangesOnly if set, specify that the sync should only send |
| 639 | * up local changes. This is typically used for a local sync, a user override of |
| 640 | * too many deletions, or a sync after a calendar is unselected. |
| 641 | * @param url the url feed for the calendar to sync (may be null, in which case a poll of |
| 642 | * all feeds is done.) |
| 643 | */ |
| 644 | void scheduleSync(Account account, boolean uploadChangesOnly, String url) { |
| 645 | Bundle extras = new Bundle(); |
| 646 | if (uploadChangesOnly) { |
| 647 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, uploadChangesOnly); |
| 648 | } |
| 649 | if (url != null) { |
| 650 | extras.putString("feed", url); |
| 651 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 652 | } |
| 653 | ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras); |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 654 | } |
Erik | ba1b94a | 2010-07-20 17:50:50 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 657 | private String eventInfoToString(EventInfo eventInfo) { |
| 658 | String tmp = "Unknown"; |
| 659 | |
| 660 | StringBuilder builder = new StringBuilder(); |
Michael Chan | ab29d9e | 2010-07-21 06:08:47 -0700 | [diff] [blame] | 661 | if ((eventInfo.eventType & EventType.GO_TO) != 0) { |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 662 | tmp = "Go to time/event"; |
| 663 | } else if ((eventInfo.eventType & EventType.CREATE_EVENT) != 0) { |
| 664 | tmp = "New event"; |
| 665 | } else if ((eventInfo.eventType & EventType.VIEW_EVENT) != 0) { |
| 666 | tmp = "View event"; |
Michael Chan | 6d4ce6e | 2011-01-11 11:15:16 -0800 | [diff] [blame] | 667 | } else if ((eventInfo.eventType & EventType.VIEW_EVENT_DETAILS) != 0) { |
| 668 | tmp = "View details"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 669 | } else if ((eventInfo.eventType & EventType.EDIT_EVENT) != 0) { |
| 670 | tmp = "Edit event"; |
| 671 | } else if ((eventInfo.eventType & EventType.DELETE_EVENT) != 0) { |
| 672 | tmp = "Delete event"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 673 | } else if ((eventInfo.eventType & EventType.LAUNCH_SETTINGS) != 0) { |
| 674 | tmp = "Launch settings"; |
Erik | 3f348f3 | 2010-08-10 13:17:19 -0700 | [diff] [blame] | 675 | } else if ((eventInfo.eventType & EventType.EVENTS_CHANGED) != 0) { |
| 676 | tmp = "Refresh events"; |
Mason Tang | 4003d1c | 2010-08-17 13:50:45 -0700 | [diff] [blame] | 677 | } else if ((eventInfo.eventType & EventType.SEARCH) != 0) { |
| 678 | tmp = "Search"; |
Michael Chan | 6d4ce6e | 2011-01-11 11:15:16 -0800 | [diff] [blame] | 679 | } else if ((eventInfo.eventType & EventType.USER_HOME) != 0) { |
| 680 | tmp = "Gone home"; |
| 681 | } else if ((eventInfo.eventType & EventType.UPDATE_TITLE) != 0) { |
| 682 | tmp = "Update title"; |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 683 | } |
| 684 | builder.append(tmp); |
| 685 | builder.append(": id="); |
| 686 | builder.append(eventInfo.id); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 687 | builder.append(", selected="); |
| 688 | builder.append(eventInfo.selectedTime); |
| 689 | builder.append(", start="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 690 | builder.append(eventInfo.startTime); |
Michael Chan | d6734db | 2010-07-22 00:48:08 -0700 | [diff] [blame] | 691 | builder.append(", end="); |
Michael Chan | 3458a17 | 2010-07-13 17:58:21 -0700 | [diff] [blame] | 692 | builder.append(eventInfo.endTime); |
| 693 | builder.append(", viewType="); |
| 694 | builder.append(eventInfo.viewType); |
| 695 | builder.append(", x="); |
| 696 | builder.append(eventInfo.x); |
| 697 | builder.append(", y="); |
| 698 | builder.append(eventInfo.y); |
| 699 | return builder.toString(); |
| 700 | } |
Michael Chan | 4970159 | 2010-06-30 11:04:03 -0700 | [diff] [blame] | 701 | } |